Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * wpa_supplicant - P2P |
| 3 | * Copyright (c) 2009-2010, Atheros Communications |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4 | * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6 | * This software may be distributed under the terms of the BSD license. |
| 7 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include "includes.h" |
| 11 | |
| 12 | #include "common.h" |
| 13 | #include "eloop.h" |
| 14 | #include "common/ieee802_11_common.h" |
| 15 | #include "common/ieee802_11_defs.h" |
| 16 | #include "common/wpa_ctrl.h" |
| 17 | #include "wps/wps_i.h" |
| 18 | #include "p2p/p2p.h" |
| 19 | #include "ap/hostapd.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 20 | #include "ap/ap_config.h" |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 21 | #include "ap/sta_info.h" |
| 22 | #include "ap/ap_drv_ops.h" |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 23 | #include "ap/wps_hostapd.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 24 | #include "ap/p2p_hostapd.h" |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 25 | #include "eapol_supp/eapol_supp_sm.h" |
| 26 | #include "rsn_supp/wpa.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 27 | #include "wpa_supplicant_i.h" |
| 28 | #include "driver_i.h" |
| 29 | #include "ap.h" |
| 30 | #include "config_ssid.h" |
| 31 | #include "config.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 32 | #include "notify.h" |
| 33 | #include "scan.h" |
| 34 | #include "bss.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 35 | #include "offchannel.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 36 | #include "wps_supplicant.h" |
| 37 | #include "p2p_supplicant.h" |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 38 | #include "wifi_display.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | /* |
| 42 | * How many times to try to scan to find the GO before giving up on join |
| 43 | * request. |
| 44 | */ |
| 45 | #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10 |
| 46 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 47 | #define P2P_AUTO_PD_SCAN_ATTEMPTS 5 |
| 48 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 49 | #ifndef P2P_MAX_CLIENT_IDLE |
| 50 | /* |
| 51 | * How many seconds to try to reconnect to the GO when connection in P2P client |
| 52 | * role has been lost. |
| 53 | */ |
| 54 | #define P2P_MAX_CLIENT_IDLE 10 |
| 55 | #endif /* P2P_MAX_CLIENT_IDLE */ |
| 56 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 57 | #ifndef P2P_MAX_INITIAL_CONN_WAIT |
| 58 | /* |
| 59 | * How many seconds to wait for initial 4-way handshake to get completed after |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 60 | * WPS provisioning step or after the re-invocation of a persistent group on a |
| 61 | * P2P Client. |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 62 | */ |
| 63 | #define P2P_MAX_INITIAL_CONN_WAIT 10 |
| 64 | #endif /* P2P_MAX_INITIAL_CONN_WAIT */ |
| 65 | |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 66 | #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO |
| 67 | /* |
| 68 | * How many seconds to wait for initial 4-way handshake to get completed after |
| 69 | * WPS provisioning step on the GO. This controls the extra time the P2P |
| 70 | * operation is considered to be in progress (e.g., to delay other scans) after |
| 71 | * WPS provisioning has been completed on the GO during group formation. |
| 72 | */ |
| 73 | #define P2P_MAX_INITIAL_CONN_WAIT_GO 10 |
| 74 | #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */ |
| 75 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 76 | #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE |
| 77 | /* |
| 78 | * How many seconds to wait for initial 4-way handshake to get completed after |
| 79 | * re-invocation of a persistent group on the GO when the client is expected |
| 80 | * to connect automatically (no user interaction). |
| 81 | */ |
| 82 | #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15 |
| 83 | #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */ |
| 84 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 85 | #ifndef P2P_CONCURRENT_SEARCH_DELAY |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 86 | #define P2P_CONCURRENT_SEARCH_DELAY 500 |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 87 | #endif /* P2P_CONCURRENT_SEARCH_DELAY */ |
| 88 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 89 | #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-" |
| 90 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 91 | enum p2p_group_removal_reason { |
| 92 | P2P_GROUP_REMOVAL_UNKNOWN, |
| 93 | P2P_GROUP_REMOVAL_SILENT, |
| 94 | P2P_GROUP_REMOVAL_FORMATION_FAILED, |
| 95 | P2P_GROUP_REMOVAL_REQUESTED, |
| 96 | P2P_GROUP_REMOVAL_IDLE_TIMEOUT, |
| 97 | P2P_GROUP_REMOVAL_UNAVAILABLE, |
| 98 | P2P_GROUP_REMOVAL_GO_ENDING_SESSION, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 99 | P2P_GROUP_REMOVAL_PSK_FAILURE, |
| 100 | P2P_GROUP_REMOVAL_FREQ_CONFLICT |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Jouni Malinen | dc7b713 | 2012-09-14 12:53:47 -0700 | [diff] [blame] | 103 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 104 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx); |
| 105 | static struct wpa_supplicant * |
| 106 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, |
| 107 | int go); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 108 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq, |
| 109 | const u8 *ssid, size_t ssid_len); |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 110 | static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq, |
| 111 | const u8 *ssid, size_t ssid_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 112 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx); |
| 113 | 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] | 114 | const u8 *dev_addr, enum p2p_wps_method wps_method, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 115 | int auto_join, int freq, |
| 116 | const u8 *ssid, size_t ssid_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 117 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s); |
| 118 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s); |
| 119 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx); |
| 120 | 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] | 121 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, |
| 122 | void *timeout_ctx); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 123 | 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] | 124 | static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, |
| 125 | int group_added); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 126 | static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s); |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 127 | static void wpas_stop_listen(void *ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 128 | |
| 129 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 130 | /* |
| 131 | * Get the number of concurrent channels that the HW can operate, but that are |
| 132 | * currently not in use by any of the wpa_supplicant interfaces. |
| 133 | */ |
| 134 | static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s) |
| 135 | { |
| 136 | int *freqs; |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 137 | int num, unused; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 138 | |
| 139 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 140 | if (!freqs) |
| 141 | return -1; |
| 142 | |
| 143 | num = get_shared_radio_freqs(wpa_s, freqs, |
| 144 | wpa_s->num_multichan_concurrent); |
| 145 | os_free(freqs); |
| 146 | |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 147 | unused = wpa_s->num_multichan_concurrent - num; |
| 148 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused); |
| 149 | return unused; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
| 153 | /* |
| 154 | * Get the frequencies that are currently in use by one or more of the virtual |
| 155 | * interfaces, and that are also valid for P2P operation. |
| 156 | */ |
| 157 | static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s, |
| 158 | int *p2p_freqs, unsigned int len) |
| 159 | { |
| 160 | int *freqs; |
| 161 | unsigned int num, i, j; |
| 162 | |
| 163 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 164 | if (!freqs) |
| 165 | return -1; |
| 166 | |
| 167 | num = get_shared_radio_freqs(wpa_s, freqs, |
| 168 | wpa_s->num_multichan_concurrent); |
| 169 | |
| 170 | os_memset(p2p_freqs, 0, sizeof(int) * len); |
| 171 | |
| 172 | for (i = 0, j = 0; i < num && j < len; i++) { |
| 173 | if (p2p_supported_freq(wpa_s->global->p2p, freqs[i])) |
| 174 | p2p_freqs[j++] = freqs[i]; |
| 175 | } |
| 176 | |
| 177 | os_free(freqs); |
| 178 | |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 179 | dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j); |
| 180 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 181 | return j; |
| 182 | } |
| 183 | |
| 184 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 185 | static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s, |
| 186 | int freq) |
| 187 | { |
| 188 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 189 | return; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 190 | if (wpa_s->parent->conf->p2p_ignore_shared_freq && |
| 191 | freq > 0 && wpa_s->num_multichan_concurrent > 1 && |
| 192 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 193 | wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration", |
| 194 | freq); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 195 | freq = 0; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 196 | } |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 197 | p2p_set_own_freq_preference(wpa_s->global->p2p, freq); |
| 198 | } |
| 199 | |
| 200 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 201 | static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s, |
| 202 | struct wpa_scan_results *scan_res) |
| 203 | { |
| 204 | size_t i; |
| 205 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 206 | if (wpa_s->p2p_scan_work) { |
| 207 | struct wpa_radio_work *work = wpa_s->p2p_scan_work; |
| 208 | wpa_s->p2p_scan_work = NULL; |
| 209 | radio_work_done(work); |
| 210 | } |
| 211 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 212 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 213 | return; |
| 214 | |
| 215 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)", |
| 216 | (int) scan_res->num); |
| 217 | |
| 218 | for (i = 0; i < scan_res->num; i++) { |
| 219 | struct wpa_scan_res *bss = scan_res->res[i]; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 220 | struct os_reltime time_tmp_age, entry_ts; |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 221 | const u8 *ies; |
| 222 | size_t ies_len; |
| 223 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 224 | time_tmp_age.sec = bss->age / 1000; |
| 225 | time_tmp_age.usec = (bss->age % 1000) * 1000; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 226 | os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts); |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 227 | |
| 228 | ies = (const u8 *) (bss + 1); |
| 229 | ies_len = bss->ie_len; |
| 230 | if (bss->beacon_ie_len > 0 && |
| 231 | !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) && |
| 232 | wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) { |
| 233 | wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for " |
| 234 | MACSTR, MAC2STR(bss->bssid)); |
| 235 | ies = ies + ies_len; |
| 236 | ies_len = bss->beacon_ie_len; |
| 237 | } |
| 238 | |
| 239 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 240 | if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid, |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 241 | bss->freq, &entry_ts, bss->level, |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 242 | ies, ies_len) > 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | |
| 246 | p2p_scan_res_handled(wpa_s->global->p2p); |
| 247 | } |
| 248 | |
| 249 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 250 | static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit) |
| 251 | { |
| 252 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 253 | struct wpa_driver_scan_params *params = work->ctx; |
| 254 | int ret; |
| 255 | |
| 256 | if (deinit) { |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 257 | if (!work->started) { |
| 258 | wpa_scan_free_params(params); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | wpa_s->p2p_scan_work = NULL; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
| 266 | ret = wpa_drv_scan(wpa_s, params); |
| 267 | wpa_scan_free_params(params); |
| 268 | work->ctx = NULL; |
| 269 | if (ret) { |
| 270 | radio_work_done(work); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | os_get_reltime(&wpa_s->scan_trigger_time); |
| 275 | wpa_s->scan_res_handler = wpas_p2p_scan_res_handler; |
| 276 | wpa_s->own_scan_requested = 1; |
| 277 | wpa_s->p2p_scan_work = work; |
| 278 | } |
| 279 | |
| 280 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 281 | static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, |
| 282 | unsigned int num_req_dev_types, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 283 | const u8 *req_dev_types, const u8 *dev_id, u16 pw_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 284 | { |
| 285 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 286 | struct wpa_driver_scan_params *params = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 287 | struct wpabuf *wps_ie, *ies; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 288 | size_t ielen; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 289 | u8 *n; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 290 | |
| 291 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 292 | return -1; |
| 293 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 294 | if (wpa_s->p2p_scan_work) { |
| 295 | wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending"); |
| 296 | return -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 299 | params = os_zalloc(sizeof(*params)); |
| 300 | if (params == NULL) |
| 301 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 302 | |
| 303 | /* P2P Wildcard SSID */ |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 304 | params->num_ssids = 1; |
| 305 | n = os_malloc(P2P_WILDCARD_SSID_LEN); |
| 306 | if (n == NULL) |
| 307 | goto fail; |
| 308 | os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); |
| 309 | params->ssids[0].ssid = n; |
| 310 | params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 311 | |
| 312 | wpa_s->wps->dev.p2p = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 313 | wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev, |
| 314 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 315 | num_req_dev_types, req_dev_types); |
| 316 | if (wps_ie == NULL) |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 317 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 318 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 319 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
| 320 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 321 | if (ies == NULL) { |
| 322 | wpabuf_free(wps_ie); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 323 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 324 | } |
| 325 | wpabuf_put_buf(ies, wps_ie); |
| 326 | wpabuf_free(wps_ie); |
| 327 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 328 | p2p_scan_ie(wpa_s->global->p2p, ies, dev_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 329 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 330 | params->p2p_probe = 1; |
| 331 | n = os_malloc(wpabuf_len(ies)); |
| 332 | if (n == NULL) { |
| 333 | wpabuf_free(ies); |
| 334 | goto fail; |
| 335 | } |
| 336 | os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies)); |
| 337 | params->extra_ies = n; |
| 338 | params->extra_ies_len = wpabuf_len(ies); |
| 339 | wpabuf_free(ies); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 340 | |
| 341 | switch (type) { |
| 342 | case P2P_SCAN_SOCIAL: |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 343 | params->freqs = os_malloc(4 * 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] = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 350 | break; |
| 351 | case P2P_SCAN_FULL: |
| 352 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 353 | case P2P_SCAN_SOCIAL_PLUS_ONE: |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 354 | params->freqs = os_malloc(5 * sizeof(int)); |
| 355 | if (params->freqs == NULL) |
| 356 | goto fail; |
| 357 | params->freqs[0] = 2412; |
| 358 | params->freqs[1] = 2437; |
| 359 | params->freqs[2] = 2462; |
| 360 | params->freqs[3] = freq; |
| 361 | params->freqs[4] = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 365 | radio_remove_works(wpa_s, "p2p-scan", 0); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 366 | if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb, |
| 367 | params) < 0) |
| 368 | goto fail; |
| 369 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 370 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 371 | fail: |
| 372 | wpa_scan_free_params(params); |
| 373 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 377 | static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface) |
| 378 | { |
| 379 | switch (p2p_group_interface) { |
| 380 | case P2P_GROUP_INTERFACE_PENDING: |
| 381 | return WPA_IF_P2P_GROUP; |
| 382 | case P2P_GROUP_INTERFACE_GO: |
| 383 | return WPA_IF_P2P_GO; |
| 384 | case P2P_GROUP_INTERFACE_CLIENT: |
| 385 | return WPA_IF_P2P_CLIENT; |
| 386 | } |
| 387 | |
| 388 | return WPA_IF_P2P_GROUP; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s, |
| 393 | const u8 *ssid, |
| 394 | size_t ssid_len, int *go) |
| 395 | { |
| 396 | struct wpa_ssid *s; |
| 397 | |
| 398 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 399 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 400 | if (s->disabled != 0 || !s->p2p_group || |
| 401 | s->ssid_len != ssid_len || |
| 402 | os_memcmp(ssid, s->ssid, ssid_len) != 0) |
| 403 | continue; |
| 404 | if (s->mode == WPAS_MODE_P2P_GO && |
| 405 | s != wpa_s->current_ssid) |
| 406 | continue; |
| 407 | if (go) |
| 408 | *go = s->mode == WPAS_MODE_P2P_GO; |
| 409 | return wpa_s; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 417 | static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s, |
| 418 | enum p2p_group_removal_reason removal_reason) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 419 | { |
| 420 | struct wpa_ssid *ssid; |
| 421 | char *gtype; |
| 422 | const char *reason; |
| 423 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 424 | ssid = wpa_s->current_ssid; |
| 425 | if (ssid == NULL) { |
| 426 | /* |
| 427 | * The current SSID was not known, but there may still be a |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 428 | * pending P2P group interface waiting for provisioning or a |
| 429 | * P2P group that is trying to reconnect. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 430 | */ |
| 431 | ssid = wpa_s->conf->ssid; |
| 432 | while (ssid) { |
Jouni Malinen | 9d71283 | 2012-10-05 11:01:57 -0700 | [diff] [blame] | 433 | if (ssid->p2p_group && ssid->disabled != 2) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 434 | break; |
| 435 | ssid = ssid->next; |
| 436 | } |
Jouni Malinen | 5c44edb | 2012-08-31 21:35:32 +0300 | [diff] [blame] | 437 | if (ssid == NULL && |
| 438 | wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) |
| 439 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 440 | wpa_printf(MSG_ERROR, "P2P: P2P group interface " |
| 441 | "not found"); |
| 442 | return -1; |
| 443 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 444 | } |
| 445 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO) |
| 446 | gtype = "GO"; |
| 447 | else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT || |
| 448 | (ssid && ssid->mode == WPAS_MODE_INFRA)) { |
| 449 | wpa_s->reassociate = 0; |
| 450 | wpa_s->disconnected = 1; |
| 451 | wpa_supplicant_deauthenticate(wpa_s, |
| 452 | WLAN_REASON_DEAUTH_LEAVING); |
| 453 | gtype = "client"; |
| 454 | } else |
| 455 | gtype = "GO"; |
| 456 | if (wpa_s->cross_connect_in_use) { |
| 457 | wpa_s->cross_connect_in_use = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 458 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 459 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 460 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 461 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 462 | switch (removal_reason) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 463 | case P2P_GROUP_REMOVAL_REQUESTED: |
| 464 | reason = " reason=REQUESTED"; |
| 465 | break; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 466 | case P2P_GROUP_REMOVAL_FORMATION_FAILED: |
| 467 | reason = " reason=FORMATION_FAILED"; |
| 468 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 469 | case P2P_GROUP_REMOVAL_IDLE_TIMEOUT: |
| 470 | reason = " reason=IDLE"; |
| 471 | break; |
| 472 | case P2P_GROUP_REMOVAL_UNAVAILABLE: |
| 473 | reason = " reason=UNAVAILABLE"; |
| 474 | break; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 475 | case P2P_GROUP_REMOVAL_GO_ENDING_SESSION: |
| 476 | reason = " reason=GO_ENDING_SESSION"; |
| 477 | break; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 478 | case P2P_GROUP_REMOVAL_PSK_FAILURE: |
| 479 | reason = " reason=PSK_FAILURE"; |
| 480 | break; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 481 | case P2P_GROUP_REMOVAL_FREQ_CONFLICT: |
| 482 | reason = " reason=FREQ_CONFLICT"; |
| 483 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 484 | default: |
| 485 | reason = ""; |
| 486 | break; |
| 487 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 488 | if (removal_reason != P2P_GROUP_REMOVAL_SILENT) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 489 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 490 | P2P_EVENT_GROUP_REMOVED "%s %s%s", |
| 491 | wpa_s->ifname, gtype, reason); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 492 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 493 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 494 | if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0) |
| 495 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 496 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 497 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 498 | if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 499 | wpa_s->parent, NULL) > 0) { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 500 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation " |
| 501 | "timeout"); |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 502 | wpa_s->p2p_in_provisioning = 0; |
| 503 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 504 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 505 | wpa_s->p2p_in_invitation = 0; |
| 506 | |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 507 | /* |
| 508 | * Make sure wait for the first client does not remain active after the |
| 509 | * group has been removed. |
| 510 | */ |
| 511 | wpa_s->global->p2p_go_wait_client.sec = 0; |
| 512 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 513 | if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 514 | wpas_notify_p2p_group_removed(wpa_s, ssid, gtype); |
| 515 | |
| 516 | if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { |
| 517 | struct wpa_global *global; |
| 518 | char *ifname; |
| 519 | enum wpa_driver_if_type type; |
| 520 | wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s", |
| 521 | wpa_s->ifname); |
| 522 | global = wpa_s->global; |
| 523 | ifname = os_strdup(wpa_s->ifname); |
| 524 | type = wpas_p2p_if_type(wpa_s->p2p_group_interface); |
Dmitry Shmidt | e15c7b5 | 2011-08-03 15:04:35 -0700 | [diff] [blame] | 525 | wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 526 | wpa_s = global->ifaces; |
| 527 | if (wpa_s && ifname) |
| 528 | wpa_drv_if_remove(wpa_s, type, ifname); |
| 529 | os_free(ifname); |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 530 | return 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 533 | if (!wpa_s->p2p_go_group_formation_completed) { |
| 534 | wpa_s->global->p2p_group_formation = NULL; |
| 535 | wpa_s->p2p_in_provisioning = 0; |
| 536 | } |
| 537 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 538 | wpa_s->show_group_started = 0; |
| 539 | os_free(wpa_s->go_params); |
| 540 | wpa_s->go_params = NULL; |
| 541 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 542 | wpa_s->waiting_presence_resp = 0; |
| 543 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 544 | wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network"); |
| 545 | if (ssid && (ssid->p2p_group || |
| 546 | ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || |
| 547 | (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) { |
| 548 | int id = ssid->id; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 549 | if (ssid == wpa_s->current_ssid) { |
| 550 | wpa_sm_set_config(wpa_s->wpa, NULL); |
| 551 | eapol_sm_notify_config(wpa_s->eapol, NULL, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 552 | wpa_s->current_ssid = NULL; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 553 | } |
| 554 | /* |
| 555 | * Networks objects created during any P2P activities are not |
| 556 | * exposed out as they might/will confuse certain non-P2P aware |
| 557 | * applications since these network objects won't behave like |
| 558 | * regular ones. |
| 559 | * |
| 560 | * Likewise, we don't send out network removed signals for such |
| 561 | * network objects. |
| 562 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 563 | wpa_config_remove_network(wpa_s->conf, id); |
| 564 | wpa_supplicant_clear_status(wpa_s); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 565 | wpa_supplicant_cancel_sched_scan(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 566 | } else { |
| 567 | wpa_printf(MSG_DEBUG, "P2P: Temporary group network not " |
| 568 | "found"); |
| 569 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 570 | if (wpa_s->ap_iface) |
| 571 | wpa_supplicant_ap_deinit(wpa_s); |
| 572 | else |
| 573 | wpa_drv_deinit_p2p_cli(wpa_s); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 574 | |
| 575 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |
| 579 | static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s, |
| 580 | u8 *go_dev_addr, |
| 581 | const u8 *ssid, size_t ssid_len) |
| 582 | { |
| 583 | struct wpa_bss *bss; |
| 584 | const u8 *bssid; |
| 585 | struct wpabuf *p2p; |
| 586 | u8 group_capab; |
| 587 | const u8 *addr; |
| 588 | |
| 589 | if (wpa_s->go_params) |
| 590 | bssid = wpa_s->go_params->peer_interface_addr; |
| 591 | else |
| 592 | bssid = wpa_s->bssid; |
| 593 | |
| 594 | bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 595 | if (bss == NULL && wpa_s->go_params && |
| 596 | !is_zero_ether_addr(wpa_s->go_params->peer_device_addr)) |
| 597 | bss = wpa_bss_get_p2p_dev_addr( |
| 598 | wpa_s, wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 599 | if (bss == NULL) { |
| 600 | u8 iface_addr[ETH_ALEN]; |
| 601 | if (p2p_get_interface_addr(wpa_s->global->p2p, bssid, |
| 602 | iface_addr) == 0) |
| 603 | bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len); |
| 604 | } |
| 605 | if (bss == NULL) { |
| 606 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " |
| 607 | "group is persistent - BSS " MACSTR " not found", |
| 608 | MAC2STR(bssid)); |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 613 | if (p2p == NULL) |
| 614 | p2p = wpa_bss_get_vendor_ie_multi_beacon(bss, |
| 615 | P2P_IE_VENDOR_TYPE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 616 | if (p2p == NULL) { |
| 617 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " |
| 618 | "group is persistent - BSS " MACSTR |
| 619 | " did not include P2P IE", MAC2STR(bssid)); |
| 620 | wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs", |
| 621 | (u8 *) (bss + 1), bss->ie_len); |
| 622 | wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs", |
| 623 | ((u8 *) bss + 1) + bss->ie_len, |
| 624 | bss->beacon_ie_len); |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | group_capab = p2p_get_group_capab(p2p); |
| 629 | addr = p2p_get_go_dev_addr(p2p); |
| 630 | wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: " |
| 631 | "group_capab=0x%x", group_capab); |
| 632 | if (addr) { |
| 633 | os_memcpy(go_dev_addr, addr, ETH_ALEN); |
| 634 | wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR, |
| 635 | MAC2STR(addr)); |
| 636 | } else |
| 637 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 638 | wpabuf_free(p2p); |
| 639 | |
| 640 | wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x " |
| 641 | "go_dev_addr=" MACSTR, |
| 642 | MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr)); |
| 643 | |
| 644 | return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP; |
| 645 | } |
| 646 | |
| 647 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 648 | static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s, |
| 649 | struct wpa_ssid *ssid, |
| 650 | const u8 *go_dev_addr) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 651 | { |
| 652 | struct wpa_ssid *s; |
| 653 | int changed = 0; |
| 654 | |
| 655 | wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent " |
| 656 | "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr)); |
| 657 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 658 | if (s->disabled == 2 && |
| 659 | os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 && |
| 660 | s->ssid_len == ssid->ssid_len && |
| 661 | os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0) |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | if (s) { |
| 666 | wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group " |
| 667 | "entry"); |
| 668 | if (ssid->passphrase && !s->passphrase) |
| 669 | changed = 1; |
| 670 | else if (ssid->passphrase && s->passphrase && |
| 671 | os_strcmp(ssid->passphrase, s->passphrase) != 0) |
| 672 | changed = 1; |
| 673 | } else { |
| 674 | wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group " |
| 675 | "entry"); |
| 676 | changed = 1; |
| 677 | s = wpa_config_add_network(wpa_s->conf); |
| 678 | if (s == NULL) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 679 | return -1; |
| 680 | |
| 681 | /* |
| 682 | * Instead of network_added we emit persistent_group_added |
| 683 | * notification. Also to keep the defense checks in |
| 684 | * persistent_group obj registration method, we set the |
| 685 | * relevant flags in s to designate it as a persistent group. |
| 686 | */ |
| 687 | s->p2p_group = 1; |
| 688 | s->p2p_persistent_group = 1; |
| 689 | wpas_notify_persistent_group_added(wpa_s, s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 690 | wpa_config_set_network_defaults(s); |
| 691 | } |
| 692 | |
| 693 | s->p2p_group = 1; |
| 694 | s->p2p_persistent_group = 1; |
| 695 | s->disabled = 2; |
| 696 | s->bssid_set = 1; |
| 697 | os_memcpy(s->bssid, go_dev_addr, ETH_ALEN); |
| 698 | s->mode = ssid->mode; |
| 699 | s->auth_alg = WPA_AUTH_ALG_OPEN; |
| 700 | s->key_mgmt = WPA_KEY_MGMT_PSK; |
| 701 | s->proto = WPA_PROTO_RSN; |
| 702 | s->pairwise_cipher = WPA_CIPHER_CCMP; |
| 703 | s->export_keys = 1; |
| 704 | if (ssid->passphrase) { |
| 705 | os_free(s->passphrase); |
| 706 | s->passphrase = os_strdup(ssid->passphrase); |
| 707 | } |
| 708 | if (ssid->psk_set) { |
| 709 | s->psk_set = 1; |
| 710 | os_memcpy(s->psk, ssid->psk, 32); |
| 711 | } |
| 712 | if (s->passphrase && !s->psk_set) |
| 713 | wpa_config_update_psk(s); |
| 714 | if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) { |
| 715 | os_free(s->ssid); |
| 716 | s->ssid = os_malloc(ssid->ssid_len); |
| 717 | } |
| 718 | if (s->ssid) { |
| 719 | s->ssid_len = ssid->ssid_len; |
| 720 | os_memcpy(s->ssid, ssid->ssid, s->ssid_len); |
| 721 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 722 | if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) { |
| 723 | dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list); |
| 724 | wpa_s->global->add_psk = NULL; |
| 725 | changed = 1; |
| 726 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 727 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 728 | if (changed && wpa_s->conf->update_config && |
| 729 | wpa_config_write(wpa_s->confname, wpa_s->conf)) { |
| 730 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
| 731 | } |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 732 | |
| 733 | return s->id; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 737 | static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s, |
| 738 | const u8 *addr) |
| 739 | { |
| 740 | struct wpa_ssid *ssid, *s; |
| 741 | u8 *n; |
| 742 | size_t i; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 743 | int found = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 744 | |
| 745 | ssid = wpa_s->current_ssid; |
| 746 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || |
| 747 | !ssid->p2p_persistent_group) |
| 748 | return; |
| 749 | |
| 750 | for (s = wpa_s->parent->conf->ssid; s; s = s->next) { |
| 751 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) |
| 752 | continue; |
| 753 | |
| 754 | if (s->ssid_len == ssid->ssid_len && |
| 755 | os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0) |
| 756 | break; |
| 757 | } |
| 758 | |
| 759 | if (s == NULL) |
| 760 | return; |
| 761 | |
| 762 | for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) { |
| 763 | if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 764 | ETH_ALEN) != 0) |
| 765 | continue; |
| 766 | |
| 767 | if (i == s->num_p2p_clients - 1) |
| 768 | return; /* already the most recent entry */ |
| 769 | |
| 770 | /* move the entry to mark it most recent */ |
| 771 | os_memmove(s->p2p_client_list + i * ETH_ALEN, |
| 772 | s->p2p_client_list + (i + 1) * ETH_ALEN, |
| 773 | (s->num_p2p_clients - i - 1) * ETH_ALEN); |
| 774 | os_memcpy(s->p2p_client_list + |
| 775 | (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN); |
| 776 | found = 1; |
| 777 | break; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 778 | } |
| 779 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 780 | if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) { |
| 781 | n = os_realloc_array(s->p2p_client_list, |
| 782 | s->num_p2p_clients + 1, ETH_ALEN); |
| 783 | if (n == NULL) |
| 784 | return; |
| 785 | os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN); |
| 786 | s->p2p_client_list = n; |
| 787 | s->num_p2p_clients++; |
| 788 | } else if (!found) { |
| 789 | /* Not enough room for an additional entry - drop the oldest |
| 790 | * entry */ |
| 791 | os_memmove(s->p2p_client_list, |
| 792 | s->p2p_client_list + ETH_ALEN, |
| 793 | (s->num_p2p_clients - 1) * ETH_ALEN); |
| 794 | os_memcpy(s->p2p_client_list + |
| 795 | (s->num_p2p_clients - 1) * ETH_ALEN, |
| 796 | addr, ETH_ALEN); |
| 797 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 798 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 799 | if (wpa_s->parent->conf->update_config && |
| 800 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 801 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 805 | static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s, |
| 806 | int success) |
| 807 | { |
| 808 | struct wpa_ssid *ssid; |
| 809 | const char *ssid_txt; |
| 810 | int client; |
| 811 | int persistent; |
| 812 | u8 go_dev_addr[ETH_ALEN]; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 813 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 814 | |
| 815 | /* |
| 816 | * This callback is likely called for the main interface. Update wpa_s |
| 817 | * to use the group interface if a new interface was created for the |
| 818 | * group. |
| 819 | */ |
| 820 | if (wpa_s->global->p2p_group_formation) |
| 821 | wpa_s = wpa_s->global->p2p_group_formation; |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 822 | if (wpa_s->p2p_go_group_formation_completed) { |
| 823 | wpa_s->global->p2p_group_formation = NULL; |
| 824 | wpa_s->p2p_in_provisioning = 0; |
| 825 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 826 | |
| 827 | if (!success) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 828 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 829 | P2P_EVENT_GROUP_FORMATION_FAILURE); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 830 | wpas_p2p_group_delete(wpa_s, |
| 831 | P2P_GROUP_REMOVAL_FORMATION_FAILED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 832 | return; |
| 833 | } |
| 834 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 835 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 836 | P2P_EVENT_GROUP_FORMATION_SUCCESS); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 837 | |
| 838 | ssid = wpa_s->current_ssid; |
| 839 | if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { |
| 840 | ssid->mode = WPAS_MODE_P2P_GO; |
| 841 | p2p_group_notif_formation_done(wpa_s->p2p_group); |
| 842 | wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL); |
| 843 | } |
| 844 | |
| 845 | persistent = 0; |
| 846 | if (ssid) { |
| 847 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); |
| 848 | client = ssid->mode == WPAS_MODE_INFRA; |
| 849 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 850 | persistent = ssid->p2p_persistent_group; |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 851 | os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr, |
| 852 | ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 853 | } else |
| 854 | persistent = wpas_p2p_persistent_group(wpa_s, |
| 855 | go_dev_addr, |
| 856 | ssid->ssid, |
| 857 | ssid->ssid_len); |
| 858 | } else { |
| 859 | ssid_txt = ""; |
| 860 | client = wpa_s->p2p_group_interface == |
| 861 | P2P_GROUP_INTERFACE_CLIENT; |
| 862 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 863 | } |
| 864 | |
| 865 | wpa_s->show_group_started = 0; |
| 866 | if (client) { |
| 867 | /* |
| 868 | * Indicate event only after successfully completed 4-way |
| 869 | * handshake, i.e., when the interface is ready for data |
| 870 | * packets. |
| 871 | */ |
| 872 | wpa_s->show_group_started = 1; |
| 873 | } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) { |
| 874 | char psk[65]; |
| 875 | wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 876 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 877 | "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" |
| 878 | MACSTR "%s", |
| 879 | wpa_s->ifname, ssid_txt, ssid->frequency, psk, |
| 880 | MAC2STR(go_dev_addr), |
| 881 | persistent ? " [PERSISTENT]" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 882 | wpas_p2p_cross_connect_setup(wpa_s); |
| 883 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 884 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 885 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 886 | "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" " |
| 887 | "go_dev_addr=" MACSTR "%s", |
| 888 | wpa_s->ifname, ssid_txt, |
| 889 | ssid ? ssid->frequency : 0, |
| 890 | ssid && ssid->passphrase ? ssid->passphrase : "", |
| 891 | MAC2STR(go_dev_addr), |
| 892 | persistent ? " [PERSISTENT]" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 893 | wpas_p2p_cross_connect_setup(wpa_s); |
| 894 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 895 | } |
| 896 | |
| 897 | if (persistent) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 898 | network_id = wpas_p2p_store_persistent_group(wpa_s->parent, |
| 899 | ssid, go_dev_addr); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 900 | else { |
| 901 | os_free(wpa_s->global->add_psk); |
| 902 | wpa_s->global->add_psk = NULL; |
| 903 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 904 | if (network_id < 0 && ssid) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 905 | network_id = ssid->id; |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 906 | if (!client) { |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 907 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 908 | os_get_reltime(&wpa_s->global->p2p_go_wait_client); |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 909 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 913 | struct send_action_work { |
| 914 | unsigned int freq; |
| 915 | u8 dst[ETH_ALEN]; |
| 916 | u8 src[ETH_ALEN]; |
| 917 | u8 bssid[ETH_ALEN]; |
| 918 | size_t len; |
| 919 | unsigned int wait_time; |
| 920 | u8 buf[0]; |
| 921 | }; |
| 922 | |
| 923 | |
| 924 | static void wpas_p2p_send_action_work_timeout(void *eloop_ctx, |
| 925 | void *timeout_ctx) |
| 926 | { |
| 927 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 928 | |
| 929 | if (!wpa_s->p2p_send_action_work) |
| 930 | return; |
| 931 | |
| 932 | wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out"); |
| 933 | os_free(wpa_s->p2p_send_action_work->ctx); |
| 934 | radio_work_done(wpa_s->p2p_send_action_work); |
| 935 | wpa_s->p2p_send_action_work = NULL; |
| 936 | } |
| 937 | |
| 938 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 939 | static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s, |
| 940 | unsigned int freq, |
| 941 | const u8 *dst, const u8 *src, |
| 942 | const u8 *bssid, |
| 943 | const u8 *data, size_t data_len, |
| 944 | enum offchannel_send_action_result |
| 945 | result) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 946 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 947 | enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 948 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 949 | if (wpa_s->p2p_send_action_work) { |
| 950 | struct send_action_work *awork; |
| 951 | awork = wpa_s->p2p_send_action_work->ctx; |
| 952 | if (awork->wait_time == 0) { |
| 953 | os_free(awork); |
| 954 | radio_work_done(wpa_s->p2p_send_action_work); |
| 955 | wpa_s->p2p_send_action_work = NULL; |
| 956 | } else { |
| 957 | /* |
| 958 | * In theory, this should not be needed, but number of |
| 959 | * places in the P2P code is still using non-zero wait |
| 960 | * time for the last Action frame in the sequence and |
| 961 | * some of these do not call send_action_done(). |
| 962 | */ |
| 963 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, |
| 964 | wpa_s, NULL); |
| 965 | eloop_register_timeout( |
| 966 | 0, awork->wait_time * 1000, |
| 967 | wpas_p2p_send_action_work_timeout, |
| 968 | wpa_s, NULL); |
| 969 | } |
| 970 | } |
| 971 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 972 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) |
| 973 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 974 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 975 | switch (result) { |
| 976 | case OFFCHANNEL_SEND_ACTION_SUCCESS: |
| 977 | res = P2P_SEND_ACTION_SUCCESS; |
| 978 | break; |
| 979 | case OFFCHANNEL_SEND_ACTION_NO_ACK: |
| 980 | res = P2P_SEND_ACTION_NO_ACK; |
| 981 | break; |
| 982 | case OFFCHANNEL_SEND_ACTION_FAILED: |
| 983 | res = P2P_SEND_ACTION_FAILED; |
| 984 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 987 | 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] | 988 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 989 | if (result != OFFCHANNEL_SEND_ACTION_SUCCESS && |
| 990 | wpa_s->pending_pd_before_join && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 991 | (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 992 | os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) && |
| 993 | wpa_s->p2p_fallback_to_go_neg) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 994 | wpa_s->pending_pd_before_join = 0; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 995 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req " |
| 996 | "during p2p_connect-auto"); |
| 997 | wpas_p2p_fallback_to_go_neg(wpa_s, 0); |
| 998 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1003 | static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit) |
| 1004 | { |
| 1005 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 1006 | struct send_action_work *awork = work->ctx; |
| 1007 | |
| 1008 | if (deinit) { |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 1009 | if (work->started) { |
| 1010 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, |
| 1011 | wpa_s, NULL); |
| 1012 | wpa_s->p2p_send_action_work = NULL; |
| 1013 | offchannel_send_action_done(wpa_s); |
| 1014 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1015 | os_free(awork); |
| 1016 | return; |
| 1017 | } |
| 1018 | |
| 1019 | if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src, |
| 1020 | awork->bssid, awork->buf, awork->len, |
| 1021 | awork->wait_time, |
| 1022 | wpas_p2p_send_action_tx_status, 1) < 0) { |
| 1023 | os_free(awork); |
| 1024 | radio_work_done(work); |
| 1025 | return; |
| 1026 | } |
| 1027 | wpa_s->p2p_send_action_work = work; |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | static int wpas_send_action_work(struct wpa_supplicant *wpa_s, |
| 1032 | unsigned int freq, const u8 *dst, |
| 1033 | const u8 *src, const u8 *bssid, const u8 *buf, |
| 1034 | size_t len, unsigned int wait_time) |
| 1035 | { |
| 1036 | struct send_action_work *awork; |
| 1037 | |
| 1038 | if (wpa_s->p2p_send_action_work) { |
| 1039 | wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending"); |
| 1040 | return -1; |
| 1041 | } |
| 1042 | |
| 1043 | awork = os_zalloc(sizeof(*awork) + len); |
| 1044 | if (awork == NULL) |
| 1045 | return -1; |
| 1046 | |
| 1047 | awork->freq = freq; |
| 1048 | os_memcpy(awork->dst, dst, ETH_ALEN); |
| 1049 | os_memcpy(awork->src, src, ETH_ALEN); |
| 1050 | os_memcpy(awork->bssid, bssid, ETH_ALEN); |
| 1051 | awork->len = len; |
| 1052 | awork->wait_time = wait_time; |
| 1053 | os_memcpy(awork->buf, buf, len); |
| 1054 | |
| 1055 | if (radio_add_work(wpa_s, freq, "p2p-send-action", 0, |
| 1056 | wpas_send_action_cb, awork) < 0) { |
| 1057 | os_free(awork); |
| 1058 | return -1; |
| 1059 | } |
| 1060 | |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1065 | static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst, |
| 1066 | const u8 *src, const u8 *bssid, const u8 *buf, |
| 1067 | size_t len, unsigned int wait_time) |
| 1068 | { |
| 1069 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1070 | int listen_freq = -1, send_freq = -1; |
| 1071 | |
| 1072 | if (wpa_s->p2p_listen_work) |
| 1073 | listen_freq = wpa_s->p2p_listen_work->freq; |
| 1074 | if (wpa_s->p2p_send_action_work) |
| 1075 | send_freq = wpa_s->p2p_send_action_work->freq; |
| 1076 | if (listen_freq != (int) freq && send_freq != (int) freq) { |
| 1077 | wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)", |
| 1078 | listen_freq, send_freq); |
| 1079 | return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf, |
| 1080 | len, wait_time); |
| 1081 | } |
| 1082 | |
| 1083 | 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] | 1084 | return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len, |
| 1085 | wait_time, |
| 1086 | wpas_p2p_send_action_tx_status, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | |
| 1090 | static void wpas_send_action_done(void *ctx) |
| 1091 | { |
| 1092 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1093 | |
| 1094 | if (wpa_s->p2p_send_action_work) { |
| 1095 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, |
| 1096 | wpa_s, NULL); |
| 1097 | os_free(wpa_s->p2p_send_action_work->ctx); |
| 1098 | radio_work_done(wpa_s->p2p_send_action_work); |
| 1099 | wpa_s->p2p_send_action_work = NULL; |
| 1100 | } |
| 1101 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1102 | offchannel_send_action_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s, |
| 1107 | struct p2p_go_neg_results *params) |
| 1108 | { |
| 1109 | if (wpa_s->go_params == NULL) { |
| 1110 | wpa_s->go_params = os_malloc(sizeof(*params)); |
| 1111 | if (wpa_s->go_params == NULL) |
| 1112 | return -1; |
| 1113 | } |
| 1114 | os_memcpy(wpa_s->go_params, params, sizeof(*params)); |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | |
| 1119 | static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s, |
| 1120 | struct p2p_go_neg_results *res) |
| 1121 | { |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1122 | wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR |
| 1123 | " dev_addr " MACSTR " wps_method %d", |
| 1124 | MAC2STR(res->peer_interface_addr), |
| 1125 | MAC2STR(res->peer_device_addr), res->wps_method); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1126 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID", |
| 1127 | res->ssid, res->ssid_len); |
| 1128 | wpa_supplicant_ap_deinit(wpa_s); |
| 1129 | wpas_copy_go_neg_results(wpa_s, res); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1130 | if (res->wps_method == WPS_PBC) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1131 | wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1132 | #ifdef CONFIG_WPS_NFC |
| 1133 | } else if (res->wps_method == WPS_NFC) { |
| 1134 | wpas_wps_start_nfc(wpa_s, res->peer_device_addr, |
| 1135 | res->peer_interface_addr, |
| 1136 | wpa_s->parent->p2p_oob_dev_pw, |
| 1137 | wpa_s->parent->p2p_oob_dev_pw_id, 1, |
| 1138 | wpa_s->parent->p2p_oob_dev_pw_id == |
| 1139 | DEV_PW_NFC_CONNECTION_HANDOVER ? |
| 1140 | wpa_s->parent->p2p_peer_oob_pubkey_hash : |
| 1141 | NULL, |
| 1142 | NULL, 0, 0); |
| 1143 | #endif /* CONFIG_WPS_NFC */ |
| 1144 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1145 | u16 dev_pw_id = DEV_PW_DEFAULT; |
| 1146 | if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD) |
| 1147 | dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED; |
| 1148 | wpas_wps_start_pin(wpa_s, res->peer_interface_addr, |
| 1149 | wpa_s->p2p_pin, 1, dev_pw_id); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1154 | static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s, |
| 1155 | struct wpa_ssid *ssid) |
| 1156 | { |
| 1157 | struct wpa_ssid *persistent; |
| 1158 | struct psk_list_entry *psk; |
| 1159 | struct hostapd_data *hapd; |
| 1160 | |
| 1161 | if (!wpa_s->ap_iface) |
| 1162 | return; |
| 1163 | |
| 1164 | persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid, |
| 1165 | ssid->ssid_len); |
| 1166 | if (persistent == NULL) |
| 1167 | return; |
| 1168 | |
| 1169 | hapd = wpa_s->ap_iface->bss[0]; |
| 1170 | |
| 1171 | dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry, |
| 1172 | list) { |
| 1173 | struct hostapd_wpa_psk *hpsk; |
| 1174 | |
| 1175 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for " |
| 1176 | MACSTR " psk=%d", |
| 1177 | MAC2STR(psk->addr), psk->p2p); |
| 1178 | hpsk = os_zalloc(sizeof(*hpsk)); |
| 1179 | if (hpsk == NULL) |
| 1180 | break; |
| 1181 | os_memcpy(hpsk->psk, psk->psk, PMK_LEN); |
| 1182 | if (psk->p2p) |
| 1183 | os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN); |
| 1184 | else |
| 1185 | os_memcpy(hpsk->addr, psk->addr, ETH_ALEN); |
| 1186 | hpsk->next = hapd->conf->ssid.wpa_psk; |
| 1187 | hapd->conf->ssid.wpa_psk = hpsk; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1192 | static void p2p_go_configured(void *ctx, void *data) |
| 1193 | { |
| 1194 | struct wpa_supplicant *wpa_s = ctx; |
| 1195 | struct p2p_go_neg_results *params = data; |
| 1196 | struct wpa_ssid *ssid; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1197 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1198 | |
| 1199 | ssid = wpa_s->current_ssid; |
| 1200 | if (ssid && ssid->mode == WPAS_MODE_P2P_GO) { |
| 1201 | wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning"); |
| 1202 | if (wpa_s->global->p2p_group_formation == wpa_s) |
| 1203 | wpa_s->global->p2p_group_formation = NULL; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1204 | if (os_strlen(params->passphrase) > 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1205 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 1206 | P2P_EVENT_GROUP_STARTED |
| 1207 | "%s GO ssid=\"%s\" freq=%d " |
| 1208 | "passphrase=\"%s\" go_dev_addr=" MACSTR |
| 1209 | "%s", wpa_s->ifname, |
| 1210 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len), |
| 1211 | ssid->frequency, params->passphrase, |
| 1212 | MAC2STR(wpa_s->global->p2p_dev_addr), |
| 1213 | params->persistent_group ? |
| 1214 | " [PERSISTENT]" : ""); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1215 | } else { |
| 1216 | char psk[65]; |
| 1217 | wpa_snprintf_hex(psk, sizeof(psk), params->psk, |
| 1218 | sizeof(params->psk)); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1219 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 1220 | P2P_EVENT_GROUP_STARTED |
| 1221 | "%s GO ssid=\"%s\" freq=%d psk=%s " |
| 1222 | "go_dev_addr=" MACSTR "%s", |
| 1223 | wpa_s->ifname, |
| 1224 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len), |
| 1225 | ssid->frequency, psk, |
| 1226 | MAC2STR(wpa_s->global->p2p_dev_addr), |
| 1227 | params->persistent_group ? |
| 1228 | " [PERSISTENT]" : ""); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1229 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1230 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1231 | os_get_reltime(&wpa_s->global->p2p_go_wait_client); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1232 | if (params->persistent_group) { |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1233 | network_id = wpas_p2p_store_persistent_group( |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1234 | wpa_s->parent, ssid, |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 1235 | wpa_s->global->p2p_dev_addr); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1236 | wpas_p2p_add_psk_list(wpa_s, ssid); |
| 1237 | } |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1238 | if (network_id < 0) |
| 1239 | network_id = ssid->id; |
| 1240 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1241 | wpas_p2p_cross_connect_setup(wpa_s); |
| 1242 | wpas_p2p_set_group_idle_timeout(wpa_s); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 1243 | |
| 1244 | if (wpa_s->p2p_first_connection_timeout) { |
| 1245 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1246 | "P2P: Start group formation timeout of %d seconds until first data connection on GO", |
| 1247 | wpa_s->p2p_first_connection_timeout); |
| 1248 | wpa_s->p2p_go_group_formation_completed = 0; |
| 1249 | wpa_s->global->p2p_group_formation = wpa_s; |
| 1250 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1251 | wpa_s->parent, NULL); |
| 1252 | eloop_register_timeout( |
| 1253 | wpa_s->p2p_first_connection_timeout, 0, |
| 1254 | wpas_p2p_group_formation_timeout, |
| 1255 | wpa_s->parent, NULL); |
| 1256 | } |
| 1257 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1258 | return; |
| 1259 | } |
| 1260 | |
| 1261 | wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning"); |
| 1262 | if (wpa_supplicant_ap_mac_addr_filter(wpa_s, |
| 1263 | params->peer_interface_addr)) { |
| 1264 | wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address " |
| 1265 | "filtering"); |
| 1266 | return; |
| 1267 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1268 | if (params->wps_method == WPS_PBC) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1269 | wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1270 | params->peer_device_addr); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1271 | #ifdef CONFIG_WPS_NFC |
| 1272 | } else if (params->wps_method == WPS_NFC) { |
| 1273 | if (wpa_s->parent->p2p_oob_dev_pw_id != |
| 1274 | DEV_PW_NFC_CONNECTION_HANDOVER && |
| 1275 | !wpa_s->parent->p2p_oob_dev_pw) { |
| 1276 | wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known"); |
| 1277 | return; |
| 1278 | } |
| 1279 | wpas_ap_wps_add_nfc_pw( |
| 1280 | wpa_s, wpa_s->parent->p2p_oob_dev_pw_id, |
| 1281 | wpa_s->parent->p2p_oob_dev_pw, |
| 1282 | wpa_s->parent->p2p_peer_oob_pk_hash_known ? |
| 1283 | wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL); |
| 1284 | #endif /* CONFIG_WPS_NFC */ |
| 1285 | } else if (wpa_s->p2p_pin[0]) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1286 | wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1287 | wpa_s->p2p_pin, NULL, 0, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1288 | os_free(wpa_s->go_params); |
| 1289 | wpa_s->go_params = NULL; |
| 1290 | } |
| 1291 | |
| 1292 | |
| 1293 | static void wpas_start_wps_go(struct wpa_supplicant *wpa_s, |
| 1294 | struct p2p_go_neg_results *params, |
| 1295 | int group_formation) |
| 1296 | { |
| 1297 | struct wpa_ssid *ssid; |
| 1298 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1299 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO"); |
| 1300 | if (wpas_copy_go_neg_results(wpa_s, params) < 0) { |
| 1301 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation " |
| 1302 | "results"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1303 | return; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1304 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1305 | |
| 1306 | ssid = wpa_config_add_network(wpa_s->conf); |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1307 | if (ssid == NULL) { |
| 1308 | 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] | 1309 | return; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1310 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1311 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1312 | wpa_s->show_group_started = 0; |
| 1313 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1314 | wpa_config_set_network_defaults(ssid); |
| 1315 | ssid->temporary = 1; |
| 1316 | ssid->p2p_group = 1; |
| 1317 | ssid->p2p_persistent_group = params->persistent_group; |
| 1318 | ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION : |
| 1319 | WPAS_MODE_P2P_GO; |
| 1320 | ssid->frequency = params->freq; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1321 | ssid->ht40 = params->ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1322 | ssid->vht = params->vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1323 | ssid->ssid = os_zalloc(params->ssid_len + 1); |
| 1324 | if (ssid->ssid) { |
| 1325 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); |
| 1326 | ssid->ssid_len = params->ssid_len; |
| 1327 | } |
| 1328 | ssid->auth_alg = WPA_AUTH_ALG_OPEN; |
| 1329 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; |
| 1330 | ssid->proto = WPA_PROTO_RSN; |
| 1331 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1332 | if (os_strlen(params->passphrase) > 0) { |
| 1333 | ssid->passphrase = os_strdup(params->passphrase); |
| 1334 | if (ssid->passphrase == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1335 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 1336 | "P2P: Failed to copy passphrase for GO"); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1337 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 1338 | return; |
| 1339 | } |
| 1340 | } else |
| 1341 | ssid->passphrase = NULL; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1342 | ssid->psk_set = params->psk_set; |
| 1343 | if (ssid->psk_set) |
| 1344 | os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1345 | else if (ssid->passphrase) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1346 | wpa_config_update_psk(ssid); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1347 | ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1348 | |
| 1349 | wpa_s->ap_configured_cb = p2p_go_configured; |
| 1350 | wpa_s->ap_configured_cb_ctx = wpa_s; |
| 1351 | wpa_s->ap_configured_cb_data = wpa_s->go_params; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1352 | wpa_s->connect_without_scan = ssid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1353 | wpa_s->reassociate = 1; |
| 1354 | wpa_s->disconnected = 0; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1355 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to " |
| 1356 | "start GO)"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1357 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
| 1358 | } |
| 1359 | |
| 1360 | |
| 1361 | static void wpas_p2p_clone_config(struct wpa_supplicant *dst, |
| 1362 | const struct wpa_supplicant *src) |
| 1363 | { |
| 1364 | struct wpa_config *d; |
| 1365 | const struct wpa_config *s; |
| 1366 | |
| 1367 | d = dst->conf; |
| 1368 | s = src->conf; |
| 1369 | |
| 1370 | #define C(n) if (s->n) d->n = os_strdup(s->n) |
| 1371 | C(device_name); |
| 1372 | C(manufacturer); |
| 1373 | C(model_name); |
| 1374 | C(model_number); |
| 1375 | C(serial_number); |
| 1376 | C(config_methods); |
| 1377 | #undef C |
| 1378 | |
| 1379 | os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN); |
| 1380 | os_memcpy(d->sec_device_type, s->sec_device_type, |
| 1381 | sizeof(d->sec_device_type)); |
| 1382 | d->num_sec_device_types = s->num_sec_device_types; |
| 1383 | |
| 1384 | d->p2p_group_idle = s->p2p_group_idle; |
| 1385 | d->p2p_intra_bss = s->p2p_intra_bss; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1386 | d->persistent_reconnect = s->persistent_reconnect; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1387 | d->max_num_sta = s->max_num_sta; |
| 1388 | d->pbc_in_m1 = s->pbc_in_m1; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1389 | d->ignore_old_scan_res = s->ignore_old_scan_res; |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 1390 | d->beacon_int = s->beacon_int; |
Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 1391 | d->dtim_period = s->dtim_period; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 1392 | d->disassoc_low_ack = s->disassoc_low_ack; |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 1393 | d->disable_scan_offload = s->disable_scan_offload; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1394 | |
| 1395 | if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) { |
| 1396 | d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey); |
| 1397 | d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey); |
| 1398 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1402 | static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s, |
| 1403 | char *ifname, size_t len) |
| 1404 | { |
| 1405 | char *ifname_ptr = wpa_s->ifname; |
| 1406 | |
| 1407 | if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX, |
| 1408 | os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) { |
| 1409 | ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1; |
| 1410 | } |
| 1411 | |
| 1412 | os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx); |
| 1413 | if (os_strlen(ifname) >= IFNAMSIZ && |
| 1414 | os_strlen(wpa_s->ifname) < IFNAMSIZ) { |
| 1415 | /* Try to avoid going over the IFNAMSIZ length limit */ |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1416 | os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1421 | static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, |
| 1422 | enum wpa_driver_if_type type) |
| 1423 | { |
| 1424 | char ifname[120], force_ifname[120]; |
| 1425 | |
| 1426 | if (wpa_s->pending_interface_name[0]) { |
| 1427 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists " |
| 1428 | "- skip creation of a new one"); |
| 1429 | if (is_zero_ether_addr(wpa_s->pending_interface_addr)) { |
| 1430 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual address " |
| 1431 | "unknown?! ifname='%s'", |
| 1432 | wpa_s->pending_interface_name); |
| 1433 | return -1; |
| 1434 | } |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1438 | wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1439 | force_ifname[0] = '\0'; |
| 1440 | |
| 1441 | wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group", |
| 1442 | ifname); |
| 1443 | wpa_s->p2p_group_idx++; |
| 1444 | |
| 1445 | wpa_s->pending_interface_type = type; |
| 1446 | if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname, |
| 1447 | wpa_s->pending_interface_addr, NULL) < 0) { |
| 1448 | wpa_printf(MSG_ERROR, "P2P: Failed to create new group " |
| 1449 | "interface"); |
| 1450 | return -1; |
| 1451 | } |
| 1452 | |
| 1453 | if (force_ifname[0]) { |
| 1454 | wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s", |
| 1455 | force_ifname); |
| 1456 | os_strlcpy(wpa_s->pending_interface_name, force_ifname, |
| 1457 | sizeof(wpa_s->pending_interface_name)); |
| 1458 | } else |
| 1459 | os_strlcpy(wpa_s->pending_interface_name, ifname, |
| 1460 | sizeof(wpa_s->pending_interface_name)); |
| 1461 | wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr " |
| 1462 | MACSTR, wpa_s->pending_interface_name, |
| 1463 | MAC2STR(wpa_s->pending_interface_addr)); |
| 1464 | |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
| 1468 | |
| 1469 | static void wpas_p2p_remove_pending_group_interface( |
| 1470 | struct wpa_supplicant *wpa_s) |
| 1471 | { |
| 1472 | if (!wpa_s->pending_interface_name[0] || |
| 1473 | is_zero_ether_addr(wpa_s->pending_interface_addr)) |
| 1474 | return; /* No pending virtual interface */ |
| 1475 | |
| 1476 | wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s", |
| 1477 | wpa_s->pending_interface_name); |
| 1478 | wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type, |
| 1479 | wpa_s->pending_interface_name); |
| 1480 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); |
| 1481 | wpa_s->pending_interface_name[0] = '\0'; |
| 1482 | } |
| 1483 | |
| 1484 | |
| 1485 | static struct wpa_supplicant * |
| 1486 | wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go) |
| 1487 | { |
| 1488 | struct wpa_interface iface; |
| 1489 | struct wpa_supplicant *group_wpa_s; |
| 1490 | |
| 1491 | if (!wpa_s->pending_interface_name[0]) { |
| 1492 | wpa_printf(MSG_ERROR, "P2P: No pending group interface"); |
| 1493 | if (!wpas_p2p_create_iface(wpa_s)) |
| 1494 | return NULL; |
| 1495 | /* |
| 1496 | * Something has forced us to remove the pending interface; try |
| 1497 | * to create a new one and hope for the best that we will get |
| 1498 | * the same local address. |
| 1499 | */ |
| 1500 | if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : |
| 1501 | WPA_IF_P2P_CLIENT) < 0) |
| 1502 | return NULL; |
| 1503 | } |
| 1504 | |
| 1505 | os_memset(&iface, 0, sizeof(iface)); |
| 1506 | iface.ifname = wpa_s->pending_interface_name; |
| 1507 | iface.driver = wpa_s->driver->name; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1508 | if (wpa_s->conf->ctrl_interface == NULL && |
| 1509 | wpa_s->parent != wpa_s && |
| 1510 | wpa_s->p2p_mgmt && |
| 1511 | (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) |
| 1512 | iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface; |
| 1513 | else |
| 1514 | iface.ctrl_interface = wpa_s->conf->ctrl_interface; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1515 | iface.driver_param = wpa_s->conf->driver_param; |
| 1516 | group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface); |
| 1517 | if (group_wpa_s == NULL) { |
| 1518 | wpa_printf(MSG_ERROR, "P2P: Failed to create new " |
| 1519 | "wpa_supplicant interface"); |
| 1520 | return NULL; |
| 1521 | } |
| 1522 | wpa_s->pending_interface_name[0] = '\0'; |
| 1523 | group_wpa_s->parent = wpa_s; |
| 1524 | group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO : |
| 1525 | P2P_GROUP_INTERFACE_CLIENT; |
| 1526 | wpa_s->global->p2p_group_formation = group_wpa_s; |
| 1527 | |
| 1528 | wpas_p2p_clone_config(group_wpa_s, wpa_s); |
| 1529 | |
| 1530 | return group_wpa_s; |
| 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, |
| 1535 | void *timeout_ctx) |
| 1536 | { |
| 1537 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 1538 | wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out"); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1539 | wpas_p2p_group_formation_failed(wpa_s); |
| 1540 | } |
| 1541 | |
| 1542 | |
| 1543 | void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s) |
| 1544 | { |
| 1545 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1546 | wpa_s->parent, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1547 | if (wpa_s->global->p2p) |
| 1548 | p2p_group_formation_failed(wpa_s->global->p2p); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1549 | wpas_group_formation_completed(wpa_s, 0); |
| 1550 | } |
| 1551 | |
| 1552 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1553 | static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s) |
| 1554 | { |
| 1555 | wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure"); |
| 1556 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1557 | wpa_s->parent, NULL); |
| 1558 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 1559 | wpa_s->parent, NULL); |
| 1560 | wpa_s->global->p2p_fail_on_wps_complete = 0; |
| 1561 | } |
| 1562 | |
| 1563 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1564 | void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s) |
| 1565 | { |
| 1566 | if (wpa_s->global->p2p_group_formation != wpa_s) |
| 1567 | return; |
| 1568 | /* Speed up group formation timeout since this cannot succeed */ |
| 1569 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1570 | wpa_s->parent, NULL); |
| 1571 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 1572 | wpa_s->parent, NULL); |
| 1573 | } |
| 1574 | |
| 1575 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1576 | 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] | 1577 | { |
| 1578 | struct wpa_supplicant *wpa_s = ctx; |
| 1579 | |
| 1580 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 1581 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 1582 | wpa_s->off_channel_freq = 0; |
| 1583 | wpa_s->roc_waiting_drv_freq = 0; |
| 1584 | } |
| 1585 | |
| 1586 | if (res->status) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1587 | wpa_msg_global(wpa_s, MSG_INFO, |
| 1588 | P2P_EVENT_GO_NEG_FAILURE "status=%d", |
| 1589 | res->status); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1590 | wpas_notify_p2p_go_neg_completed(wpa_s, res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1591 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 1592 | return; |
| 1593 | } |
| 1594 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1595 | if (wpa_s->p2p_go_ht40) |
| 1596 | res->ht40 = 1; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1597 | if (wpa_s->p2p_go_vht) |
| 1598 | res->vht = 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1599 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 1600 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s " |
| 1601 | "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR |
| 1602 | " wps_method=%s", |
| 1603 | res->role_go ? "GO" : "client", res->freq, res->ht40, |
| 1604 | MAC2STR(res->peer_device_addr), |
| 1605 | MAC2STR(res->peer_interface_addr), |
| 1606 | p2p_wps_method_text(res->wps_method)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1607 | wpas_notify_p2p_go_neg_completed(wpa_s, res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1608 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1609 | if (res->role_go && wpa_s->p2p_persistent_id >= 0) { |
| 1610 | struct wpa_ssid *ssid; |
| 1611 | ssid = wpa_config_get_network(wpa_s->conf, |
| 1612 | wpa_s->p2p_persistent_id); |
| 1613 | if (ssid && ssid->disabled == 2 && |
| 1614 | ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) { |
| 1615 | size_t len = os_strlen(ssid->passphrase); |
| 1616 | wpa_printf(MSG_DEBUG, "P2P: Override passphrase based " |
| 1617 | "on requested persistent group"); |
| 1618 | os_memcpy(res->passphrase, ssid->passphrase, len); |
| 1619 | res->passphrase[len] = '\0'; |
| 1620 | } |
| 1621 | } |
| 1622 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1623 | if (wpa_s->create_p2p_iface) { |
| 1624 | struct wpa_supplicant *group_wpa_s = |
| 1625 | wpas_p2p_init_group_interface(wpa_s, res->role_go); |
| 1626 | if (group_wpa_s == NULL) { |
| 1627 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 1628 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, |
| 1629 | wpa_s, NULL); |
| 1630 | wpas_p2p_group_formation_failed(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1631 | return; |
| 1632 | } |
| 1633 | if (group_wpa_s != wpa_s) { |
| 1634 | os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin, |
| 1635 | sizeof(group_wpa_s->p2p_pin)); |
| 1636 | group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method; |
| 1637 | } |
| 1638 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); |
| 1639 | wpa_s->pending_interface_name[0] = '\0'; |
| 1640 | group_wpa_s->p2p_in_provisioning = 1; |
| 1641 | |
| 1642 | if (res->role_go) |
| 1643 | wpas_start_wps_go(group_wpa_s, res, 1); |
| 1644 | else |
| 1645 | wpas_start_wps_enrollee(group_wpa_s, res); |
| 1646 | } else { |
| 1647 | wpa_s->p2p_in_provisioning = 1; |
| 1648 | wpa_s->global->p2p_group_formation = wpa_s; |
| 1649 | |
| 1650 | if (res->role_go) |
| 1651 | wpas_start_wps_go(wpa_s, res, 1); |
| 1652 | else |
| 1653 | wpas_start_wps_enrollee(ctx, res); |
| 1654 | } |
| 1655 | |
| 1656 | wpa_s->p2p_long_listen = 0; |
| 1657 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 1658 | |
| 1659 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 1660 | eloop_register_timeout(15 + res->peer_config_timeout / 100, |
| 1661 | (res->peer_config_timeout % 100) * 10000, |
| 1662 | wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 1663 | } |
| 1664 | |
| 1665 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1666 | 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] | 1667 | { |
| 1668 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1669 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR |
| 1670 | " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1671 | |
| 1672 | wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id); |
| 1673 | } |
| 1674 | |
| 1675 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1676 | static void wpas_dev_found(void *ctx, const u8 *addr, |
| 1677 | const struct p2p_peer_info *info, |
| 1678 | int new_device) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1679 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1680 | #ifndef CONFIG_NO_STDOUT_DEBUG |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1681 | struct wpa_supplicant *wpa_s = ctx; |
| 1682 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1683 | char *wfd_dev_info_hex = NULL; |
| 1684 | |
Irfan Sheriff | 8367dc9 | 2012-09-09 17:08:19 -0700 | [diff] [blame] | 1685 | #ifdef CONFIG_WIFI_DISPLAY |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1686 | wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems, |
| 1687 | WFD_SUBELEM_DEVICE_INFO); |
Irfan Sheriff | 8367dc9 | 2012-09-09 17:08:19 -0700 | [diff] [blame] | 1688 | #endif /* CONFIG_WIFI_DISPLAY */ |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1689 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1690 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR |
| 1691 | " p2p_dev_addr=" MACSTR |
| 1692 | " pri_dev_type=%s name='%s' config_methods=0x%x " |
| 1693 | "dev_capab=0x%x group_capab=0x%x%s%s", |
| 1694 | MAC2STR(addr), MAC2STR(info->p2p_device_addr), |
| 1695 | wps_dev_type_bin2str(info->pri_dev_type, devtype, |
| 1696 | sizeof(devtype)), |
| 1697 | info->device_name, info->config_methods, |
| 1698 | info->dev_capab, info->group_capab, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1699 | wfd_dev_info_hex ? " wfd_dev_info=0x" : "", |
| 1700 | wfd_dev_info_hex ? wfd_dev_info_hex : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1701 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1702 | os_free(wfd_dev_info_hex); |
Dmitry Shmidt | 9767226 | 2014-02-03 13:02:54 -0800 | [diff] [blame] | 1703 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1704 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1705 | wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device); |
| 1706 | } |
| 1707 | |
| 1708 | |
| 1709 | static void wpas_dev_lost(void *ctx, const u8 *dev_addr) |
| 1710 | { |
| 1711 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1712 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1713 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST |
| 1714 | "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1715 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1716 | wpas_notify_p2p_device_lost(wpa_s, dev_addr); |
| 1717 | } |
| 1718 | |
| 1719 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1720 | static void wpas_find_stopped(void *ctx) |
| 1721 | { |
| 1722 | struct wpa_supplicant *wpa_s = ctx; |
| 1723 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED); |
| 1724 | } |
| 1725 | |
| 1726 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1727 | struct wpas_p2p_listen_work { |
| 1728 | unsigned int freq; |
| 1729 | unsigned int duration; |
| 1730 | struct wpabuf *probe_resp_ie; |
| 1731 | }; |
| 1732 | |
| 1733 | |
| 1734 | static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork) |
| 1735 | { |
| 1736 | if (lwork == NULL) |
| 1737 | return; |
| 1738 | wpabuf_free(lwork->probe_resp_ie); |
| 1739 | os_free(lwork); |
| 1740 | } |
| 1741 | |
| 1742 | |
| 1743 | static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s) |
| 1744 | { |
| 1745 | struct wpas_p2p_listen_work *lwork; |
| 1746 | |
| 1747 | if (!wpa_s->p2p_listen_work) |
| 1748 | return; |
| 1749 | |
| 1750 | lwork = wpa_s->p2p_listen_work->ctx; |
| 1751 | wpas_p2p_listen_work_free(lwork); |
| 1752 | radio_work_done(wpa_s->p2p_listen_work); |
| 1753 | wpa_s->p2p_listen_work = NULL; |
| 1754 | } |
| 1755 | |
| 1756 | |
| 1757 | static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit) |
| 1758 | { |
| 1759 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 1760 | struct wpas_p2p_listen_work *lwork = work->ctx; |
| 1761 | |
| 1762 | if (deinit) { |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 1763 | if (work->started) { |
| 1764 | wpa_s->p2p_listen_work = NULL; |
| 1765 | wpas_stop_listen(wpa_s); |
| 1766 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1767 | wpas_p2p_listen_work_free(lwork); |
| 1768 | return; |
| 1769 | } |
| 1770 | |
| 1771 | wpa_s->p2p_listen_work = work; |
| 1772 | |
| 1773 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL); |
| 1774 | |
| 1775 | if (wpa_drv_probe_req_report(wpa_s, 1) < 0) { |
| 1776 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to " |
| 1777 | "report received Probe Request frames"); |
| 1778 | wpas_p2p_listen_work_done(wpa_s); |
| 1779 | return; |
| 1780 | } |
| 1781 | |
| 1782 | wpa_s->pending_listen_freq = lwork->freq; |
| 1783 | wpa_s->pending_listen_duration = lwork->duration; |
| 1784 | |
| 1785 | if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0) |
| 1786 | { |
| 1787 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver " |
| 1788 | "to remain on channel (%u MHz) for Listen " |
| 1789 | "state", lwork->freq); |
| 1790 | wpas_p2p_listen_work_done(wpa_s); |
| 1791 | wpa_s->pending_listen_freq = 0; |
| 1792 | return; |
| 1793 | } |
| 1794 | wpa_s->off_channel_freq = 0; |
| 1795 | wpa_s->roc_waiting_drv_freq = lwork->freq; |
| 1796 | } |
| 1797 | |
| 1798 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1799 | static int wpas_start_listen(void *ctx, unsigned int freq, |
| 1800 | unsigned int duration, |
| 1801 | const struct wpabuf *probe_resp_ie) |
| 1802 | { |
| 1803 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1804 | struct wpas_p2p_listen_work *lwork; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1805 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1806 | if (wpa_s->p2p_listen_work) { |
| 1807 | 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] | 1808 | return -1; |
| 1809 | } |
| 1810 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1811 | lwork = os_zalloc(sizeof(*lwork)); |
| 1812 | if (lwork == NULL) |
| 1813 | return -1; |
| 1814 | lwork->freq = freq; |
| 1815 | lwork->duration = duration; |
| 1816 | if (probe_resp_ie) { |
| 1817 | lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie); |
| 1818 | if (lwork->probe_resp_ie == NULL) { |
| 1819 | wpas_p2p_listen_work_free(lwork); |
| 1820 | return -1; |
| 1821 | } |
| 1822 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1823 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1824 | if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb, |
| 1825 | lwork) < 0) { |
| 1826 | wpas_p2p_listen_work_free(lwork); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1827 | return -1; |
| 1828 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1829 | |
| 1830 | return 0; |
| 1831 | } |
| 1832 | |
| 1833 | |
| 1834 | static void wpas_stop_listen(void *ctx) |
| 1835 | { |
| 1836 | struct wpa_supplicant *wpa_s = ctx; |
| 1837 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 1838 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 1839 | wpa_s->off_channel_freq = 0; |
| 1840 | wpa_s->roc_waiting_drv_freq = 0; |
| 1841 | } |
| 1842 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL); |
| 1843 | wpa_drv_probe_req_report(wpa_s, 0); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1844 | wpas_p2p_listen_work_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | |
| 1848 | static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf) |
| 1849 | { |
| 1850 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1851 | 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] | 1852 | } |
| 1853 | |
| 1854 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1855 | /* |
| 1856 | * DNS Header section is used only to calculate compression pointers, so the |
| 1857 | * contents of this data does not matter, but the length needs to be reserved |
| 1858 | * in the virtual packet. |
| 1859 | */ |
| 1860 | #define DNS_HEADER_LEN 12 |
| 1861 | |
| 1862 | /* |
| 1863 | * 27-octet in-memory packet from P2P specification containing two implied |
| 1864 | * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN |
| 1865 | */ |
| 1866 | #define P2P_SD_IN_MEMORY_LEN 27 |
| 1867 | |
| 1868 | static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start, |
| 1869 | u8 **spos, const u8 *end) |
| 1870 | { |
| 1871 | while (*spos < end) { |
| 1872 | u8 val = ((*spos)[0] & 0xc0) >> 6; |
| 1873 | int len; |
| 1874 | |
| 1875 | if (val == 1 || val == 2) { |
| 1876 | /* These are reserved values in RFC 1035 */ |
| 1877 | wpa_printf(MSG_DEBUG, "P2P: Invalid domain name " |
| 1878 | "sequence starting with 0x%x", val); |
| 1879 | return -1; |
| 1880 | } |
| 1881 | |
| 1882 | if (val == 3) { |
| 1883 | u16 offset; |
| 1884 | u8 *spos_tmp; |
| 1885 | |
| 1886 | /* Offset */ |
| 1887 | if (*spos + 2 > end) { |
| 1888 | wpa_printf(MSG_DEBUG, "P2P: No room for full " |
| 1889 | "DNS offset field"); |
| 1890 | return -1; |
| 1891 | } |
| 1892 | |
| 1893 | offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1]; |
| 1894 | if (offset >= *spos - start) { |
| 1895 | wpa_printf(MSG_DEBUG, "P2P: Invalid DNS " |
| 1896 | "pointer offset %u", offset); |
| 1897 | return -1; |
| 1898 | } |
| 1899 | |
| 1900 | (*spos) += 2; |
| 1901 | spos_tmp = start + offset; |
| 1902 | return p2p_sd_dns_uncompress_label(upos, uend, start, |
| 1903 | &spos_tmp, |
| 1904 | *spos - 2); |
| 1905 | } |
| 1906 | |
| 1907 | /* Label */ |
| 1908 | len = (*spos)[0] & 0x3f; |
| 1909 | if (len == 0) |
| 1910 | return 0; |
| 1911 | |
| 1912 | (*spos)++; |
| 1913 | if (*spos + len > end) { |
| 1914 | wpa_printf(MSG_DEBUG, "P2P: Invalid domain name " |
| 1915 | "sequence - no room for label with length " |
| 1916 | "%u", len); |
| 1917 | return -1; |
| 1918 | } |
| 1919 | |
| 1920 | if (*upos + len + 2 > uend) |
| 1921 | return -2; |
| 1922 | |
| 1923 | os_memcpy(*upos, *spos, len); |
| 1924 | *spos += len; |
| 1925 | *upos += len; |
| 1926 | (*upos)[0] = '.'; |
| 1927 | (*upos)++; |
| 1928 | (*upos)[0] = '\0'; |
| 1929 | } |
| 1930 | |
| 1931 | return 0; |
| 1932 | } |
| 1933 | |
| 1934 | |
| 1935 | /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet. |
| 1936 | * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is |
| 1937 | * not large enough */ |
| 1938 | static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg, |
| 1939 | size_t msg_len, size_t offset) |
| 1940 | { |
| 1941 | /* 27-octet in-memory packet from P2P specification */ |
| 1942 | const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01" |
| 1943 | "\x04_udp\xC0\x11\x00\x0C\x00\x01"; |
| 1944 | u8 *tmp, *end, *spos; |
| 1945 | char *upos, *uend; |
| 1946 | int ret = 0; |
| 1947 | |
| 1948 | if (buf_len < 2) |
| 1949 | return -1; |
| 1950 | if (offset > msg_len) |
| 1951 | return -1; |
| 1952 | |
| 1953 | tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len); |
| 1954 | if (tmp == NULL) |
| 1955 | return -1; |
| 1956 | spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN; |
| 1957 | end = spos + msg_len; |
| 1958 | spos += offset; |
| 1959 | |
| 1960 | os_memset(tmp, 0, DNS_HEADER_LEN); |
| 1961 | os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN); |
| 1962 | os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len); |
| 1963 | |
| 1964 | upos = buf; |
| 1965 | uend = buf + buf_len; |
| 1966 | |
| 1967 | ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end); |
| 1968 | if (ret) { |
| 1969 | os_free(tmp); |
| 1970 | return ret; |
| 1971 | } |
| 1972 | |
| 1973 | if (upos == buf) { |
| 1974 | upos[0] = '.'; |
| 1975 | upos[1] = '\0'; |
| 1976 | } else if (upos[-1] == '.') |
| 1977 | upos[-1] = '\0'; |
| 1978 | |
| 1979 | os_free(tmp); |
| 1980 | return 0; |
| 1981 | } |
| 1982 | |
| 1983 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1984 | static struct p2p_srv_bonjour * |
| 1985 | wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s, |
| 1986 | const struct wpabuf *query) |
| 1987 | { |
| 1988 | struct p2p_srv_bonjour *bsrv; |
| 1989 | size_t len; |
| 1990 | |
| 1991 | len = wpabuf_len(query); |
| 1992 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, |
| 1993 | struct p2p_srv_bonjour, list) { |
| 1994 | if (len == wpabuf_len(bsrv->query) && |
| 1995 | os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query), |
| 1996 | len) == 0) |
| 1997 | return bsrv; |
| 1998 | } |
| 1999 | return NULL; |
| 2000 | } |
| 2001 | |
| 2002 | |
| 2003 | static struct p2p_srv_upnp * |
| 2004 | wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version, |
| 2005 | const char *service) |
| 2006 | { |
| 2007 | struct p2p_srv_upnp *usrv; |
| 2008 | |
| 2009 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
| 2010 | struct p2p_srv_upnp, list) { |
| 2011 | if (version == usrv->version && |
| 2012 | os_strcmp(service, usrv->service) == 0) |
| 2013 | return usrv; |
| 2014 | } |
| 2015 | return NULL; |
| 2016 | } |
| 2017 | |
| 2018 | |
| 2019 | static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto, |
| 2020 | u8 srv_trans_id) |
| 2021 | { |
| 2022 | u8 *len_pos; |
| 2023 | |
| 2024 | if (wpabuf_tailroom(resp) < 5) |
| 2025 | return; |
| 2026 | |
| 2027 | /* Length (to be filled) */ |
| 2028 | len_pos = wpabuf_put(resp, 2); |
| 2029 | wpabuf_put_u8(resp, srv_proto); |
| 2030 | wpabuf_put_u8(resp, srv_trans_id); |
| 2031 | /* Status Code */ |
| 2032 | wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE); |
| 2033 | /* Response Data: empty */ |
| 2034 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 2035 | } |
| 2036 | |
| 2037 | |
| 2038 | static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s, |
| 2039 | struct wpabuf *resp, u8 srv_trans_id) |
| 2040 | { |
| 2041 | struct p2p_srv_bonjour *bsrv; |
| 2042 | u8 *len_pos; |
| 2043 | |
| 2044 | wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services"); |
| 2045 | |
| 2046 | if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { |
| 2047 | wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available"); |
| 2048 | return; |
| 2049 | } |
| 2050 | |
| 2051 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, |
| 2052 | struct p2p_srv_bonjour, list) { |
| 2053 | if (wpabuf_tailroom(resp) < |
| 2054 | 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) |
| 2055 | return; |
| 2056 | /* Length (to be filled) */ |
| 2057 | len_pos = wpabuf_put(resp, 2); |
| 2058 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); |
| 2059 | wpabuf_put_u8(resp, srv_trans_id); |
| 2060 | /* Status Code */ |
| 2061 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 2062 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service", |
| 2063 | wpabuf_head(bsrv->resp), |
| 2064 | wpabuf_len(bsrv->resp)); |
| 2065 | /* Response Data */ |
| 2066 | wpabuf_put_buf(resp, bsrv->query); /* Key */ |
| 2067 | wpabuf_put_buf(resp, bsrv->resp); /* Value */ |
| 2068 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - |
| 2069 | 2); |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2074 | static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query, |
| 2075 | size_t query_len) |
| 2076 | { |
| 2077 | char str_rx[256], str_srv[256]; |
| 2078 | |
| 2079 | if (query_len < 3 || wpabuf_len(bsrv->query) < 3) |
| 2080 | return 0; /* Too short to include DNS Type and Version */ |
| 2081 | if (os_memcmp(query + query_len - 3, |
| 2082 | wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3, |
| 2083 | 3) != 0) |
| 2084 | return 0; /* Mismatch in DNS Type or Version */ |
| 2085 | if (query_len == wpabuf_len(bsrv->query) && |
| 2086 | os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0) |
| 2087 | return 1; /* Binary match */ |
| 2088 | |
| 2089 | if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3, |
| 2090 | 0)) |
| 2091 | return 0; /* Failed to uncompress query */ |
| 2092 | if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv), |
| 2093 | wpabuf_head(bsrv->query), |
| 2094 | wpabuf_len(bsrv->query) - 3, 0)) |
| 2095 | return 0; /* Failed to uncompress service */ |
| 2096 | |
| 2097 | return os_strcmp(str_rx, str_srv) == 0; |
| 2098 | } |
| 2099 | |
| 2100 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2101 | static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s, |
| 2102 | struct wpabuf *resp, u8 srv_trans_id, |
| 2103 | const u8 *query, size_t query_len) |
| 2104 | { |
| 2105 | struct p2p_srv_bonjour *bsrv; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2106 | u8 *len_pos; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2107 | int matches = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2108 | |
| 2109 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour", |
| 2110 | query, query_len); |
| 2111 | if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { |
| 2112 | wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available"); |
| 2113 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR, |
| 2114 | srv_trans_id); |
| 2115 | return; |
| 2116 | } |
| 2117 | |
| 2118 | if (query_len == 0) { |
| 2119 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); |
| 2120 | return; |
| 2121 | } |
| 2122 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2123 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, |
| 2124 | struct p2p_srv_bonjour, list) { |
| 2125 | if (!match_bonjour_query(bsrv, query, query_len)) |
| 2126 | continue; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2127 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2128 | if (wpabuf_tailroom(resp) < |
| 2129 | 5 + query_len + wpabuf_len(bsrv->resp)) |
| 2130 | return; |
| 2131 | |
| 2132 | matches++; |
| 2133 | |
| 2134 | /* Length (to be filled) */ |
| 2135 | len_pos = wpabuf_put(resp, 2); |
| 2136 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); |
| 2137 | wpabuf_put_u8(resp, srv_trans_id); |
| 2138 | |
| 2139 | /* Status Code */ |
| 2140 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 2141 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service", |
| 2142 | wpabuf_head(bsrv->resp), |
| 2143 | wpabuf_len(bsrv->resp)); |
| 2144 | |
| 2145 | /* Response Data */ |
| 2146 | wpabuf_put_data(resp, query, query_len); /* Key */ |
| 2147 | wpabuf_put_buf(resp, bsrv->resp); /* Value */ |
| 2148 | |
| 2149 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 2150 | } |
| 2151 | |
| 2152 | if (matches == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2153 | wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not " |
| 2154 | "available"); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2155 | if (wpabuf_tailroom(resp) < 5) |
| 2156 | return; |
| 2157 | |
| 2158 | /* Length (to be filled) */ |
| 2159 | len_pos = wpabuf_put(resp, 2); |
| 2160 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); |
| 2161 | wpabuf_put_u8(resp, srv_trans_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2162 | |
| 2163 | /* Status Code */ |
| 2164 | wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); |
| 2165 | /* Response Data: empty */ |
| 2166 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - |
| 2167 | 2); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2168 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | |
| 2172 | static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s, |
| 2173 | struct wpabuf *resp, u8 srv_trans_id) |
| 2174 | { |
| 2175 | struct p2p_srv_upnp *usrv; |
| 2176 | u8 *len_pos; |
| 2177 | |
| 2178 | wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services"); |
| 2179 | |
| 2180 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) { |
| 2181 | wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available"); |
| 2182 | return; |
| 2183 | } |
| 2184 | |
| 2185 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
| 2186 | struct p2p_srv_upnp, list) { |
| 2187 | if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service)) |
| 2188 | return; |
| 2189 | |
| 2190 | /* Length (to be filled) */ |
| 2191 | len_pos = wpabuf_put(resp, 2); |
| 2192 | wpabuf_put_u8(resp, P2P_SERV_UPNP); |
| 2193 | wpabuf_put_u8(resp, srv_trans_id); |
| 2194 | |
| 2195 | /* Status Code */ |
| 2196 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 2197 | /* Response Data */ |
| 2198 | wpabuf_put_u8(resp, usrv->version); |
| 2199 | wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s", |
| 2200 | usrv->service); |
| 2201 | wpabuf_put_str(resp, usrv->service); |
| 2202 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - |
| 2203 | 2); |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | |
| 2208 | static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s, |
| 2209 | struct wpabuf *resp, u8 srv_trans_id, |
| 2210 | const u8 *query, size_t query_len) |
| 2211 | { |
| 2212 | struct p2p_srv_upnp *usrv; |
| 2213 | u8 *len_pos; |
| 2214 | u8 version; |
| 2215 | char *str; |
| 2216 | int count = 0; |
| 2217 | |
| 2218 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP", |
| 2219 | query, query_len); |
| 2220 | |
| 2221 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) { |
| 2222 | wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available"); |
| 2223 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP, |
| 2224 | srv_trans_id); |
| 2225 | return; |
| 2226 | } |
| 2227 | |
| 2228 | if (query_len == 0) { |
| 2229 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); |
| 2230 | return; |
| 2231 | } |
| 2232 | |
| 2233 | if (wpabuf_tailroom(resp) < 5) |
| 2234 | return; |
| 2235 | |
| 2236 | /* Length (to be filled) */ |
| 2237 | len_pos = wpabuf_put(resp, 2); |
| 2238 | wpabuf_put_u8(resp, P2P_SERV_UPNP); |
| 2239 | wpabuf_put_u8(resp, srv_trans_id); |
| 2240 | |
| 2241 | version = query[0]; |
| 2242 | str = os_malloc(query_len); |
| 2243 | if (str == NULL) |
| 2244 | return; |
| 2245 | os_memcpy(str, query + 1, query_len - 1); |
| 2246 | str[query_len - 1] = '\0'; |
| 2247 | |
| 2248 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
| 2249 | struct p2p_srv_upnp, list) { |
| 2250 | if (version != usrv->version) |
| 2251 | continue; |
| 2252 | |
| 2253 | if (os_strcmp(str, "ssdp:all") != 0 && |
| 2254 | os_strstr(usrv->service, str) == NULL) |
| 2255 | continue; |
| 2256 | |
| 2257 | if (wpabuf_tailroom(resp) < 2) |
| 2258 | break; |
| 2259 | if (count == 0) { |
| 2260 | /* Status Code */ |
| 2261 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 2262 | /* Response Data */ |
| 2263 | wpabuf_put_u8(resp, version); |
| 2264 | } else |
| 2265 | wpabuf_put_u8(resp, ','); |
| 2266 | |
| 2267 | count++; |
| 2268 | |
| 2269 | wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s", |
| 2270 | usrv->service); |
| 2271 | if (wpabuf_tailroom(resp) < os_strlen(usrv->service)) |
| 2272 | break; |
| 2273 | wpabuf_put_str(resp, usrv->service); |
| 2274 | } |
| 2275 | os_free(str); |
| 2276 | |
| 2277 | if (count == 0) { |
| 2278 | wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not " |
| 2279 | "available"); |
| 2280 | /* Status Code */ |
| 2281 | wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); |
| 2282 | /* Response Data: empty */ |
| 2283 | } |
| 2284 | |
| 2285 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 2286 | } |
| 2287 | |
| 2288 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2289 | #ifdef CONFIG_WIFI_DISPLAY |
| 2290 | static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s, |
| 2291 | struct wpabuf *resp, u8 srv_trans_id, |
| 2292 | const u8 *query, size_t query_len) |
| 2293 | { |
| 2294 | const u8 *pos; |
| 2295 | u8 role; |
| 2296 | u8 *len_pos; |
| 2297 | |
| 2298 | wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len); |
| 2299 | |
| 2300 | if (!wpa_s->global->wifi_display) { |
| 2301 | wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available"); |
| 2302 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY, |
| 2303 | srv_trans_id); |
| 2304 | return; |
| 2305 | } |
| 2306 | |
| 2307 | if (query_len < 1) { |
| 2308 | wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device " |
| 2309 | "Role"); |
| 2310 | return; |
| 2311 | } |
| 2312 | |
| 2313 | if (wpabuf_tailroom(resp) < 5) |
| 2314 | return; |
| 2315 | |
| 2316 | pos = query; |
| 2317 | role = *pos++; |
| 2318 | wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role); |
| 2319 | |
| 2320 | /* TODO: role specific handling */ |
| 2321 | |
| 2322 | /* Length (to be filled) */ |
| 2323 | len_pos = wpabuf_put(resp, 2); |
| 2324 | wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY); |
| 2325 | wpabuf_put_u8(resp, srv_trans_id); |
| 2326 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */ |
| 2327 | |
| 2328 | while (pos < query + query_len) { |
| 2329 | if (*pos < MAX_WFD_SUBELEMS && |
| 2330 | wpa_s->global->wfd_subelem[*pos] && |
| 2331 | wpabuf_tailroom(resp) >= |
| 2332 | wpabuf_len(wpa_s->global->wfd_subelem[*pos])) { |
| 2333 | wpa_printf(MSG_DEBUG, "P2P: Add WSD response " |
| 2334 | "subelement %u", *pos); |
| 2335 | wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]); |
| 2336 | } |
| 2337 | pos++; |
| 2338 | } |
| 2339 | |
| 2340 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 2341 | } |
| 2342 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 2343 | |
| 2344 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2345 | static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token, |
| 2346 | u16 update_indic, const u8 *tlvs, size_t tlvs_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2347 | { |
| 2348 | struct wpa_supplicant *wpa_s = ctx; |
| 2349 | const u8 *pos = tlvs; |
| 2350 | const u8 *end = tlvs + tlvs_len; |
| 2351 | const u8 *tlv_end; |
| 2352 | u16 slen; |
| 2353 | struct wpabuf *resp; |
| 2354 | u8 srv_proto, srv_trans_id; |
| 2355 | size_t buf_len; |
| 2356 | char *buf; |
| 2357 | |
| 2358 | wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs", |
| 2359 | tlvs, tlvs_len); |
| 2360 | buf_len = 2 * tlvs_len + 1; |
| 2361 | buf = os_malloc(buf_len); |
| 2362 | if (buf) { |
| 2363 | wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); |
| 2364 | wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d " |
| 2365 | MACSTR " %u %u %s", |
| 2366 | freq, MAC2STR(sa), dialog_token, update_indic, |
| 2367 | buf); |
| 2368 | os_free(buf); |
| 2369 | } |
| 2370 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2371 | if (wpa_s->p2p_sd_over_ctrl_iface) { |
| 2372 | wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token, |
| 2373 | update_indic, tlvs, tlvs_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2374 | return; /* to be processed by an external program */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2375 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2376 | |
| 2377 | resp = wpabuf_alloc(10000); |
| 2378 | if (resp == NULL) |
| 2379 | return; |
| 2380 | |
| 2381 | while (pos + 1 < end) { |
| 2382 | wpa_printf(MSG_DEBUG, "P2P: Service Request TLV"); |
| 2383 | slen = WPA_GET_LE16(pos); |
| 2384 | pos += 2; |
| 2385 | if (pos + slen > end || slen < 2) { |
| 2386 | wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data " |
| 2387 | "length"); |
| 2388 | wpabuf_free(resp); |
| 2389 | return; |
| 2390 | } |
| 2391 | tlv_end = pos + slen; |
| 2392 | |
| 2393 | srv_proto = *pos++; |
| 2394 | wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", |
| 2395 | srv_proto); |
| 2396 | srv_trans_id = *pos++; |
| 2397 | wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", |
| 2398 | srv_trans_id); |
| 2399 | |
| 2400 | wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data", |
| 2401 | pos, tlv_end - pos); |
| 2402 | |
| 2403 | |
| 2404 | if (wpa_s->force_long_sd) { |
| 2405 | wpa_printf(MSG_DEBUG, "P2P: SD test - force long " |
| 2406 | "response"); |
| 2407 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); |
| 2408 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); |
| 2409 | goto done; |
| 2410 | } |
| 2411 | |
| 2412 | switch (srv_proto) { |
| 2413 | case P2P_SERV_ALL_SERVICES: |
| 2414 | wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request " |
| 2415 | "for all services"); |
| 2416 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) && |
| 2417 | dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { |
| 2418 | wpa_printf(MSG_DEBUG, "P2P: No service " |
| 2419 | "discovery protocols available"); |
| 2420 | wpas_sd_add_proto_not_avail( |
| 2421 | resp, P2P_SERV_ALL_SERVICES, |
| 2422 | srv_trans_id); |
| 2423 | break; |
| 2424 | } |
| 2425 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); |
| 2426 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); |
| 2427 | break; |
| 2428 | case P2P_SERV_BONJOUR: |
| 2429 | wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id, |
| 2430 | pos, tlv_end - pos); |
| 2431 | break; |
| 2432 | case P2P_SERV_UPNP: |
| 2433 | wpas_sd_req_upnp(wpa_s, resp, srv_trans_id, |
| 2434 | pos, tlv_end - pos); |
| 2435 | break; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2436 | #ifdef CONFIG_WIFI_DISPLAY |
| 2437 | case P2P_SERV_WIFI_DISPLAY: |
| 2438 | wpas_sd_req_wfd(wpa_s, resp, srv_trans_id, |
| 2439 | pos, tlv_end - pos); |
| 2440 | break; |
| 2441 | #endif /* CONFIG_WIFI_DISPLAY */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2442 | default: |
| 2443 | wpa_printf(MSG_DEBUG, "P2P: Unavailable service " |
| 2444 | "protocol %u", srv_proto); |
| 2445 | wpas_sd_add_proto_not_avail(resp, srv_proto, |
| 2446 | srv_trans_id); |
| 2447 | break; |
| 2448 | } |
| 2449 | |
| 2450 | pos = tlv_end; |
| 2451 | } |
| 2452 | |
| 2453 | done: |
| 2454 | wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token, |
| 2455 | update_indic, tlvs, tlvs_len); |
| 2456 | |
| 2457 | wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp); |
| 2458 | |
| 2459 | wpabuf_free(resp); |
| 2460 | } |
| 2461 | |
| 2462 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2463 | static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic, |
| 2464 | const u8 *tlvs, size_t tlvs_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2465 | { |
| 2466 | struct wpa_supplicant *wpa_s = ctx; |
| 2467 | const u8 *pos = tlvs; |
| 2468 | const u8 *end = tlvs + tlvs_len; |
| 2469 | const u8 *tlv_end; |
| 2470 | u16 slen; |
| 2471 | size_t buf_len; |
| 2472 | char *buf; |
| 2473 | |
| 2474 | wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs", |
| 2475 | tlvs, tlvs_len); |
| 2476 | if (tlvs_len > 1500) { |
| 2477 | /* TODO: better way for handling this */ |
| 2478 | wpa_msg_ctrl(wpa_s, MSG_INFO, |
| 2479 | P2P_EVENT_SERV_DISC_RESP MACSTR |
| 2480 | " %u <long response: %u bytes>", |
| 2481 | MAC2STR(sa), update_indic, |
| 2482 | (unsigned int) tlvs_len); |
| 2483 | } else { |
| 2484 | buf_len = 2 * tlvs_len + 1; |
| 2485 | buf = os_malloc(buf_len); |
| 2486 | if (buf) { |
| 2487 | wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); |
| 2488 | wpa_msg_ctrl(wpa_s, MSG_INFO, |
| 2489 | P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s", |
| 2490 | MAC2STR(sa), update_indic, buf); |
| 2491 | os_free(buf); |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | while (pos < end) { |
| 2496 | u8 srv_proto, srv_trans_id, status; |
| 2497 | |
| 2498 | wpa_printf(MSG_DEBUG, "P2P: Service Response TLV"); |
| 2499 | slen = WPA_GET_LE16(pos); |
| 2500 | pos += 2; |
| 2501 | if (pos + slen > end || slen < 3) { |
| 2502 | wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data " |
| 2503 | "length"); |
| 2504 | return; |
| 2505 | } |
| 2506 | tlv_end = pos + slen; |
| 2507 | |
| 2508 | srv_proto = *pos++; |
| 2509 | wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", |
| 2510 | srv_proto); |
| 2511 | srv_trans_id = *pos++; |
| 2512 | wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", |
| 2513 | srv_trans_id); |
| 2514 | status = *pos++; |
| 2515 | wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u", |
| 2516 | status); |
| 2517 | |
| 2518 | wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data", |
| 2519 | pos, tlv_end - pos); |
| 2520 | |
| 2521 | pos = tlv_end; |
| 2522 | } |
| 2523 | |
| 2524 | wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len); |
| 2525 | } |
| 2526 | |
| 2527 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2528 | u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 2529 | const struct wpabuf *tlvs) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2530 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2531 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2532 | return 0; |
| 2533 | return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2537 | u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 2538 | u8 version, const char *query) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2539 | { |
| 2540 | struct wpabuf *tlvs; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2541 | u64 ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2542 | |
| 2543 | tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query)); |
| 2544 | if (tlvs == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2545 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2546 | wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query)); |
| 2547 | wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */ |
| 2548 | wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */ |
| 2549 | wpabuf_put_u8(tlvs, version); |
| 2550 | wpabuf_put_str(tlvs, query); |
| 2551 | ret = wpas_p2p_sd_request(wpa_s, dst, tlvs); |
| 2552 | wpabuf_free(tlvs); |
| 2553 | return ret; |
| 2554 | } |
| 2555 | |
| 2556 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2557 | #ifdef CONFIG_WIFI_DISPLAY |
| 2558 | |
| 2559 | static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 2560 | const struct wpabuf *tlvs) |
| 2561 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2562 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 2563 | return 0; |
| 2564 | return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs); |
| 2565 | } |
| 2566 | |
| 2567 | |
| 2568 | #define MAX_WFD_SD_SUBELEMS 20 |
| 2569 | |
| 2570 | static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role, |
| 2571 | const char *subelems) |
| 2572 | { |
| 2573 | u8 *len; |
| 2574 | const char *pos; |
| 2575 | int val; |
| 2576 | int count = 0; |
| 2577 | |
| 2578 | len = wpabuf_put(tlvs, 2); |
| 2579 | wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */ |
| 2580 | wpabuf_put_u8(tlvs, id); /* Service Transaction ID */ |
| 2581 | |
| 2582 | wpabuf_put_u8(tlvs, role); |
| 2583 | |
| 2584 | pos = subelems; |
| 2585 | while (*pos) { |
| 2586 | val = atoi(pos); |
| 2587 | if (val >= 0 && val < 256) { |
| 2588 | wpabuf_put_u8(tlvs, val); |
| 2589 | count++; |
| 2590 | if (count == MAX_WFD_SD_SUBELEMS) |
| 2591 | break; |
| 2592 | } |
| 2593 | pos = os_strchr(pos + 1, ','); |
| 2594 | if (pos == NULL) |
| 2595 | break; |
| 2596 | pos++; |
| 2597 | } |
| 2598 | |
| 2599 | WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2); |
| 2600 | } |
| 2601 | |
| 2602 | |
| 2603 | u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s, |
| 2604 | const u8 *dst, const char *role) |
| 2605 | { |
| 2606 | struct wpabuf *tlvs; |
| 2607 | u64 ret; |
| 2608 | const char *subelems; |
| 2609 | u8 id = 1; |
| 2610 | |
| 2611 | subelems = os_strchr(role, ' '); |
| 2612 | if (subelems == NULL) |
| 2613 | return 0; |
| 2614 | subelems++; |
| 2615 | |
| 2616 | tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS)); |
| 2617 | if (tlvs == NULL) |
| 2618 | return 0; |
| 2619 | |
| 2620 | if (os_strstr(role, "[source]")) |
| 2621 | wfd_add_sd_req_role(tlvs, id++, 0x00, subelems); |
| 2622 | if (os_strstr(role, "[pri-sink]")) |
| 2623 | wfd_add_sd_req_role(tlvs, id++, 0x01, subelems); |
| 2624 | if (os_strstr(role, "[sec-sink]")) |
| 2625 | wfd_add_sd_req_role(tlvs, id++, 0x02, subelems); |
| 2626 | if (os_strstr(role, "[source+sink]")) |
| 2627 | wfd_add_sd_req_role(tlvs, id++, 0x03, subelems); |
| 2628 | |
| 2629 | ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs); |
| 2630 | wpabuf_free(tlvs); |
| 2631 | return ret; |
| 2632 | } |
| 2633 | |
| 2634 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 2635 | |
| 2636 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2637 | 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] | 2638 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2639 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 2640 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2641 | return p2p_sd_cancel_request(wpa_s->global->p2p, |
| 2642 | (void *) (uintptr_t) req); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | |
| 2646 | void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq, |
| 2647 | const u8 *dst, u8 dialog_token, |
| 2648 | const struct wpabuf *resp_tlvs) |
| 2649 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2650 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 2651 | return; |
| 2652 | p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token, |
| 2653 | resp_tlvs); |
| 2654 | } |
| 2655 | |
Dmitry Shmidt | eaf261d | 2013-08-14 15:30:08 -0700 | [diff] [blame] | 2656 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2657 | void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s) |
| 2658 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2659 | if (wpa_s->global->p2p) |
| 2660 | p2p_sd_service_update(wpa_s->global->p2p); |
| 2661 | } |
| 2662 | |
| 2663 | |
| 2664 | static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv) |
| 2665 | { |
| 2666 | dl_list_del(&bsrv->list); |
| 2667 | wpabuf_free(bsrv->query); |
| 2668 | wpabuf_free(bsrv->resp); |
| 2669 | os_free(bsrv); |
| 2670 | } |
| 2671 | |
| 2672 | |
| 2673 | static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv) |
| 2674 | { |
| 2675 | dl_list_del(&usrv->list); |
| 2676 | os_free(usrv->service); |
| 2677 | os_free(usrv); |
| 2678 | } |
| 2679 | |
| 2680 | |
| 2681 | void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s) |
| 2682 | { |
| 2683 | struct p2p_srv_bonjour *bsrv, *bn; |
| 2684 | struct p2p_srv_upnp *usrv, *un; |
| 2685 | |
| 2686 | dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour, |
| 2687 | struct p2p_srv_bonjour, list) |
| 2688 | wpas_p2p_srv_bonjour_free(bsrv); |
| 2689 | |
| 2690 | dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp, |
| 2691 | struct p2p_srv_upnp, list) |
| 2692 | wpas_p2p_srv_upnp_free(usrv); |
| 2693 | |
| 2694 | wpas_p2p_sd_service_update(wpa_s); |
| 2695 | } |
| 2696 | |
| 2697 | |
| 2698 | int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s, |
| 2699 | struct wpabuf *query, struct wpabuf *resp) |
| 2700 | { |
| 2701 | struct p2p_srv_bonjour *bsrv; |
| 2702 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2703 | bsrv = os_zalloc(sizeof(*bsrv)); |
| 2704 | if (bsrv == NULL) |
| 2705 | return -1; |
| 2706 | bsrv->query = query; |
| 2707 | bsrv->resp = resp; |
| 2708 | dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list); |
| 2709 | |
| 2710 | wpas_p2p_sd_service_update(wpa_s); |
| 2711 | return 0; |
| 2712 | } |
| 2713 | |
| 2714 | |
| 2715 | int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s, |
| 2716 | const struct wpabuf *query) |
| 2717 | { |
| 2718 | struct p2p_srv_bonjour *bsrv; |
| 2719 | |
| 2720 | bsrv = wpas_p2p_service_get_bonjour(wpa_s, query); |
| 2721 | if (bsrv == NULL) |
| 2722 | return -1; |
| 2723 | wpas_p2p_srv_bonjour_free(bsrv); |
| 2724 | wpas_p2p_sd_service_update(wpa_s); |
| 2725 | return 0; |
| 2726 | } |
| 2727 | |
| 2728 | |
| 2729 | int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version, |
| 2730 | const char *service) |
| 2731 | { |
| 2732 | struct p2p_srv_upnp *usrv; |
| 2733 | |
| 2734 | if (wpas_p2p_service_get_upnp(wpa_s, version, service)) |
| 2735 | return 0; /* Already listed */ |
| 2736 | usrv = os_zalloc(sizeof(*usrv)); |
| 2737 | if (usrv == NULL) |
| 2738 | return -1; |
| 2739 | usrv->version = version; |
| 2740 | usrv->service = os_strdup(service); |
| 2741 | if (usrv->service == NULL) { |
| 2742 | os_free(usrv); |
| 2743 | return -1; |
| 2744 | } |
| 2745 | dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list); |
| 2746 | |
| 2747 | wpas_p2p_sd_service_update(wpa_s); |
| 2748 | return 0; |
| 2749 | } |
| 2750 | |
| 2751 | |
| 2752 | int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version, |
| 2753 | const char *service) |
| 2754 | { |
| 2755 | struct p2p_srv_upnp *usrv; |
| 2756 | |
| 2757 | usrv = wpas_p2p_service_get_upnp(wpa_s, version, service); |
| 2758 | if (usrv == NULL) |
| 2759 | return -1; |
| 2760 | wpas_p2p_srv_upnp_free(usrv); |
| 2761 | wpas_p2p_sd_service_update(wpa_s); |
| 2762 | return 0; |
| 2763 | } |
| 2764 | |
| 2765 | |
| 2766 | static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s, |
| 2767 | const u8 *peer, const char *params, |
| 2768 | unsigned int generated_pin) |
| 2769 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2770 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR |
| 2771 | " %08d%s", MAC2STR(peer), generated_pin, params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | |
| 2775 | static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s, |
| 2776 | const u8 *peer, const char *params) |
| 2777 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2778 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR |
| 2779 | "%s", MAC2STR(peer), params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2783 | static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods, |
| 2784 | const u8 *dev_addr, const u8 *pri_dev_type, |
| 2785 | const char *dev_name, u16 supp_config_methods, |
| 2786 | u8 dev_capab, u8 group_capab, const u8 *group_id, |
| 2787 | size_t group_id_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2788 | { |
| 2789 | struct wpa_supplicant *wpa_s = ctx; |
| 2790 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2791 | char params[300]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2792 | u8 empty_dev_type[8]; |
| 2793 | unsigned int generated_pin = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2794 | struct wpa_supplicant *group = NULL; |
| 2795 | |
| 2796 | if (group_id) { |
| 2797 | for (group = wpa_s->global->ifaces; group; group = group->next) |
| 2798 | { |
| 2799 | struct wpa_ssid *s = group->current_ssid; |
| 2800 | if (s != NULL && |
| 2801 | s->mode == WPAS_MODE_P2P_GO && |
| 2802 | group_id_len - ETH_ALEN == s->ssid_len && |
| 2803 | os_memcmp(group_id + ETH_ALEN, s->ssid, |
| 2804 | s->ssid_len) == 0) |
| 2805 | break; |
| 2806 | } |
| 2807 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2808 | |
| 2809 | if (pri_dev_type == NULL) { |
| 2810 | os_memset(empty_dev_type, 0, sizeof(empty_dev_type)); |
| 2811 | pri_dev_type = empty_dev_type; |
| 2812 | } |
| 2813 | os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR |
| 2814 | " pri_dev_type=%s name='%s' config_methods=0x%x " |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2815 | "dev_capab=0x%x group_capab=0x%x%s%s", |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2816 | MAC2STR(dev_addr), |
| 2817 | wps_dev_type_bin2str(pri_dev_type, devtype, |
| 2818 | sizeof(devtype)), |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2819 | dev_name, supp_config_methods, dev_capab, group_capab, |
| 2820 | group ? " group=" : "", |
| 2821 | group ? group->ifname : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2822 | params[sizeof(params) - 1] = '\0'; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2823 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2824 | if (config_methods & WPS_CONFIG_DISPLAY) { |
| 2825 | generated_pin = wps_generate_pin(); |
| 2826 | wpas_prov_disc_local_display(wpa_s, peer, params, |
| 2827 | generated_pin); |
| 2828 | } else if (config_methods & WPS_CONFIG_KEYPAD) |
| 2829 | wpas_prov_disc_local_keypad(wpa_s, peer, params); |
| 2830 | else if (config_methods & WPS_CONFIG_PUSHBUTTON) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2831 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ |
| 2832 | MACSTR "%s", MAC2STR(peer), params); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2833 | |
| 2834 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */, |
| 2835 | P2P_PROV_DISC_SUCCESS, |
| 2836 | config_methods, generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2840 | 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] | 2841 | { |
| 2842 | struct wpa_supplicant *wpa_s = ctx; |
| 2843 | unsigned int generated_pin = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2844 | char params[20]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2845 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2846 | if (wpa_s->pending_pd_before_join && |
| 2847 | (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || |
| 2848 | os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { |
| 2849 | wpa_s->pending_pd_before_join = 0; |
| 2850 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " |
| 2851 | "join-existing-group operation"); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2852 | wpas_p2p_join_start(wpa_s, 0, NULL, 0); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2853 | return; |
| 2854 | } |
| 2855 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2856 | if (wpa_s->pending_pd_use == AUTO_PD_JOIN || |
| 2857 | wpa_s->pending_pd_use == AUTO_PD_GO_NEG) |
| 2858 | os_snprintf(params, sizeof(params), " peer_go=%d", |
| 2859 | wpa_s->pending_pd_use == AUTO_PD_JOIN); |
| 2860 | else |
| 2861 | params[0] = '\0'; |
| 2862 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2863 | if (config_methods & WPS_CONFIG_DISPLAY) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2864 | wpas_prov_disc_local_keypad(wpa_s, peer, params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2865 | else if (config_methods & WPS_CONFIG_KEYPAD) { |
| 2866 | generated_pin = wps_generate_pin(); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2867 | wpas_prov_disc_local_display(wpa_s, peer, params, |
| 2868 | generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2869 | } else if (config_methods & WPS_CONFIG_PUSHBUTTON) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2870 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP |
| 2871 | MACSTR "%s", MAC2STR(peer), params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2872 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2873 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, |
| 2874 | P2P_PROV_DISC_SUCCESS, |
| 2875 | config_methods, generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2879 | static void wpas_prov_disc_fail(void *ctx, const u8 *peer, |
| 2880 | enum p2p_prov_disc_status status) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2881 | { |
| 2882 | struct wpa_supplicant *wpa_s = ctx; |
| 2883 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2884 | if (wpa_s->p2p_fallback_to_go_neg) { |
| 2885 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto " |
| 2886 | "failed - fall back to GO Negotiation"); |
| 2887 | wpas_p2p_fallback_to_go_neg(wpa_s, 0); |
| 2888 | return; |
| 2889 | } |
| 2890 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2891 | if (status == P2P_PROV_DISC_TIMEOUT_JOIN) { |
Dmitry Shmidt | 2b38048 | 2012-09-13 10:52:13 -0700 | [diff] [blame] | 2892 | wpa_s->pending_pd_before_join = 0; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2893 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " |
| 2894 | "join-existing-group operation (no ACK for PD " |
| 2895 | "Req attempts)"); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2896 | wpas_p2p_join_start(wpa_s, 0, NULL, 0); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2897 | return; |
Dmitry Shmidt | 2b38048 | 2012-09-13 10:52:13 -0700 | [diff] [blame] | 2898 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2899 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2900 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE |
| 2901 | " p2p_dev_addr=" MACSTR " status=%d", |
| 2902 | MAC2STR(peer), status); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2903 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2904 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, |
| 2905 | status, 0, 0); |
| 2906 | } |
| 2907 | |
| 2908 | |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2909 | static int freq_included(const struct p2p_channels *channels, unsigned int freq) |
| 2910 | { |
| 2911 | if (channels == NULL) |
| 2912 | return 1; /* Assume no restrictions */ |
| 2913 | return p2p_channels_includes_freq(channels, freq); |
| 2914 | |
| 2915 | } |
| 2916 | |
| 2917 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2918 | static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid, |
| 2919 | const u8 *go_dev_addr, const u8 *ssid, |
| 2920 | size_t ssid_len, int *go, u8 *group_bssid, |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2921 | int *force_freq, int persistent_group, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2922 | const struct p2p_channels *channels, |
| 2923 | int dev_pw_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2924 | { |
| 2925 | struct wpa_supplicant *wpa_s = ctx; |
| 2926 | struct wpa_ssid *s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2927 | int res; |
| 2928 | struct wpa_supplicant *grp; |
| 2929 | |
| 2930 | if (!persistent_group) { |
| 2931 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 2932 | " to join an active group (SSID: %s)", |
| 2933 | MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2934 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && |
| 2935 | (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN) |
| 2936 | == 0 || |
| 2937 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) { |
| 2938 | wpa_printf(MSG_DEBUG, "P2P: Accept previously " |
| 2939 | "authorized invitation"); |
| 2940 | goto accept_inv; |
| 2941 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2942 | |
| 2943 | #ifdef CONFIG_WPS_NFC |
| 2944 | if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled && |
| 2945 | dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) { |
| 2946 | wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag"); |
| 2947 | wpa_s->parent->p2p_wps_method = WPS_NFC; |
| 2948 | wpa_s->parent->pending_join_wps_method = WPS_NFC; |
| 2949 | os_memcpy(wpa_s->parent->pending_join_dev_addr, |
| 2950 | go_dev_addr, ETH_ALEN); |
| 2951 | os_memcpy(wpa_s->parent->pending_join_iface_addr, |
| 2952 | bssid, ETH_ALEN); |
| 2953 | goto accept_inv; |
| 2954 | } |
| 2955 | #endif /* CONFIG_WPS_NFC */ |
| 2956 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2957 | /* |
| 2958 | * Do not accept the invitation automatically; notify user and |
| 2959 | * request approval. |
| 2960 | */ |
| 2961 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 2962 | } |
| 2963 | |
| 2964 | grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go); |
| 2965 | if (grp) { |
| 2966 | wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already " |
| 2967 | "running persistent group"); |
| 2968 | if (*go) |
| 2969 | os_memcpy(group_bssid, grp->own_addr, ETH_ALEN); |
| 2970 | goto accept_inv; |
| 2971 | } |
| 2972 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2973 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && |
| 2974 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) { |
| 2975 | wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated " |
| 2976 | "invitation to re-invoke a persistent group"); |
| 2977 | } else if (!wpa_s->conf->persistent_reconnect) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2978 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 2979 | |
| 2980 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 2981 | if (s->disabled == 2 && |
| 2982 | os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 && |
| 2983 | s->ssid_len == ssid_len && |
| 2984 | os_memcmp(ssid, s->ssid, ssid_len) == 0) |
| 2985 | break; |
| 2986 | } |
| 2987 | |
| 2988 | if (!s) { |
| 2989 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR |
| 2990 | " requested reinvocation of an unknown group", |
| 2991 | MAC2STR(sa)); |
| 2992 | return P2P_SC_FAIL_UNKNOWN_GROUP; |
| 2993 | } |
| 2994 | |
| 2995 | if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) { |
| 2996 | *go = 1; |
| 2997 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { |
| 2998 | wpa_printf(MSG_DEBUG, "P2P: The only available " |
| 2999 | "interface is already in use - reject " |
| 3000 | "invitation"); |
| 3001 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; |
| 3002 | } |
| 3003 | os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN); |
| 3004 | } else if (s->mode == WPAS_MODE_P2P_GO) { |
| 3005 | *go = 1; |
| 3006 | if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0) |
| 3007 | { |
| 3008 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 3009 | "interface address for the group"); |
| 3010 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; |
| 3011 | } |
| 3012 | os_memcpy(group_bssid, wpa_s->pending_interface_addr, |
| 3013 | ETH_ALEN); |
| 3014 | } |
| 3015 | |
| 3016 | accept_inv: |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3017 | wpas_p2p_set_own_freq_preference(wpa_s, 0); |
| 3018 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3019 | /* Get one of the frequencies currently in use */ |
| 3020 | if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) { |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 3021 | 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] | 3022 | wpas_p2p_set_own_freq_preference(wpa_s, res); |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 3023 | |
| 3024 | if (wpa_s->num_multichan_concurrent < 2 || |
| 3025 | wpas_p2p_num_unused_channels(wpa_s) < 1) { |
| 3026 | wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces"); |
| 3027 | *force_freq = res; |
| 3028 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3029 | } |
| 3030 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3031 | if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 && |
| 3032 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 3033 | if (*go == 0) { |
| 3034 | /* We are the client */ |
| 3035 | wpa_printf(MSG_DEBUG, "P2P: Peer was found to be " |
| 3036 | "running a GO but we are capable of MCC, " |
| 3037 | "figure out the best channel to use"); |
| 3038 | *force_freq = 0; |
| 3039 | } else if (!freq_included(channels, *force_freq)) { |
| 3040 | /* We are the GO, and *force_freq is not in the |
| 3041 | * intersection */ |
| 3042 | wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not " |
| 3043 | "in intersection but we are capable of MCC, " |
| 3044 | "figure out the best channel to use", |
| 3045 | *force_freq); |
| 3046 | *force_freq = 0; |
| 3047 | } |
| 3048 | } |
| 3049 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3050 | return P2P_SC_SUCCESS; |
| 3051 | } |
| 3052 | |
| 3053 | |
| 3054 | static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid, |
| 3055 | const u8 *ssid, size_t ssid_len, |
| 3056 | const u8 *go_dev_addr, u8 status, |
| 3057 | int op_freq) |
| 3058 | { |
| 3059 | struct wpa_supplicant *wpa_s = ctx; |
| 3060 | struct wpa_ssid *s; |
| 3061 | |
| 3062 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 3063 | if (s->disabled == 2 && |
| 3064 | s->ssid_len == ssid_len && |
| 3065 | os_memcmp(ssid, s->ssid, ssid_len) == 0) |
| 3066 | break; |
| 3067 | } |
| 3068 | |
| 3069 | if (status == P2P_SC_SUCCESS) { |
| 3070 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 3071 | " was accepted; op_freq=%d MHz, SSID=%s", |
| 3072 | MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3073 | if (s) { |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 3074 | int go = s->mode == WPAS_MODE_P2P_GO; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3075 | wpas_p2p_group_add_persistent( |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3076 | wpa_s, s, go, 0, op_freq, 0, 0, NULL, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 3077 | go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3078 | } else if (bssid) { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3079 | wpa_s->user_initiated_pd = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3080 | wpas_p2p_join(wpa_s, bssid, go_dev_addr, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 3081 | wpa_s->p2p_wps_method, 0, op_freq, |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 3082 | ssid, ssid_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3083 | } |
| 3084 | return; |
| 3085 | } |
| 3086 | |
| 3087 | if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { |
| 3088 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR |
| 3089 | " was rejected (status %u)", MAC2STR(sa), status); |
| 3090 | return; |
| 3091 | } |
| 3092 | |
| 3093 | if (!s) { |
| 3094 | if (bssid) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3095 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3096 | P2P_EVENT_INVITATION_RECEIVED |
| 3097 | "sa=" MACSTR " go_dev_addr=" MACSTR |
| 3098 | " bssid=" MACSTR " unknown-network", |
| 3099 | MAC2STR(sa), MAC2STR(go_dev_addr), |
| 3100 | MAC2STR(bssid)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3101 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3102 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3103 | P2P_EVENT_INVITATION_RECEIVED |
| 3104 | "sa=" MACSTR " go_dev_addr=" MACSTR |
| 3105 | " unknown-network", |
| 3106 | MAC2STR(sa), MAC2STR(go_dev_addr)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3107 | } |
| 3108 | return; |
| 3109 | } |
| 3110 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3111 | if (s->mode == WPAS_MODE_P2P_GO && op_freq) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3112 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED |
| 3113 | "sa=" MACSTR " persistent=%d freq=%d", |
| 3114 | MAC2STR(sa), s->id, op_freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3115 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3116 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED |
| 3117 | "sa=" MACSTR " persistent=%d", |
| 3118 | MAC2STR(sa), s->id); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3119 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3123 | static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s, |
| 3124 | struct wpa_ssid *ssid, |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3125 | const u8 *peer, int inv) |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3126 | { |
| 3127 | size_t i; |
| 3128 | |
| 3129 | if (ssid == NULL) |
| 3130 | return; |
| 3131 | |
| 3132 | for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) { |
| 3133 | if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer, |
| 3134 | ETH_ALEN) == 0) |
| 3135 | break; |
| 3136 | } |
| 3137 | if (i >= ssid->num_p2p_clients) { |
| 3138 | if (ssid->mode != WPAS_MODE_P2P_GO && |
| 3139 | os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) { |
| 3140 | wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d " |
| 3141 | "due to invitation result", ssid->id); |
| 3142 | wpas_notify_network_removed(wpa_s, ssid); |
| 3143 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 3144 | return; |
| 3145 | } |
| 3146 | return; /* Peer not found in client list */ |
| 3147 | } |
| 3148 | |
| 3149 | wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent " |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3150 | "group %d client list%s", |
| 3151 | MAC2STR(peer), ssid->id, |
| 3152 | inv ? " due to invitation result" : ""); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3153 | os_memmove(ssid->p2p_client_list + i * ETH_ALEN, |
| 3154 | ssid->p2p_client_list + (i + 1) * ETH_ALEN, |
| 3155 | (ssid->num_p2p_clients - i - 1) * ETH_ALEN); |
| 3156 | ssid->num_p2p_clients--; |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3157 | if (wpa_s->parent->conf->update_config && |
| 3158 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 3159 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | |
| 3163 | static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s, |
| 3164 | const u8 *peer) |
| 3165 | { |
| 3166 | struct wpa_ssid *ssid; |
| 3167 | |
| 3168 | wpa_s = wpa_s->global->p2p_invite_group; |
| 3169 | if (wpa_s == NULL) |
| 3170 | return; /* No known invitation group */ |
| 3171 | ssid = wpa_s->current_ssid; |
| 3172 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || |
| 3173 | !ssid->p2p_persistent_group) |
| 3174 | return; /* Not operating as a GO in persistent group */ |
| 3175 | ssid = wpas_p2p_get_persistent(wpa_s->parent, peer, |
| 3176 | ssid->ssid, ssid->ssid_len); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3177 | wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3181 | static void wpas_invitation_result(void *ctx, int status, const u8 *bssid, |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3182 | const struct p2p_channels *channels, |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3183 | const u8 *peer, int neg_freq, |
| 3184 | int peer_oper_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3185 | { |
| 3186 | struct wpa_supplicant *wpa_s = ctx; |
| 3187 | struct wpa_ssid *ssid; |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3188 | int freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3189 | |
| 3190 | if (bssid) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3191 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT |
| 3192 | "status=%d " MACSTR, |
| 3193 | status, MAC2STR(bssid)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3194 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3195 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT |
| 3196 | "status=%d ", status); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3197 | } |
| 3198 | wpas_notify_p2p_invitation_result(wpa_s, status, bssid); |
| 3199 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3200 | wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR, |
| 3201 | status, MAC2STR(peer)); |
| 3202 | if (wpa_s->pending_invite_ssid_id == -1) { |
| 3203 | if (status == P2P_SC_FAIL_UNKNOWN_GROUP) |
| 3204 | wpas_remove_persistent_client(wpa_s, peer); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3205 | return; /* Invitation to active group */ |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3206 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3207 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3208 | if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { |
| 3209 | wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another " |
| 3210 | "invitation exchange to indicate readiness for " |
| 3211 | "re-invocation"); |
| 3212 | } |
| 3213 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3214 | if (status != P2P_SC_SUCCESS) { |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3215 | if (status == P2P_SC_FAIL_UNKNOWN_GROUP) { |
| 3216 | ssid = wpa_config_get_network( |
| 3217 | wpa_s->conf, wpa_s->pending_invite_ssid_id); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3218 | wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3219 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3220 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 3221 | return; |
| 3222 | } |
| 3223 | |
| 3224 | ssid = wpa_config_get_network(wpa_s->conf, |
| 3225 | wpa_s->pending_invite_ssid_id); |
| 3226 | if (ssid == NULL) { |
| 3227 | wpa_printf(MSG_ERROR, "P2P: Could not find persistent group " |
| 3228 | "data matching with invitation"); |
| 3229 | return; |
| 3230 | } |
| 3231 | |
Jouni Malinen | 2c5b17d | 2012-10-13 21:52:46 -0700 | [diff] [blame] | 3232 | /* |
| 3233 | * The peer could have missed our ctrl::ack frame for Invitation |
| 3234 | * Response and continue retransmitting the frame. To reduce the |
| 3235 | * likelihood of the peer not getting successful TX status for the |
| 3236 | * Invitation Response frame, wait a short time here before starting |
| 3237 | * the persistent group so that we will remain on the current channel to |
| 3238 | * acknowledge any possible retransmission from the peer. |
| 3239 | */ |
| 3240 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before " |
| 3241 | "starting persistent group"); |
| 3242 | os_sleep(0, 50000); |
| 3243 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3244 | if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO && |
| 3245 | freq_included(channels, neg_freq)) |
| 3246 | freq = neg_freq; |
| 3247 | else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO && |
| 3248 | freq_included(channels, peer_oper_freq)) |
| 3249 | freq = peer_oper_freq; |
| 3250 | else |
| 3251 | freq = 0; |
| 3252 | |
| 3253 | wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s", |
| 3254 | freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3255 | wpas_p2p_group_add_persistent(wpa_s, ssid, |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 3256 | ssid->mode == WPAS_MODE_P2P_GO, |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 3257 | wpa_s->p2p_persistent_go_freq, |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3258 | freq, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3259 | wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht, |
| 3260 | channels, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 3261 | ssid->mode == WPAS_MODE_P2P_GO ? |
| 3262 | P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : |
| 3263 | 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3267 | static int wpas_p2p_disallowed_freq(struct wpa_global *global, |
| 3268 | unsigned int freq) |
| 3269 | { |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 3270 | if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq)) |
| 3271 | return 1; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 3272 | return freq_range_list_includes(&global->p2p_disallow_freq, freq); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | |
| 3276 | static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan) |
| 3277 | { |
| 3278 | reg->channel[reg->channels] = chan; |
| 3279 | reg->channels++; |
| 3280 | } |
| 3281 | |
| 3282 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3283 | static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3284 | struct p2p_channels *chan, |
| 3285 | struct p2p_channels *cli_chan) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3286 | { |
| 3287 | int i, cla = 0; |
| 3288 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3289 | os_memset(cli_chan, 0, sizeof(*cli_chan)); |
| 3290 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3291 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz " |
| 3292 | "band"); |
| 3293 | |
| 3294 | /* Operating class 81 - 2.4 GHz band channels 1..13 */ |
| 3295 | chan->reg_class[cla].reg_class = 81; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3296 | chan->reg_class[cla].channels = 0; |
| 3297 | for (i = 0; i < 11; i++) { |
| 3298 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5)) |
| 3299 | wpas_p2p_add_chan(&chan->reg_class[cla], i + 1); |
| 3300 | } |
| 3301 | if (chan->reg_class[cla].channels) |
| 3302 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3303 | |
| 3304 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz " |
| 3305 | "band"); |
| 3306 | |
| 3307 | /* Operating class 115 - 5 GHz, channels 36-48 */ |
| 3308 | chan->reg_class[cla].reg_class = 115; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3309 | chan->reg_class[cla].channels = 0; |
| 3310 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5)) |
| 3311 | wpas_p2p_add_chan(&chan->reg_class[cla], 36); |
| 3312 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5)) |
| 3313 | wpas_p2p_add_chan(&chan->reg_class[cla], 40); |
| 3314 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5)) |
| 3315 | wpas_p2p_add_chan(&chan->reg_class[cla], 44); |
| 3316 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5)) |
| 3317 | wpas_p2p_add_chan(&chan->reg_class[cla], 48); |
| 3318 | if (chan->reg_class[cla].channels) |
| 3319 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3320 | |
| 3321 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz " |
| 3322 | "band"); |
| 3323 | |
| 3324 | /* Operating class 124 - 5 GHz, channels 149,153,157,161 */ |
| 3325 | chan->reg_class[cla].reg_class = 124; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3326 | chan->reg_class[cla].channels = 0; |
| 3327 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5)) |
| 3328 | wpas_p2p_add_chan(&chan->reg_class[cla], 149); |
| 3329 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5)) |
| 3330 | wpas_p2p_add_chan(&chan->reg_class[cla], 153); |
| 3331 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5)) |
| 3332 | wpas_p2p_add_chan(&chan->reg_class[cla], 157); |
| 3333 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5)) |
| 3334 | wpas_p2p_add_chan(&chan->reg_class[cla], 161); |
| 3335 | if (chan->reg_class[cla].channels) |
| 3336 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3337 | |
| 3338 | chan->reg_classes = cla; |
| 3339 | return 0; |
| 3340 | } |
| 3341 | |
| 3342 | |
| 3343 | static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes, |
| 3344 | u16 num_modes, |
| 3345 | enum hostapd_hw_mode mode) |
| 3346 | { |
| 3347 | u16 i; |
| 3348 | |
| 3349 | for (i = 0; i < num_modes; i++) { |
| 3350 | if (modes[i].mode == mode) |
| 3351 | return &modes[i]; |
| 3352 | } |
| 3353 | |
| 3354 | return NULL; |
| 3355 | } |
| 3356 | |
| 3357 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3358 | enum chan_allowed { |
| 3359 | NOT_ALLOWED, PASSIVE_ONLY, ALLOWED |
| 3360 | }; |
| 3361 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3362 | static int has_channel(struct wpa_global *global, |
| 3363 | struct hostapd_hw_modes *mode, u8 chan, int *flags) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3364 | { |
| 3365 | int i; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3366 | unsigned int freq; |
| 3367 | |
| 3368 | freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) + |
| 3369 | chan * 5; |
| 3370 | if (wpas_p2p_disallowed_freq(global, freq)) |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3371 | return NOT_ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3372 | |
| 3373 | for (i = 0; i < mode->num_channels; i++) { |
| 3374 | if (mode->channels[i].chan == chan) { |
| 3375 | if (flags) |
| 3376 | *flags = mode->channels[i].flag; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3377 | if (mode->channels[i].flag & |
| 3378 | (HOSTAPD_CHAN_DISABLED | |
| 3379 | HOSTAPD_CHAN_RADAR)) |
| 3380 | return NOT_ALLOWED; |
| 3381 | if (mode->channels[i].flag & |
| 3382 | (HOSTAPD_CHAN_PASSIVE_SCAN | |
| 3383 | HOSTAPD_CHAN_NO_IBSS)) |
| 3384 | return PASSIVE_ONLY; |
| 3385 | return ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3386 | } |
| 3387 | } |
| 3388 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3389 | return NOT_ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | |
| 3393 | struct p2p_oper_class_map { |
| 3394 | enum hostapd_hw_mode mode; |
| 3395 | u8 op_class; |
| 3396 | u8 min_chan; |
| 3397 | u8 max_chan; |
| 3398 | u8 inc; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3399 | enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3400 | }; |
| 3401 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3402 | static struct p2p_oper_class_map op_class[] = { |
| 3403 | { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 }, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3404 | #if 0 /* Do not enable HT40 on 2 GHz for now */ |
| 3405 | { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS }, |
| 3406 | { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS }, |
| 3407 | #endif |
| 3408 | { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 }, |
| 3409 | { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 }, |
| 3410 | { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS }, |
| 3411 | { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS }, |
| 3412 | { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS }, |
| 3413 | { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS }, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3414 | |
| 3415 | /* |
| 3416 | * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center |
| 3417 | * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of |
| 3418 | * 80 MHz, but currently use the following definition for simplicity |
| 3419 | * (these center frequencies are not actual channels, which makes |
| 3420 | * has_channel() fail). wpas_p2p_verify_80mhz() should take care of |
| 3421 | * removing invalid channels. |
| 3422 | */ |
| 3423 | { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 }, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3424 | { -1, 0, 0, 0, 0, BW20 } |
| 3425 | }; |
| 3426 | |
| 3427 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3428 | static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s, |
| 3429 | struct hostapd_hw_modes *mode, |
| 3430 | u8 channel) |
| 3431 | { |
| 3432 | u8 center_channels[] = { 42, 58, 106, 122, 138, 155 }; |
| 3433 | unsigned int i; |
| 3434 | |
| 3435 | if (mode->mode != HOSTAPD_MODE_IEEE80211A) |
| 3436 | return 0; |
| 3437 | |
| 3438 | for (i = 0; i < ARRAY_SIZE(center_channels); i++) |
| 3439 | /* |
| 3440 | * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48), |
| 3441 | * so the center channel is 6 channels away from the start/end. |
| 3442 | */ |
| 3443 | if (channel >= center_channels[i] - 6 && |
| 3444 | channel <= center_channels[i] + 6) |
| 3445 | return center_channels[i]; |
| 3446 | |
| 3447 | return 0; |
| 3448 | } |
| 3449 | |
| 3450 | |
| 3451 | static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s, |
| 3452 | struct hostapd_hw_modes *mode, |
| 3453 | u8 channel, u8 bw) |
| 3454 | { |
| 3455 | u8 center_chan; |
| 3456 | int i, flags; |
| 3457 | enum chan_allowed res, ret = ALLOWED; |
| 3458 | |
| 3459 | center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel); |
| 3460 | if (!center_chan) |
| 3461 | return NOT_ALLOWED; |
| 3462 | if (center_chan >= 58 && center_chan <= 138) |
| 3463 | return NOT_ALLOWED; /* Do not allow DFS channels for P2P */ |
| 3464 | |
| 3465 | /* check all the channels are available */ |
| 3466 | for (i = 0; i < 4; i++) { |
| 3467 | int adj_chan = center_chan - 6 + i * 4; |
| 3468 | |
| 3469 | res = has_channel(wpa_s->global, mode, adj_chan, &flags); |
| 3470 | if (res == NOT_ALLOWED) |
| 3471 | return NOT_ALLOWED; |
| 3472 | if (res == PASSIVE_ONLY) |
| 3473 | ret = PASSIVE_ONLY; |
| 3474 | |
| 3475 | if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) |
| 3476 | return NOT_ALLOWED; |
| 3477 | if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) |
| 3478 | return NOT_ALLOWED; |
| 3479 | if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) |
| 3480 | return NOT_ALLOWED; |
| 3481 | if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)) |
| 3482 | return NOT_ALLOWED; |
| 3483 | } |
| 3484 | |
| 3485 | return ret; |
| 3486 | } |
| 3487 | |
| 3488 | |
| 3489 | static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s, |
| 3490 | struct hostapd_hw_modes *mode, |
| 3491 | u8 channel, u8 bw) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3492 | { |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 3493 | int flag = 0; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3494 | enum chan_allowed res, res2; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3495 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3496 | res2 = res = has_channel(wpa_s->global, mode, channel, &flag); |
| 3497 | if (bw == BW40MINUS) { |
| 3498 | if (!(flag & HOSTAPD_CHAN_HT40MINUS)) |
| 3499 | return NOT_ALLOWED; |
| 3500 | res2 = has_channel(wpa_s->global, mode, channel - 4, NULL); |
| 3501 | } else if (bw == BW40PLUS) { |
| 3502 | if (!(flag & HOSTAPD_CHAN_HT40PLUS)) |
| 3503 | return NOT_ALLOWED; |
| 3504 | res2 = has_channel(wpa_s->global, mode, channel + 4, NULL); |
| 3505 | } else if (bw == BW80) { |
| 3506 | res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw); |
| 3507 | } |
| 3508 | |
| 3509 | if (res == NOT_ALLOWED || res2 == NOT_ALLOWED) |
| 3510 | return NOT_ALLOWED; |
| 3511 | if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY) |
| 3512 | return PASSIVE_ONLY; |
| 3513 | return res; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3514 | } |
| 3515 | |
| 3516 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3517 | static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3518 | struct p2p_channels *chan, |
| 3519 | struct p2p_channels *cli_chan) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3520 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3521 | struct hostapd_hw_modes *mode; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3522 | int cla, op, cli_cla; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3523 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3524 | if (wpa_s->hw.modes == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3525 | wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching " |
| 3526 | "of all supported channels; assume dualband " |
| 3527 | "support"); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3528 | return wpas_p2p_default_channels(wpa_s, chan, cli_chan); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3529 | } |
| 3530 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3531 | cla = cli_cla = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3532 | |
| 3533 | for (op = 0; op_class[op].op_class; op++) { |
| 3534 | struct p2p_oper_class_map *o = &op_class[op]; |
| 3535 | u8 ch; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3536 | struct p2p_reg_class *reg = NULL, *cli_reg = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3537 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3538 | 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] | 3539 | if (mode == NULL) |
| 3540 | continue; |
| 3541 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3542 | enum chan_allowed res; |
| 3543 | res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); |
| 3544 | if (res == ALLOWED) { |
| 3545 | if (reg == NULL) { |
| 3546 | wpa_printf(MSG_DEBUG, "P2P: Add operating class %u", |
| 3547 | o->op_class); |
| 3548 | reg = &chan->reg_class[cla]; |
| 3549 | cla++; |
| 3550 | reg->reg_class = o->op_class; |
| 3551 | } |
| 3552 | reg->channel[reg->channels] = ch; |
| 3553 | reg->channels++; |
| 3554 | } else if (res == PASSIVE_ONLY && |
| 3555 | wpa_s->conf->p2p_add_cli_chan) { |
| 3556 | if (cli_reg == NULL) { |
| 3557 | wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)", |
| 3558 | o->op_class); |
| 3559 | cli_reg = &cli_chan->reg_class[cli_cla]; |
| 3560 | cli_cla++; |
| 3561 | cli_reg->reg_class = o->op_class; |
| 3562 | } |
| 3563 | cli_reg->channel[cli_reg->channels] = ch; |
| 3564 | cli_reg->channels++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3565 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3566 | } |
| 3567 | if (reg) { |
| 3568 | wpa_hexdump(MSG_DEBUG, "P2P: Channels", |
| 3569 | reg->channel, reg->channels); |
| 3570 | } |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3571 | if (cli_reg) { |
| 3572 | wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)", |
| 3573 | cli_reg->channel, cli_reg->channels); |
| 3574 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | chan->reg_classes = cla; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3578 | cli_chan->reg_classes = cli_cla; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3579 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3580 | return 0; |
| 3581 | } |
| 3582 | |
| 3583 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3584 | int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s, |
| 3585 | struct hostapd_hw_modes *mode, u8 channel) |
| 3586 | { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3587 | int op; |
| 3588 | enum chan_allowed ret; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3589 | |
| 3590 | for (op = 0; op_class[op].op_class; op++) { |
| 3591 | struct p2p_oper_class_map *o = &op_class[op]; |
| 3592 | u8 ch; |
| 3593 | |
| 3594 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { |
| 3595 | if (o->mode != HOSTAPD_MODE_IEEE80211A || |
| 3596 | o->bw == BW20 || ch != channel) |
| 3597 | continue; |
| 3598 | ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3599 | if (ret == ALLOWED) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3600 | return (o->bw == BW40MINUS) ? -1 : 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3601 | } |
| 3602 | } |
| 3603 | return 0; |
| 3604 | } |
| 3605 | |
| 3606 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3607 | int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s, |
| 3608 | struct hostapd_hw_modes *mode, u8 channel) |
| 3609 | { |
| 3610 | if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80)) |
| 3611 | return 0; |
| 3612 | |
| 3613 | return wpas_p2p_get_center_80mhz(wpa_s, mode, channel); |
| 3614 | } |
| 3615 | |
| 3616 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3617 | static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf, |
| 3618 | size_t buf_len) |
| 3619 | { |
| 3620 | struct wpa_supplicant *wpa_s = ctx; |
| 3621 | |
| 3622 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3623 | if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0) |
| 3624 | break; |
| 3625 | } |
| 3626 | if (wpa_s == NULL) |
| 3627 | return -1; |
| 3628 | |
| 3629 | return wpa_drv_get_noa(wpa_s, buf, buf_len); |
| 3630 | } |
| 3631 | |
| 3632 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3633 | static int wpas_go_connected(void *ctx, const u8 *dev_addr) |
| 3634 | { |
| 3635 | struct wpa_supplicant *wpa_s = ctx; |
| 3636 | |
| 3637 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3638 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 3639 | if (ssid == NULL) |
| 3640 | continue; |
| 3641 | if (ssid->mode != WPAS_MODE_INFRA) |
| 3642 | continue; |
| 3643 | if (wpa_s->wpa_state != WPA_COMPLETED && |
| 3644 | wpa_s->wpa_state != WPA_GROUP_HANDSHAKE) |
| 3645 | continue; |
| 3646 | if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0) |
| 3647 | return 1; |
| 3648 | } |
| 3649 | |
| 3650 | return 0; |
| 3651 | } |
| 3652 | |
| 3653 | |
Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 3654 | static int wpas_is_concurrent_session_active(void *ctx) |
| 3655 | { |
| 3656 | struct wpa_supplicant *wpa_s = ctx; |
| 3657 | struct wpa_supplicant *ifs; |
| 3658 | |
| 3659 | for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) { |
| 3660 | if (ifs == wpa_s) |
| 3661 | continue; |
| 3662 | if (ifs->wpa_state > WPA_ASSOCIATED) |
| 3663 | return 1; |
| 3664 | } |
| 3665 | return 0; |
| 3666 | } |
| 3667 | |
| 3668 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3669 | static void wpas_p2p_debug_print(void *ctx, int level, const char *msg) |
| 3670 | { |
| 3671 | struct wpa_supplicant *wpa_s = ctx; |
| 3672 | wpa_msg_global(wpa_s, level, "P2P: %s", msg); |
| 3673 | } |
| 3674 | |
| 3675 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3676 | int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s) |
| 3677 | { |
| 3678 | struct wpa_interface iface; |
| 3679 | struct wpa_supplicant *p2pdev_wpa_s; |
| 3680 | char ifname[100]; |
| 3681 | char force_name[100]; |
| 3682 | int ret; |
| 3683 | |
| 3684 | os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s", |
| 3685 | wpa_s->ifname); |
| 3686 | force_name[0] = '\0'; |
| 3687 | wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE; |
| 3688 | ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL, |
| 3689 | force_name, wpa_s->pending_interface_addr, NULL); |
| 3690 | if (ret < 0) { |
| 3691 | wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface"); |
| 3692 | return ret; |
| 3693 | } |
| 3694 | os_strlcpy(wpa_s->pending_interface_name, ifname, |
| 3695 | sizeof(wpa_s->pending_interface_name)); |
| 3696 | |
| 3697 | os_memset(&iface, 0, sizeof(iface)); |
| 3698 | iface.p2p_mgmt = 1; |
| 3699 | iface.ifname = wpa_s->pending_interface_name; |
| 3700 | iface.driver = wpa_s->driver->name; |
| 3701 | iface.driver_param = wpa_s->conf->driver_param; |
Dmitry Shmidt | 2ac5f60 | 2014-03-07 10:08:21 -0800 | [diff] [blame] | 3702 | |
| 3703 | /* |
| 3704 | * If a P2P Device configuration file was given, use it as the interface |
| 3705 | * configuration file (instead of using parent's configuration file. |
| 3706 | */ |
| 3707 | if (wpa_s->conf_p2p_dev) { |
| 3708 | iface.confname = wpa_s->conf_p2p_dev; |
| 3709 | iface.ctrl_interface = NULL; |
| 3710 | } else { |
| 3711 | iface.confname = wpa_s->confname; |
| 3712 | iface.ctrl_interface = wpa_s->conf->ctrl_interface; |
| 3713 | } |
| 3714 | iface.conf_p2p_dev = NULL; |
| 3715 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3716 | p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface); |
| 3717 | if (!p2pdev_wpa_s) { |
| 3718 | wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface"); |
| 3719 | return -1; |
| 3720 | } |
| 3721 | p2pdev_wpa_s->parent = wpa_s; |
| 3722 | |
| 3723 | wpa_s->pending_interface_name[0] = '\0'; |
| 3724 | return 0; |
| 3725 | } |
| 3726 | |
| 3727 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3728 | static void wpas_presence_resp(void *ctx, const u8 *src, u8 status, |
| 3729 | const u8 *noa, size_t noa_len) |
| 3730 | { |
| 3731 | struct wpa_supplicant *wpa_s, *intf = ctx; |
| 3732 | char hex[100]; |
| 3733 | |
| 3734 | for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3735 | if (wpa_s->waiting_presence_resp) |
| 3736 | break; |
| 3737 | } |
| 3738 | if (!wpa_s) { |
| 3739 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response"); |
| 3740 | return; |
| 3741 | } |
| 3742 | wpa_s->waiting_presence_resp = 0; |
| 3743 | |
| 3744 | wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len); |
| 3745 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR |
| 3746 | " status=%u noa=%s", MAC2STR(src), status, hex); |
| 3747 | } |
| 3748 | |
| 3749 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3750 | /** |
| 3751 | * wpas_p2p_init - Initialize P2P module for %wpa_supplicant |
| 3752 | * @global: Pointer to global data from wpa_supplicant_init() |
| 3753 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 3754 | * Returns: 0 on success, -1 on failure |
| 3755 | */ |
| 3756 | int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s) |
| 3757 | { |
| 3758 | struct p2p_config p2p; |
| 3759 | unsigned int r; |
| 3760 | int i; |
| 3761 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3762 | if (wpa_s->conf->p2p_disabled) |
| 3763 | return 0; |
| 3764 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3765 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) |
| 3766 | return 0; |
| 3767 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3768 | if (global->p2p) |
| 3769 | return 0; |
| 3770 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3771 | os_memset(&p2p, 0, sizeof(p2p)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3772 | p2p.cb_ctx = wpa_s; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3773 | p2p.debug_print = wpas_p2p_debug_print; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3774 | p2p.p2p_scan = wpas_p2p_scan; |
| 3775 | p2p.send_action = wpas_send_action; |
| 3776 | p2p.send_action_done = wpas_send_action_done; |
| 3777 | p2p.go_neg_completed = wpas_go_neg_completed; |
| 3778 | p2p.go_neg_req_rx = wpas_go_neg_req_rx; |
| 3779 | p2p.dev_found = wpas_dev_found; |
| 3780 | p2p.dev_lost = wpas_dev_lost; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3781 | p2p.find_stopped = wpas_find_stopped; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3782 | p2p.start_listen = wpas_start_listen; |
| 3783 | p2p.stop_listen = wpas_stop_listen; |
| 3784 | p2p.send_probe_resp = wpas_send_probe_resp; |
| 3785 | p2p.sd_request = wpas_sd_request; |
| 3786 | p2p.sd_response = wpas_sd_response; |
| 3787 | p2p.prov_disc_req = wpas_prov_disc_req; |
| 3788 | p2p.prov_disc_resp = wpas_prov_disc_resp; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 3789 | p2p.prov_disc_fail = wpas_prov_disc_fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3790 | p2p.invitation_process = wpas_invitation_process; |
| 3791 | p2p.invitation_received = wpas_invitation_received; |
| 3792 | p2p.invitation_result = wpas_invitation_result; |
| 3793 | p2p.get_noa = wpas_get_noa; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3794 | p2p.go_connected = wpas_go_connected; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3795 | p2p.presence_resp = wpas_presence_resp; |
Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 3796 | p2p.is_concurrent_session_active = wpas_is_concurrent_session_active; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3797 | |
| 3798 | 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] | 3799 | 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] | 3800 | p2p.dev_name = wpa_s->conf->device_name; |
| 3801 | p2p.manufacturer = wpa_s->conf->manufacturer; |
| 3802 | p2p.model_name = wpa_s->conf->model_name; |
| 3803 | p2p.model_number = wpa_s->conf->model_number; |
| 3804 | p2p.serial_number = wpa_s->conf->serial_number; |
| 3805 | if (wpa_s->wps) { |
| 3806 | os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16); |
| 3807 | p2p.config_methods = wpa_s->wps->config_methods; |
| 3808 | } |
| 3809 | |
| 3810 | if (wpa_s->conf->p2p_listen_reg_class && |
| 3811 | wpa_s->conf->p2p_listen_channel) { |
| 3812 | p2p.reg_class = wpa_s->conf->p2p_listen_reg_class; |
| 3813 | p2p.channel = wpa_s->conf->p2p_listen_channel; |
| 3814 | } else { |
| 3815 | p2p.reg_class = 81; |
| 3816 | /* |
| 3817 | * Pick one of the social channels randomly as the listen |
| 3818 | * channel. |
| 3819 | */ |
| 3820 | os_get_random((u8 *) &r, sizeof(r)); |
| 3821 | p2p.channel = 1 + (r % 3) * 5; |
| 3822 | } |
| 3823 | wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel); |
| 3824 | |
| 3825 | if (wpa_s->conf->p2p_oper_reg_class && |
| 3826 | wpa_s->conf->p2p_oper_channel) { |
| 3827 | p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class; |
| 3828 | p2p.op_channel = wpa_s->conf->p2p_oper_channel; |
| 3829 | p2p.cfg_op_channel = 1; |
| 3830 | wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: " |
| 3831 | "%d:%d", p2p.op_reg_class, p2p.op_channel); |
| 3832 | |
| 3833 | } else { |
| 3834 | p2p.op_reg_class = 81; |
| 3835 | /* |
| 3836 | * Use random operation channel from (1, 6, 11) if no other |
| 3837 | * preference is indicated. |
| 3838 | */ |
| 3839 | os_get_random((u8 *) &r, sizeof(r)); |
| 3840 | p2p.op_channel = 1 + (r % 3) * 5; |
| 3841 | p2p.cfg_op_channel = 0; |
| 3842 | wpa_printf(MSG_DEBUG, "P2P: Random operating channel: " |
| 3843 | "%d:%d", p2p.op_reg_class, p2p.op_channel); |
| 3844 | } |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 3845 | |
| 3846 | if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) { |
| 3847 | p2p.pref_chan = wpa_s->conf->p2p_pref_chan; |
| 3848 | p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan; |
| 3849 | } |
| 3850 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3851 | if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) { |
| 3852 | os_memcpy(p2p.country, wpa_s->conf->country, 2); |
| 3853 | p2p.country[2] = 0x04; |
| 3854 | } else |
| 3855 | os_memcpy(p2p.country, "XX\x04", 3); |
| 3856 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3857 | if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3858 | wpa_printf(MSG_ERROR, "P2P: Failed to configure supported " |
| 3859 | "channel list"); |
| 3860 | return -1; |
| 3861 | } |
| 3862 | |
| 3863 | os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type, |
| 3864 | WPS_DEV_TYPE_LEN); |
| 3865 | |
| 3866 | p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types; |
| 3867 | os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type, |
| 3868 | p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN); |
| 3869 | |
| 3870 | p2p.concurrent_operations = !!(wpa_s->drv_flags & |
| 3871 | WPA_DRIVER_FLAGS_P2P_CONCURRENT); |
| 3872 | |
| 3873 | p2p.max_peers = 100; |
| 3874 | |
| 3875 | if (wpa_s->conf->p2p_ssid_postfix) { |
| 3876 | p2p.ssid_postfix_len = |
| 3877 | os_strlen(wpa_s->conf->p2p_ssid_postfix); |
| 3878 | if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix)) |
| 3879 | p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix); |
| 3880 | os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix, |
| 3881 | p2p.ssid_postfix_len); |
| 3882 | } |
| 3883 | |
| 3884 | p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss; |
| 3885 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3886 | p2p.max_listen = wpa_s->max_remain_on_chan; |
| 3887 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3888 | global->p2p = p2p_init(&p2p); |
| 3889 | if (global->p2p == NULL) |
| 3890 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3891 | global->p2p_init_wpa_s = wpa_s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3892 | |
| 3893 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { |
| 3894 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) |
| 3895 | continue; |
| 3896 | p2p_add_wps_vendor_extension( |
| 3897 | global->p2p, wpa_s->conf->wps_vendor_ext[i]); |
| 3898 | } |
| 3899 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3900 | p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq); |
| 3901 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3902 | return 0; |
| 3903 | } |
| 3904 | |
| 3905 | |
| 3906 | /** |
| 3907 | * wpas_p2p_deinit - Deinitialize per-interface P2P data |
| 3908 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 3909 | * |
| 3910 | * This function deinitialize per-interface P2P data. |
| 3911 | */ |
| 3912 | void wpas_p2p_deinit(struct wpa_supplicant *wpa_s) |
| 3913 | { |
| 3914 | if (wpa_s->driver && wpa_s->drv_priv) |
| 3915 | wpa_drv_probe_req_report(wpa_s, 0); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3916 | |
| 3917 | if (wpa_s->go_params) { |
| 3918 | /* Clear any stored provisioning info */ |
| 3919 | p2p_clear_provisioning_info( |
| 3920 | wpa_s->global->p2p, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3921 | wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3922 | } |
| 3923 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3924 | os_free(wpa_s->go_params); |
| 3925 | wpa_s->go_params = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3926 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 3927 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 3928 | wpa_s->p2p_long_listen = 0; |
| 3929 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 3930 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); |
| 3931 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 3932 | eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3933 | wpas_p2p_listen_work_done(wpa_s); |
| 3934 | if (wpa_s->p2p_send_action_work) { |
| 3935 | os_free(wpa_s->p2p_send_action_work->ctx); |
| 3936 | radio_work_done(wpa_s->p2p_send_action_work); |
| 3937 | wpa_s->p2p_send_action_work = NULL; |
| 3938 | } |
| 3939 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3940 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 3941 | wpabuf_free(wpa_s->p2p_oob_dev_pw); |
| 3942 | wpa_s->p2p_oob_dev_pw = NULL; |
| 3943 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3944 | /* TODO: remove group interface from the driver if this wpa_s instance |
| 3945 | * is on top of a P2P group interface */ |
| 3946 | } |
| 3947 | |
| 3948 | |
| 3949 | /** |
| 3950 | * wpas_p2p_deinit_global - Deinitialize global P2P module |
| 3951 | * @global: Pointer to global data from wpa_supplicant_init() |
| 3952 | * |
| 3953 | * This function deinitializes the global (per device) P2P module. |
| 3954 | */ |
| 3955 | void wpas_p2p_deinit_global(struct wpa_global *global) |
| 3956 | { |
| 3957 | struct wpa_supplicant *wpa_s, *tmp; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3958 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3959 | wpa_s = global->ifaces; |
| 3960 | if (wpa_s) |
| 3961 | wpas_p2p_service_flush(wpa_s); |
| 3962 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3963 | if (global->p2p == NULL) |
| 3964 | return; |
| 3965 | |
| 3966 | /* Remove remaining P2P group interfaces */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3967 | while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) |
| 3968 | wpa_s = wpa_s->next; |
| 3969 | while (wpa_s) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3970 | tmp = global->ifaces; |
| 3971 | while (tmp && |
| 3972 | (tmp == wpa_s || |
| 3973 | tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) { |
| 3974 | tmp = tmp->next; |
| 3975 | } |
| 3976 | if (tmp == NULL) |
| 3977 | break; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3978 | /* Disconnect from the P2P group and deinit the interface */ |
| 3979 | wpas_p2p_disconnect(tmp); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3980 | } |
| 3981 | |
| 3982 | /* |
| 3983 | * Deinit GO data on any possibly remaining interface (if main |
| 3984 | * interface is used as GO). |
| 3985 | */ |
| 3986 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3987 | if (wpa_s->ap_iface) |
| 3988 | wpas_p2p_group_deinit(wpa_s); |
| 3989 | } |
| 3990 | |
| 3991 | p2p_deinit(global->p2p); |
| 3992 | global->p2p = NULL; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3993 | global->p2p_init_wpa_s = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3994 | } |
| 3995 | |
| 3996 | |
| 3997 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s) |
| 3998 | { |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3999 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) && |
| 4000 | wpa_s->conf->p2p_no_group_iface) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4001 | return 0; /* separate interface disabled per configuration */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4002 | if (wpa_s->drv_flags & |
| 4003 | (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE | |
| 4004 | WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P)) |
| 4005 | return 1; /* P2P group requires a new interface in every case |
| 4006 | */ |
| 4007 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)) |
| 4008 | return 0; /* driver does not support concurrent operations */ |
| 4009 | if (wpa_s->global->ifaces->next) |
| 4010 | return 1; /* more that one interface already in use */ |
| 4011 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) |
| 4012 | return 1; /* this interface is already in use */ |
| 4013 | return 0; |
| 4014 | } |
| 4015 | |
| 4016 | |
| 4017 | static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s, |
| 4018 | const u8 *peer_addr, |
| 4019 | enum p2p_wps_method wps_method, |
| 4020 | int go_intent, const u8 *own_interface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4021 | unsigned int force_freq, int persistent_group, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4022 | struct wpa_ssid *ssid, unsigned int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4023 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4024 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
| 4025 | persistent_group = 2; |
| 4026 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4027 | /* |
| 4028 | * Increase GO config timeout if HT40 is used since it takes some time |
| 4029 | * to scan channels for coex purposes before the BSS can be started. |
| 4030 | */ |
| 4031 | p2p_set_config_timeout(wpa_s->global->p2p, |
| 4032 | wpa_s->p2p_go_ht40 ? 255 : 100, 20); |
| 4033 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4034 | return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method, |
| 4035 | go_intent, own_interface_addr, force_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4036 | persistent_group, ssid ? ssid->ssid : NULL, |
| 4037 | ssid ? ssid->ssid_len : 0, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4038 | wpa_s->p2p_pd_before_go_neg, pref_freq, |
| 4039 | wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id : |
| 4040 | 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | |
| 4044 | static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s, |
| 4045 | const u8 *peer_addr, |
| 4046 | enum p2p_wps_method wps_method, |
| 4047 | int go_intent, const u8 *own_interface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4048 | unsigned int force_freq, int persistent_group, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4049 | struct wpa_ssid *ssid, unsigned int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4050 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4051 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
| 4052 | persistent_group = 2; |
| 4053 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4054 | return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method, |
| 4055 | go_intent, own_interface_addr, force_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4056 | persistent_group, ssid ? ssid->ssid : NULL, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4057 | ssid ? ssid->ssid_len : 0, pref_freq, |
| 4058 | wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id : |
| 4059 | 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | |
| 4063 | static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s) |
| 4064 | { |
| 4065 | wpa_s->p2p_join_scan_count++; |
| 4066 | wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d", |
| 4067 | wpa_s->p2p_join_scan_count); |
| 4068 | if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) { |
| 4069 | wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR |
| 4070 | " for join operationg - stop join attempt", |
| 4071 | MAC2STR(wpa_s->pending_join_iface_addr)); |
| 4072 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4073 | if (wpa_s->p2p_auto_pd) { |
| 4074 | wpa_s->p2p_auto_pd = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4075 | wpa_msg_global(wpa_s, MSG_INFO, |
| 4076 | P2P_EVENT_PROV_DISC_FAILURE |
| 4077 | " p2p_dev_addr=" MACSTR " status=N/A", |
| 4078 | MAC2STR(wpa_s->pending_join_dev_addr)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4079 | return; |
| 4080 | } |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4081 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 4082 | P2P_EVENT_GROUP_FORMATION_FAILURE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4083 | } |
| 4084 | } |
| 4085 | |
| 4086 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4087 | static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq) |
| 4088 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4089 | int *freqs, res, num, i; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4090 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4091 | if (wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 4092 | /* Multiple channels are supported and not all are in use */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4093 | return 0; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4094 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4095 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4096 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 4097 | if (!freqs) |
| 4098 | return 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4099 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4100 | num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, |
| 4101 | wpa_s->num_multichan_concurrent); |
| 4102 | if (num < 0) { |
| 4103 | res = 1; |
| 4104 | goto exit_free; |
| 4105 | } |
| 4106 | |
| 4107 | for (i = 0; i < num; i++) { |
| 4108 | if (freqs[i] == freq) { |
| 4109 | wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used", |
| 4110 | freq); |
| 4111 | res = 0; |
| 4112 | goto exit_free; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4113 | } |
| 4114 | } |
| 4115 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4116 | res = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4117 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4118 | exit_free: |
| 4119 | os_free(freqs); |
| 4120 | return res; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4121 | } |
| 4122 | |
| 4123 | |
| 4124 | static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s, |
| 4125 | const u8 *peer_dev_addr) |
| 4126 | { |
| 4127 | struct wpa_bss *bss; |
| 4128 | int updated; |
| 4129 | |
| 4130 | bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr); |
| 4131 | if (bss == NULL) |
| 4132 | return -1; |
| 4133 | if (bss->last_update_idx < wpa_s->bss_update_idx) { |
| 4134 | wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the " |
| 4135 | "last scan"); |
| 4136 | return 0; |
| 4137 | } |
| 4138 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4139 | updated = os_reltime_before(&wpa_s->p2p_auto_started, |
| 4140 | &bss->last_update); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4141 | wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at " |
| 4142 | "%ld.%06ld (%supdated in last scan)", |
| 4143 | bss->last_update.sec, bss->last_update.usec, |
| 4144 | updated ? "": "not "); |
| 4145 | |
| 4146 | return updated; |
| 4147 | } |
| 4148 | |
| 4149 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4150 | static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s, |
| 4151 | struct wpa_scan_results *scan_res) |
| 4152 | { |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4153 | struct wpa_bss *bss = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4154 | int freq; |
| 4155 | u8 iface_addr[ETH_ALEN]; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4156 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4157 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4158 | |
| 4159 | if (wpa_s->global->p2p_disabled) |
| 4160 | return; |
| 4161 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4162 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin", |
| 4163 | scan_res ? (int) scan_res->num : -1, |
| 4164 | wpa_s->p2p_auto_join ? "auto_" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4165 | |
| 4166 | if (scan_res) |
| 4167 | wpas_p2p_scan_res_handler(wpa_s, scan_res); |
| 4168 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4169 | if (wpa_s->p2p_auto_pd) { |
| 4170 | int join = wpas_p2p_peer_go(wpa_s, |
| 4171 | wpa_s->pending_join_dev_addr); |
| 4172 | if (join == 0 && |
| 4173 | wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) { |
| 4174 | wpa_s->auto_pd_scan_retry++; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4175 | bss = wpa_bss_get_bssid_latest( |
| 4176 | wpa_s, wpa_s->pending_join_dev_addr); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4177 | if (bss) { |
| 4178 | freq = bss->freq; |
| 4179 | wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for " |
| 4180 | "the peer " MACSTR " at %d MHz", |
| 4181 | wpa_s->auto_pd_scan_retry, |
| 4182 | MAC2STR(wpa_s-> |
| 4183 | pending_join_dev_addr), |
| 4184 | freq); |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4185 | wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4186 | return; |
| 4187 | } |
| 4188 | } |
| 4189 | |
| 4190 | if (join < 0) |
| 4191 | join = 0; |
| 4192 | |
| 4193 | wpa_s->p2p_auto_pd = 0; |
| 4194 | wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG; |
| 4195 | wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d", |
| 4196 | MAC2STR(wpa_s->pending_join_dev_addr), join); |
| 4197 | if (p2p_prov_disc_req(wpa_s->global->p2p, |
| 4198 | wpa_s->pending_join_dev_addr, |
| 4199 | wpa_s->pending_pd_config_methods, join, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4200 | 0, wpa_s->user_initiated_pd) < 0) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4201 | wpa_s->p2p_auto_pd = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4202 | wpa_msg_global(wpa_s, MSG_INFO, |
| 4203 | P2P_EVENT_PROV_DISC_FAILURE |
| 4204 | " p2p_dev_addr=" MACSTR " status=N/A", |
| 4205 | MAC2STR(wpa_s->pending_join_dev_addr)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4206 | } |
| 4207 | return; |
| 4208 | } |
| 4209 | |
| 4210 | if (wpa_s->p2p_auto_join) { |
| 4211 | int join = wpas_p2p_peer_go(wpa_s, |
| 4212 | wpa_s->pending_join_dev_addr); |
| 4213 | if (join < 0) { |
| 4214 | wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be " |
| 4215 | "running a GO -> use GO Negotiation"); |
| 4216 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, |
| 4217 | wpa_s->p2p_pin, wpa_s->p2p_wps_method, |
| 4218 | wpa_s->p2p_persistent_group, 0, 0, 0, |
| 4219 | wpa_s->p2p_go_intent, |
| 4220 | wpa_s->p2p_connect_freq, |
| 4221 | wpa_s->p2p_persistent_id, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4222 | wpa_s->p2p_pd_before_go_neg, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4223 | wpa_s->p2p_go_ht40, |
| 4224 | wpa_s->p2p_go_vht); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4225 | return; |
| 4226 | } |
| 4227 | |
| 4228 | wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> " |
| 4229 | "try to join the group", join ? "" : |
| 4230 | " in older scan"); |
| 4231 | if (!join) |
| 4232 | wpa_s->p2p_fallback_to_go_neg = 1; |
| 4233 | } |
| 4234 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4235 | freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4236 | wpa_s->pending_join_iface_addr); |
| 4237 | if (freq < 0 && |
| 4238 | p2p_get_interface_addr(wpa_s->global->p2p, |
| 4239 | wpa_s->pending_join_dev_addr, |
| 4240 | iface_addr) == 0 && |
| 4241 | os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0) |
| 4242 | { |
| 4243 | wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface " |
| 4244 | "address for join from " MACSTR " to " MACSTR |
| 4245 | " based on newly discovered P2P peer entry", |
| 4246 | MAC2STR(wpa_s->pending_join_iface_addr), |
| 4247 | MAC2STR(iface_addr)); |
| 4248 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, |
| 4249 | ETH_ALEN); |
| 4250 | |
| 4251 | freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4252 | wpa_s->pending_join_iface_addr); |
| 4253 | } |
| 4254 | if (freq >= 0) { |
| 4255 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " |
| 4256 | "from P2P peer table: %d MHz", freq); |
| 4257 | } |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4258 | if (wpa_s->p2p_join_ssid_len) { |
| 4259 | wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID " |
| 4260 | MACSTR " and SSID %s", |
| 4261 | MAC2STR(wpa_s->pending_join_iface_addr), |
| 4262 | wpa_ssid_txt(wpa_s->p2p_join_ssid, |
| 4263 | wpa_s->p2p_join_ssid_len)); |
| 4264 | bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr, |
| 4265 | wpa_s->p2p_join_ssid, |
| 4266 | wpa_s->p2p_join_ssid_len); |
| 4267 | } |
| 4268 | if (!bss) { |
| 4269 | wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID " |
| 4270 | MACSTR, MAC2STR(wpa_s->pending_join_iface_addr)); |
| 4271 | bss = wpa_bss_get_bssid_latest(wpa_s, |
| 4272 | wpa_s->pending_join_iface_addr); |
| 4273 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4274 | if (bss) { |
| 4275 | freq = bss->freq; |
| 4276 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 4277 | "from BSS table: %d MHz (SSID %s)", freq, |
| 4278 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4279 | } |
| 4280 | if (freq > 0) { |
| 4281 | u16 method; |
| 4282 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4283 | if (wpas_check_freq_conflict(wpa_s, freq) > 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4284 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 4285 | P2P_EVENT_GROUP_FORMATION_FAILURE |
| 4286 | "reason=FREQ_CONFLICT"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4287 | return; |
| 4288 | } |
| 4289 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4290 | wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request " |
| 4291 | "prior to joining an existing group (GO " MACSTR |
| 4292 | " freq=%u MHz)", |
| 4293 | MAC2STR(wpa_s->pending_join_dev_addr), freq); |
| 4294 | wpa_s->pending_pd_before_join = 1; |
| 4295 | |
| 4296 | switch (wpa_s->pending_join_wps_method) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4297 | case WPS_PIN_DISPLAY: |
| 4298 | method = WPS_CONFIG_KEYPAD; |
| 4299 | break; |
| 4300 | case WPS_PIN_KEYPAD: |
| 4301 | method = WPS_CONFIG_DISPLAY; |
| 4302 | break; |
| 4303 | case WPS_PBC: |
| 4304 | method = WPS_CONFIG_PUSHBUTTON; |
| 4305 | break; |
| 4306 | default: |
| 4307 | method = 0; |
| 4308 | break; |
| 4309 | } |
| 4310 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4311 | if ((p2p_get_provisioning_info(wpa_s->global->p2p, |
| 4312 | wpa_s->pending_join_dev_addr) == |
| 4313 | method)) { |
| 4314 | /* |
| 4315 | * We have already performed provision discovery for |
| 4316 | * joining the group. Proceed directly to join |
| 4317 | * operation without duplicated provision discovery. */ |
| 4318 | wpa_printf(MSG_DEBUG, "P2P: Provision discovery " |
| 4319 | "with " MACSTR " already done - proceed to " |
| 4320 | "join", |
| 4321 | MAC2STR(wpa_s->pending_join_dev_addr)); |
| 4322 | wpa_s->pending_pd_before_join = 0; |
| 4323 | goto start; |
| 4324 | } |
| 4325 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4326 | if (p2p_prov_disc_req(wpa_s->global->p2p, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4327 | wpa_s->pending_join_dev_addr, method, 1, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4328 | freq, wpa_s->user_initiated_pd) < 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4329 | wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision " |
| 4330 | "Discovery Request before joining an " |
| 4331 | "existing group"); |
| 4332 | wpa_s->pending_pd_before_join = 0; |
| 4333 | goto start; |
| 4334 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4335 | return; |
| 4336 | } |
| 4337 | |
| 4338 | wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later"); |
| 4339 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4340 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); |
| 4341 | wpas_p2p_check_join_scan_limit(wpa_s); |
| 4342 | return; |
| 4343 | |
| 4344 | start: |
| 4345 | /* Start join operation immediately */ |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4346 | wpas_p2p_join_start(wpa_s, 0, NULL, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4350 | static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq, |
| 4351 | const u8 *ssid, size_t ssid_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4352 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4353 | int ret; |
| 4354 | struct wpa_driver_scan_params params; |
| 4355 | struct wpabuf *wps_ie, *ies; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4356 | size_t ielen; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4357 | int freqs[2] = { 0, 0 }; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4358 | |
| 4359 | os_memset(¶ms, 0, sizeof(params)); |
| 4360 | |
| 4361 | /* P2P Wildcard SSID */ |
| 4362 | params.num_ssids = 1; |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4363 | if (ssid && ssid_len) { |
| 4364 | params.ssids[0].ssid = ssid; |
| 4365 | params.ssids[0].ssid_len = ssid_len; |
| 4366 | os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len); |
| 4367 | wpa_s->p2p_join_ssid_len = ssid_len; |
| 4368 | } else { |
| 4369 | params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; |
| 4370 | params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; |
| 4371 | wpa_s->p2p_join_ssid_len = 0; |
| 4372 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4373 | |
| 4374 | wpa_s->wps->dev.p2p = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4375 | wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev, |
| 4376 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0, |
| 4377 | NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4378 | if (wps_ie == NULL) { |
| 4379 | wpas_p2p_scan_res_join(wpa_s, NULL); |
| 4380 | return; |
| 4381 | } |
| 4382 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4383 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
| 4384 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4385 | if (ies == NULL) { |
| 4386 | wpabuf_free(wps_ie); |
| 4387 | wpas_p2p_scan_res_join(wpa_s, NULL); |
| 4388 | return; |
| 4389 | } |
| 4390 | wpabuf_put_buf(ies, wps_ie); |
| 4391 | wpabuf_free(wps_ie); |
| 4392 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 4393 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4394 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4395 | params.p2p_probe = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4396 | params.extra_ies = wpabuf_head(ies); |
| 4397 | params.extra_ies_len = wpabuf_len(ies); |
Dmitry Shmidt | 9e3f8ee | 2014-01-17 10:52:01 -0800 | [diff] [blame] | 4398 | |
| 4399 | if (!freq) { |
| 4400 | int oper_freq; |
| 4401 | /* |
| 4402 | * If freq is not provided, check the operating freq of the GO |
| 4403 | * and use a single channel scan on if possible. |
| 4404 | */ |
| 4405 | oper_freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4406 | wpa_s->pending_join_iface_addr); |
| 4407 | if (oper_freq > 0) |
| 4408 | freq = oper_freq; |
| 4409 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4410 | if (freq > 0) { |
| 4411 | freqs[0] = freq; |
| 4412 | params.freqs = freqs; |
| 4413 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4414 | |
| 4415 | /* |
| 4416 | * Run a scan to update BSS table and start Provision Discovery once |
| 4417 | * the new scan results become available. |
| 4418 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4419 | ret = wpa_drv_scan(wpa_s, ¶ms); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4420 | if (!ret) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4421 | os_get_reltime(&wpa_s->scan_trigger_time); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4422 | wpa_s->scan_res_handler = wpas_p2p_scan_res_join; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4423 | wpa_s->own_scan_requested = 1; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4424 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4425 | |
| 4426 | wpabuf_free(ies); |
| 4427 | |
| 4428 | if (ret) { |
| 4429 | wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - " |
| 4430 | "try again later"); |
| 4431 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4432 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); |
| 4433 | wpas_p2p_check_join_scan_limit(wpa_s); |
| 4434 | } |
| 4435 | } |
| 4436 | |
| 4437 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4438 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx) |
| 4439 | { |
| 4440 | struct wpa_supplicant *wpa_s = eloop_ctx; |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4441 | wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4445 | 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] | 4446 | const u8 *dev_addr, enum p2p_wps_method wps_method, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4447 | int auto_join, int op_freq, |
| 4448 | const u8 *ssid, size_t ssid_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4449 | { |
| 4450 | wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface " |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4451 | MACSTR " dev " MACSTR " op_freq=%d)%s", |
| 4452 | MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4453 | auto_join ? " (auto_join)" : ""); |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4454 | if (ssid && ssid_len) { |
| 4455 | wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s", |
| 4456 | wpa_ssid_txt(ssid, ssid_len)); |
| 4457 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4458 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4459 | wpa_s->p2p_auto_pd = 0; |
| 4460 | wpa_s->p2p_auto_join = !!auto_join; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4461 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN); |
| 4462 | os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN); |
| 4463 | wpa_s->pending_join_wps_method = wps_method; |
| 4464 | |
| 4465 | /* Make sure we are not running find during connection establishment */ |
| 4466 | wpas_p2p_stop_find(wpa_s); |
| 4467 | |
| 4468 | wpa_s->p2p_join_scan_count = 0; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4469 | wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4470 | return 0; |
| 4471 | } |
| 4472 | |
| 4473 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4474 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq, |
| 4475 | const u8 *ssid, size_t ssid_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4476 | { |
| 4477 | struct wpa_supplicant *group; |
| 4478 | struct p2p_go_neg_results res; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4479 | struct wpa_bss *bss; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4480 | |
| 4481 | group = wpas_p2p_get_group_iface(wpa_s, 0, 0); |
| 4482 | if (group == NULL) |
| 4483 | return -1; |
| 4484 | if (group != wpa_s) { |
| 4485 | os_memcpy(group->p2p_pin, wpa_s->p2p_pin, |
| 4486 | sizeof(group->p2p_pin)); |
| 4487 | group->p2p_wps_method = wpa_s->p2p_wps_method; |
Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 4488 | } else { |
| 4489 | /* |
| 4490 | * Need to mark the current interface for p2p_group_formation |
| 4491 | * when a separate group interface is not used. This is needed |
| 4492 | * to allow p2p_cancel stop a pending p2p_connect-join. |
| 4493 | * wpas_p2p_init_group_interface() addresses this for the case |
| 4494 | * where a separate group interface is used. |
| 4495 | */ |
| 4496 | wpa_s->global->p2p_group_formation = wpa_s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4497 | } |
| 4498 | |
| 4499 | group->p2p_in_provisioning = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4500 | 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] | 4501 | |
| 4502 | os_memset(&res, 0, sizeof(res)); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4503 | os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4504 | os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr, |
| 4505 | ETH_ALEN); |
| 4506 | res.wps_method = wpa_s->pending_join_wps_method; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4507 | if (freq && ssid && ssid_len) { |
| 4508 | res.freq = freq; |
| 4509 | res.ssid_len = ssid_len; |
| 4510 | os_memcpy(res.ssid, ssid, ssid_len); |
| 4511 | } else { |
| 4512 | bss = wpa_bss_get_bssid_latest(wpa_s, |
| 4513 | wpa_s->pending_join_iface_addr); |
| 4514 | if (bss) { |
| 4515 | res.freq = bss->freq; |
| 4516 | res.ssid_len = bss->ssid_len; |
| 4517 | os_memcpy(res.ssid, bss->ssid, bss->ssid_len); |
| 4518 | wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)", |
| 4519 | bss->freq, |
| 4520 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); |
| 4521 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4522 | } |
| 4523 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 4524 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 4525 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to " |
| 4526 | "starting client"); |
| 4527 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 4528 | wpa_s->off_channel_freq = 0; |
| 4529 | wpa_s->roc_waiting_drv_freq = 0; |
| 4530 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4531 | wpas_start_wps_enrollee(group, &res); |
| 4532 | |
| 4533 | /* |
| 4534 | * Allow a longer timeout for join-a-running-group than normal 15 |
| 4535 | * second group formation timeout since the GO may not have authorized |
| 4536 | * our connection yet. |
| 4537 | */ |
| 4538 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 4539 | eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout, |
| 4540 | wpa_s, NULL); |
| 4541 | |
| 4542 | return 0; |
| 4543 | } |
| 4544 | |
| 4545 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4546 | 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] | 4547 | int *force_freq, int *pref_freq, int go) |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4548 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4549 | int *freqs, res; |
| 4550 | unsigned int freq_in_use = 0, num, i; |
| 4551 | |
| 4552 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 4553 | if (!freqs) |
| 4554 | return -1; |
| 4555 | |
| 4556 | num = get_shared_radio_freqs(wpa_s, freqs, |
| 4557 | wpa_s->num_multichan_concurrent); |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 4558 | wpa_printf(MSG_DEBUG, |
| 4559 | "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u", |
| 4560 | freq, wpa_s->num_multichan_concurrent, num); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4561 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4562 | if (freq > 0) { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4563 | int ret; |
| 4564 | if (go) |
| 4565 | ret = p2p_supported_freq(wpa_s->global->p2p, freq); |
| 4566 | else |
| 4567 | ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq); |
| 4568 | if (!ret) { |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4569 | wpa_printf(MSG_DEBUG, "P2P: The forced channel " |
| 4570 | "(%u MHz) is not supported for P2P uses", |
| 4571 | freq); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4572 | res = -3; |
| 4573 | goto exit_free; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4574 | } |
| 4575 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4576 | for (i = 0; i < num; i++) { |
| 4577 | if (freqs[i] == freq) |
| 4578 | freq_in_use = 1; |
| 4579 | } |
| 4580 | |
| 4581 | if (num == wpa_s->num_multichan_concurrent && !freq_in_use) { |
| 4582 | wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels", |
| 4583 | freq); |
| 4584 | res = -2; |
| 4585 | goto exit_free; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4586 | } |
| 4587 | wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " |
| 4588 | "requested channel (%u MHz)", freq); |
| 4589 | *force_freq = freq; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4590 | goto exit_ok; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4591 | } |
| 4592 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4593 | for (i = 0; i < num; i++) { |
| 4594 | if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i])) |
| 4595 | continue; |
| 4596 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4597 | if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4598 | wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using", |
| 4599 | freqs[i]); |
Dmitry Shmidt | 51a47d5 | 2013-09-10 10:52:57 -0700 | [diff] [blame] | 4600 | *pref_freq = freqs[i]; |
Dmitry Shmidt | 51a47d5 | 2013-09-10 10:52:57 -0700 | [diff] [blame] | 4601 | } else { |
| 4602 | 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] | 4603 | freqs[i]); |
Dmitry Shmidt | 51a47d5 | 2013-09-10 10:52:57 -0700 | [diff] [blame] | 4604 | *force_freq = freqs[i]; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4605 | } |
| 4606 | break; |
| 4607 | } |
| 4608 | |
| 4609 | if (i == num) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4610 | if (num < wpa_s->num_multichan_concurrent && num > 0) { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4611 | wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel"); |
| 4612 | *force_freq = 0; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4613 | } else if (num < wpa_s->num_multichan_concurrent) { |
| 4614 | wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel"); |
| 4615 | *force_freq = 0; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4616 | } else { |
| 4617 | wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group"); |
| 4618 | res = -2; |
| 4619 | goto exit_free; |
| 4620 | } |
| 4621 | } |
| 4622 | |
| 4623 | exit_ok: |
| 4624 | res = 0; |
| 4625 | exit_free: |
| 4626 | os_free(freqs); |
| 4627 | return res; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4631 | /** |
| 4632 | * wpas_p2p_connect - Request P2P Group Formation to be started |
| 4633 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4634 | * @peer_addr: Address of the peer P2P Device |
| 4635 | * @pin: PIN to use during provisioning or %NULL to indicate PBC mode |
| 4636 | * @persistent_group: Whether to create a persistent group |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4637 | * @auto_join: Whether to select join vs. GO Negotiation automatically |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4638 | * @join: Whether to join an existing group (as a client) instead of starting |
| 4639 | * Group Owner negotiation; @peer_addr is BSSID in that case |
| 4640 | * @auth: Whether to only authorize the connection instead of doing that and |
| 4641 | * initiating Group Owner negotiation |
| 4642 | * @go_intent: GO Intent or -1 to use default |
| 4643 | * @freq: Frequency for the group or 0 for auto-selection |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4644 | * @persistent_id: Persistent group credentials to use for forcing GO |
| 4645 | * parameters or -1 to generate new values (SSID/passphrase) |
| 4646 | * @pd: Whether to send Provision Discovery prior to GO Negotiation as an |
| 4647 | * interoperability workaround when initiating group formation |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4648 | * @ht40: Start GO with 40 MHz channel width |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4649 | * @vht: Start GO with VHT support |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4650 | * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified |
| 4651 | * failure, -2 on failure due to channel not currently available, |
| 4652 | * -3 if forced channel is not supported |
| 4653 | */ |
| 4654 | int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
| 4655 | const char *pin, enum p2p_wps_method wps_method, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4656 | int persistent_group, int auto_join, int join, int auth, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4657 | int go_intent, int freq, int persistent_id, int pd, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4658 | int ht40, int vht) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4659 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4660 | int force_freq = 0, pref_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4661 | int ret = 0, res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4662 | enum wpa_driver_if_type iftype; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4663 | const u8 *if_addr; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4664 | struct wpa_ssid *ssid = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4665 | |
| 4666 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4667 | return -1; |
| 4668 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4669 | if (persistent_id >= 0) { |
| 4670 | ssid = wpa_config_get_network(wpa_s->conf, persistent_id); |
| 4671 | if (ssid == NULL || ssid->disabled != 2 || |
| 4672 | ssid->mode != WPAS_MODE_P2P_GO) |
| 4673 | return -1; |
| 4674 | } |
| 4675 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4676 | os_free(wpa_s->global->add_psk); |
| 4677 | wpa_s->global->add_psk = NULL; |
| 4678 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4679 | wpa_s->global->p2p_fail_on_wps_complete = 0; |
| 4680 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4681 | if (go_intent < 0) |
| 4682 | go_intent = wpa_s->conf->p2p_go_intent; |
| 4683 | |
| 4684 | if (!auth) |
| 4685 | wpa_s->p2p_long_listen = 0; |
| 4686 | |
| 4687 | wpa_s->p2p_wps_method = wps_method; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4688 | wpa_s->p2p_persistent_group = !!persistent_group; |
| 4689 | wpa_s->p2p_persistent_id = persistent_id; |
| 4690 | wpa_s->p2p_go_intent = go_intent; |
| 4691 | wpa_s->p2p_connect_freq = freq; |
| 4692 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 4693 | wpa_s->p2p_pd_before_go_neg = !!pd; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4694 | wpa_s->p2p_go_ht40 = !!ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4695 | wpa_s->p2p_go_vht = !!vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4696 | |
| 4697 | if (pin) |
| 4698 | os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin)); |
| 4699 | else if (wps_method == WPS_PIN_DISPLAY) { |
| 4700 | ret = wps_generate_pin(); |
| 4701 | os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d", |
| 4702 | ret); |
| 4703 | wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s", |
| 4704 | wpa_s->p2p_pin); |
| 4705 | } else |
| 4706 | wpa_s->p2p_pin[0] = '\0'; |
| 4707 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4708 | if (join || auto_join) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4709 | u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN]; |
| 4710 | if (auth) { |
| 4711 | wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to " |
| 4712 | "connect a running group from " MACSTR, |
| 4713 | MAC2STR(peer_addr)); |
| 4714 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); |
| 4715 | return ret; |
| 4716 | } |
| 4717 | os_memcpy(dev_addr, peer_addr, ETH_ALEN); |
| 4718 | if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr, |
| 4719 | iface_addr) < 0) { |
| 4720 | os_memcpy(iface_addr, peer_addr, ETH_ALEN); |
| 4721 | p2p_get_dev_addr(wpa_s->global->p2p, peer_addr, |
| 4722 | dev_addr); |
| 4723 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4724 | if (auto_join) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4725 | os_get_reltime(&wpa_s->p2p_auto_started); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4726 | wpa_printf(MSG_DEBUG, "P2P: Auto join started at " |
| 4727 | "%ld.%06ld", |
| 4728 | wpa_s->p2p_auto_started.sec, |
| 4729 | wpa_s->p2p_auto_started.usec); |
| 4730 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4731 | wpa_s->user_initiated_pd = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4732 | if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4733 | auto_join, freq, NULL, 0) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4734 | return -1; |
| 4735 | return ret; |
| 4736 | } |
| 4737 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4738 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 4739 | go_intent == 15); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4740 | if (res) |
| 4741 | return res; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 4742 | wpas_p2p_set_own_freq_preference(wpa_s, |
| 4743 | force_freq ? force_freq : pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4744 | |
| 4745 | wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); |
| 4746 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4747 | if (wpa_s->create_p2p_iface) { |
| 4748 | /* Prepare to add a new interface for the group */ |
| 4749 | iftype = WPA_IF_P2P_GROUP; |
| 4750 | if (go_intent == 15) |
| 4751 | iftype = WPA_IF_P2P_GO; |
| 4752 | if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { |
| 4753 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 4754 | "interface for the group"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4755 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4756 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4757 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4758 | if_addr = wpa_s->pending_interface_addr; |
| 4759 | } else |
| 4760 | if_addr = wpa_s->own_addr; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4761 | |
| 4762 | if (auth) { |
| 4763 | if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4764 | go_intent, if_addr, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4765 | force_freq, persistent_group, ssid, |
| 4766 | pref_freq) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4767 | return -1; |
| 4768 | return ret; |
| 4769 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4770 | |
| 4771 | if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, |
| 4772 | go_intent, if_addr, force_freq, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4773 | persistent_group, ssid, pref_freq) < 0) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4774 | if (wpa_s->create_p2p_iface) |
| 4775 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4776 | return -1; |
| 4777 | } |
| 4778 | return ret; |
| 4779 | } |
| 4780 | |
| 4781 | |
| 4782 | /** |
| 4783 | * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start |
| 4784 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4785 | * @freq: Frequency of the channel in MHz |
| 4786 | * @duration: Duration of the stay on the channel in milliseconds |
| 4787 | * |
| 4788 | * This callback is called when the driver indicates that it has started the |
| 4789 | * requested remain-on-channel duration. |
| 4790 | */ |
| 4791 | void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s, |
| 4792 | unsigned int freq, unsigned int duration) |
| 4793 | { |
| 4794 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4795 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4796 | if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) { |
| 4797 | p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq, |
| 4798 | wpa_s->pending_listen_duration); |
| 4799 | wpa_s->pending_listen_freq = 0; |
Dmitry Shmidt | 7f93d6f | 2014-02-21 11:22:49 -0800 | [diff] [blame] | 4800 | } else { |
| 4801 | wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)", |
| 4802 | wpa_s->off_channel_freq, wpa_s->pending_listen_freq, |
| 4803 | freq, duration); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4804 | } |
| 4805 | } |
| 4806 | |
| 4807 | |
| 4808 | static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, |
| 4809 | unsigned int timeout) |
| 4810 | { |
| 4811 | /* Limit maximum Listen state time based on driver limitation. */ |
| 4812 | if (timeout > wpa_s->max_remain_on_chan) |
| 4813 | timeout = wpa_s->max_remain_on_chan; |
| 4814 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4815 | return p2p_listen(wpa_s->global->p2p, timeout); |
| 4816 | } |
| 4817 | |
| 4818 | |
| 4819 | /** |
| 4820 | * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout |
| 4821 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4822 | * @freq: Frequency of the channel in MHz |
| 4823 | * |
| 4824 | * This callback is called when the driver indicates that a remain-on-channel |
| 4825 | * operation has been completed, i.e., the duration on the requested channel |
| 4826 | * has timed out. |
| 4827 | */ |
| 4828 | void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s, |
| 4829 | unsigned int freq) |
| 4830 | { |
| 4831 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback " |
| 4832 | "(p2p_long_listen=%d ms pending_action_tx=%p)", |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4833 | wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s)); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4834 | wpas_p2p_listen_work_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4835 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4836 | return; |
| 4837 | if (p2p_listen_end(wpa_s->global->p2p, freq) > 0) |
| 4838 | return; /* P2P module started a new operation */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4839 | if (offchannel_pending_action_tx(wpa_s)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4840 | return; |
| 4841 | if (wpa_s->p2p_long_listen > 0) |
| 4842 | wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan; |
| 4843 | if (wpa_s->p2p_long_listen > 0) { |
| 4844 | wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state"); |
| 4845 | wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4846 | } else { |
| 4847 | /* |
| 4848 | * When listen duration is over, stop listen & update p2p_state |
| 4849 | * to IDLE. |
| 4850 | */ |
| 4851 | p2p_stop_listen(wpa_s->global->p2p); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4852 | } |
| 4853 | } |
| 4854 | |
| 4855 | |
| 4856 | /** |
| 4857 | * wpas_p2p_group_remove - Remove a P2P group |
| 4858 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4859 | * @ifname: Network interface name of the group interface or "*" to remove all |
| 4860 | * groups |
| 4861 | * Returns: 0 on success, -1 on failure |
| 4862 | * |
| 4863 | * This function is used to remove a P2P group. This can be used to disconnect |
| 4864 | * from a group in which the local end is a P2P Client or to end a P2P Group in |
| 4865 | * case the local end is the Group Owner. If a virtual network interface was |
| 4866 | * created for this group, that interface will be removed. Otherwise, only the |
| 4867 | * configured P2P group network will be removed from the interface. |
| 4868 | */ |
| 4869 | int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname) |
| 4870 | { |
| 4871 | struct wpa_global *global = wpa_s->global; |
| 4872 | |
| 4873 | if (os_strcmp(ifname, "*") == 0) { |
| 4874 | struct wpa_supplicant *prev; |
| 4875 | wpa_s = global->ifaces; |
| 4876 | while (wpa_s) { |
| 4877 | prev = wpa_s; |
| 4878 | wpa_s = wpa_s->next; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 4879 | if (prev->p2p_group_interface != |
| 4880 | NOT_P2P_GROUP_INTERFACE || |
| 4881 | (prev->current_ssid && |
| 4882 | prev->current_ssid->p2p_group)) |
| 4883 | wpas_p2p_disconnect(prev); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4884 | } |
| 4885 | return 0; |
| 4886 | } |
| 4887 | |
| 4888 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 4889 | if (os_strcmp(wpa_s->ifname, ifname) == 0) |
| 4890 | break; |
| 4891 | } |
| 4892 | |
| 4893 | return wpas_p2p_disconnect(wpa_s); |
| 4894 | } |
| 4895 | |
| 4896 | |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4897 | static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq) |
| 4898 | { |
| 4899 | unsigned int r; |
| 4900 | |
| 4901 | if (freq == 2) { |
| 4902 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz " |
| 4903 | "band"); |
| 4904 | if (wpa_s->best_24_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4905 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 4906 | wpa_s->best_24_freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4907 | freq = wpa_s->best_24_freq; |
| 4908 | wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band " |
| 4909 | "channel: %d MHz", freq); |
| 4910 | } else { |
| 4911 | os_get_random((u8 *) &r, sizeof(r)); |
| 4912 | freq = 2412 + (r % 3) * 25; |
| 4913 | wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band " |
| 4914 | "channel: %d MHz", freq); |
| 4915 | } |
| 4916 | } |
| 4917 | |
| 4918 | if (freq == 5) { |
| 4919 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz " |
| 4920 | "band"); |
| 4921 | if (wpa_s->best_5_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4922 | p2p_supported_freq_go(wpa_s->global->p2p, |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4923 | wpa_s->best_5_freq)) { |
| 4924 | freq = wpa_s->best_5_freq; |
| 4925 | wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band " |
| 4926 | "channel: %d MHz", freq); |
| 4927 | } else { |
| 4928 | os_get_random((u8 *) &r, sizeof(r)); |
| 4929 | freq = 5180 + (r % 4) * 20; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4930 | if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4931 | wpa_printf(MSG_DEBUG, "P2P: Could not select " |
| 4932 | "5 GHz channel for P2P group"); |
| 4933 | return -1; |
| 4934 | } |
| 4935 | wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band " |
| 4936 | "channel: %d MHz", freq); |
| 4937 | } |
| 4938 | } |
| 4939 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4940 | if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4941 | wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO " |
| 4942 | "(%u MHz) is not supported for P2P uses", |
| 4943 | freq); |
| 4944 | return -1; |
| 4945 | } |
| 4946 | |
| 4947 | return freq; |
| 4948 | } |
| 4949 | |
| 4950 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4951 | static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s, |
| 4952 | struct p2p_go_neg_results *params, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4953 | int freq, int ht40, int vht, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4954 | const struct p2p_channels *channels) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4955 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4956 | int res, *freqs; |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 4957 | unsigned int pref_freq; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4958 | unsigned int num, i; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4959 | |
| 4960 | os_memset(params, 0, sizeof(*params)); |
| 4961 | params->role_go = 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4962 | params->ht40 = ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4963 | params->vht = vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4964 | if (freq) { |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4965 | if (!freq_included(channels, freq)) { |
| 4966 | wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not " |
| 4967 | "accepted", freq); |
| 4968 | return -1; |
| 4969 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4970 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced " |
| 4971 | "frequency %d MHz", freq); |
| 4972 | params->freq = freq; |
| 4973 | } else if (wpa_s->conf->p2p_oper_reg_class == 81 && |
| 4974 | wpa_s->conf->p2p_oper_channel >= 1 && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4975 | wpa_s->conf->p2p_oper_channel <= 11 && |
| 4976 | freq_included(channels, |
| 4977 | 2407 + 5 * wpa_s->conf->p2p_oper_channel)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4978 | params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel; |
| 4979 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
| 4980 | "frequency %d MHz", params->freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4981 | } else if ((wpa_s->conf->p2p_oper_reg_class == 115 || |
| 4982 | wpa_s->conf->p2p_oper_reg_class == 116 || |
| 4983 | wpa_s->conf->p2p_oper_reg_class == 117 || |
| 4984 | wpa_s->conf->p2p_oper_reg_class == 124 || |
| 4985 | wpa_s->conf->p2p_oper_reg_class == 126 || |
| 4986 | wpa_s->conf->p2p_oper_reg_class == 127) && |
| 4987 | freq_included(channels, |
| 4988 | 5000 + 5 * wpa_s->conf->p2p_oper_channel)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4989 | params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel; |
| 4990 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
| 4991 | "frequency %d MHz", params->freq); |
| 4992 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 4993 | wpa_s->best_overall_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4994 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 4995 | wpa_s->best_overall_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4996 | freq_included(channels, wpa_s->best_overall_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4997 | params->freq = wpa_s->best_overall_freq; |
| 4998 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall " |
| 4999 | "channel %d MHz", params->freq); |
| 5000 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 5001 | wpa_s->best_24_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5002 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 5003 | wpa_s->best_24_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5004 | freq_included(channels, wpa_s->best_24_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5005 | params->freq = wpa_s->best_24_freq; |
| 5006 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz " |
| 5007 | "channel %d MHz", params->freq); |
| 5008 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 5009 | wpa_s->best_5_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5010 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 5011 | wpa_s->best_5_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5012 | freq_included(channels, wpa_s->best_5_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5013 | params->freq = wpa_s->best_5_freq; |
| 5014 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz " |
| 5015 | "channel %d MHz", params->freq); |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 5016 | } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p, |
| 5017 | channels))) { |
| 5018 | params->freq = pref_freq; |
| 5019 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred " |
| 5020 | "channels", params->freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5021 | } else { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5022 | int chan; |
| 5023 | for (chan = 0; chan < 11; chan++) { |
| 5024 | params->freq = 2412 + chan * 5; |
| 5025 | if (!wpas_p2p_disallowed_freq(wpa_s->global, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5026 | params->freq) && |
| 5027 | freq_included(channels, params->freq)) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5028 | break; |
| 5029 | } |
| 5030 | if (chan == 11) { |
| 5031 | wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel " |
| 5032 | "allowed"); |
| 5033 | return -1; |
| 5034 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5035 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference " |
| 5036 | "known)", params->freq); |
| 5037 | } |
| 5038 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5039 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 5040 | if (!freqs) |
| 5041 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5042 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5043 | res = wpas_p2p_valid_oper_freqs(wpa_s, freqs, |
| 5044 | wpa_s->num_multichan_concurrent); |
| 5045 | if (res < 0) { |
| 5046 | os_free(freqs); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5047 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5048 | } |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5049 | num = res; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5050 | |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 5051 | for (i = 0; i < num; i++) { |
| 5052 | if (freq && freqs[i] == freq) |
| 5053 | break; |
| 5054 | if (!freq && freq_included(channels, freqs[i])) { |
| 5055 | wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)", |
| 5056 | freqs[i]); |
| 5057 | params->freq = freqs[i]; |
| 5058 | break; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5059 | } |
| 5060 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 5061 | |
| 5062 | if (i == num) { |
| 5063 | if (wpas_p2p_num_unused_channels(wpa_s) <= 0) { |
| 5064 | if (freq) |
| 5065 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq); |
| 5066 | else |
| 5067 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using"); |
| 5068 | os_free(freqs); |
| 5069 | return -1; |
| 5070 | } else if (num == 0) { |
| 5071 | wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels"); |
| 5072 | } else { |
| 5073 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels"); |
| 5074 | } |
| 5075 | } |
| 5076 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5077 | os_free(freqs); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5078 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5079 | } |
| 5080 | |
| 5081 | |
| 5082 | static struct wpa_supplicant * |
| 5083 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, |
| 5084 | int go) |
| 5085 | { |
| 5086 | struct wpa_supplicant *group_wpa_s; |
| 5087 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5088 | if (!wpas_p2p_create_iface(wpa_s)) { |
| 5089 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group " |
| 5090 | "operations"); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5091 | wpa_s->p2p_first_connection_timeout = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5092 | return wpa_s; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5093 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5094 | |
| 5095 | 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] | 5096 | WPA_IF_P2P_CLIENT) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5097 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 5098 | "P2P: Failed to add group interface"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5099 | return NULL; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5100 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5101 | group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go); |
| 5102 | if (group_wpa_s == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5103 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 5104 | "P2P: Failed to initialize group interface"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5105 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 5106 | return NULL; |
| 5107 | } |
| 5108 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5109 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s", |
| 5110 | group_wpa_s->ifname); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5111 | group_wpa_s->p2p_first_connection_timeout = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5112 | return group_wpa_s; |
| 5113 | } |
| 5114 | |
| 5115 | |
| 5116 | /** |
| 5117 | * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner |
| 5118 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 5119 | * @persistent_group: Whether to create a persistent group |
| 5120 | * @freq: Frequency for the group or 0 to indicate no hardcoding |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5121 | * @ht40: Start GO with 40 MHz channel width |
| 5122 | * @vht: Start GO with VHT support |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5123 | * Returns: 0 on success, -1 on failure |
| 5124 | * |
| 5125 | * This function creates a new P2P group with the local end as the Group Owner, |
| 5126 | * i.e., without using Group Owner Negotiation. |
| 5127 | */ |
| 5128 | 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] | 5129 | int freq, int ht40, int vht) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5130 | { |
| 5131 | struct p2p_go_neg_results params; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5132 | |
| 5133 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5134 | return -1; |
| 5135 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5136 | os_free(wpa_s->global->add_psk); |
| 5137 | wpa_s->global->add_psk = NULL; |
| 5138 | |
Dmitry Shmidt | dca3979 | 2011-09-06 11:17:33 -0700 | [diff] [blame] | 5139 | /* Make sure we are not running find during connection establishment */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5140 | wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND"); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5141 | wpas_p2p_stop_find_oper(wpa_s); |
Dmitry Shmidt | dca3979 | 2011-09-06 11:17:33 -0700 | [diff] [blame] | 5142 | |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5143 | freq = wpas_p2p_select_go_freq(wpa_s, freq); |
| 5144 | if (freq < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5145 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5146 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5147 | 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] | 5148 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5149 | if (params.freq && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5150 | !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5151 | wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO " |
| 5152 | "(%u MHz) is not supported for P2P uses", |
| 5153 | params.freq); |
| 5154 | return -1; |
| 5155 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5156 | p2p_go_params(wpa_s->global->p2p, ¶ms); |
| 5157 | params.persistent_group = persistent_group; |
| 5158 | |
| 5159 | wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1); |
| 5160 | if (wpa_s == NULL) |
| 5161 | return -1; |
| 5162 | wpas_start_wps_go(wpa_s, ¶ms, 0); |
| 5163 | |
| 5164 | return 0; |
| 5165 | } |
| 5166 | |
| 5167 | |
| 5168 | static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5169 | struct wpa_ssid *params, int addr_allocated, |
| 5170 | int freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5171 | { |
| 5172 | struct wpa_ssid *ssid; |
| 5173 | |
| 5174 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0); |
| 5175 | if (wpa_s == NULL) |
| 5176 | return -1; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5177 | wpa_s->p2p_last_4way_hs_fail = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5178 | |
| 5179 | wpa_supplicant_ap_deinit(wpa_s); |
| 5180 | |
| 5181 | ssid = wpa_config_add_network(wpa_s->conf); |
| 5182 | if (ssid == NULL) |
| 5183 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5184 | wpa_config_set_network_defaults(ssid); |
| 5185 | ssid->temporary = 1; |
| 5186 | ssid->proto = WPA_PROTO_RSN; |
| 5187 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; |
| 5188 | ssid->group_cipher = WPA_CIPHER_CCMP; |
| 5189 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; |
| 5190 | ssid->ssid = os_malloc(params->ssid_len); |
| 5191 | if (ssid->ssid == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5192 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 5193 | return -1; |
| 5194 | } |
| 5195 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); |
| 5196 | ssid->ssid_len = params->ssid_len; |
| 5197 | ssid->p2p_group = 1; |
| 5198 | ssid->export_keys = 1; |
| 5199 | if (params->psk_set) { |
| 5200 | os_memcpy(ssid->psk, params->psk, 32); |
| 5201 | ssid->psk_set = 1; |
| 5202 | } |
| 5203 | if (params->passphrase) |
| 5204 | ssid->passphrase = os_strdup(params->passphrase); |
| 5205 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5206 | wpa_s->show_group_started = 1; |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5207 | wpa_s->p2p_in_invitation = 1; |
| 5208 | wpa_s->p2p_invite_go_freq = freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5209 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5210 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent, |
| 5211 | NULL); |
| 5212 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, |
| 5213 | wpas_p2p_group_formation_timeout, |
| 5214 | wpa_s->parent, NULL); |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 5215 | wpa_supplicant_select_network(wpa_s, ssid); |
| 5216 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5217 | return 0; |
| 5218 | } |
| 5219 | |
| 5220 | |
| 5221 | int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s, |
| 5222 | struct wpa_ssid *ssid, int addr_allocated, |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 5223 | int force_freq, int neg_freq, int ht40, |
| 5224 | int vht, const struct p2p_channels *channels, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5225 | int connection_timeout) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5226 | { |
| 5227 | struct p2p_go_neg_results params; |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 5228 | int go = 0, freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5229 | |
| 5230 | if (ssid->disabled != 2 || ssid->ssid == NULL) |
| 5231 | return -1; |
| 5232 | |
| 5233 | if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) && |
| 5234 | go == (ssid->mode == WPAS_MODE_P2P_GO)) { |
| 5235 | wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is " |
| 5236 | "already running"); |
| 5237 | return 0; |
| 5238 | } |
| 5239 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5240 | os_free(wpa_s->global->add_psk); |
| 5241 | wpa_s->global->add_psk = NULL; |
| 5242 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5243 | /* Make sure we are not running find during connection establishment */ |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5244 | wpas_p2p_stop_find_oper(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5245 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5246 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 5247 | |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 5248 | if (force_freq > 0) { |
| 5249 | freq = wpas_p2p_select_go_freq(wpa_s, force_freq); |
| 5250 | if (freq < 0) |
| 5251 | return -1; |
| 5252 | } else { |
| 5253 | freq = wpas_p2p_select_go_freq(wpa_s, neg_freq); |
| 5254 | if (freq < 0 || (freq > 0 && !freq_included(channels, freq))) |
| 5255 | freq = 0; |
| 5256 | } |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5257 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5258 | if (ssid->mode == WPAS_MODE_INFRA) |
| 5259 | return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq); |
| 5260 | |
| 5261 | if (ssid->mode != WPAS_MODE_P2P_GO) |
| 5262 | return -1; |
| 5263 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5264 | 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] | 5265 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5266 | |
| 5267 | params.role_go = 1; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5268 | params.psk_set = ssid->psk_set; |
| 5269 | if (params.psk_set) |
| 5270 | os_memcpy(params.psk, ssid->psk, sizeof(params.psk)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5271 | if (ssid->passphrase) { |
| 5272 | if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) { |
| 5273 | wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in " |
| 5274 | "persistent group"); |
| 5275 | return -1; |
| 5276 | } |
| 5277 | os_strlcpy(params.passphrase, ssid->passphrase, |
| 5278 | sizeof(params.passphrase)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5279 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5280 | os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len); |
| 5281 | params.ssid_len = ssid->ssid_len; |
| 5282 | params.persistent_group = 1; |
| 5283 | |
| 5284 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1); |
| 5285 | if (wpa_s == NULL) |
| 5286 | return -1; |
| 5287 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5288 | wpa_s->p2p_first_connection_timeout = connection_timeout; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5289 | wpas_start_wps_go(wpa_s, ¶ms, 0); |
| 5290 | |
| 5291 | return 0; |
| 5292 | } |
| 5293 | |
| 5294 | |
| 5295 | static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies, |
| 5296 | struct wpabuf *proberesp_ies) |
| 5297 | { |
| 5298 | struct wpa_supplicant *wpa_s = ctx; |
| 5299 | if (wpa_s->ap_iface) { |
| 5300 | struct hostapd_data *hapd = wpa_s->ap_iface->bss[0]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5301 | if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) { |
| 5302 | wpabuf_free(beacon_ies); |
| 5303 | wpabuf_free(proberesp_ies); |
| 5304 | return; |
| 5305 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5306 | if (beacon_ies) { |
| 5307 | wpabuf_free(hapd->p2p_beacon_ie); |
| 5308 | hapd->p2p_beacon_ie = beacon_ies; |
| 5309 | } |
| 5310 | wpabuf_free(hapd->p2p_probe_resp_ie); |
| 5311 | hapd->p2p_probe_resp_ie = proberesp_ies; |
| 5312 | } else { |
| 5313 | wpabuf_free(beacon_ies); |
| 5314 | wpabuf_free(proberesp_ies); |
| 5315 | } |
| 5316 | wpa_supplicant_ap_update_beacon(wpa_s); |
| 5317 | } |
| 5318 | |
| 5319 | |
| 5320 | static void wpas_p2p_idle_update(void *ctx, int idle) |
| 5321 | { |
| 5322 | struct wpa_supplicant *wpa_s = ctx; |
| 5323 | if (!wpa_s->ap_iface) |
| 5324 | return; |
| 5325 | wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not "); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5326 | if (idle) { |
| 5327 | if (wpa_s->global->p2p_fail_on_wps_complete && |
| 5328 | wpa_s->p2p_in_provisioning) { |
| 5329 | wpas_p2p_grpform_fail_after_wps(wpa_s); |
| 5330 | return; |
| 5331 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5332 | wpas_p2p_set_group_idle_timeout(wpa_s); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5333 | } else |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5334 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); |
| 5335 | } |
| 5336 | |
| 5337 | |
| 5338 | struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5339 | struct wpa_ssid *ssid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5340 | { |
| 5341 | struct p2p_group *group; |
| 5342 | struct p2p_group_config *cfg; |
| 5343 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5344 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5345 | return NULL; |
| 5346 | |
| 5347 | cfg = os_zalloc(sizeof(*cfg)); |
| 5348 | if (cfg == NULL) |
| 5349 | return NULL; |
| 5350 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5351 | if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5352 | cfg->persistent_group = 2; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5353 | else if (ssid->p2p_persistent_group) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5354 | cfg->persistent_group = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5355 | os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN); |
| 5356 | if (wpa_s->max_stations && |
| 5357 | wpa_s->max_stations < wpa_s->conf->max_num_sta) |
| 5358 | cfg->max_clients = wpa_s->max_stations; |
| 5359 | else |
| 5360 | cfg->max_clients = wpa_s->conf->max_num_sta; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5361 | os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len); |
| 5362 | cfg->ssid_len = ssid->ssid_len; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5363 | cfg->freq = ssid->frequency; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5364 | cfg->cb_ctx = wpa_s; |
| 5365 | cfg->ie_update = wpas_p2p_ie_update; |
| 5366 | cfg->idle_update = wpas_p2p_idle_update; |
| 5367 | |
| 5368 | group = p2p_group_init(wpa_s->global->p2p, cfg); |
| 5369 | if (group == NULL) |
| 5370 | os_free(cfg); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5371 | if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5372 | p2p_group_notif_formation_done(group); |
| 5373 | wpa_s->p2p_group = group; |
| 5374 | return group; |
| 5375 | } |
| 5376 | |
| 5377 | |
| 5378 | void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
| 5379 | int registrar) |
| 5380 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5381 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 5382 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5383 | if (!wpa_s->p2p_in_provisioning) { |
| 5384 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P " |
| 5385 | "provisioning not in progress"); |
| 5386 | return; |
| 5387 | } |
| 5388 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5389 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { |
| 5390 | u8 go_dev_addr[ETH_ALEN]; |
| 5391 | os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN); |
| 5392 | wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, |
| 5393 | ssid->ssid_len); |
| 5394 | /* Clear any stored provisioning info */ |
| 5395 | p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr); |
| 5396 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5397 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5398 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent, |
| 5399 | NULL); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5400 | wpa_s->p2p_go_group_formation_completed = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5401 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { |
| 5402 | /* |
| 5403 | * Use a separate timeout for initial data connection to |
| 5404 | * complete to allow the group to be removed automatically if |
| 5405 | * something goes wrong in this step before the P2P group idle |
| 5406 | * timeout mechanism is taken into use. |
| 5407 | */ |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5408 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 5409 | "P2P: Re-start group formation timeout (%d seconds) as client for initial connection", |
| 5410 | P2P_MAX_INITIAL_CONN_WAIT); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5411 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, |
| 5412 | wpas_p2p_group_formation_timeout, |
| 5413 | wpa_s->parent, NULL); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5414 | } else if (ssid) { |
| 5415 | /* |
| 5416 | * Use a separate timeout for initial data connection to |
| 5417 | * complete to allow the group to be removed automatically if |
| 5418 | * the client does not complete data connection successfully. |
| 5419 | */ |
| 5420 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 5421 | "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection", |
| 5422 | P2P_MAX_INITIAL_CONN_WAIT_GO); |
| 5423 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0, |
| 5424 | wpas_p2p_group_formation_timeout, |
| 5425 | wpa_s->parent, NULL); |
| 5426 | /* |
| 5427 | * Complete group formation on first successful data connection |
| 5428 | */ |
| 5429 | wpa_s->p2p_go_group_formation_completed = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5430 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5431 | if (wpa_s->global->p2p) |
| 5432 | p2p_wps_success_cb(wpa_s->global->p2p, peer_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5433 | wpas_group_formation_completed(wpa_s, 1); |
| 5434 | } |
| 5435 | |
| 5436 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5437 | void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s, |
| 5438 | struct wps_event_fail *fail) |
| 5439 | { |
| 5440 | if (!wpa_s->p2p_in_provisioning) { |
| 5441 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P " |
| 5442 | "provisioning not in progress"); |
| 5443 | return; |
| 5444 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5445 | |
| 5446 | if (wpa_s->go_params) { |
| 5447 | p2p_clear_provisioning_info( |
| 5448 | wpa_s->global->p2p, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5449 | wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5450 | } |
| 5451 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5452 | wpas_notify_p2p_wps_failed(wpa_s, fail); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5453 | |
| 5454 | if (wpa_s == wpa_s->global->p2p_group_formation) { |
| 5455 | /* |
| 5456 | * Allow some time for the failed WPS negotiation exchange to |
| 5457 | * complete, but remove the group since group formation cannot |
| 5458 | * succeed after provisioning failure. |
| 5459 | */ |
| 5460 | wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout"); |
| 5461 | wpa_s->global->p2p_fail_on_wps_complete = 1; |
| 5462 | eloop_deplete_timeout(0, 50000, |
| 5463 | wpas_p2p_group_formation_timeout, |
| 5464 | wpa_s->parent, NULL); |
| 5465 | } |
| 5466 | } |
| 5467 | |
| 5468 | |
| 5469 | int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s) |
| 5470 | { |
| 5471 | if (!wpa_s->global->p2p_fail_on_wps_complete || |
| 5472 | !wpa_s->p2p_in_provisioning) |
| 5473 | return 0; |
| 5474 | |
| 5475 | wpas_p2p_grpform_fail_after_wps(wpa_s); |
| 5476 | |
| 5477 | return 1; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5481 | 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] | 5482 | const char *config_method, |
| 5483 | enum wpas_p2p_prov_disc_use use) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5484 | { |
| 5485 | u16 config_methods; |
| 5486 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5487 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 5488 | wpa_s->pending_pd_use = NORMAL_PD; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5489 | if (os_strncmp(config_method, "display", 7) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5490 | config_methods = WPS_CONFIG_DISPLAY; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5491 | else if (os_strncmp(config_method, "keypad", 6) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5492 | config_methods = WPS_CONFIG_KEYPAD; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5493 | else if (os_strncmp(config_method, "pbc", 3) == 0 || |
| 5494 | os_strncmp(config_method, "pushbutton", 10) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5495 | config_methods = WPS_CONFIG_PUSHBUTTON; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5496 | else { |
| 5497 | wpa_printf(MSG_DEBUG, "P2P: Unknown config method"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5498 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5499 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5500 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5501 | if (use == WPAS_P2P_PD_AUTO) { |
| 5502 | os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN); |
| 5503 | wpa_s->pending_pd_config_methods = config_methods; |
| 5504 | wpa_s->p2p_auto_pd = 1; |
| 5505 | wpa_s->p2p_auto_join = 0; |
| 5506 | wpa_s->pending_pd_before_join = 0; |
| 5507 | wpa_s->auto_pd_scan_retry = 0; |
| 5508 | wpas_p2p_stop_find(wpa_s); |
| 5509 | wpa_s->p2p_join_scan_count = 0; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5510 | os_get_reltime(&wpa_s->p2p_auto_started); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5511 | wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld", |
| 5512 | wpa_s->p2p_auto_started.sec, |
| 5513 | wpa_s->p2p_auto_started.usec); |
| 5514 | wpas_p2p_join_scan(wpa_s, NULL); |
| 5515 | return 0; |
| 5516 | } |
| 5517 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5518 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) |
| 5519 | return -1; |
| 5520 | |
| 5521 | return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5522 | config_methods, use == WPAS_P2P_PD_FOR_JOIN, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5523 | 0, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5524 | } |
| 5525 | |
| 5526 | |
| 5527 | int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, |
| 5528 | char *end) |
| 5529 | { |
| 5530 | return p2p_scan_result_text(ies, ies_len, buf, end); |
| 5531 | } |
| 5532 | |
| 5533 | |
| 5534 | static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s) |
| 5535 | { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5536 | if (!offchannel_pending_action_tx(wpa_s)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5537 | return; |
| 5538 | |
| 5539 | wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new " |
| 5540 | "operation request"); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5541 | offchannel_clear_pending_action_tx(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5542 | } |
| 5543 | |
| 5544 | |
| 5545 | int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout, |
| 5546 | enum p2p_discovery_type type, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5547 | unsigned int num_req_dev_types, const u8 *req_dev_types, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5548 | const u8 *dev_id, unsigned int search_delay) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5549 | { |
| 5550 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5551 | wpa_s->p2p_long_listen = 0; |
| 5552 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5553 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL || |
| 5554 | wpa_s->p2p_in_provisioning) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5555 | return -1; |
| 5556 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5557 | wpa_supplicant_cancel_sched_scan(wpa_s); |
| 5558 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5559 | return p2p_find(wpa_s->global->p2p, timeout, type, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5560 | num_req_dev_types, req_dev_types, dev_id, |
| 5561 | search_delay); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5562 | } |
| 5563 | |
| 5564 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5565 | static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5566 | { |
| 5567 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5568 | wpa_s->p2p_long_listen = 0; |
| 5569 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 5570 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5571 | |
| 5572 | if (wpa_s->global->p2p) |
| 5573 | p2p_stop_find(wpa_s->global->p2p); |
| 5574 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5575 | return 0; |
| 5576 | } |
| 5577 | |
| 5578 | |
| 5579 | void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s) |
| 5580 | { |
| 5581 | if (wpas_p2p_stop_find_oper(wpa_s) > 0) |
| 5582 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5583 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 5584 | } |
| 5585 | |
| 5586 | |
| 5587 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx) |
| 5588 | { |
| 5589 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 5590 | wpa_s->p2p_long_listen = 0; |
| 5591 | } |
| 5592 | |
| 5593 | |
| 5594 | int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout) |
| 5595 | { |
| 5596 | int res; |
| 5597 | |
| 5598 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5599 | return -1; |
| 5600 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5601 | wpa_supplicant_cancel_sched_scan(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5602 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5603 | |
| 5604 | if (timeout == 0) { |
| 5605 | /* |
| 5606 | * This is a request for unlimited Listen state. However, at |
| 5607 | * least for now, this is mapped to a Listen state for one |
| 5608 | * hour. |
| 5609 | */ |
| 5610 | timeout = 3600; |
| 5611 | } |
| 5612 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 5613 | wpa_s->p2p_long_listen = 0; |
| 5614 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5615 | /* |
| 5616 | * Stop previous find/listen operation to avoid trying to request a new |
| 5617 | * remain-on-channel operation while the driver is still running the |
| 5618 | * previous one. |
| 5619 | */ |
| 5620 | if (wpa_s->global->p2p) |
| 5621 | p2p_stop_find(wpa_s->global->p2p); |
| 5622 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5623 | res = wpas_p2p_listen_start(wpa_s, timeout * 1000); |
| 5624 | if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) { |
| 5625 | wpa_s->p2p_long_listen = timeout * 1000; |
| 5626 | eloop_register_timeout(timeout, 0, |
| 5627 | wpas_p2p_long_listen_timeout, |
| 5628 | wpa_s, NULL); |
| 5629 | } |
| 5630 | |
| 5631 | return res; |
| 5632 | } |
| 5633 | |
| 5634 | |
| 5635 | int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, |
| 5636 | u8 *buf, size_t len, int p2p_group) |
| 5637 | { |
| 5638 | struct wpabuf *p2p_ie; |
| 5639 | int ret; |
| 5640 | |
| 5641 | if (wpa_s->global->p2p_disabled) |
| 5642 | return -1; |
| 5643 | if (wpa_s->global->p2p == NULL) |
| 5644 | return -1; |
| 5645 | if (bss == NULL) |
| 5646 | return -1; |
| 5647 | |
| 5648 | p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); |
| 5649 | ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len, |
| 5650 | p2p_group, p2p_ie); |
| 5651 | wpabuf_free(p2p_ie); |
| 5652 | |
| 5653 | return ret; |
| 5654 | } |
| 5655 | |
| 5656 | |
| 5657 | 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] | 5658 | const u8 *dst, const u8 *bssid, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5659 | const u8 *ie, size_t ie_len, int ssi_signal) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5660 | { |
| 5661 | if (wpa_s->global->p2p_disabled) |
| 5662 | return 0; |
| 5663 | if (wpa_s->global->p2p == NULL) |
| 5664 | return 0; |
| 5665 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5666 | switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid, |
| 5667 | ie, ie_len)) { |
| 5668 | case P2P_PREQ_NOT_P2P: |
| 5669 | wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len, |
| 5670 | ssi_signal); |
| 5671 | /* fall through */ |
| 5672 | case P2P_PREQ_MALFORMED: |
| 5673 | case P2P_PREQ_NOT_LISTEN: |
| 5674 | case P2P_PREQ_NOT_PROCESSED: |
| 5675 | default: /* make gcc happy */ |
| 5676 | return 0; |
| 5677 | case P2P_PREQ_PROCESSED: |
| 5678 | return 1; |
| 5679 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5680 | } |
| 5681 | |
| 5682 | |
| 5683 | void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da, |
| 5684 | const u8 *sa, const u8 *bssid, |
| 5685 | u8 category, const u8 *data, size_t len, int freq) |
| 5686 | { |
| 5687 | if (wpa_s->global->p2p_disabled) |
| 5688 | return; |
| 5689 | if (wpa_s->global->p2p == NULL) |
| 5690 | return; |
| 5691 | |
| 5692 | p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len, |
| 5693 | freq); |
| 5694 | } |
| 5695 | |
| 5696 | |
| 5697 | void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies) |
| 5698 | { |
| 5699 | if (wpa_s->global->p2p_disabled) |
| 5700 | return; |
| 5701 | if (wpa_s->global->p2p == NULL) |
| 5702 | return; |
| 5703 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5704 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5705 | } |
| 5706 | |
| 5707 | |
| 5708 | void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s) |
| 5709 | { |
| 5710 | p2p_group_deinit(wpa_s->p2p_group); |
| 5711 | wpa_s->p2p_group = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5712 | |
| 5713 | wpa_s->ap_configured_cb = NULL; |
| 5714 | wpa_s->ap_configured_cb_ctx = NULL; |
| 5715 | wpa_s->ap_configured_cb_data = NULL; |
| 5716 | wpa_s->connect_without_scan = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5717 | } |
| 5718 | |
| 5719 | |
| 5720 | int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr) |
| 5721 | { |
| 5722 | wpa_s->p2p_long_listen = 0; |
| 5723 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5724 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5725 | return -1; |
| 5726 | |
| 5727 | return p2p_reject(wpa_s->global->p2p, addr); |
| 5728 | } |
| 5729 | |
| 5730 | |
| 5731 | /* Invite to reinvoke a persistent group */ |
| 5732 | 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] | 5733 | struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5734 | int ht40, int vht, int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5735 | { |
| 5736 | enum p2p_invite_role role; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5737 | u8 *bssid = NULL; |
| 5738 | int force_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5739 | int res; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 5740 | int no_pref_freq_given = pref_freq == 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5741 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 5742 | wpa_s->global->p2p_invite_group = NULL; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5743 | if (peer_addr) |
| 5744 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); |
| 5745 | else |
| 5746 | os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5747 | |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5748 | wpa_s->p2p_persistent_go_freq = freq; |
| 5749 | wpa_s->p2p_go_ht40 = !!ht40; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5750 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 5751 | role = P2P_INVITE_ROLE_GO; |
| 5752 | if (peer_addr == NULL) { |
| 5753 | wpa_printf(MSG_DEBUG, "P2P: Missing peer " |
| 5754 | "address in invitation command"); |
| 5755 | return -1; |
| 5756 | } |
| 5757 | if (wpas_p2p_create_iface(wpa_s)) { |
| 5758 | if (wpas_p2p_add_group_interface(wpa_s, |
| 5759 | WPA_IF_P2P_GO) < 0) { |
| 5760 | wpa_printf(MSG_ERROR, "P2P: Failed to " |
| 5761 | "allocate a new interface for the " |
| 5762 | "group"); |
| 5763 | return -1; |
| 5764 | } |
| 5765 | bssid = wpa_s->pending_interface_addr; |
| 5766 | } else |
| 5767 | bssid = wpa_s->own_addr; |
| 5768 | } else { |
| 5769 | role = P2P_INVITE_ROLE_CLIENT; |
| 5770 | peer_addr = ssid->bssid; |
| 5771 | } |
| 5772 | wpa_s->pending_invite_ssid_id = ssid->id; |
| 5773 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5774 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 5775 | role == P2P_INVITE_ROLE_GO); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5776 | if (res) |
| 5777 | return res; |
Irfan Sheriff | af84a57 | 2012-09-22 16:59:30 -0700 | [diff] [blame] | 5778 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5779 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5780 | return -1; |
| 5781 | |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 5782 | if (wpa_s->parent->conf->p2p_ignore_shared_freq && |
| 5783 | no_pref_freq_given && pref_freq > 0 && |
| 5784 | wpa_s->num_multichan_concurrent > 1 && |
| 5785 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 5786 | wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration", |
| 5787 | pref_freq); |
| 5788 | pref_freq = 0; |
| 5789 | } |
| 5790 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5791 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5792 | ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5793 | 1, pref_freq, -1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5794 | } |
| 5795 | |
| 5796 | |
| 5797 | /* Invite to join an active group */ |
| 5798 | int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname, |
| 5799 | const u8 *peer_addr, const u8 *go_dev_addr) |
| 5800 | { |
| 5801 | struct wpa_global *global = wpa_s->global; |
| 5802 | enum p2p_invite_role role; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5803 | u8 *bssid = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5804 | struct wpa_ssid *ssid; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5805 | int persistent; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5806 | int freq = 0, force_freq = 0, pref_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5807 | int res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5808 | |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5809 | wpa_s->p2p_persistent_go_freq = 0; |
| 5810 | wpa_s->p2p_go_ht40 = 0; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5811 | wpa_s->p2p_go_vht = 0; |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5812 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5813 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 5814 | if (os_strcmp(wpa_s->ifname, ifname) == 0) |
| 5815 | break; |
| 5816 | } |
| 5817 | if (wpa_s == NULL) { |
| 5818 | wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname); |
| 5819 | return -1; |
| 5820 | } |
| 5821 | |
| 5822 | ssid = wpa_s->current_ssid; |
| 5823 | if (ssid == NULL) { |
| 5824 | wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for " |
| 5825 | "invitation"); |
| 5826 | return -1; |
| 5827 | } |
| 5828 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 5829 | wpa_s->global->p2p_invite_group = wpa_s; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5830 | persistent = ssid->p2p_persistent_group && |
| 5831 | wpas_p2p_get_persistent(wpa_s->parent, peer_addr, |
| 5832 | ssid->ssid, ssid->ssid_len); |
| 5833 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5834 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 5835 | role = P2P_INVITE_ROLE_ACTIVE_GO; |
| 5836 | bssid = wpa_s->own_addr; |
| 5837 | if (go_dev_addr == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5838 | go_dev_addr = wpa_s->global->p2p_dev_addr; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5839 | freq = ssid->frequency; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5840 | } else { |
| 5841 | role = P2P_INVITE_ROLE_CLIENT; |
| 5842 | if (wpa_s->wpa_state < WPA_ASSOCIATED) { |
| 5843 | wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot " |
| 5844 | "invite to current group"); |
| 5845 | return -1; |
| 5846 | } |
| 5847 | bssid = wpa_s->bssid; |
| 5848 | if (go_dev_addr == NULL && |
| 5849 | !is_zero_ether_addr(wpa_s->go_dev_addr)) |
| 5850 | go_dev_addr = wpa_s->go_dev_addr; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5851 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : |
| 5852 | (int) wpa_s->assoc_freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5853 | } |
| 5854 | wpa_s->parent->pending_invite_ssid_id = -1; |
| 5855 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5856 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5857 | return -1; |
| 5858 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5859 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 5860 | role == P2P_INVITE_ROLE_ACTIVE_GO); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5861 | if (res) |
| 5862 | return res; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5863 | wpas_p2p_set_own_freq_preference(wpa_s, force_freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5864 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5865 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5866 | ssid->ssid, ssid->ssid_len, force_freq, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5867 | go_dev_addr, persistent, pref_freq, -1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5868 | } |
| 5869 | |
| 5870 | |
| 5871 | void wpas_p2p_completed(struct wpa_supplicant *wpa_s) |
| 5872 | { |
| 5873 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 5874 | const char *ssid_txt; |
| 5875 | u8 go_dev_addr[ETH_ALEN]; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5876 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5877 | int persistent; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5878 | int freq; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5879 | u8 ip[3 * 4]; |
| 5880 | char ip_addr[100]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5881 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5882 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) { |
| 5883 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 5884 | wpa_s->parent, NULL); |
| 5885 | } |
| 5886 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5887 | if (!wpa_s->show_group_started || !ssid) |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5888 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5889 | |
| 5890 | wpa_s->show_group_started = 0; |
| 5891 | |
| 5892 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); |
| 5893 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 5894 | if (ssid->bssid_set) |
| 5895 | os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN); |
| 5896 | persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, |
| 5897 | ssid->ssid_len); |
| 5898 | os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN); |
| 5899 | |
| 5900 | if (wpa_s->global->p2p_group_formation == wpa_s) |
| 5901 | wpa_s->global->p2p_group_formation = NULL; |
| 5902 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5903 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : |
| 5904 | (int) wpa_s->assoc_freq; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5905 | |
| 5906 | ip_addr[0] = '\0'; |
| 5907 | if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) { |
| 5908 | os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u " |
| 5909 | "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u", |
| 5910 | ip[0], ip[1], ip[2], ip[3], |
| 5911 | ip[4], ip[5], ip[6], ip[7], |
| 5912 | ip[8], ip[9], ip[10], ip[11]); |
| 5913 | } |
| 5914 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5915 | if (ssid->passphrase == NULL && ssid->psk_set) { |
| 5916 | char psk[65]; |
| 5917 | wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5918 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 5919 | "%s client ssid=\"%s\" freq=%d psk=%s " |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5920 | "go_dev_addr=" MACSTR "%s%s", |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5921 | wpa_s->ifname, ssid_txt, freq, psk, |
| 5922 | MAC2STR(go_dev_addr), |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5923 | persistent ? " [PERSISTENT]" : "", ip_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5924 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5925 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 5926 | "%s client ssid=\"%s\" freq=%d " |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5927 | "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s", |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5928 | wpa_s->ifname, ssid_txt, freq, |
| 5929 | ssid->passphrase ? ssid->passphrase : "", |
| 5930 | MAC2STR(go_dev_addr), |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5931 | persistent ? " [PERSISTENT]" : "", ip_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5932 | } |
| 5933 | |
| 5934 | if (persistent) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5935 | network_id = wpas_p2p_store_persistent_group(wpa_s->parent, |
| 5936 | ssid, go_dev_addr); |
| 5937 | if (network_id < 0) |
| 5938 | network_id = ssid->id; |
| 5939 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5940 | } |
| 5941 | |
| 5942 | |
| 5943 | int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1, |
| 5944 | u32 interval1, u32 duration2, u32 interval2) |
| 5945 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5946 | int ret; |
| 5947 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5948 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5949 | return -1; |
| 5950 | |
| 5951 | if (wpa_s->wpa_state < WPA_ASSOCIATED || |
| 5952 | wpa_s->current_ssid == NULL || |
| 5953 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA) |
| 5954 | return -1; |
| 5955 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5956 | ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid, |
| 5957 | wpa_s->own_addr, wpa_s->assoc_freq, |
| 5958 | duration1, interval1, duration2, interval2); |
| 5959 | if (ret == 0) |
| 5960 | wpa_s->waiting_presence_resp = 1; |
| 5961 | |
| 5962 | return ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5963 | } |
| 5964 | |
| 5965 | |
| 5966 | int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period, |
| 5967 | unsigned int interval) |
| 5968 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5969 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5970 | return -1; |
| 5971 | |
| 5972 | return p2p_ext_listen(wpa_s->global->p2p, period, interval); |
| 5973 | } |
| 5974 | |
| 5975 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5976 | static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s) |
| 5977 | { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5978 | if (wpa_s->current_ssid == NULL) { |
| 5979 | /* |
| 5980 | * current_ssid can be cleared when P2P client interface gets |
| 5981 | * disconnected, so assume this interface was used as P2P |
| 5982 | * client. |
| 5983 | */ |
| 5984 | return 1; |
| 5985 | } |
| 5986 | return wpa_s->current_ssid->p2p_group && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5987 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA; |
| 5988 | } |
| 5989 | |
| 5990 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5991 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx) |
| 5992 | { |
| 5993 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 5994 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5995 | 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] | 5996 | wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - " |
| 5997 | "disabled"); |
| 5998 | return; |
| 5999 | } |
| 6000 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6001 | wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate " |
| 6002 | "group"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6003 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6004 | } |
| 6005 | |
| 6006 | |
| 6007 | static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s) |
| 6008 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6009 | int timeout; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6010 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6011 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 6012 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
| 6013 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6014 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) |
| 6015 | return; |
| 6016 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6017 | timeout = wpa_s->conf->p2p_group_idle; |
| 6018 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA && |
| 6019 | (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE)) |
| 6020 | timeout = P2P_MAX_CLIENT_IDLE; |
| 6021 | |
| 6022 | if (timeout == 0) |
| 6023 | return; |
| 6024 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6025 | if (timeout < 0) { |
| 6026 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA) |
| 6027 | timeout = 0; /* special client mode no-timeout */ |
| 6028 | else |
| 6029 | return; |
| 6030 | } |
| 6031 | |
| 6032 | if (wpa_s->p2p_in_provisioning) { |
| 6033 | /* |
| 6034 | * Use the normal group formation timeout during the |
| 6035 | * provisioning phase to avoid terminating this process too |
| 6036 | * early due to group idle timeout. |
| 6037 | */ |
| 6038 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " |
| 6039 | "during provisioning"); |
| 6040 | return; |
| 6041 | } |
Deepthi Gowri | 9e4e8ac | 2013-04-16 11:57:05 +0530 | [diff] [blame] | 6042 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6043 | if (wpa_s->show_group_started) { |
| 6044 | /* |
| 6045 | * Use the normal group formation timeout between the end of |
| 6046 | * the provisioning phase and completion of 4-way handshake to |
| 6047 | * avoid terminating this process too early due to group idle |
| 6048 | * timeout. |
| 6049 | */ |
| 6050 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " |
| 6051 | "while waiting for initial 4-way handshake to " |
| 6052 | "complete"); |
| 6053 | return; |
| 6054 | } |
| 6055 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6056 | 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] | 6057 | timeout); |
| 6058 | eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout, |
| 6059 | wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6060 | } |
| 6061 | |
| 6062 | |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6063 | /* Returns 1 if the interface was removed */ |
| 6064 | int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, |
| 6065 | u16 reason_code, const u8 *ie, size_t ie_len, |
| 6066 | int locally_generated) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6067 | { |
| 6068 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6069 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6070 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6071 | if (!locally_generated) |
| 6072 | p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, |
| 6073 | ie_len); |
| 6074 | |
| 6075 | if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated && |
| 6076 | wpa_s->current_ssid && |
| 6077 | wpa_s->current_ssid->p2p_group && |
| 6078 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA) { |
| 6079 | wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group " |
| 6080 | "session is ending"); |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6081 | if (wpas_p2p_group_delete(wpa_s, |
| 6082 | P2P_GROUP_REMOVAL_GO_ENDING_SESSION) |
| 6083 | > 0) |
| 6084 | return 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6085 | } |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6086 | |
| 6087 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6088 | } |
| 6089 | |
| 6090 | |
| 6091 | 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] | 6092 | u16 reason_code, const u8 *ie, size_t ie_len, |
| 6093 | int locally_generated) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6094 | { |
| 6095 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6096 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6097 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6098 | if (!locally_generated) |
| 6099 | p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, |
| 6100 | ie_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6101 | } |
| 6102 | |
| 6103 | |
| 6104 | void wpas_p2p_update_config(struct wpa_supplicant *wpa_s) |
| 6105 | { |
| 6106 | struct p2p_data *p2p = wpa_s->global->p2p; |
| 6107 | |
| 6108 | if (p2p == NULL) |
| 6109 | return; |
| 6110 | |
| 6111 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) |
| 6112 | return; |
| 6113 | |
| 6114 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME) |
| 6115 | p2p_set_dev_name(p2p, wpa_s->conf->device_name); |
| 6116 | |
| 6117 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) |
| 6118 | p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type); |
| 6119 | |
| 6120 | if (wpa_s->wps && |
| 6121 | (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS)) |
| 6122 | p2p_set_config_methods(p2p, wpa_s->wps->config_methods); |
| 6123 | |
| 6124 | if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)) |
| 6125 | p2p_set_uuid(p2p, wpa_s->wps->uuid); |
| 6126 | |
| 6127 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) { |
| 6128 | p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer); |
| 6129 | p2p_set_model_name(p2p, wpa_s->conf->model_name); |
| 6130 | p2p_set_model_number(p2p, wpa_s->conf->model_number); |
| 6131 | p2p_set_serial_number(p2p, wpa_s->conf->serial_number); |
| 6132 | } |
| 6133 | |
| 6134 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) |
| 6135 | p2p_set_sec_dev_types(p2p, |
| 6136 | (void *) wpa_s->conf->sec_device_type, |
| 6137 | wpa_s->conf->num_sec_device_types); |
| 6138 | |
| 6139 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) { |
| 6140 | int i; |
| 6141 | p2p_remove_wps_vendor_extensions(p2p); |
| 6142 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { |
| 6143 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) |
| 6144 | continue; |
| 6145 | p2p_add_wps_vendor_extension( |
| 6146 | p2p, wpa_s->conf->wps_vendor_ext[i]); |
| 6147 | } |
| 6148 | } |
| 6149 | |
| 6150 | if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) && |
| 6151 | wpa_s->conf->country[0] && wpa_s->conf->country[1]) { |
| 6152 | char country[3]; |
| 6153 | country[0] = wpa_s->conf->country[0]; |
| 6154 | country[1] = wpa_s->conf->country[1]; |
| 6155 | country[2] = 0x04; |
| 6156 | p2p_set_country(p2p, country); |
| 6157 | } |
| 6158 | |
| 6159 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) { |
| 6160 | p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix, |
| 6161 | wpa_s->conf->p2p_ssid_postfix ? |
| 6162 | os_strlen(wpa_s->conf->p2p_ssid_postfix) : |
| 6163 | 0); |
| 6164 | } |
| 6165 | |
| 6166 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS) |
| 6167 | p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6168 | |
| 6169 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) { |
| 6170 | u8 reg_class, channel; |
| 6171 | int ret; |
| 6172 | unsigned int r; |
| 6173 | if (wpa_s->conf->p2p_listen_reg_class && |
| 6174 | wpa_s->conf->p2p_listen_channel) { |
| 6175 | reg_class = wpa_s->conf->p2p_listen_reg_class; |
| 6176 | channel = wpa_s->conf->p2p_listen_channel; |
| 6177 | } else { |
| 6178 | reg_class = 81; |
| 6179 | /* |
| 6180 | * Pick one of the social channels randomly as the |
| 6181 | * listen channel. |
| 6182 | */ |
| 6183 | os_get_random((u8 *) &r, sizeof(r)); |
| 6184 | channel = 1 + (r % 3) * 5; |
| 6185 | } |
| 6186 | ret = p2p_set_listen_channel(p2p, reg_class, channel); |
| 6187 | if (ret) |
| 6188 | wpa_printf(MSG_ERROR, "P2P: Own listen channel update " |
| 6189 | "failed: %d", ret); |
| 6190 | } |
| 6191 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) { |
| 6192 | u8 op_reg_class, op_channel, cfg_op_channel; |
| 6193 | int ret = 0; |
| 6194 | unsigned int r; |
| 6195 | if (wpa_s->conf->p2p_oper_reg_class && |
| 6196 | wpa_s->conf->p2p_oper_channel) { |
| 6197 | op_reg_class = wpa_s->conf->p2p_oper_reg_class; |
| 6198 | op_channel = wpa_s->conf->p2p_oper_channel; |
| 6199 | cfg_op_channel = 1; |
| 6200 | } else { |
| 6201 | op_reg_class = 81; |
| 6202 | /* |
| 6203 | * Use random operation channel from (1, 6, 11) |
| 6204 | *if no other preference is indicated. |
| 6205 | */ |
| 6206 | os_get_random((u8 *) &r, sizeof(r)); |
| 6207 | op_channel = 1 + (r % 3) * 5; |
| 6208 | cfg_op_channel = 0; |
| 6209 | } |
| 6210 | ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel, |
| 6211 | cfg_op_channel); |
| 6212 | if (ret) |
| 6213 | wpa_printf(MSG_ERROR, "P2P: Own oper channel update " |
| 6214 | "failed: %d", ret); |
| 6215 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6216 | |
| 6217 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) { |
| 6218 | if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan, |
| 6219 | wpa_s->conf->p2p_pref_chan) < 0) { |
| 6220 | wpa_printf(MSG_ERROR, "P2P: Preferred channel list " |
| 6221 | "update failed"); |
| 6222 | } |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6223 | |
| 6224 | if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) { |
| 6225 | wpa_printf(MSG_ERROR, "P2P: No GO channel list " |
| 6226 | "update failed"); |
| 6227 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6228 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6229 | } |
| 6230 | |
| 6231 | |
| 6232 | int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start, |
| 6233 | int duration) |
| 6234 | { |
| 6235 | if (!wpa_s->ap_iface) |
| 6236 | return -1; |
| 6237 | return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start, |
| 6238 | duration); |
| 6239 | } |
| 6240 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6241 | |
| 6242 | int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled) |
| 6243 | { |
| 6244 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6245 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6246 | |
| 6247 | wpa_s->global->cross_connection = enabled; |
| 6248 | p2p_set_cross_connect(wpa_s->global->p2p, enabled); |
| 6249 | |
| 6250 | if (!enabled) { |
| 6251 | struct wpa_supplicant *iface; |
| 6252 | |
| 6253 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) |
| 6254 | { |
| 6255 | if (iface->cross_connect_enabled == 0) |
| 6256 | continue; |
| 6257 | |
| 6258 | iface->cross_connect_enabled = 0; |
| 6259 | iface->cross_connect_in_use = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6260 | wpa_msg_global(iface->parent, MSG_INFO, |
| 6261 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 6262 | iface->ifname, |
| 6263 | iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6264 | } |
| 6265 | } |
| 6266 | |
| 6267 | return 0; |
| 6268 | } |
| 6269 | |
| 6270 | |
| 6271 | static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink) |
| 6272 | { |
| 6273 | struct wpa_supplicant *iface; |
| 6274 | |
| 6275 | if (!uplink->global->cross_connection) |
| 6276 | return; |
| 6277 | |
| 6278 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { |
| 6279 | if (!iface->cross_connect_enabled) |
| 6280 | continue; |
| 6281 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != |
| 6282 | 0) |
| 6283 | continue; |
| 6284 | if (iface->ap_iface == NULL) |
| 6285 | continue; |
| 6286 | if (iface->cross_connect_in_use) |
| 6287 | continue; |
| 6288 | |
| 6289 | iface->cross_connect_in_use = 1; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6290 | wpa_msg_global(iface->parent, MSG_INFO, |
| 6291 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", |
| 6292 | iface->ifname, iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6293 | } |
| 6294 | } |
| 6295 | |
| 6296 | |
| 6297 | static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink) |
| 6298 | { |
| 6299 | struct wpa_supplicant *iface; |
| 6300 | |
| 6301 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { |
| 6302 | if (!iface->cross_connect_enabled) |
| 6303 | continue; |
| 6304 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != |
| 6305 | 0) |
| 6306 | continue; |
| 6307 | if (!iface->cross_connect_in_use) |
| 6308 | continue; |
| 6309 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6310 | wpa_msg_global(iface->parent, MSG_INFO, |
| 6311 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 6312 | iface->ifname, iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6313 | iface->cross_connect_in_use = 0; |
| 6314 | } |
| 6315 | } |
| 6316 | |
| 6317 | |
| 6318 | void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s) |
| 6319 | { |
| 6320 | if (wpa_s->ap_iface || wpa_s->current_ssid == NULL || |
| 6321 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA || |
| 6322 | wpa_s->cross_connect_disallowed) |
| 6323 | wpas_p2p_disable_cross_connect(wpa_s); |
| 6324 | else |
| 6325 | wpas_p2p_enable_cross_connect(wpa_s); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6326 | if (!wpa_s->ap_iface && |
| 6327 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 6328 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6329 | } |
| 6330 | |
| 6331 | |
| 6332 | void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s) |
| 6333 | { |
| 6334 | wpas_p2p_disable_cross_connect(wpa_s); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6335 | if (!wpa_s->ap_iface && |
| 6336 | !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout, |
| 6337 | wpa_s, NULL)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6338 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 6339 | } |
| 6340 | |
| 6341 | |
| 6342 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s) |
| 6343 | { |
| 6344 | struct wpa_supplicant *iface; |
| 6345 | |
| 6346 | if (!wpa_s->global->cross_connection) |
| 6347 | return; |
| 6348 | |
| 6349 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 6350 | if (iface == wpa_s) |
| 6351 | continue; |
| 6352 | if (iface->drv_flags & |
| 6353 | WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE) |
| 6354 | continue; |
| 6355 | if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) |
| 6356 | continue; |
| 6357 | |
| 6358 | wpa_s->cross_connect_enabled = 1; |
| 6359 | os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname, |
| 6360 | sizeof(wpa_s->cross_connect_uplink)); |
| 6361 | wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from " |
| 6362 | "%s to %s whenever uplink is available", |
| 6363 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
| 6364 | |
| 6365 | if (iface->ap_iface || iface->current_ssid == NULL || |
| 6366 | iface->current_ssid->mode != WPAS_MODE_INFRA || |
| 6367 | iface->cross_connect_disallowed || |
| 6368 | iface->wpa_state != WPA_COMPLETED) |
| 6369 | break; |
| 6370 | |
| 6371 | wpa_s->cross_connect_in_use = 1; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6372 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 6373 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", |
| 6374 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6375 | break; |
| 6376 | } |
| 6377 | } |
| 6378 | |
| 6379 | |
| 6380 | int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s) |
| 6381 | { |
| 6382 | if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT && |
| 6383 | !wpa_s->p2p_in_provisioning) |
| 6384 | return 0; /* not P2P client operation */ |
| 6385 | |
| 6386 | wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC " |
| 6387 | "session overlap"); |
| 6388 | if (wpa_s != wpa_s->parent) |
| 6389 | wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 6390 | wpas_p2p_group_formation_failed(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6391 | return 1; |
| 6392 | } |
| 6393 | |
| 6394 | |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame^] | 6395 | void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx) |
| 6396 | { |
| 6397 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 6398 | wpas_p2p_notif_pbc_overlap(wpa_s); |
| 6399 | } |
| 6400 | |
| 6401 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6402 | void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s) |
| 6403 | { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6404 | struct p2p_channels chan, cli_chan; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6405 | |
| 6406 | if (wpa_s->global == NULL || wpa_s->global->p2p == NULL) |
| 6407 | return; |
| 6408 | |
| 6409 | os_memset(&chan, 0, sizeof(chan)); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6410 | os_memset(&cli_chan, 0, sizeof(cli_chan)); |
| 6411 | if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6412 | wpa_printf(MSG_ERROR, "P2P: Failed to update supported " |
| 6413 | "channel list"); |
| 6414 | return; |
| 6415 | } |
| 6416 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6417 | p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6418 | } |
| 6419 | |
| 6420 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6421 | static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s, |
| 6422 | struct wpa_scan_results *scan_res) |
| 6423 | { |
| 6424 | wpa_printf(MSG_DEBUG, "P2P: Ignore scan results"); |
| 6425 | } |
| 6426 | |
| 6427 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6428 | int wpas_p2p_cancel(struct wpa_supplicant *wpa_s) |
| 6429 | { |
| 6430 | struct wpa_global *global = wpa_s->global; |
| 6431 | int found = 0; |
| 6432 | const u8 *peer; |
| 6433 | |
| 6434 | if (global->p2p == NULL) |
| 6435 | return -1; |
| 6436 | |
| 6437 | wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation"); |
| 6438 | |
| 6439 | if (wpa_s->pending_interface_name[0] && |
| 6440 | !is_zero_ether_addr(wpa_s->pending_interface_addr)) |
| 6441 | found = 1; |
| 6442 | |
| 6443 | peer = p2p_get_go_neg_peer(global->p2p); |
| 6444 | if (peer) { |
| 6445 | wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer " |
| 6446 | MACSTR, MAC2STR(peer)); |
| 6447 | p2p_unauthorize(global->p2p, peer); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6448 | found = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6449 | } |
| 6450 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6451 | if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) { |
| 6452 | wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join"); |
| 6453 | wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore; |
| 6454 | found = 1; |
| 6455 | } |
| 6456 | |
| 6457 | if (wpa_s->pending_pd_before_join) { |
| 6458 | wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join"); |
| 6459 | wpa_s->pending_pd_before_join = 0; |
| 6460 | found = 1; |
| 6461 | } |
| 6462 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6463 | wpas_p2p_stop_find(wpa_s); |
| 6464 | |
| 6465 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 6466 | if (wpa_s == global->p2p_group_formation && |
| 6467 | (wpa_s->p2p_in_provisioning || |
| 6468 | wpa_s->parent->pending_interface_type == |
| 6469 | WPA_IF_P2P_CLIENT)) { |
| 6470 | wpa_printf(MSG_DEBUG, "P2P: Interface %s in group " |
| 6471 | "formation found - cancelling", |
| 6472 | wpa_s->ifname); |
| 6473 | found = 1; |
| 6474 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6475 | wpa_s->parent, NULL); |
Jouni Malinen | adddfc4 | 2012-10-03 14:31:41 -0700 | [diff] [blame] | 6476 | if (wpa_s->p2p_in_provisioning) { |
| 6477 | wpas_group_formation_completed(wpa_s, 0); |
| 6478 | break; |
| 6479 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6480 | wpas_p2p_group_delete(wpa_s, |
| 6481 | P2P_GROUP_REMOVAL_REQUESTED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6482 | break; |
| 6483 | } |
| 6484 | } |
| 6485 | |
| 6486 | if (!found) { |
| 6487 | wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found"); |
| 6488 | return -1; |
| 6489 | } |
| 6490 | |
| 6491 | return 0; |
| 6492 | } |
| 6493 | |
| 6494 | |
| 6495 | void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s) |
| 6496 | { |
| 6497 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) |
| 6498 | return; |
| 6499 | |
| 6500 | wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not " |
| 6501 | "being available anymore"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6502 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6503 | } |
| 6504 | |
| 6505 | |
| 6506 | void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s, |
| 6507 | int freq_24, int freq_5, int freq_overall) |
| 6508 | { |
| 6509 | struct p2p_data *p2p = wpa_s->global->p2p; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6510 | if (p2p == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6511 | return; |
| 6512 | p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall); |
| 6513 | } |
| 6514 | |
| 6515 | |
| 6516 | int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr) |
| 6517 | { |
| 6518 | u8 peer[ETH_ALEN]; |
| 6519 | struct p2p_data *p2p = wpa_s->global->p2p; |
| 6520 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6521 | if (p2p == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6522 | return -1; |
| 6523 | |
| 6524 | if (hwaddr_aton(addr, peer)) |
| 6525 | return -1; |
| 6526 | |
| 6527 | return p2p_unauthorize(p2p, peer); |
| 6528 | } |
| 6529 | |
| 6530 | |
| 6531 | /** |
| 6532 | * wpas_p2p_disconnect - Disconnect from a P2P Group |
| 6533 | * @wpa_s: Pointer to wpa_supplicant data |
| 6534 | * Returns: 0 on success, -1 on failure |
| 6535 | * |
| 6536 | * This can be used to disconnect from a group in which the local end is a P2P |
| 6537 | * Client or to end a P2P Group in case the local end is the Group Owner. If a |
| 6538 | * virtual network interface was created for this group, that interface will be |
| 6539 | * removed. Otherwise, only the configured P2P group network will be removed |
| 6540 | * from the interface. |
| 6541 | */ |
| 6542 | int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s) |
| 6543 | { |
| 6544 | |
| 6545 | if (wpa_s == NULL) |
| 6546 | return -1; |
| 6547 | |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6548 | return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ? |
| 6549 | -1 : 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6550 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6551 | |
| 6552 | |
| 6553 | int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s) |
| 6554 | { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6555 | int ret; |
| 6556 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6557 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6558 | return 0; |
| 6559 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6560 | ret = p2p_in_progress(wpa_s->global->p2p); |
| 6561 | if (ret == 0) { |
| 6562 | /* |
| 6563 | * Check whether there is an ongoing WPS provisioning step (or |
| 6564 | * other parts of group formation) on another interface since |
| 6565 | * p2p_in_progress() does not report this to avoid issues for |
| 6566 | * scans during such provisioning step. |
| 6567 | */ |
| 6568 | if (wpa_s->global->p2p_group_formation && |
| 6569 | wpa_s->global->p2p_group_formation != wpa_s) { |
| 6570 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) " |
| 6571 | "in group formation", |
| 6572 | wpa_s->global->p2p_group_formation->ifname); |
| 6573 | ret = 1; |
| 6574 | } |
| 6575 | } |
| 6576 | |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6577 | if (!ret && wpa_s->global->p2p_go_wait_client.sec) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6578 | struct os_reltime now; |
| 6579 | os_get_reltime(&now); |
| 6580 | if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client, |
| 6581 | P2P_MAX_INITIAL_CONN_WAIT_GO)) { |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6582 | /* Wait for the first client has expired */ |
| 6583 | wpa_s->global->p2p_go_wait_client.sec = 0; |
| 6584 | } else { |
| 6585 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation"); |
| 6586 | ret = 1; |
| 6587 | } |
| 6588 | } |
| 6589 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6590 | return ret; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6591 | } |
| 6592 | |
| 6593 | |
| 6594 | void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s, |
| 6595 | struct wpa_ssid *ssid) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6596 | { |
| 6597 | if (wpa_s->p2p_in_provisioning && ssid->p2p_group && |
| 6598 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6599 | wpa_s->parent, NULL) > 0) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6600 | /** |
| 6601 | * Remove the network by scheduling the group formation |
| 6602 | * timeout to happen immediately. The teardown code |
| 6603 | * needs to be scheduled to run asynch later so that we |
| 6604 | * don't delete data from under ourselves unexpectedly. |
| 6605 | * Calling wpas_p2p_group_formation_timeout directly |
| 6606 | * causes a series of crashes in WPS failure scenarios. |
| 6607 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6608 | wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to " |
| 6609 | "P2P group network getting removed"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6610 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 6611 | wpa_s->parent, NULL); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6612 | } |
| 6613 | } |
| 6614 | |
| 6615 | |
| 6616 | struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6617 | const u8 *addr, const u8 *ssid, |
| 6618 | size_t ssid_len) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6619 | { |
| 6620 | struct wpa_ssid *s; |
| 6621 | size_t i; |
| 6622 | |
| 6623 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 6624 | if (s->disabled != 2) |
| 6625 | continue; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6626 | if (ssid && |
| 6627 | (ssid_len != s->ssid_len || |
| 6628 | os_memcmp(ssid, s->ssid, ssid_len) != 0)) |
| 6629 | continue; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6630 | if (addr == NULL) { |
| 6631 | if (s->mode == WPAS_MODE_P2P_GO) |
| 6632 | return s; |
| 6633 | continue; |
| 6634 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6635 | if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0) |
| 6636 | return s; /* peer is GO in the persistent group */ |
| 6637 | if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL) |
| 6638 | continue; |
| 6639 | for (i = 0; i < s->num_p2p_clients; i++) { |
| 6640 | if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, |
| 6641 | addr, ETH_ALEN) == 0) |
| 6642 | return s; /* peer is P2P client in persistent |
| 6643 | * group */ |
| 6644 | } |
| 6645 | } |
| 6646 | |
| 6647 | return NULL; |
| 6648 | } |
| 6649 | |
| 6650 | |
| 6651 | void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s, |
| 6652 | const u8 *addr) |
| 6653 | { |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 6654 | if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6655 | wpa_s->parent, NULL) > 0) { |
| 6656 | /* |
| 6657 | * This can happen if WPS provisioning step is not terminated |
| 6658 | * cleanly (e.g., P2P Client does not send WSC_Done). Since the |
| 6659 | * peer was able to connect, there is no need to time out group |
| 6660 | * formation after this, though. In addition, this is used with |
| 6661 | * the initial connection wait on the GO as a separate formation |
| 6662 | * timeout and as such, expected to be hit after the initial WPS |
| 6663 | * provisioning step. |
| 6664 | */ |
| 6665 | wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection"); |
| 6666 | } |
| 6667 | if (!wpa_s->p2p_go_group_formation_completed) { |
| 6668 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection"); |
| 6669 | wpa_s->p2p_go_group_formation_completed = 1; |
| 6670 | wpa_s->global->p2p_group_formation = NULL; |
| 6671 | wpa_s->p2p_in_provisioning = 0; |
| 6672 | } |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6673 | wpa_s->global->p2p_go_wait_client.sec = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6674 | if (addr == NULL) |
| 6675 | return; |
| 6676 | wpas_p2p_add_persistent_group_client(wpa_s, addr); |
| 6677 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6678 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6679 | |
| 6680 | static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, |
| 6681 | int group_added) |
| 6682 | { |
| 6683 | struct wpa_supplicant *group = wpa_s; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6684 | if (wpa_s->global->p2p_group_formation) |
| 6685 | group = wpa_s->global->p2p_group_formation; |
| 6686 | wpa_s = wpa_s->parent; |
| 6687 | offchannel_send_action_done(wpa_s); |
| 6688 | if (group_added) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6689 | wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6690 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation"); |
| 6691 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin, |
| 6692 | wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0, |
| 6693 | 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq, |
| 6694 | wpa_s->p2p_persistent_id, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6695 | wpa_s->p2p_pd_before_go_neg, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6696 | wpa_s->p2p_go_ht40, |
| 6697 | wpa_s->p2p_go_vht); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6698 | } |
| 6699 | |
| 6700 | |
| 6701 | int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s) |
| 6702 | { |
| 6703 | if (!wpa_s->p2p_fallback_to_go_neg || |
| 6704 | wpa_s->p2p_in_provisioning <= 5) |
| 6705 | return 0; |
| 6706 | |
| 6707 | if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0) |
| 6708 | return 0; /* peer operating as a GO */ |
| 6709 | |
| 6710 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - " |
| 6711 | "fallback to GO Negotiation"); |
| 6712 | wpas_p2p_fallback_to_go_neg(wpa_s, 1); |
| 6713 | |
| 6714 | return 1; |
| 6715 | } |
| 6716 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6717 | |
| 6718 | unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s) |
| 6719 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6720 | struct wpa_supplicant *ifs; |
| 6721 | |
| 6722 | if (wpa_s->wpa_state > WPA_SCANNING) { |
| 6723 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to " |
| 6724 | "concurrent operation", |
| 6725 | P2P_CONCURRENT_SEARCH_DELAY); |
| 6726 | return P2P_CONCURRENT_SEARCH_DELAY; |
| 6727 | } |
| 6728 | |
Dmitry Shmidt | 01904cf | 2013-12-05 11:08:35 -0800 | [diff] [blame] | 6729 | dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant, |
| 6730 | radio_list) { |
| 6731 | if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6732 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search " |
| 6733 | "delay due to concurrent operation on " |
| 6734 | "interface %s", |
| 6735 | P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname); |
| 6736 | return P2P_CONCURRENT_SEARCH_DELAY; |
| 6737 | } |
| 6738 | } |
| 6739 | |
| 6740 | return 0; |
| 6741 | } |
| 6742 | |
Dmitry Shmidt | 37d4d6a | 2013-03-18 13:09:42 -0700 | [diff] [blame] | 6743 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6744 | static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s, |
| 6745 | struct wpa_ssid *s, const u8 *addr, |
| 6746 | int iface_addr) |
| 6747 | { |
| 6748 | struct psk_list_entry *psk, *tmp; |
| 6749 | int changed = 0; |
| 6750 | |
| 6751 | dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry, |
| 6752 | list) { |
| 6753 | if ((iface_addr && !psk->p2p && |
| 6754 | os_memcmp(addr, psk->addr, ETH_ALEN) == 0) || |
| 6755 | (!iface_addr && psk->p2p && |
| 6756 | os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) { |
| 6757 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 6758 | "P2P: Remove persistent group PSK list entry for " |
| 6759 | MACSTR " p2p=%u", |
| 6760 | MAC2STR(psk->addr), psk->p2p); |
| 6761 | dl_list_del(&psk->list); |
| 6762 | os_free(psk); |
| 6763 | changed++; |
| 6764 | } |
| 6765 | } |
| 6766 | |
| 6767 | return changed; |
| 6768 | } |
| 6769 | |
| 6770 | |
| 6771 | void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr, |
| 6772 | const u8 *p2p_dev_addr, |
| 6773 | const u8 *psk, size_t psk_len) |
| 6774 | { |
| 6775 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 6776 | struct wpa_ssid *persistent; |
| 6777 | struct psk_list_entry *p; |
| 6778 | |
| 6779 | if (psk_len != sizeof(p->psk)) |
| 6780 | return; |
| 6781 | |
| 6782 | if (p2p_dev_addr) { |
| 6783 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR |
| 6784 | " p2p_dev_addr=" MACSTR, |
| 6785 | MAC2STR(mac_addr), MAC2STR(p2p_dev_addr)); |
| 6786 | if (is_zero_ether_addr(p2p_dev_addr)) |
| 6787 | p2p_dev_addr = NULL; |
| 6788 | } else { |
| 6789 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR, |
| 6790 | MAC2STR(mac_addr)); |
| 6791 | } |
| 6792 | |
| 6793 | if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { |
| 6794 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation"); |
| 6795 | /* To be added to persistent group once created */ |
| 6796 | if (wpa_s->global->add_psk == NULL) { |
| 6797 | wpa_s->global->add_psk = os_zalloc(sizeof(*p)); |
| 6798 | if (wpa_s->global->add_psk == NULL) |
| 6799 | return; |
| 6800 | } |
| 6801 | p = wpa_s->global->add_psk; |
| 6802 | if (p2p_dev_addr) { |
| 6803 | p->p2p = 1; |
| 6804 | os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); |
| 6805 | } else { |
| 6806 | p->p2p = 0; |
| 6807 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
| 6808 | } |
| 6809 | os_memcpy(p->psk, psk, psk_len); |
| 6810 | return; |
| 6811 | } |
| 6812 | |
| 6813 | if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) { |
| 6814 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO"); |
| 6815 | return; |
| 6816 | } |
| 6817 | |
| 6818 | persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid, |
| 6819 | ssid->ssid_len); |
| 6820 | if (!persistent) { |
| 6821 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK"); |
| 6822 | return; |
| 6823 | } |
| 6824 | |
| 6825 | p = os_zalloc(sizeof(*p)); |
| 6826 | if (p == NULL) |
| 6827 | return; |
| 6828 | if (p2p_dev_addr) { |
| 6829 | p->p2p = 1; |
| 6830 | os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); |
| 6831 | } else { |
| 6832 | p->p2p = 0; |
| 6833 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
| 6834 | } |
| 6835 | os_memcpy(p->psk, psk, psk_len); |
| 6836 | |
| 6837 | if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) { |
| 6838 | struct psk_list_entry *last; |
| 6839 | last = dl_list_last(&persistent->psk_list, |
| 6840 | struct psk_list_entry, list); |
| 6841 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for " |
| 6842 | MACSTR " (p2p=%u) to make room for a new one", |
| 6843 | MAC2STR(last->addr), last->p2p); |
| 6844 | dl_list_del(&last->list); |
| 6845 | os_free(last); |
| 6846 | } |
| 6847 | |
| 6848 | wpas_p2p_remove_psk_entry(wpa_s->parent, persistent, |
| 6849 | p2p_dev_addr ? p2p_dev_addr : mac_addr, |
| 6850 | p2p_dev_addr == NULL); |
| 6851 | if (p2p_dev_addr) { |
| 6852 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr=" |
| 6853 | MACSTR, MAC2STR(p2p_dev_addr)); |
| 6854 | } else { |
| 6855 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR, |
| 6856 | MAC2STR(mac_addr)); |
| 6857 | } |
| 6858 | dl_list_add(&persistent->psk_list, &p->list); |
| 6859 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6860 | if (wpa_s->parent->conf->update_config && |
| 6861 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 6862 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6863 | } |
| 6864 | |
| 6865 | |
| 6866 | static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s, |
| 6867 | struct wpa_ssid *s, const u8 *addr, |
| 6868 | int iface_addr) |
| 6869 | { |
| 6870 | int res; |
| 6871 | |
| 6872 | res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 6873 | if (res > 0 && wpa_s->conf->update_config && |
| 6874 | wpa_config_write(wpa_s->confname, wpa_s->conf)) |
| 6875 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 6876 | "P2P: Failed to update configuration"); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6877 | } |
| 6878 | |
| 6879 | |
| 6880 | static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s, |
| 6881 | const u8 *peer, int iface_addr) |
| 6882 | { |
| 6883 | struct hostapd_data *hapd; |
| 6884 | struct hostapd_wpa_psk *psk, *prev, *rem; |
| 6885 | struct sta_info *sta; |
| 6886 | |
| 6887 | if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL || |
| 6888 | wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO) |
| 6889 | return; |
| 6890 | |
| 6891 | /* Remove per-station PSK entry */ |
| 6892 | hapd = wpa_s->ap_iface->bss[0]; |
| 6893 | prev = NULL; |
| 6894 | psk = hapd->conf->ssid.wpa_psk; |
| 6895 | while (psk) { |
| 6896 | if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) || |
| 6897 | (!iface_addr && |
| 6898 | os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) { |
| 6899 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for " |
| 6900 | MACSTR " iface_addr=%d", |
| 6901 | MAC2STR(peer), iface_addr); |
| 6902 | if (prev) |
| 6903 | prev->next = psk->next; |
| 6904 | else |
| 6905 | hapd->conf->ssid.wpa_psk = psk->next; |
| 6906 | rem = psk; |
| 6907 | psk = psk->next; |
| 6908 | os_free(rem); |
| 6909 | } else { |
| 6910 | prev = psk; |
| 6911 | psk = psk->next; |
| 6912 | } |
| 6913 | } |
| 6914 | |
| 6915 | /* Disconnect from group */ |
| 6916 | if (iface_addr) |
| 6917 | sta = ap_get_sta(hapd, peer); |
| 6918 | else |
| 6919 | sta = ap_get_sta_p2p(hapd, peer); |
| 6920 | if (sta) { |
| 6921 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR |
| 6922 | " (iface_addr=%d) from group", |
| 6923 | MAC2STR(peer), iface_addr); |
| 6924 | hostapd_drv_sta_deauth(hapd, sta->addr, |
| 6925 | WLAN_REASON_DEAUTH_LEAVING); |
| 6926 | ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING); |
| 6927 | } |
| 6928 | } |
| 6929 | |
| 6930 | |
| 6931 | void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer, |
| 6932 | int iface_addr) |
| 6933 | { |
| 6934 | struct wpa_ssid *s; |
| 6935 | struct wpa_supplicant *w; |
| 6936 | |
| 6937 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer)); |
| 6938 | |
| 6939 | /* Remove from any persistent group */ |
| 6940 | for (s = wpa_s->parent->conf->ssid; s; s = s->next) { |
| 6941 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) |
| 6942 | continue; |
| 6943 | if (!iface_addr) |
| 6944 | wpas_remove_persistent_peer(wpa_s, s, peer, 0); |
| 6945 | wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr); |
| 6946 | } |
| 6947 | |
| 6948 | /* Remove from any operating group */ |
| 6949 | for (w = wpa_s->global->ifaces; w; w = w->next) |
| 6950 | wpas_p2p_remove_client_go(w, peer, iface_addr); |
| 6951 | } |
| 6952 | |
| 6953 | |
| 6954 | static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx) |
| 6955 | { |
| 6956 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 6957 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE); |
| 6958 | } |
| 6959 | |
| 6960 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 6961 | static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx) |
| 6962 | { |
| 6963 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 6964 | |
| 6965 | wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group"); |
| 6966 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT); |
| 6967 | } |
| 6968 | |
| 6969 | |
| 6970 | int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq, |
| 6971 | struct wpa_ssid *ssid) |
| 6972 | { |
| 6973 | struct wpa_supplicant *iface; |
| 6974 | |
| 6975 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 6976 | if (!iface->current_ssid || |
| 6977 | iface->current_ssid->frequency == freq || |
| 6978 | (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE && |
| 6979 | !iface->current_ssid->p2p_group)) |
| 6980 | continue; |
| 6981 | |
| 6982 | /* Remove the connection with least priority */ |
| 6983 | if (!wpas_is_p2p_prioritized(iface)) { |
| 6984 | /* STA connection has priority over existing |
| 6985 | * P2P connection, so remove the interface. */ |
| 6986 | wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict"); |
| 6987 | eloop_register_timeout(0, 0, |
| 6988 | wpas_p2p_group_freq_conflict, |
| 6989 | iface, NULL); |
| 6990 | /* If connection in progress is P2P connection, do not |
| 6991 | * proceed for the connection. */ |
| 6992 | if (wpa_s == iface) |
| 6993 | return -1; |
| 6994 | else |
| 6995 | return 0; |
| 6996 | } else { |
| 6997 | /* P2P connection has priority, disable the STA network |
| 6998 | */ |
| 6999 | wpa_supplicant_disable_network(wpa_s->global->ifaces, |
| 7000 | ssid); |
| 7001 | wpa_msg(wpa_s->global->ifaces, MSG_INFO, |
| 7002 | WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id); |
| 7003 | os_memset(wpa_s->global->ifaces->pending_bssid, 0, |
| 7004 | ETH_ALEN); |
| 7005 | /* If P2P connection is in progress, continue |
| 7006 | * connecting...*/ |
| 7007 | if (wpa_s == iface) |
| 7008 | return 0; |
| 7009 | else |
| 7010 | return -1; |
| 7011 | } |
| 7012 | } |
| 7013 | |
| 7014 | return 0; |
| 7015 | } |
| 7016 | |
| 7017 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7018 | int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s) |
| 7019 | { |
| 7020 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 7021 | |
| 7022 | if (ssid == NULL || !ssid->p2p_group) |
| 7023 | return 0; |
| 7024 | |
| 7025 | if (wpa_s->p2p_last_4way_hs_fail && |
| 7026 | wpa_s->p2p_last_4way_hs_fail == ssid) { |
| 7027 | u8 go_dev_addr[ETH_ALEN]; |
| 7028 | struct wpa_ssid *persistent; |
| 7029 | |
| 7030 | if (wpas_p2p_persistent_group(wpa_s, go_dev_addr, |
| 7031 | ssid->ssid, |
| 7032 | ssid->ssid_len) <= 0) { |
| 7033 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group"); |
| 7034 | goto disconnect; |
| 7035 | } |
| 7036 | |
| 7037 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr=" |
| 7038 | MACSTR, MAC2STR(go_dev_addr)); |
| 7039 | persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr, |
| 7040 | ssid->ssid, |
| 7041 | ssid->ssid_len); |
| 7042 | if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) { |
| 7043 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored"); |
| 7044 | goto disconnect; |
| 7045 | } |
| 7046 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 7047 | P2P_EVENT_PERSISTENT_PSK_FAIL "%d", |
| 7048 | persistent->id); |
| 7049 | disconnect: |
| 7050 | wpa_s->p2p_last_4way_hs_fail = NULL; |
| 7051 | /* |
| 7052 | * Remove the group from a timeout to avoid issues with caller |
| 7053 | * continuing to use the interface if this is on a P2P group |
| 7054 | * interface. |
| 7055 | */ |
| 7056 | eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal, |
| 7057 | wpa_s, NULL); |
| 7058 | return 1; |
| 7059 | } |
| 7060 | |
| 7061 | wpa_s->p2p_last_4way_hs_fail = ssid; |
| 7062 | return 0; |
| 7063 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7064 | |
| 7065 | |
| 7066 | #ifdef CONFIG_WPS_NFC |
| 7067 | |
| 7068 | static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc, |
| 7069 | struct wpabuf *p2p) |
| 7070 | { |
| 7071 | struct wpabuf *ret; |
| 7072 | size_t wsc_len; |
| 7073 | |
| 7074 | if (p2p == NULL) { |
| 7075 | wpabuf_free(wsc); |
| 7076 | wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover"); |
| 7077 | return NULL; |
| 7078 | } |
| 7079 | |
| 7080 | wsc_len = wsc ? wpabuf_len(wsc) : 0; |
| 7081 | ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p)); |
| 7082 | if (ret == NULL) { |
| 7083 | wpabuf_free(wsc); |
| 7084 | wpabuf_free(p2p); |
| 7085 | return NULL; |
| 7086 | } |
| 7087 | |
| 7088 | wpabuf_put_be16(ret, wsc_len); |
| 7089 | if (wsc) |
| 7090 | wpabuf_put_buf(ret, wsc); |
| 7091 | wpabuf_put_be16(ret, wpabuf_len(p2p)); |
| 7092 | wpabuf_put_buf(ret, p2p); |
| 7093 | |
| 7094 | wpabuf_free(wsc); |
| 7095 | wpabuf_free(p2p); |
| 7096 | wpa_hexdump_buf(MSG_DEBUG, |
| 7097 | "P2P: Generated NFC connection handover message", ret); |
| 7098 | |
| 7099 | if (ndef && ret) { |
| 7100 | struct wpabuf *tmp; |
| 7101 | tmp = ndef_build_p2p(ret); |
| 7102 | wpabuf_free(ret); |
| 7103 | if (tmp == NULL) { |
| 7104 | wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request"); |
| 7105 | return NULL; |
| 7106 | } |
| 7107 | ret = tmp; |
| 7108 | } |
| 7109 | |
| 7110 | return ret; |
| 7111 | } |
| 7112 | |
| 7113 | |
| 7114 | static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s, |
| 7115 | struct wpa_ssid **ssid, u8 *go_dev_addr) |
| 7116 | { |
| 7117 | struct wpa_supplicant *iface; |
| 7118 | |
| 7119 | if (go_dev_addr) |
| 7120 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 7121 | if (ssid) |
| 7122 | *ssid = NULL; |
| 7123 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 7124 | if (iface->wpa_state < WPA_ASSOCIATING || |
| 7125 | iface->current_ssid == NULL || iface->assoc_freq == 0 || |
| 7126 | !iface->current_ssid->p2p_group || |
| 7127 | iface->current_ssid->mode != WPAS_MODE_INFRA) |
| 7128 | continue; |
| 7129 | if (ssid) |
| 7130 | *ssid = iface->current_ssid; |
| 7131 | if (go_dev_addr) |
| 7132 | os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN); |
| 7133 | return iface->assoc_freq; |
| 7134 | } |
| 7135 | return 0; |
| 7136 | } |
| 7137 | |
| 7138 | |
| 7139 | struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s, |
| 7140 | int ndef) |
| 7141 | { |
| 7142 | struct wpabuf *wsc, *p2p; |
| 7143 | struct wpa_ssid *ssid; |
| 7144 | u8 go_dev_addr[ETH_ALEN]; |
| 7145 | int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr); |
| 7146 | |
| 7147 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) { |
| 7148 | wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request"); |
| 7149 | return NULL; |
| 7150 | } |
| 7151 | |
| 7152 | if (wpa_s->conf->wps_nfc_dh_pubkey == NULL && |
| 7153 | wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, |
| 7154 | &wpa_s->conf->wps_nfc_dh_privkey) < 0) { |
| 7155 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request"); |
| 7156 | return NULL; |
| 7157 | } |
| 7158 | |
| 7159 | if (cli_freq == 0) { |
| 7160 | wsc = wps_build_nfc_handover_req_p2p( |
| 7161 | wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey); |
| 7162 | } else |
| 7163 | wsc = NULL; |
| 7164 | p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq, |
| 7165 | go_dev_addr, ssid ? ssid->ssid : NULL, |
| 7166 | ssid ? ssid->ssid_len : 0); |
| 7167 | |
| 7168 | return wpas_p2p_nfc_handover(ndef, wsc, p2p); |
| 7169 | } |
| 7170 | |
| 7171 | |
| 7172 | struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s, |
| 7173 | int ndef, int tag) |
| 7174 | { |
| 7175 | struct wpabuf *wsc, *p2p; |
| 7176 | struct wpa_ssid *ssid; |
| 7177 | u8 go_dev_addr[ETH_ALEN]; |
| 7178 | int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr); |
| 7179 | |
| 7180 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 7181 | return NULL; |
| 7182 | |
| 7183 | if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL && |
| 7184 | wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, |
| 7185 | &wpa_s->conf->wps_nfc_dh_privkey) < 0) |
| 7186 | return NULL; |
| 7187 | |
| 7188 | if (cli_freq == 0) { |
| 7189 | wsc = wps_build_nfc_handover_sel_p2p( |
| 7190 | wpa_s->parent->wps, |
| 7191 | tag ? wpa_s->conf->wps_nfc_dev_pw_id : |
| 7192 | DEV_PW_NFC_CONNECTION_HANDOVER, |
| 7193 | wpa_s->conf->wps_nfc_dh_pubkey, |
| 7194 | tag ? wpa_s->conf->wps_nfc_dev_pw : NULL); |
| 7195 | } else |
| 7196 | wsc = NULL; |
| 7197 | p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq, |
| 7198 | go_dev_addr, ssid ? ssid->ssid : NULL, |
| 7199 | ssid ? ssid->ssid_len : 0); |
| 7200 | |
| 7201 | return wpas_p2p_nfc_handover(ndef, wsc, p2p); |
| 7202 | } |
| 7203 | |
| 7204 | |
| 7205 | static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s, |
| 7206 | struct p2p_nfc_params *params) |
| 7207 | { |
| 7208 | wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC " |
| 7209 | "connection handover (freq=%d)", |
| 7210 | params->go_freq); |
| 7211 | |
| 7212 | if (params->go_freq && params->go_ssid_len) { |
| 7213 | wpa_s->p2p_wps_method = WPS_NFC; |
| 7214 | wpa_s->pending_join_wps_method = WPS_NFC; |
| 7215 | os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN); |
| 7216 | os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr, |
| 7217 | ETH_ALEN); |
| 7218 | return wpas_p2p_join_start(wpa_s, params->go_freq, |
| 7219 | params->go_ssid, |
| 7220 | params->go_ssid_len); |
| 7221 | } |
| 7222 | |
| 7223 | return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, |
| 7224 | WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent, |
| 7225 | params->go_freq, -1, 0, 1, 1); |
| 7226 | } |
| 7227 | |
| 7228 | |
| 7229 | static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s, |
| 7230 | struct p2p_nfc_params *params, int tag) |
| 7231 | { |
| 7232 | int res, persistent; |
| 7233 | struct wpa_ssid *ssid; |
| 7234 | |
| 7235 | wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC " |
| 7236 | "connection handover"); |
| 7237 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 7238 | ssid = wpa_s->current_ssid; |
| 7239 | if (ssid == NULL) |
| 7240 | continue; |
| 7241 | if (ssid->mode != WPAS_MODE_P2P_GO) |
| 7242 | continue; |
| 7243 | if (wpa_s->ap_iface == NULL) |
| 7244 | continue; |
| 7245 | break; |
| 7246 | } |
| 7247 | if (wpa_s == NULL) { |
| 7248 | wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface"); |
| 7249 | return -1; |
| 7250 | } |
| 7251 | |
| 7252 | if (wpa_s->parent->p2p_oob_dev_pw_id != |
| 7253 | DEV_PW_NFC_CONNECTION_HANDOVER && |
| 7254 | !wpa_s->parent->p2p_oob_dev_pw) { |
| 7255 | wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known"); |
| 7256 | return -1; |
| 7257 | } |
| 7258 | res = wpas_ap_wps_add_nfc_pw( |
| 7259 | wpa_s, wpa_s->parent->p2p_oob_dev_pw_id, |
| 7260 | wpa_s->parent->p2p_oob_dev_pw, |
| 7261 | wpa_s->parent->p2p_peer_oob_pk_hash_known ? |
| 7262 | wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL); |
| 7263 | if (res) |
| 7264 | return res; |
| 7265 | |
| 7266 | if (!tag) { |
| 7267 | wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation"); |
| 7268 | return 0; |
| 7269 | } |
| 7270 | |
| 7271 | if (!params->peer || |
| 7272 | !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE)) |
| 7273 | return 0; |
| 7274 | |
| 7275 | wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR |
| 7276 | " to join", MAC2STR(params->peer->p2p_device_addr)); |
| 7277 | |
| 7278 | wpa_s->global->p2p_invite_group = wpa_s; |
| 7279 | persistent = ssid->p2p_persistent_group && |
| 7280 | wpas_p2p_get_persistent(wpa_s->parent, |
| 7281 | params->peer->p2p_device_addr, |
| 7282 | ssid->ssid, ssid->ssid_len); |
| 7283 | wpa_s->parent->pending_invite_ssid_id = -1; |
| 7284 | |
| 7285 | return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr, |
| 7286 | P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr, |
| 7287 | ssid->ssid, ssid->ssid_len, ssid->frequency, |
| 7288 | wpa_s->global->p2p_dev_addr, persistent, 0, |
| 7289 | wpa_s->parent->p2p_oob_dev_pw_id); |
| 7290 | } |
| 7291 | |
| 7292 | |
| 7293 | static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s, |
| 7294 | struct p2p_nfc_params *params, |
| 7295 | int forced_freq) |
| 7296 | { |
| 7297 | wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC " |
| 7298 | "connection handover"); |
| 7299 | return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, |
| 7300 | WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent, |
| 7301 | forced_freq, -1, 0, 1, 1); |
| 7302 | } |
| 7303 | |
| 7304 | |
| 7305 | static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s, |
| 7306 | struct p2p_nfc_params *params, |
| 7307 | int forced_freq) |
| 7308 | { |
| 7309 | int res; |
| 7310 | |
| 7311 | wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC " |
| 7312 | "connection handover"); |
| 7313 | res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, |
| 7314 | WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent, |
| 7315 | forced_freq, -1, 0, 1, 1); |
| 7316 | if (res) |
| 7317 | return res; |
| 7318 | |
| 7319 | res = wpas_p2p_listen(wpa_s, 60); |
| 7320 | if (res) { |
| 7321 | p2p_unauthorize(wpa_s->global->p2p, |
| 7322 | params->peer->p2p_device_addr); |
| 7323 | } |
| 7324 | |
| 7325 | return res; |
| 7326 | } |
| 7327 | |
| 7328 | |
| 7329 | static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s, |
| 7330 | const struct wpabuf *data, |
| 7331 | int sel, int tag, int forced_freq) |
| 7332 | { |
| 7333 | const u8 *pos, *end; |
| 7334 | u16 len, id; |
| 7335 | struct p2p_nfc_params params; |
| 7336 | int res; |
| 7337 | |
| 7338 | os_memset(¶ms, 0, sizeof(params)); |
| 7339 | params.sel = sel; |
| 7340 | |
| 7341 | wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data); |
| 7342 | |
| 7343 | pos = wpabuf_head(data); |
| 7344 | end = pos + wpabuf_len(data); |
| 7345 | |
| 7346 | if (end - pos < 2) { |
| 7347 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC " |
| 7348 | "attributes"); |
| 7349 | return -1; |
| 7350 | } |
| 7351 | len = WPA_GET_BE16(pos); |
| 7352 | pos += 2; |
| 7353 | if (pos + len > end) { |
| 7354 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC " |
| 7355 | "attributes"); |
| 7356 | return -1; |
| 7357 | } |
| 7358 | params.wsc_attr = pos; |
| 7359 | params.wsc_len = len; |
| 7360 | pos += len; |
| 7361 | |
| 7362 | if (end - pos < 2) { |
| 7363 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P " |
| 7364 | "attributes"); |
| 7365 | return -1; |
| 7366 | } |
| 7367 | len = WPA_GET_BE16(pos); |
| 7368 | pos += 2; |
| 7369 | if (pos + len > end) { |
| 7370 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P " |
| 7371 | "attributes"); |
| 7372 | return -1; |
| 7373 | } |
| 7374 | params.p2p_attr = pos; |
| 7375 | params.p2p_len = len; |
| 7376 | pos += len; |
| 7377 | |
| 7378 | wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes", |
| 7379 | params.wsc_attr, params.wsc_len); |
| 7380 | wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes", |
| 7381 | params.p2p_attr, params.p2p_len); |
| 7382 | if (pos < end) { |
| 7383 | wpa_hexdump(MSG_DEBUG, |
| 7384 | "P2P: Ignored extra data after P2P attributes", |
| 7385 | pos, end - pos); |
| 7386 | } |
| 7387 | |
| 7388 | res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms); |
| 7389 | if (res) |
| 7390 | return res; |
| 7391 | |
| 7392 | if (params.next_step == NO_ACTION) |
| 7393 | return 0; |
| 7394 | |
| 7395 | if (params.next_step == BOTH_GO) { |
| 7396 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR, |
| 7397 | MAC2STR(params.peer->p2p_device_addr)); |
| 7398 | return 0; |
| 7399 | } |
| 7400 | |
| 7401 | if (params.next_step == PEER_CLIENT) { |
| 7402 | if (!is_zero_ether_addr(params.go_dev_addr)) { |
| 7403 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT |
| 7404 | "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR |
| 7405 | " ssid=\"%s\"", |
| 7406 | MAC2STR(params.peer->p2p_device_addr), |
| 7407 | params.go_freq, |
| 7408 | MAC2STR(params.go_dev_addr), |
| 7409 | wpa_ssid_txt(params.go_ssid, |
| 7410 | params.go_ssid_len)); |
| 7411 | } else { |
| 7412 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT |
| 7413 | "peer=" MACSTR " freq=%d", |
| 7414 | MAC2STR(params.peer->p2p_device_addr), |
| 7415 | params.go_freq); |
| 7416 | } |
| 7417 | return 0; |
| 7418 | } |
| 7419 | |
| 7420 | if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) { |
| 7421 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer=" |
| 7422 | MACSTR, MAC2STR(params.peer->p2p_device_addr)); |
| 7423 | return 0; |
| 7424 | } |
| 7425 | |
| 7426 | wpabuf_free(wpa_s->p2p_oob_dev_pw); |
| 7427 | wpa_s->p2p_oob_dev_pw = NULL; |
| 7428 | |
| 7429 | if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) { |
| 7430 | wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw " |
| 7431 | "received"); |
| 7432 | return -1; |
| 7433 | } |
| 7434 | |
| 7435 | id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN); |
| 7436 | wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id); |
| 7437 | wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash", |
| 7438 | params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN); |
| 7439 | os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash, |
| 7440 | params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN); |
| 7441 | wpa_s->p2p_peer_oob_pk_hash_known = 1; |
| 7442 | |
| 7443 | if (tag) { |
| 7444 | if (id < 0x10) { |
| 7445 | wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid " |
| 7446 | "peer OOB Device Password Id %u", id); |
| 7447 | return -1; |
| 7448 | } |
| 7449 | wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB " |
| 7450 | "Device Password Id %u", id); |
| 7451 | wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password", |
| 7452 | params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2, |
| 7453 | params.oob_dev_pw_len - |
| 7454 | WPS_OOB_PUBKEY_HASH_LEN - 2); |
| 7455 | wpa_s->p2p_oob_dev_pw_id = id; |
| 7456 | wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy( |
| 7457 | params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2, |
| 7458 | params.oob_dev_pw_len - |
| 7459 | WPS_OOB_PUBKEY_HASH_LEN - 2); |
| 7460 | if (wpa_s->p2p_oob_dev_pw == NULL) |
| 7461 | return -1; |
| 7462 | |
| 7463 | if (wpa_s->conf->wps_nfc_dh_pubkey == NULL && |
| 7464 | wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, |
| 7465 | &wpa_s->conf->wps_nfc_dh_privkey) < 0) |
| 7466 | return -1; |
| 7467 | } else { |
| 7468 | wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake " |
| 7469 | "without Device Password"); |
| 7470 | wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER; |
| 7471 | } |
| 7472 | |
| 7473 | switch (params.next_step) { |
| 7474 | case NO_ACTION: |
| 7475 | case BOTH_GO: |
| 7476 | case PEER_CLIENT: |
| 7477 | /* already covered above */ |
| 7478 | return 0; |
| 7479 | case JOIN_GROUP: |
| 7480 | return wpas_p2p_nfc_join_group(wpa_s, ¶ms); |
| 7481 | case AUTH_JOIN: |
| 7482 | return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag); |
| 7483 | case INIT_GO_NEG: |
| 7484 | return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq); |
| 7485 | case RESP_GO_NEG: |
| 7486 | /* TODO: use own OOB Dev Pw */ |
| 7487 | return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq); |
| 7488 | } |
| 7489 | |
| 7490 | return -1; |
| 7491 | } |
| 7492 | |
| 7493 | |
| 7494 | int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s, |
| 7495 | const struct wpabuf *data, int forced_freq) |
| 7496 | { |
| 7497 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 7498 | return -1; |
| 7499 | |
| 7500 | return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq); |
| 7501 | } |
| 7502 | |
| 7503 | |
| 7504 | int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init, |
| 7505 | const struct wpabuf *req, |
| 7506 | const struct wpabuf *sel, int forced_freq) |
| 7507 | { |
| 7508 | struct wpabuf *tmp; |
| 7509 | int ret; |
| 7510 | |
| 7511 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 7512 | return -1; |
| 7513 | |
| 7514 | wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported"); |
| 7515 | |
| 7516 | wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req", |
| 7517 | wpabuf_head(req), wpabuf_len(req)); |
| 7518 | wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel", |
| 7519 | wpabuf_head(sel), wpabuf_len(sel)); |
| 7520 | if (forced_freq) |
| 7521 | wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq); |
| 7522 | tmp = ndef_parse_p2p(init ? sel : req); |
| 7523 | if (tmp == NULL) { |
| 7524 | wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF"); |
| 7525 | return -1; |
| 7526 | } |
| 7527 | |
| 7528 | ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0, |
| 7529 | forced_freq); |
| 7530 | wpabuf_free(tmp); |
| 7531 | |
| 7532 | return ret; |
| 7533 | } |
| 7534 | |
| 7535 | |
| 7536 | int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled) |
| 7537 | { |
| 7538 | const u8 *if_addr; |
| 7539 | int go_intent = wpa_s->conf->p2p_go_intent; |
| 7540 | struct wpa_supplicant *iface; |
| 7541 | |
| 7542 | if (wpa_s->global->p2p == NULL) |
| 7543 | return -1; |
| 7544 | |
| 7545 | if (!enabled) { |
| 7546 | wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag"); |
| 7547 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) |
| 7548 | { |
| 7549 | if (!iface->ap_iface) |
| 7550 | continue; |
| 7551 | hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]); |
| 7552 | } |
| 7553 | p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0, |
| 7554 | 0, NULL); |
| 7555 | if (wpa_s->p2p_nfc_tag_enabled) |
| 7556 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 7557 | wpa_s->p2p_nfc_tag_enabled = 0; |
| 7558 | return 0; |
| 7559 | } |
| 7560 | |
| 7561 | if (wpa_s->global->p2p_disabled) |
| 7562 | return -1; |
| 7563 | |
| 7564 | if (wpa_s->conf->wps_nfc_dh_pubkey == NULL || |
| 7565 | wpa_s->conf->wps_nfc_dh_privkey == NULL || |
| 7566 | wpa_s->conf->wps_nfc_dev_pw == NULL || |
| 7567 | wpa_s->conf->wps_nfc_dev_pw_id < 0x10) { |
| 7568 | wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured " |
| 7569 | "to allow static handover cases"); |
| 7570 | return -1; |
| 7571 | } |
| 7572 | |
| 7573 | wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag"); |
| 7574 | |
| 7575 | wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id; |
| 7576 | wpabuf_free(wpa_s->p2p_oob_dev_pw); |
| 7577 | wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw); |
| 7578 | if (wpa_s->p2p_oob_dev_pw == NULL) |
| 7579 | return -1; |
| 7580 | wpa_s->p2p_peer_oob_pk_hash_known = 0; |
| 7581 | |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 7582 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO || |
| 7583 | wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) { |
| 7584 | /* |
| 7585 | * P2P Group Interface present and the command came on group |
| 7586 | * interface, so enable the token for the current interface. |
| 7587 | */ |
| 7588 | wpa_s->create_p2p_iface = 0; |
| 7589 | } else { |
| 7590 | wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); |
| 7591 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7592 | |
| 7593 | if (wpa_s->create_p2p_iface) { |
| 7594 | enum wpa_driver_if_type iftype; |
| 7595 | /* Prepare to add a new interface for the group */ |
| 7596 | iftype = WPA_IF_P2P_GROUP; |
| 7597 | if (go_intent == 15) |
| 7598 | iftype = WPA_IF_P2P_GO; |
| 7599 | if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { |
| 7600 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 7601 | "interface for the group"); |
| 7602 | return -1; |
| 7603 | } |
| 7604 | |
| 7605 | if_addr = wpa_s->pending_interface_addr; |
| 7606 | } else |
| 7607 | if_addr = wpa_s->own_addr; |
| 7608 | |
| 7609 | wpa_s->p2p_nfc_tag_enabled = enabled; |
| 7610 | |
| 7611 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 7612 | struct hostapd_data *hapd; |
| 7613 | if (iface->ap_iface == NULL) |
| 7614 | continue; |
| 7615 | hapd = iface->ap_iface->bss[0]; |
| 7616 | wpabuf_free(hapd->conf->wps_nfc_dh_pubkey); |
| 7617 | hapd->conf->wps_nfc_dh_pubkey = |
| 7618 | wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey); |
| 7619 | wpabuf_free(hapd->conf->wps_nfc_dh_privkey); |
| 7620 | hapd->conf->wps_nfc_dh_privkey = |
| 7621 | wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey); |
| 7622 | wpabuf_free(hapd->conf->wps_nfc_dev_pw); |
| 7623 | hapd->conf->wps_nfc_dev_pw = |
| 7624 | wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw); |
| 7625 | hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id; |
| 7626 | |
| 7627 | if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) { |
| 7628 | wpa_dbg(iface, MSG_DEBUG, |
| 7629 | "P2P: Failed to enable NFC Tag for GO"); |
| 7630 | } |
| 7631 | } |
| 7632 | p2p_set_authorized_oob_dev_pw_id( |
| 7633 | wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent, |
| 7634 | if_addr); |
| 7635 | |
| 7636 | return 0; |
| 7637 | } |
| 7638 | |
| 7639 | #endif /* CONFIG_WPS_NFC */ |