Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * wpa_supplicant - P2P |
| 3 | * Copyright (c) 2009-2010, Atheros Communications |
| 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
| 10 | |
| 11 | #include "common.h" |
| 12 | #include "eloop.h" |
| 13 | #include "common/ieee802_11_common.h" |
| 14 | #include "common/ieee802_11_defs.h" |
| 15 | #include "common/wpa_ctrl.h" |
| 16 | #include "wps/wps_i.h" |
| 17 | #include "p2p/p2p.h" |
| 18 | #include "ap/hostapd.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 19 | #include "ap/ap_config.h" |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 20 | #include "ap/sta_info.h" |
| 21 | #include "ap/ap_drv_ops.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 22 | #include "ap/p2p_hostapd.h" |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 23 | #include "eapol_supp/eapol_supp_sm.h" |
| 24 | #include "rsn_supp/wpa.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 25 | #include "wpa_supplicant_i.h" |
| 26 | #include "driver_i.h" |
| 27 | #include "ap.h" |
| 28 | #include "config_ssid.h" |
| 29 | #include "config.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 30 | #include "notify.h" |
| 31 | #include "scan.h" |
| 32 | #include "bss.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 33 | #include "offchannel.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 34 | #include "wps_supplicant.h" |
| 35 | #include "p2p_supplicant.h" |
| 36 | |
| 37 | |
| 38 | /* |
| 39 | * How many times to try to scan to find the GO before giving up on join |
| 40 | * request. |
| 41 | */ |
| 42 | #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10 |
| 43 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 44 | #define P2P_AUTO_PD_SCAN_ATTEMPTS 5 |
| 45 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 46 | #ifndef P2P_MAX_CLIENT_IDLE |
| 47 | /* |
| 48 | * How many seconds to try to reconnect to the GO when connection in P2P client |
| 49 | * role has been lost. |
| 50 | */ |
| 51 | #define P2P_MAX_CLIENT_IDLE 10 |
| 52 | #endif /* P2P_MAX_CLIENT_IDLE */ |
| 53 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 54 | #ifndef P2P_MAX_INITIAL_CONN_WAIT |
| 55 | /* |
| 56 | * How many seconds to wait for initial 4-way handshake to get completed after |
| 57 | * WPS provisioning step. |
| 58 | */ |
| 59 | #define P2P_MAX_INITIAL_CONN_WAIT 10 |
| 60 | #endif /* P2P_MAX_INITIAL_CONN_WAIT */ |
| 61 | |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 62 | #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO |
| 63 | /* |
| 64 | * How many seconds to wait for initial 4-way handshake to get completed after |
| 65 | * WPS provisioning step on the GO. This controls the extra time the P2P |
| 66 | * operation is considered to be in progress (e.g., to delay other scans) after |
| 67 | * WPS provisioning has been completed on the GO during group formation. |
| 68 | */ |
| 69 | #define P2P_MAX_INITIAL_CONN_WAIT_GO 10 |
| 70 | #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */ |
| 71 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 72 | #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE |
| 73 | /* |
| 74 | * How many seconds to wait for initial 4-way handshake to get completed after |
| 75 | * re-invocation of a persistent group on the GO when the client is expected |
| 76 | * to connect automatically (no user interaction). |
| 77 | */ |
| 78 | #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15 |
| 79 | #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */ |
| 80 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 81 | #ifndef P2P_CONCURRENT_SEARCH_DELAY |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 82 | #define P2P_CONCURRENT_SEARCH_DELAY 500 |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 83 | #endif /* P2P_CONCURRENT_SEARCH_DELAY */ |
| 84 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 85 | #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-" |
| 86 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 87 | enum p2p_group_removal_reason { |
| 88 | P2P_GROUP_REMOVAL_UNKNOWN, |
| 89 | P2P_GROUP_REMOVAL_SILENT, |
| 90 | P2P_GROUP_REMOVAL_FORMATION_FAILED, |
| 91 | P2P_GROUP_REMOVAL_REQUESTED, |
| 92 | P2P_GROUP_REMOVAL_IDLE_TIMEOUT, |
| 93 | P2P_GROUP_REMOVAL_UNAVAILABLE, |
| 94 | P2P_GROUP_REMOVAL_GO_ENDING_SESSION, |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 95 | P2P_GROUP_REMOVAL_PSK_FAILURE, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 96 | #ifdef ANDROID_P2P |
| 97 | P2P_GROUP_REMOVAL_FREQ_CONFLICT |
| 98 | #endif |
| 99 | }; |
| 100 | |
Jouni Malinen | dc7b713 | 2012-09-14 12:53:47 -0700 | [diff] [blame] | 101 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 102 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx); |
| 103 | static struct wpa_supplicant * |
| 104 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, |
| 105 | int go); |
| 106 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 107 | static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 108 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx); |
| 109 | static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 110 | const u8 *dev_addr, enum p2p_wps_method wps_method, |
| 111 | int auto_join); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 112 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s); |
| 113 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s); |
| 114 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx); |
| 115 | static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 116 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, |
| 117 | void *timeout_ctx); |
Dmitry Shmidt | 1cf4573 | 2013-04-29 17:36:45 -0700 | [diff] [blame] | 118 | #ifdef ANDROID_P2P |
| 119 | static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx); |
| 120 | #endif |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 121 | static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, |
| 122 | int group_added); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 123 | static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 124 | |
| 125 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 126 | /* |
| 127 | * Get the number of concurrent channels that the HW can operate, but that are |
| 128 | * currently not in use by any of the wpa_supplicant interfaces. |
| 129 | */ |
| 130 | static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s) |
| 131 | { |
| 132 | int *freqs; |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 133 | int num, unused; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 134 | |
| 135 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 136 | if (!freqs) |
| 137 | return -1; |
| 138 | |
| 139 | num = get_shared_radio_freqs(wpa_s, freqs, |
| 140 | wpa_s->num_multichan_concurrent); |
| 141 | os_free(freqs); |
| 142 | |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 143 | unused = wpa_s->num_multichan_concurrent - num; |
| 144 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused); |
| 145 | return unused; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | /* |
| 150 | * Get the frequencies that are currently in use by one or more of the virtual |
| 151 | * interfaces, and that are also valid for P2P operation. |
| 152 | */ |
| 153 | static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s, |
| 154 | int *p2p_freqs, unsigned int len) |
| 155 | { |
| 156 | int *freqs; |
| 157 | unsigned int num, i, j; |
| 158 | |
| 159 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 160 | if (!freqs) |
| 161 | return -1; |
| 162 | |
| 163 | num = get_shared_radio_freqs(wpa_s, freqs, |
| 164 | wpa_s->num_multichan_concurrent); |
| 165 | |
| 166 | os_memset(p2p_freqs, 0, sizeof(int) * len); |
| 167 | |
| 168 | for (i = 0, j = 0; i < num && j < len; i++) { |
| 169 | if (p2p_supported_freq(wpa_s->global->p2p, freqs[i])) |
| 170 | p2p_freqs[j++] = freqs[i]; |
| 171 | } |
| 172 | |
| 173 | os_free(freqs); |
| 174 | |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 175 | dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j); |
| 176 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 177 | return j; |
| 178 | } |
| 179 | |
| 180 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 181 | static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s, |
| 182 | int freq) |
| 183 | { |
| 184 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 185 | return; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 186 | if (wpa_s->parent->conf->p2p_ignore_shared_freq && |
| 187 | freq > 0 && wpa_s->num_multichan_concurrent > 1 && |
| 188 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 189 | wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration", |
| 190 | freq); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 191 | freq = 0; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 192 | } |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 193 | p2p_set_own_freq_preference(wpa_s->global->p2p, freq); |
| 194 | } |
| 195 | |
| 196 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 197 | static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s, |
| 198 | struct wpa_scan_results *scan_res) |
| 199 | { |
| 200 | size_t i; |
| 201 | |
| 202 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 203 | return; |
| 204 | |
| 205 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)", |
| 206 | (int) scan_res->num); |
| 207 | |
| 208 | for (i = 0; i < scan_res->num; i++) { |
| 209 | struct wpa_scan_res *bss = scan_res->res[i]; |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 210 | struct os_time time_tmp_age, entry_ts; |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 211 | const u8 *ies; |
| 212 | size_t ies_len; |
| 213 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 214 | time_tmp_age.sec = bss->age / 1000; |
| 215 | time_tmp_age.usec = (bss->age % 1000) * 1000; |
| 216 | os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts); |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 217 | |
| 218 | ies = (const u8 *) (bss + 1); |
| 219 | ies_len = bss->ie_len; |
| 220 | if (bss->beacon_ie_len > 0 && |
| 221 | !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) && |
| 222 | wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) { |
| 223 | wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for " |
| 224 | MACSTR, MAC2STR(bss->bssid)); |
| 225 | ies = ies + ies_len; |
| 226 | ies_len = bss->beacon_ie_len; |
| 227 | } |
| 228 | |
| 229 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 230 | if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid, |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 231 | bss->freq, &entry_ts, bss->level, |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 232 | ies, ies_len) > 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 233 | break; |
| 234 | } |
| 235 | |
| 236 | p2p_scan_res_handled(wpa_s->global->p2p); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, |
| 241 | unsigned int num_req_dev_types, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 242 | const u8 *req_dev_types, const u8 *dev_id, u16 pw_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 243 | { |
| 244 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 245 | struct wpa_supplicant *ifs; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 246 | struct wpa_driver_scan_params params; |
| 247 | int ret; |
| 248 | struct wpabuf *wps_ie, *ies; |
| 249 | int social_channels[] = { 2412, 2437, 2462, 0, 0 }; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 250 | size_t ielen; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 251 | |
| 252 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 253 | return -1; |
| 254 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 255 | for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) { |
| 256 | if (ifs->sta_scan_pending && |
Dmitry Shmidt | 37d4d6a | 2013-03-18 13:09:42 -0700 | [diff] [blame] | 257 | (wpas_scan_scheduled(ifs) || ifs->scanning) && |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 258 | wpas_p2p_in_progress(wpa_s) == 2) { |
| 259 | wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow " |
| 260 | "pending station mode scan to be " |
| 261 | "completed on interface %s", ifs->ifname); |
Jouni Malinen | dc7b713 | 2012-09-14 12:53:47 -0700 | [diff] [blame] | 262 | wpa_s->global->p2p_cb_on_scan_complete = 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 263 | wpa_supplicant_req_scan(ifs, 0, 0); |
| 264 | return 1; |
| 265 | } |
| 266 | } |
| 267 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 268 | os_memset(¶ms, 0, sizeof(params)); |
| 269 | |
| 270 | /* P2P Wildcard SSID */ |
| 271 | params.num_ssids = 1; |
| 272 | params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; |
| 273 | params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; |
| 274 | |
| 275 | wpa_s->wps->dev.p2p = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 276 | wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev, |
| 277 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 278 | num_req_dev_types, req_dev_types); |
| 279 | if (wps_ie == NULL) |
| 280 | return -1; |
| 281 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 282 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
| 283 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 284 | if (ies == NULL) { |
| 285 | wpabuf_free(wps_ie); |
| 286 | return -1; |
| 287 | } |
| 288 | wpabuf_put_buf(ies, wps_ie); |
| 289 | wpabuf_free(wps_ie); |
| 290 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 291 | p2p_scan_ie(wpa_s->global->p2p, ies, dev_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 292 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 293 | params.p2p_probe = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 294 | params.extra_ies = wpabuf_head(ies); |
| 295 | params.extra_ies_len = wpabuf_len(ies); |
| 296 | |
| 297 | switch (type) { |
| 298 | case P2P_SCAN_SOCIAL: |
| 299 | params.freqs = social_channels; |
| 300 | break; |
| 301 | case P2P_SCAN_FULL: |
| 302 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 303 | case P2P_SCAN_SOCIAL_PLUS_ONE: |
| 304 | social_channels[3] = freq; |
| 305 | params.freqs = social_channels; |
| 306 | break; |
| 307 | } |
| 308 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 309 | ret = wpa_drv_scan(wpa_s, ¶ms); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 310 | |
| 311 | wpabuf_free(ies); |
| 312 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 313 | if (ret) { |
Jouni Malinen | 043a5a9 | 2012-09-13 18:03:14 -0700 | [diff] [blame] | 314 | for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) { |
| 315 | if (ifs->scanning || |
| 316 | ifs->scan_res_handler == wpas_p2p_scan_res_handler) { |
| 317 | wpa_s->global->p2p_cb_on_scan_complete = 1; |
| 318 | ret = 1; |
| 319 | break; |
| 320 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 321 | } |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 322 | } else { |
| 323 | os_get_time(&wpa_s->scan_trigger_time); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 324 | wpa_s->scan_res_handler = wpas_p2p_scan_res_handler; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 325 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 326 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 331 | static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface) |
| 332 | { |
| 333 | switch (p2p_group_interface) { |
| 334 | case P2P_GROUP_INTERFACE_PENDING: |
| 335 | return WPA_IF_P2P_GROUP; |
| 336 | case P2P_GROUP_INTERFACE_GO: |
| 337 | return WPA_IF_P2P_GO; |
| 338 | case P2P_GROUP_INTERFACE_CLIENT: |
| 339 | return WPA_IF_P2P_CLIENT; |
| 340 | } |
| 341 | |
| 342 | return WPA_IF_P2P_GROUP; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s, |
| 347 | const u8 *ssid, |
| 348 | size_t ssid_len, int *go) |
| 349 | { |
| 350 | struct wpa_ssid *s; |
| 351 | |
| 352 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 353 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 354 | if (s->disabled != 0 || !s->p2p_group || |
| 355 | s->ssid_len != ssid_len || |
| 356 | os_memcmp(ssid, s->ssid, ssid_len) != 0) |
| 357 | continue; |
| 358 | if (s->mode == WPAS_MODE_P2P_GO && |
| 359 | s != wpa_s->current_ssid) |
| 360 | continue; |
| 361 | if (go) |
| 362 | *go = s->mode == WPAS_MODE_P2P_GO; |
| 363 | return wpa_s; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 371 | static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s, |
| 372 | enum p2p_group_removal_reason removal_reason) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 373 | { |
| 374 | struct wpa_ssid *ssid; |
| 375 | char *gtype; |
| 376 | const char *reason; |
| 377 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 378 | ssid = wpa_s->current_ssid; |
| 379 | if (ssid == NULL) { |
| 380 | /* |
| 381 | * The current SSID was not known, but there may still be a |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 382 | * pending P2P group interface waiting for provisioning or a |
| 383 | * P2P group that is trying to reconnect. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 384 | */ |
| 385 | ssid = wpa_s->conf->ssid; |
| 386 | while (ssid) { |
Jouni Malinen | 9d71283 | 2012-10-05 11:01:57 -0700 | [diff] [blame] | 387 | if (ssid->p2p_group && ssid->disabled != 2) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 388 | break; |
| 389 | ssid = ssid->next; |
| 390 | } |
Jouni Malinen | 5c44edb | 2012-08-31 21:35:32 +0300 | [diff] [blame] | 391 | if (ssid == NULL && |
| 392 | wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) |
| 393 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 394 | wpa_printf(MSG_ERROR, "P2P: P2P group interface " |
| 395 | "not found"); |
| 396 | return -1; |
| 397 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 398 | } |
| 399 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO) |
| 400 | gtype = "GO"; |
| 401 | else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT || |
| 402 | (ssid && ssid->mode == WPAS_MODE_INFRA)) { |
| 403 | wpa_s->reassociate = 0; |
| 404 | wpa_s->disconnected = 1; |
| 405 | wpa_supplicant_deauthenticate(wpa_s, |
| 406 | WLAN_REASON_DEAUTH_LEAVING); |
| 407 | gtype = "client"; |
| 408 | } else |
| 409 | gtype = "GO"; |
| 410 | if (wpa_s->cross_connect_in_use) { |
| 411 | wpa_s->cross_connect_in_use = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 412 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 413 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 414 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 415 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 416 | switch (removal_reason) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 417 | case P2P_GROUP_REMOVAL_REQUESTED: |
| 418 | reason = " reason=REQUESTED"; |
| 419 | break; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 420 | case P2P_GROUP_REMOVAL_FORMATION_FAILED: |
| 421 | reason = " reason=FORMATION_FAILED"; |
| 422 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 423 | case P2P_GROUP_REMOVAL_IDLE_TIMEOUT: |
| 424 | reason = " reason=IDLE"; |
| 425 | break; |
| 426 | case P2P_GROUP_REMOVAL_UNAVAILABLE: |
| 427 | reason = " reason=UNAVAILABLE"; |
| 428 | break; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 429 | case P2P_GROUP_REMOVAL_GO_ENDING_SESSION: |
| 430 | reason = " reason=GO_ENDING_SESSION"; |
| 431 | break; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 432 | case P2P_GROUP_REMOVAL_PSK_FAILURE: |
| 433 | reason = " reason=PSK_FAILURE"; |
| 434 | break; |
Dmitry Shmidt | 687922c | 2012-03-26 14:02:32 -0700 | [diff] [blame] | 435 | #ifdef ANDROID_P2P |
| 436 | case P2P_GROUP_REMOVAL_FREQ_CONFLICT: |
| 437 | reason = " reason=FREQ_CONFLICT"; |
| 438 | break; |
| 439 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 440 | default: |
| 441 | reason = ""; |
| 442 | break; |
| 443 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 444 | if (removal_reason != P2P_GROUP_REMOVAL_SILENT) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 445 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 446 | P2P_EVENT_GROUP_REMOVED "%s %s%s", |
| 447 | wpa_s->ifname, gtype, reason); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 448 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 449 | |
Dmitry Shmidt | 1cf4573 | 2013-04-29 17:36:45 -0700 | [diff] [blame] | 450 | #ifdef ANDROID_P2P |
| 451 | eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL); |
| 452 | #endif |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 453 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 454 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 455 | if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 456 | wpa_s->parent, NULL) > 0) { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 457 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation " |
| 458 | "timeout"); |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 459 | wpa_s->p2p_in_provisioning = 0; |
| 460 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 461 | |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 462 | /* |
| 463 | * Make sure wait for the first client does not remain active after the |
| 464 | * group has been removed. |
| 465 | */ |
| 466 | wpa_s->global->p2p_go_wait_client.sec = 0; |
| 467 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 468 | if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 469 | wpas_notify_p2p_group_removed(wpa_s, ssid, gtype); |
| 470 | |
| 471 | if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { |
| 472 | struct wpa_global *global; |
| 473 | char *ifname; |
| 474 | enum wpa_driver_if_type type; |
| 475 | wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s", |
| 476 | wpa_s->ifname); |
| 477 | global = wpa_s->global; |
| 478 | ifname = os_strdup(wpa_s->ifname); |
| 479 | type = wpas_p2p_if_type(wpa_s->p2p_group_interface); |
Dmitry Shmidt | e15c7b5 | 2011-08-03 15:04:35 -0700 | [diff] [blame] | 480 | wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 481 | wpa_s = global->ifaces; |
| 482 | if (wpa_s && ifname) |
| 483 | wpa_drv_if_remove(wpa_s, type, ifname); |
| 484 | os_free(ifname); |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 485 | return 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 488 | if (!wpa_s->p2p_go_group_formation_completed) { |
| 489 | wpa_s->global->p2p_group_formation = NULL; |
| 490 | wpa_s->p2p_in_provisioning = 0; |
| 491 | } |
| 492 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 493 | wpa_s->show_group_started = 0; |
| 494 | os_free(wpa_s->go_params); |
| 495 | wpa_s->go_params = NULL; |
| 496 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 497 | wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network"); |
| 498 | if (ssid && (ssid->p2p_group || |
| 499 | ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || |
| 500 | (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) { |
| 501 | int id = ssid->id; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 502 | if (ssid == wpa_s->current_ssid) { |
| 503 | wpa_sm_set_config(wpa_s->wpa, NULL); |
| 504 | eapol_sm_notify_config(wpa_s->eapol, NULL, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 505 | wpa_s->current_ssid = NULL; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 506 | } |
| 507 | /* |
| 508 | * Networks objects created during any P2P activities are not |
| 509 | * exposed out as they might/will confuse certain non-P2P aware |
| 510 | * applications since these network objects won't behave like |
| 511 | * regular ones. |
| 512 | * |
| 513 | * Likewise, we don't send out network removed signals for such |
| 514 | * network objects. |
| 515 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 516 | wpa_config_remove_network(wpa_s->conf, id); |
| 517 | wpa_supplicant_clear_status(wpa_s); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 518 | wpa_supplicant_cancel_sched_scan(wpa_s); |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 519 | wpa_s->sta_scan_pending = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 520 | } else { |
| 521 | wpa_printf(MSG_DEBUG, "P2P: Temporary group network not " |
| 522 | "found"); |
| 523 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 524 | if (wpa_s->ap_iface) |
| 525 | wpa_supplicant_ap_deinit(wpa_s); |
| 526 | else |
| 527 | wpa_drv_deinit_p2p_cli(wpa_s); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 528 | |
| 529 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | |
| 533 | static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s, |
| 534 | u8 *go_dev_addr, |
| 535 | const u8 *ssid, size_t ssid_len) |
| 536 | { |
| 537 | struct wpa_bss *bss; |
| 538 | const u8 *bssid; |
| 539 | struct wpabuf *p2p; |
| 540 | u8 group_capab; |
| 541 | const u8 *addr; |
| 542 | |
| 543 | if (wpa_s->go_params) |
| 544 | bssid = wpa_s->go_params->peer_interface_addr; |
| 545 | else |
| 546 | bssid = wpa_s->bssid; |
| 547 | |
| 548 | bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len); |
| 549 | if (bss == NULL) { |
| 550 | u8 iface_addr[ETH_ALEN]; |
| 551 | if (p2p_get_interface_addr(wpa_s->global->p2p, bssid, |
| 552 | iface_addr) == 0) |
| 553 | bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len); |
| 554 | } |
| 555 | if (bss == NULL) { |
| 556 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " |
| 557 | "group is persistent - BSS " MACSTR " not found", |
| 558 | MAC2STR(bssid)); |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 563 | if (p2p == NULL) |
| 564 | p2p = wpa_bss_get_vendor_ie_multi_beacon(bss, |
| 565 | P2P_IE_VENDOR_TYPE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 566 | if (p2p == NULL) { |
| 567 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " |
| 568 | "group is persistent - BSS " MACSTR |
| 569 | " did not include P2P IE", MAC2STR(bssid)); |
| 570 | wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs", |
| 571 | (u8 *) (bss + 1), bss->ie_len); |
| 572 | wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs", |
| 573 | ((u8 *) bss + 1) + bss->ie_len, |
| 574 | bss->beacon_ie_len); |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | group_capab = p2p_get_group_capab(p2p); |
| 579 | addr = p2p_get_go_dev_addr(p2p); |
| 580 | wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: " |
| 581 | "group_capab=0x%x", group_capab); |
| 582 | if (addr) { |
| 583 | os_memcpy(go_dev_addr, addr, ETH_ALEN); |
| 584 | wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR, |
| 585 | MAC2STR(addr)); |
| 586 | } else |
| 587 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 588 | wpabuf_free(p2p); |
| 589 | |
| 590 | wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x " |
| 591 | "go_dev_addr=" MACSTR, |
| 592 | MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr)); |
| 593 | |
| 594 | return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP; |
| 595 | } |
| 596 | |
| 597 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 598 | static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s, |
| 599 | struct wpa_ssid *ssid, |
| 600 | const u8 *go_dev_addr) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 601 | { |
| 602 | struct wpa_ssid *s; |
| 603 | int changed = 0; |
| 604 | |
| 605 | wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent " |
| 606 | "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr)); |
| 607 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 608 | if (s->disabled == 2 && |
| 609 | os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 && |
| 610 | s->ssid_len == ssid->ssid_len && |
| 611 | os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0) |
| 612 | break; |
| 613 | } |
| 614 | |
| 615 | if (s) { |
| 616 | wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group " |
| 617 | "entry"); |
| 618 | if (ssid->passphrase && !s->passphrase) |
| 619 | changed = 1; |
| 620 | else if (ssid->passphrase && s->passphrase && |
| 621 | os_strcmp(ssid->passphrase, s->passphrase) != 0) |
| 622 | changed = 1; |
| 623 | } else { |
| 624 | wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group " |
| 625 | "entry"); |
| 626 | changed = 1; |
| 627 | s = wpa_config_add_network(wpa_s->conf); |
| 628 | if (s == NULL) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 629 | return -1; |
| 630 | |
| 631 | /* |
| 632 | * Instead of network_added we emit persistent_group_added |
| 633 | * notification. Also to keep the defense checks in |
| 634 | * persistent_group obj registration method, we set the |
| 635 | * relevant flags in s to designate it as a persistent group. |
| 636 | */ |
| 637 | s->p2p_group = 1; |
| 638 | s->p2p_persistent_group = 1; |
| 639 | wpas_notify_persistent_group_added(wpa_s, s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 640 | wpa_config_set_network_defaults(s); |
| 641 | } |
| 642 | |
| 643 | s->p2p_group = 1; |
| 644 | s->p2p_persistent_group = 1; |
| 645 | s->disabled = 2; |
| 646 | s->bssid_set = 1; |
| 647 | os_memcpy(s->bssid, go_dev_addr, ETH_ALEN); |
| 648 | s->mode = ssid->mode; |
| 649 | s->auth_alg = WPA_AUTH_ALG_OPEN; |
| 650 | s->key_mgmt = WPA_KEY_MGMT_PSK; |
| 651 | s->proto = WPA_PROTO_RSN; |
| 652 | s->pairwise_cipher = WPA_CIPHER_CCMP; |
| 653 | s->export_keys = 1; |
| 654 | if (ssid->passphrase) { |
| 655 | os_free(s->passphrase); |
| 656 | s->passphrase = os_strdup(ssid->passphrase); |
| 657 | } |
| 658 | if (ssid->psk_set) { |
| 659 | s->psk_set = 1; |
| 660 | os_memcpy(s->psk, ssid->psk, 32); |
| 661 | } |
| 662 | if (s->passphrase && !s->psk_set) |
| 663 | wpa_config_update_psk(s); |
| 664 | if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) { |
| 665 | os_free(s->ssid); |
| 666 | s->ssid = os_malloc(ssid->ssid_len); |
| 667 | } |
| 668 | if (s->ssid) { |
| 669 | s->ssid_len = ssid->ssid_len; |
| 670 | os_memcpy(s->ssid, ssid->ssid, s->ssid_len); |
| 671 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 672 | if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) { |
| 673 | dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list); |
| 674 | wpa_s->global->add_psk = NULL; |
| 675 | changed = 1; |
| 676 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 677 | |
| 678 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 679 | if (changed && wpa_s->conf->update_config && |
| 680 | wpa_config_write(wpa_s->confname, wpa_s->conf)) { |
| 681 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
| 682 | } |
| 683 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 684 | |
| 685 | return s->id; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 689 | static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s, |
| 690 | const u8 *addr) |
| 691 | { |
| 692 | struct wpa_ssid *ssid, *s; |
| 693 | u8 *n; |
| 694 | size_t i; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 695 | int found = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 696 | |
| 697 | ssid = wpa_s->current_ssid; |
| 698 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || |
| 699 | !ssid->p2p_persistent_group) |
| 700 | return; |
| 701 | |
| 702 | for (s = wpa_s->parent->conf->ssid; s; s = s->next) { |
| 703 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) |
| 704 | continue; |
| 705 | |
| 706 | if (s->ssid_len == ssid->ssid_len && |
| 707 | os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0) |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | if (s == NULL) |
| 712 | return; |
| 713 | |
| 714 | for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) { |
| 715 | if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 716 | ETH_ALEN) != 0) |
| 717 | continue; |
| 718 | |
| 719 | if (i == s->num_p2p_clients - 1) |
| 720 | return; /* already the most recent entry */ |
| 721 | |
| 722 | /* move the entry to mark it most recent */ |
| 723 | os_memmove(s->p2p_client_list + i * ETH_ALEN, |
| 724 | s->p2p_client_list + (i + 1) * ETH_ALEN, |
| 725 | (s->num_p2p_clients - i - 1) * ETH_ALEN); |
| 726 | os_memcpy(s->p2p_client_list + |
| 727 | (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN); |
| 728 | found = 1; |
| 729 | break; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 732 | if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) { |
| 733 | n = os_realloc_array(s->p2p_client_list, |
| 734 | s->num_p2p_clients + 1, ETH_ALEN); |
| 735 | if (n == NULL) |
| 736 | return; |
| 737 | os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN); |
| 738 | s->p2p_client_list = n; |
| 739 | s->num_p2p_clients++; |
| 740 | } else if (!found) { |
| 741 | /* Not enough room for an additional entry - drop the oldest |
| 742 | * entry */ |
| 743 | os_memmove(s->p2p_client_list, |
| 744 | s->p2p_client_list + ETH_ALEN, |
| 745 | (s->num_p2p_clients - 1) * ETH_ALEN); |
| 746 | os_memcpy(s->p2p_client_list + |
| 747 | (s->num_p2p_clients - 1) * ETH_ALEN, |
| 748 | addr, ETH_ALEN); |
| 749 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 750 | |
| 751 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 752 | if (wpa_s->parent->conf->update_config && |
| 753 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 754 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
| 755 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
| 756 | } |
| 757 | |
| 758 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 759 | static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s, |
| 760 | int success) |
| 761 | { |
| 762 | struct wpa_ssid *ssid; |
| 763 | const char *ssid_txt; |
| 764 | int client; |
| 765 | int persistent; |
| 766 | u8 go_dev_addr[ETH_ALEN]; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 767 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 768 | |
| 769 | /* |
| 770 | * This callback is likely called for the main interface. Update wpa_s |
| 771 | * to use the group interface if a new interface was created for the |
| 772 | * group. |
| 773 | */ |
| 774 | if (wpa_s->global->p2p_group_formation) |
| 775 | wpa_s = wpa_s->global->p2p_group_formation; |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 776 | if (wpa_s->p2p_go_group_formation_completed) { |
| 777 | wpa_s->global->p2p_group_formation = NULL; |
| 778 | wpa_s->p2p_in_provisioning = 0; |
| 779 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 780 | |
| 781 | if (!success) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 782 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 783 | P2P_EVENT_GROUP_FORMATION_FAILURE); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 784 | wpas_p2p_group_delete(wpa_s, |
| 785 | P2P_GROUP_REMOVAL_FORMATION_FAILED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 786 | return; |
| 787 | } |
| 788 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 789 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 790 | P2P_EVENT_GROUP_FORMATION_SUCCESS); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 791 | |
| 792 | ssid = wpa_s->current_ssid; |
| 793 | if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { |
| 794 | ssid->mode = WPAS_MODE_P2P_GO; |
| 795 | p2p_group_notif_formation_done(wpa_s->p2p_group); |
| 796 | wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL); |
| 797 | } |
| 798 | |
| 799 | persistent = 0; |
| 800 | if (ssid) { |
| 801 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); |
| 802 | client = ssid->mode == WPAS_MODE_INFRA; |
| 803 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 804 | persistent = ssid->p2p_persistent_group; |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 805 | os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr, |
| 806 | ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 807 | } else |
| 808 | persistent = wpas_p2p_persistent_group(wpa_s, |
| 809 | go_dev_addr, |
| 810 | ssid->ssid, |
| 811 | ssid->ssid_len); |
| 812 | } else { |
| 813 | ssid_txt = ""; |
| 814 | client = wpa_s->p2p_group_interface == |
| 815 | P2P_GROUP_INTERFACE_CLIENT; |
| 816 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 817 | } |
| 818 | |
| 819 | wpa_s->show_group_started = 0; |
| 820 | if (client) { |
| 821 | /* |
| 822 | * Indicate event only after successfully completed 4-way |
| 823 | * handshake, i.e., when the interface is ready for data |
| 824 | * packets. |
| 825 | */ |
| 826 | wpa_s->show_group_started = 1; |
| 827 | } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) { |
| 828 | char psk[65]; |
| 829 | wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 830 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 831 | "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" |
| 832 | MACSTR "%s", |
| 833 | wpa_s->ifname, ssid_txt, ssid->frequency, psk, |
| 834 | MAC2STR(go_dev_addr), |
| 835 | persistent ? " [PERSISTENT]" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 836 | wpas_p2p_cross_connect_setup(wpa_s); |
| 837 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 838 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 839 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 840 | "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" " |
| 841 | "go_dev_addr=" MACSTR "%s", |
| 842 | wpa_s->ifname, ssid_txt, |
| 843 | ssid ? ssid->frequency : 0, |
| 844 | ssid && ssid->passphrase ? ssid->passphrase : "", |
| 845 | MAC2STR(go_dev_addr), |
| 846 | persistent ? " [PERSISTENT]" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 847 | wpas_p2p_cross_connect_setup(wpa_s); |
| 848 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 849 | } |
| 850 | |
| 851 | if (persistent) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 852 | network_id = wpas_p2p_store_persistent_group(wpa_s->parent, |
| 853 | ssid, go_dev_addr); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 854 | else { |
| 855 | os_free(wpa_s->global->add_psk); |
| 856 | wpa_s->global->add_psk = NULL; |
| 857 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 858 | if (network_id < 0 && ssid) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 859 | network_id = ssid->id; |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 860 | if (!client) { |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 861 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 862 | os_get_time(&wpa_s->global->p2p_go_wait_client); |
| 863 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 867 | static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s, |
| 868 | unsigned int freq, |
| 869 | const u8 *dst, const u8 *src, |
| 870 | const u8 *bssid, |
| 871 | const u8 *data, size_t data_len, |
| 872 | enum offchannel_send_action_result |
| 873 | result) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 874 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 875 | enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 876 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 877 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) |
| 878 | return; |
| 879 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 880 | return; |
| 881 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 882 | switch (result) { |
| 883 | case OFFCHANNEL_SEND_ACTION_SUCCESS: |
| 884 | res = P2P_SEND_ACTION_SUCCESS; |
| 885 | break; |
| 886 | case OFFCHANNEL_SEND_ACTION_NO_ACK: |
| 887 | res = P2P_SEND_ACTION_NO_ACK; |
| 888 | break; |
| 889 | case OFFCHANNEL_SEND_ACTION_FAILED: |
| 890 | res = P2P_SEND_ACTION_FAILED; |
| 891 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 894 | 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] | 895 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 896 | if (result != OFFCHANNEL_SEND_ACTION_SUCCESS && |
| 897 | wpa_s->pending_pd_before_join && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 898 | (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 899 | os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) && |
| 900 | wpa_s->p2p_fallback_to_go_neg) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 901 | wpa_s->pending_pd_before_join = 0; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 902 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req " |
| 903 | "during p2p_connect-auto"); |
| 904 | wpas_p2p_fallback_to_go_neg(wpa_s, 0); |
| 905 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
| 909 | |
| 910 | static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst, |
| 911 | const u8 *src, const u8 *bssid, const u8 *buf, |
| 912 | size_t len, unsigned int wait_time) |
| 913 | { |
| 914 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 915 | return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len, |
| 916 | wait_time, |
| 917 | wpas_p2p_send_action_tx_status, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | |
| 921 | static void wpas_send_action_done(void *ctx) |
| 922 | { |
| 923 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 924 | offchannel_send_action_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | |
| 928 | static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s, |
| 929 | struct p2p_go_neg_results *params) |
| 930 | { |
| 931 | if (wpa_s->go_params == NULL) { |
| 932 | wpa_s->go_params = os_malloc(sizeof(*params)); |
| 933 | if (wpa_s->go_params == NULL) |
| 934 | return -1; |
| 935 | } |
| 936 | os_memcpy(wpa_s->go_params, params, sizeof(*params)); |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | |
| 941 | static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s, |
| 942 | struct p2p_go_neg_results *res) |
| 943 | { |
| 944 | wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR, |
| 945 | MAC2STR(res->peer_interface_addr)); |
| 946 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID", |
| 947 | res->ssid, res->ssid_len); |
| 948 | wpa_supplicant_ap_deinit(wpa_s); |
| 949 | wpas_copy_go_neg_results(wpa_s, res); |
| 950 | if (res->wps_method == WPS_PBC) |
| 951 | wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1); |
| 952 | else { |
| 953 | u16 dev_pw_id = DEV_PW_DEFAULT; |
| 954 | if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD) |
| 955 | dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED; |
| 956 | wpas_wps_start_pin(wpa_s, res->peer_interface_addr, |
| 957 | wpa_s->p2p_pin, 1, dev_pw_id); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 962 | static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s, |
| 963 | struct wpa_ssid *ssid) |
| 964 | { |
| 965 | struct wpa_ssid *persistent; |
| 966 | struct psk_list_entry *psk; |
| 967 | struct hostapd_data *hapd; |
| 968 | |
| 969 | if (!wpa_s->ap_iface) |
| 970 | return; |
| 971 | |
| 972 | persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid, |
| 973 | ssid->ssid_len); |
| 974 | if (persistent == NULL) |
| 975 | return; |
| 976 | |
| 977 | hapd = wpa_s->ap_iface->bss[0]; |
| 978 | |
| 979 | dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry, |
| 980 | list) { |
| 981 | struct hostapd_wpa_psk *hpsk; |
| 982 | |
| 983 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for " |
| 984 | MACSTR " psk=%d", |
| 985 | MAC2STR(psk->addr), psk->p2p); |
| 986 | hpsk = os_zalloc(sizeof(*hpsk)); |
| 987 | if (hpsk == NULL) |
| 988 | break; |
| 989 | os_memcpy(hpsk->psk, psk->psk, PMK_LEN); |
| 990 | if (psk->p2p) |
| 991 | os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN); |
| 992 | else |
| 993 | os_memcpy(hpsk->addr, psk->addr, ETH_ALEN); |
| 994 | hpsk->next = hapd->conf->ssid.wpa_psk; |
| 995 | hapd->conf->ssid.wpa_psk = hpsk; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1000 | static void p2p_go_configured(void *ctx, void *data) |
| 1001 | { |
| 1002 | struct wpa_supplicant *wpa_s = ctx; |
| 1003 | struct p2p_go_neg_results *params = data; |
| 1004 | struct wpa_ssid *ssid; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1005 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1006 | |
| 1007 | ssid = wpa_s->current_ssid; |
| 1008 | if (ssid && ssid->mode == WPAS_MODE_P2P_GO) { |
| 1009 | wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning"); |
| 1010 | if (wpa_s->global->p2p_group_formation == wpa_s) |
| 1011 | wpa_s->global->p2p_group_formation = NULL; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1012 | if (os_strlen(params->passphrase) > 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1013 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 1014 | P2P_EVENT_GROUP_STARTED |
| 1015 | "%s GO ssid=\"%s\" freq=%d " |
| 1016 | "passphrase=\"%s\" go_dev_addr=" MACSTR |
| 1017 | "%s", wpa_s->ifname, |
| 1018 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len), |
| 1019 | ssid->frequency, params->passphrase, |
| 1020 | MAC2STR(wpa_s->global->p2p_dev_addr), |
| 1021 | params->persistent_group ? |
| 1022 | " [PERSISTENT]" : ""); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1023 | } else { |
| 1024 | char psk[65]; |
| 1025 | wpa_snprintf_hex(psk, sizeof(psk), params->psk, |
| 1026 | sizeof(params->psk)); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1027 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 1028 | P2P_EVENT_GROUP_STARTED |
| 1029 | "%s GO ssid=\"%s\" freq=%d psk=%s " |
| 1030 | "go_dev_addr=" MACSTR "%s", |
| 1031 | wpa_s->ifname, |
| 1032 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len), |
| 1033 | ssid->frequency, psk, |
| 1034 | MAC2STR(wpa_s->global->p2p_dev_addr), |
| 1035 | params->persistent_group ? |
| 1036 | " [PERSISTENT]" : ""); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1037 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1038 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 1039 | os_get_time(&wpa_s->global->p2p_go_wait_client); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1040 | if (params->persistent_group) { |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1041 | network_id = wpas_p2p_store_persistent_group( |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1042 | wpa_s->parent, ssid, |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 1043 | wpa_s->global->p2p_dev_addr); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1044 | wpas_p2p_add_psk_list(wpa_s, ssid); |
| 1045 | } |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1046 | if (network_id < 0) |
| 1047 | network_id = ssid->id; |
| 1048 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1049 | wpas_p2p_cross_connect_setup(wpa_s); |
| 1050 | wpas_p2p_set_group_idle_timeout(wpa_s); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 1051 | |
| 1052 | if (wpa_s->p2p_first_connection_timeout) { |
| 1053 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1054 | "P2P: Start group formation timeout of %d seconds until first data connection on GO", |
| 1055 | wpa_s->p2p_first_connection_timeout); |
| 1056 | wpa_s->p2p_go_group_formation_completed = 0; |
| 1057 | wpa_s->global->p2p_group_formation = wpa_s; |
| 1058 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1059 | wpa_s->parent, NULL); |
| 1060 | eloop_register_timeout( |
| 1061 | wpa_s->p2p_first_connection_timeout, 0, |
| 1062 | wpas_p2p_group_formation_timeout, |
| 1063 | wpa_s->parent, NULL); |
| 1064 | } |
| 1065 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning"); |
| 1070 | if (wpa_supplicant_ap_mac_addr_filter(wpa_s, |
| 1071 | params->peer_interface_addr)) { |
| 1072 | wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address " |
| 1073 | "filtering"); |
| 1074 | return; |
| 1075 | } |
| 1076 | if (params->wps_method == WPS_PBC) |
| 1077 | wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1078 | params->peer_device_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1079 | else if (wpa_s->p2p_pin[0]) |
| 1080 | wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1081 | wpa_s->p2p_pin, NULL, 0, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1082 | os_free(wpa_s->go_params); |
| 1083 | wpa_s->go_params = NULL; |
| 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | static void wpas_start_wps_go(struct wpa_supplicant *wpa_s, |
| 1088 | struct p2p_go_neg_results *params, |
| 1089 | int group_formation) |
| 1090 | { |
| 1091 | struct wpa_ssid *ssid; |
| 1092 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1093 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO"); |
| 1094 | if (wpas_copy_go_neg_results(wpa_s, params) < 0) { |
| 1095 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation " |
| 1096 | "results"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1097 | return; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1098 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1099 | |
| 1100 | ssid = wpa_config_add_network(wpa_s->conf); |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1101 | if (ssid == NULL) { |
| 1102 | 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] | 1103 | return; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1104 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1105 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1106 | wpa_s->show_group_started = 0; |
| 1107 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1108 | wpa_config_set_network_defaults(ssid); |
| 1109 | ssid->temporary = 1; |
| 1110 | ssid->p2p_group = 1; |
| 1111 | ssid->p2p_persistent_group = params->persistent_group; |
| 1112 | ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION : |
| 1113 | WPAS_MODE_P2P_GO; |
| 1114 | ssid->frequency = params->freq; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1115 | ssid->ht40 = params->ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1116 | ssid->vht = params->vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1117 | ssid->ssid = os_zalloc(params->ssid_len + 1); |
| 1118 | if (ssid->ssid) { |
| 1119 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); |
| 1120 | ssid->ssid_len = params->ssid_len; |
| 1121 | } |
| 1122 | ssid->auth_alg = WPA_AUTH_ALG_OPEN; |
| 1123 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; |
| 1124 | ssid->proto = WPA_PROTO_RSN; |
| 1125 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1126 | if (os_strlen(params->passphrase) > 0) { |
| 1127 | ssid->passphrase = os_strdup(params->passphrase); |
| 1128 | if (ssid->passphrase == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1129 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 1130 | "P2P: Failed to copy passphrase for GO"); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1131 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 1132 | return; |
| 1133 | } |
| 1134 | } else |
| 1135 | ssid->passphrase = NULL; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1136 | ssid->psk_set = params->psk_set; |
| 1137 | if (ssid->psk_set) |
| 1138 | os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1139 | else if (ssid->passphrase) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1140 | wpa_config_update_psk(ssid); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1141 | ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1142 | |
| 1143 | wpa_s->ap_configured_cb = p2p_go_configured; |
| 1144 | wpa_s->ap_configured_cb_ctx = wpa_s; |
| 1145 | wpa_s->ap_configured_cb_data = wpa_s->go_params; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1146 | wpa_s->connect_without_scan = ssid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1147 | wpa_s->reassociate = 1; |
| 1148 | wpa_s->disconnected = 0; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1149 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to " |
| 1150 | "start GO)"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1151 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | static void wpas_p2p_clone_config(struct wpa_supplicant *dst, |
| 1156 | const struct wpa_supplicant *src) |
| 1157 | { |
| 1158 | struct wpa_config *d; |
| 1159 | const struct wpa_config *s; |
| 1160 | |
| 1161 | d = dst->conf; |
| 1162 | s = src->conf; |
| 1163 | |
| 1164 | #define C(n) if (s->n) d->n = os_strdup(s->n) |
| 1165 | C(device_name); |
| 1166 | C(manufacturer); |
| 1167 | C(model_name); |
| 1168 | C(model_number); |
| 1169 | C(serial_number); |
| 1170 | C(config_methods); |
| 1171 | #undef C |
| 1172 | |
| 1173 | os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN); |
| 1174 | os_memcpy(d->sec_device_type, s->sec_device_type, |
| 1175 | sizeof(d->sec_device_type)); |
| 1176 | d->num_sec_device_types = s->num_sec_device_types; |
| 1177 | |
| 1178 | d->p2p_group_idle = s->p2p_group_idle; |
| 1179 | d->p2p_intra_bss = s->p2p_intra_bss; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1180 | d->persistent_reconnect = s->persistent_reconnect; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1181 | d->max_num_sta = s->max_num_sta; |
| 1182 | d->pbc_in_m1 = s->pbc_in_m1; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1183 | d->ignore_old_scan_res = s->ignore_old_scan_res; |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 1184 | d->beacon_int = s->beacon_int; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 1185 | d->disassoc_low_ack = s->disassoc_low_ack; |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 1186 | d->disable_scan_offload = s->disable_scan_offload; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1190 | static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s, |
| 1191 | char *ifname, size_t len) |
| 1192 | { |
| 1193 | char *ifname_ptr = wpa_s->ifname; |
| 1194 | |
| 1195 | if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX, |
| 1196 | os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) { |
| 1197 | ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1; |
| 1198 | } |
| 1199 | |
| 1200 | os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx); |
| 1201 | if (os_strlen(ifname) >= IFNAMSIZ && |
| 1202 | os_strlen(wpa_s->ifname) < IFNAMSIZ) { |
| 1203 | /* Try to avoid going over the IFNAMSIZ length limit */ |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1204 | os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1209 | static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, |
| 1210 | enum wpa_driver_if_type type) |
| 1211 | { |
| 1212 | char ifname[120], force_ifname[120]; |
| 1213 | |
| 1214 | if (wpa_s->pending_interface_name[0]) { |
| 1215 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists " |
| 1216 | "- skip creation of a new one"); |
| 1217 | if (is_zero_ether_addr(wpa_s->pending_interface_addr)) { |
| 1218 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual address " |
| 1219 | "unknown?! ifname='%s'", |
| 1220 | wpa_s->pending_interface_name); |
| 1221 | return -1; |
| 1222 | } |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1226 | wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1227 | force_ifname[0] = '\0'; |
| 1228 | |
| 1229 | wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group", |
| 1230 | ifname); |
| 1231 | wpa_s->p2p_group_idx++; |
| 1232 | |
| 1233 | wpa_s->pending_interface_type = type; |
| 1234 | if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname, |
| 1235 | wpa_s->pending_interface_addr, NULL) < 0) { |
| 1236 | wpa_printf(MSG_ERROR, "P2P: Failed to create new group " |
| 1237 | "interface"); |
| 1238 | return -1; |
| 1239 | } |
| 1240 | |
| 1241 | if (force_ifname[0]) { |
| 1242 | wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s", |
| 1243 | force_ifname); |
| 1244 | os_strlcpy(wpa_s->pending_interface_name, force_ifname, |
| 1245 | sizeof(wpa_s->pending_interface_name)); |
| 1246 | } else |
| 1247 | os_strlcpy(wpa_s->pending_interface_name, ifname, |
| 1248 | sizeof(wpa_s->pending_interface_name)); |
| 1249 | wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr " |
| 1250 | MACSTR, wpa_s->pending_interface_name, |
| 1251 | MAC2STR(wpa_s->pending_interface_addr)); |
| 1252 | |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
| 1256 | |
| 1257 | static void wpas_p2p_remove_pending_group_interface( |
| 1258 | struct wpa_supplicant *wpa_s) |
| 1259 | { |
| 1260 | if (!wpa_s->pending_interface_name[0] || |
| 1261 | is_zero_ether_addr(wpa_s->pending_interface_addr)) |
| 1262 | return; /* No pending virtual interface */ |
| 1263 | |
| 1264 | wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s", |
| 1265 | wpa_s->pending_interface_name); |
| 1266 | wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type, |
| 1267 | wpa_s->pending_interface_name); |
| 1268 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); |
| 1269 | wpa_s->pending_interface_name[0] = '\0'; |
| 1270 | } |
| 1271 | |
| 1272 | |
| 1273 | static struct wpa_supplicant * |
| 1274 | wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go) |
| 1275 | { |
| 1276 | struct wpa_interface iface; |
| 1277 | struct wpa_supplicant *group_wpa_s; |
| 1278 | |
| 1279 | if (!wpa_s->pending_interface_name[0]) { |
| 1280 | wpa_printf(MSG_ERROR, "P2P: No pending group interface"); |
| 1281 | if (!wpas_p2p_create_iface(wpa_s)) |
| 1282 | return NULL; |
| 1283 | /* |
| 1284 | * Something has forced us to remove the pending interface; try |
| 1285 | * to create a new one and hope for the best that we will get |
| 1286 | * the same local address. |
| 1287 | */ |
| 1288 | if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : |
| 1289 | WPA_IF_P2P_CLIENT) < 0) |
| 1290 | return NULL; |
| 1291 | } |
| 1292 | |
| 1293 | os_memset(&iface, 0, sizeof(iface)); |
| 1294 | iface.ifname = wpa_s->pending_interface_name; |
| 1295 | iface.driver = wpa_s->driver->name; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1296 | if (wpa_s->conf->ctrl_interface == NULL && |
| 1297 | wpa_s->parent != wpa_s && |
| 1298 | wpa_s->p2p_mgmt && |
| 1299 | (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) |
| 1300 | iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface; |
| 1301 | else |
| 1302 | iface.ctrl_interface = wpa_s->conf->ctrl_interface; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1303 | iface.driver_param = wpa_s->conf->driver_param; |
| 1304 | group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface); |
| 1305 | if (group_wpa_s == NULL) { |
| 1306 | wpa_printf(MSG_ERROR, "P2P: Failed to create new " |
| 1307 | "wpa_supplicant interface"); |
| 1308 | return NULL; |
| 1309 | } |
| 1310 | wpa_s->pending_interface_name[0] = '\0'; |
| 1311 | group_wpa_s->parent = wpa_s; |
| 1312 | group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO : |
| 1313 | P2P_GROUP_INTERFACE_CLIENT; |
| 1314 | wpa_s->global->p2p_group_formation = group_wpa_s; |
| 1315 | |
| 1316 | wpas_p2p_clone_config(group_wpa_s, wpa_s); |
| 1317 | |
| 1318 | return group_wpa_s; |
| 1319 | } |
| 1320 | |
| 1321 | |
| 1322 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, |
| 1323 | void *timeout_ctx) |
| 1324 | { |
| 1325 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 1326 | wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out"); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1327 | wpas_p2p_group_formation_failed(wpa_s); |
| 1328 | } |
| 1329 | |
| 1330 | |
| 1331 | void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s) |
| 1332 | { |
| 1333 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1334 | wpa_s->parent, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1335 | if (wpa_s->global->p2p) |
| 1336 | p2p_group_formation_failed(wpa_s->global->p2p); |
| 1337 | else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 1338 | wpa_drv_p2p_group_formation_failed(wpa_s); |
| 1339 | wpas_group_formation_completed(wpa_s, 0); |
| 1340 | } |
| 1341 | |
| 1342 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1343 | void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s) |
| 1344 | { |
| 1345 | if (wpa_s->global->p2p_group_formation != wpa_s) |
| 1346 | return; |
| 1347 | /* Speed up group formation timeout since this cannot succeed */ |
| 1348 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1349 | wpa_s->parent, NULL); |
| 1350 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 1351 | wpa_s->parent, NULL); |
| 1352 | } |
| 1353 | |
| 1354 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1355 | void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res) |
| 1356 | { |
| 1357 | struct wpa_supplicant *wpa_s = ctx; |
| 1358 | |
| 1359 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 1360 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 1361 | wpa_s->off_channel_freq = 0; |
| 1362 | wpa_s->roc_waiting_drv_freq = 0; |
| 1363 | } |
| 1364 | |
| 1365 | if (res->status) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1366 | wpa_msg_global(wpa_s, MSG_INFO, |
| 1367 | P2P_EVENT_GO_NEG_FAILURE "status=%d", |
| 1368 | res->status); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1369 | wpas_notify_p2p_go_neg_completed(wpa_s, res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1370 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 1371 | return; |
| 1372 | } |
| 1373 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1374 | if (wpa_s->p2p_go_ht40) |
| 1375 | res->ht40 = 1; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1376 | if (wpa_s->p2p_go_vht) |
| 1377 | res->vht = 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1378 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 1379 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s " |
| 1380 | "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR |
| 1381 | " wps_method=%s", |
| 1382 | res->role_go ? "GO" : "client", res->freq, res->ht40, |
| 1383 | MAC2STR(res->peer_device_addr), |
| 1384 | MAC2STR(res->peer_interface_addr), |
| 1385 | p2p_wps_method_text(res->wps_method)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1386 | wpas_notify_p2p_go_neg_completed(wpa_s, res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1387 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1388 | if (res->role_go && wpa_s->p2p_persistent_id >= 0) { |
| 1389 | struct wpa_ssid *ssid; |
| 1390 | ssid = wpa_config_get_network(wpa_s->conf, |
| 1391 | wpa_s->p2p_persistent_id); |
| 1392 | if (ssid && ssid->disabled == 2 && |
| 1393 | ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) { |
| 1394 | size_t len = os_strlen(ssid->passphrase); |
| 1395 | wpa_printf(MSG_DEBUG, "P2P: Override passphrase based " |
| 1396 | "on requested persistent group"); |
| 1397 | os_memcpy(res->passphrase, ssid->passphrase, len); |
| 1398 | res->passphrase[len] = '\0'; |
| 1399 | } |
| 1400 | } |
| 1401 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1402 | if (wpa_s->create_p2p_iface) { |
| 1403 | struct wpa_supplicant *group_wpa_s = |
| 1404 | wpas_p2p_init_group_interface(wpa_s, res->role_go); |
| 1405 | if (group_wpa_s == NULL) { |
| 1406 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 1407 | return; |
| 1408 | } |
| 1409 | if (group_wpa_s != wpa_s) { |
| 1410 | os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin, |
| 1411 | sizeof(group_wpa_s->p2p_pin)); |
| 1412 | group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method; |
| 1413 | } |
| 1414 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); |
| 1415 | wpa_s->pending_interface_name[0] = '\0'; |
| 1416 | group_wpa_s->p2p_in_provisioning = 1; |
| 1417 | |
| 1418 | if (res->role_go) |
| 1419 | wpas_start_wps_go(group_wpa_s, res, 1); |
| 1420 | else |
| 1421 | wpas_start_wps_enrollee(group_wpa_s, res); |
| 1422 | } else { |
| 1423 | wpa_s->p2p_in_provisioning = 1; |
| 1424 | wpa_s->global->p2p_group_formation = wpa_s; |
| 1425 | |
| 1426 | if (res->role_go) |
| 1427 | wpas_start_wps_go(wpa_s, res, 1); |
| 1428 | else |
| 1429 | wpas_start_wps_enrollee(ctx, res); |
| 1430 | } |
| 1431 | |
| 1432 | wpa_s->p2p_long_listen = 0; |
| 1433 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 1434 | |
| 1435 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 1436 | eloop_register_timeout(15 + res->peer_config_timeout / 100, |
| 1437 | (res->peer_config_timeout % 100) * 10000, |
| 1438 | wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 1439 | } |
| 1440 | |
| 1441 | |
| 1442 | void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id) |
| 1443 | { |
| 1444 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1445 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR |
| 1446 | " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1447 | |
| 1448 | wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id); |
| 1449 | } |
| 1450 | |
| 1451 | |
| 1452 | void wpas_dev_found(void *ctx, const u8 *addr, |
| 1453 | const struct p2p_peer_info *info, |
| 1454 | int new_device) |
| 1455 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1456 | #ifndef CONFIG_NO_STDOUT_DEBUG |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1457 | struct wpa_supplicant *wpa_s = ctx; |
| 1458 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
Irfan Sheriff | 8367dc9 | 2012-09-09 17:08:19 -0700 | [diff] [blame] | 1459 | #define WFD_DEV_INFO_SIZE 9 |
| 1460 | char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1]; |
Irfan Sheriff | 8d96518 | 2012-09-11 08:58:24 -0700 | [diff] [blame] | 1461 | os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex)); |
Irfan Sheriff | 8367dc9 | 2012-09-09 17:08:19 -0700 | [diff] [blame] | 1462 | #ifdef CONFIG_WIFI_DISPLAY |
| 1463 | if (info->wfd_subelems) { |
| 1464 | wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex), |
| 1465 | wpabuf_head(info->wfd_subelems), |
| 1466 | WFD_DEV_INFO_SIZE); |
| 1467 | } |
| 1468 | #endif /* CONFIG_WIFI_DISPLAY */ |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1469 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR |
| 1470 | " p2p_dev_addr=" MACSTR |
| 1471 | " pri_dev_type=%s name='%s' config_methods=0x%x " |
| 1472 | "dev_capab=0x%x group_capab=0x%x%s%s", |
| 1473 | MAC2STR(addr), MAC2STR(info->p2p_device_addr), |
| 1474 | wps_dev_type_bin2str(info->pri_dev_type, devtype, |
| 1475 | sizeof(devtype)), |
| 1476 | info->device_name, info->config_methods, |
| 1477 | info->dev_capab, info->group_capab, |
| 1478 | wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1479 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1480 | |
| 1481 | wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device); |
| 1482 | } |
| 1483 | |
| 1484 | |
| 1485 | static void wpas_dev_lost(void *ctx, const u8 *dev_addr) |
| 1486 | { |
| 1487 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1488 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1489 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST |
| 1490 | "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1491 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1492 | wpas_notify_p2p_device_lost(wpa_s, dev_addr); |
| 1493 | } |
| 1494 | |
| 1495 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1496 | static void wpas_find_stopped(void *ctx) |
| 1497 | { |
| 1498 | struct wpa_supplicant *wpa_s = ctx; |
| 1499 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED); |
| 1500 | } |
| 1501 | |
| 1502 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1503 | static int wpas_start_listen(void *ctx, unsigned int freq, |
| 1504 | unsigned int duration, |
| 1505 | const struct wpabuf *probe_resp_ie) |
| 1506 | { |
| 1507 | struct wpa_supplicant *wpa_s = ctx; |
| 1508 | |
| 1509 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL); |
| 1510 | |
| 1511 | if (wpa_drv_probe_req_report(wpa_s, 1) < 0) { |
| 1512 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to " |
| 1513 | "report received Probe Request frames"); |
| 1514 | return -1; |
| 1515 | } |
| 1516 | |
| 1517 | wpa_s->pending_listen_freq = freq; |
| 1518 | wpa_s->pending_listen_duration = duration; |
| 1519 | |
| 1520 | if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) { |
| 1521 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver " |
| 1522 | "to remain on channel (%u MHz) for Listen " |
| 1523 | "state", freq); |
| 1524 | wpa_s->pending_listen_freq = 0; |
| 1525 | return -1; |
| 1526 | } |
| 1527 | wpa_s->off_channel_freq = 0; |
| 1528 | wpa_s->roc_waiting_drv_freq = freq; |
| 1529 | |
| 1530 | return 0; |
| 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | static void wpas_stop_listen(void *ctx) |
| 1535 | { |
| 1536 | struct wpa_supplicant *wpa_s = ctx; |
| 1537 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 1538 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 1539 | wpa_s->off_channel_freq = 0; |
| 1540 | wpa_s->roc_waiting_drv_freq = 0; |
| 1541 | } |
| 1542 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL); |
| 1543 | wpa_drv_probe_req_report(wpa_s, 0); |
| 1544 | } |
| 1545 | |
| 1546 | |
| 1547 | static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf) |
| 1548 | { |
| 1549 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1550 | 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] | 1551 | } |
| 1552 | |
| 1553 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1554 | /* |
| 1555 | * DNS Header section is used only to calculate compression pointers, so the |
| 1556 | * contents of this data does not matter, but the length needs to be reserved |
| 1557 | * in the virtual packet. |
| 1558 | */ |
| 1559 | #define DNS_HEADER_LEN 12 |
| 1560 | |
| 1561 | /* |
| 1562 | * 27-octet in-memory packet from P2P specification containing two implied |
| 1563 | * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN |
| 1564 | */ |
| 1565 | #define P2P_SD_IN_MEMORY_LEN 27 |
| 1566 | |
| 1567 | static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start, |
| 1568 | u8 **spos, const u8 *end) |
| 1569 | { |
| 1570 | while (*spos < end) { |
| 1571 | u8 val = ((*spos)[0] & 0xc0) >> 6; |
| 1572 | int len; |
| 1573 | |
| 1574 | if (val == 1 || val == 2) { |
| 1575 | /* These are reserved values in RFC 1035 */ |
| 1576 | wpa_printf(MSG_DEBUG, "P2P: Invalid domain name " |
| 1577 | "sequence starting with 0x%x", val); |
| 1578 | return -1; |
| 1579 | } |
| 1580 | |
| 1581 | if (val == 3) { |
| 1582 | u16 offset; |
| 1583 | u8 *spos_tmp; |
| 1584 | |
| 1585 | /* Offset */ |
| 1586 | if (*spos + 2 > end) { |
| 1587 | wpa_printf(MSG_DEBUG, "P2P: No room for full " |
| 1588 | "DNS offset field"); |
| 1589 | return -1; |
| 1590 | } |
| 1591 | |
| 1592 | offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1]; |
| 1593 | if (offset >= *spos - start) { |
| 1594 | wpa_printf(MSG_DEBUG, "P2P: Invalid DNS " |
| 1595 | "pointer offset %u", offset); |
| 1596 | return -1; |
| 1597 | } |
| 1598 | |
| 1599 | (*spos) += 2; |
| 1600 | spos_tmp = start + offset; |
| 1601 | return p2p_sd_dns_uncompress_label(upos, uend, start, |
| 1602 | &spos_tmp, |
| 1603 | *spos - 2); |
| 1604 | } |
| 1605 | |
| 1606 | /* Label */ |
| 1607 | len = (*spos)[0] & 0x3f; |
| 1608 | if (len == 0) |
| 1609 | return 0; |
| 1610 | |
| 1611 | (*spos)++; |
| 1612 | if (*spos + len > end) { |
| 1613 | wpa_printf(MSG_DEBUG, "P2P: Invalid domain name " |
| 1614 | "sequence - no room for label with length " |
| 1615 | "%u", len); |
| 1616 | return -1; |
| 1617 | } |
| 1618 | |
| 1619 | if (*upos + len + 2 > uend) |
| 1620 | return -2; |
| 1621 | |
| 1622 | os_memcpy(*upos, *spos, len); |
| 1623 | *spos += len; |
| 1624 | *upos += len; |
| 1625 | (*upos)[0] = '.'; |
| 1626 | (*upos)++; |
| 1627 | (*upos)[0] = '\0'; |
| 1628 | } |
| 1629 | |
| 1630 | return 0; |
| 1631 | } |
| 1632 | |
| 1633 | |
| 1634 | /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet. |
| 1635 | * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is |
| 1636 | * not large enough */ |
| 1637 | static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg, |
| 1638 | size_t msg_len, size_t offset) |
| 1639 | { |
| 1640 | /* 27-octet in-memory packet from P2P specification */ |
| 1641 | const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01" |
| 1642 | "\x04_udp\xC0\x11\x00\x0C\x00\x01"; |
| 1643 | u8 *tmp, *end, *spos; |
| 1644 | char *upos, *uend; |
| 1645 | int ret = 0; |
| 1646 | |
| 1647 | if (buf_len < 2) |
| 1648 | return -1; |
| 1649 | if (offset > msg_len) |
| 1650 | return -1; |
| 1651 | |
| 1652 | tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len); |
| 1653 | if (tmp == NULL) |
| 1654 | return -1; |
| 1655 | spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN; |
| 1656 | end = spos + msg_len; |
| 1657 | spos += offset; |
| 1658 | |
| 1659 | os_memset(tmp, 0, DNS_HEADER_LEN); |
| 1660 | os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN); |
| 1661 | os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len); |
| 1662 | |
| 1663 | upos = buf; |
| 1664 | uend = buf + buf_len; |
| 1665 | |
| 1666 | ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end); |
| 1667 | if (ret) { |
| 1668 | os_free(tmp); |
| 1669 | return ret; |
| 1670 | } |
| 1671 | |
| 1672 | if (upos == buf) { |
| 1673 | upos[0] = '.'; |
| 1674 | upos[1] = '\0'; |
| 1675 | } else if (upos[-1] == '.') |
| 1676 | upos[-1] = '\0'; |
| 1677 | |
| 1678 | os_free(tmp); |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1683 | static struct p2p_srv_bonjour * |
| 1684 | wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s, |
| 1685 | const struct wpabuf *query) |
| 1686 | { |
| 1687 | struct p2p_srv_bonjour *bsrv; |
| 1688 | size_t len; |
| 1689 | |
| 1690 | len = wpabuf_len(query); |
| 1691 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, |
| 1692 | struct p2p_srv_bonjour, list) { |
| 1693 | if (len == wpabuf_len(bsrv->query) && |
| 1694 | os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query), |
| 1695 | len) == 0) |
| 1696 | return bsrv; |
| 1697 | } |
| 1698 | return NULL; |
| 1699 | } |
| 1700 | |
| 1701 | |
| 1702 | static struct p2p_srv_upnp * |
| 1703 | wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version, |
| 1704 | const char *service) |
| 1705 | { |
| 1706 | struct p2p_srv_upnp *usrv; |
| 1707 | |
| 1708 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
| 1709 | struct p2p_srv_upnp, list) { |
| 1710 | if (version == usrv->version && |
| 1711 | os_strcmp(service, usrv->service) == 0) |
| 1712 | return usrv; |
| 1713 | } |
| 1714 | return NULL; |
| 1715 | } |
| 1716 | |
| 1717 | |
| 1718 | static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto, |
| 1719 | u8 srv_trans_id) |
| 1720 | { |
| 1721 | u8 *len_pos; |
| 1722 | |
| 1723 | if (wpabuf_tailroom(resp) < 5) |
| 1724 | return; |
| 1725 | |
| 1726 | /* Length (to be filled) */ |
| 1727 | len_pos = wpabuf_put(resp, 2); |
| 1728 | wpabuf_put_u8(resp, srv_proto); |
| 1729 | wpabuf_put_u8(resp, srv_trans_id); |
| 1730 | /* Status Code */ |
| 1731 | wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE); |
| 1732 | /* Response Data: empty */ |
| 1733 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 1734 | } |
| 1735 | |
| 1736 | |
| 1737 | static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s, |
| 1738 | struct wpabuf *resp, u8 srv_trans_id) |
| 1739 | { |
| 1740 | struct p2p_srv_bonjour *bsrv; |
| 1741 | u8 *len_pos; |
| 1742 | |
| 1743 | wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services"); |
| 1744 | |
| 1745 | if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { |
| 1746 | wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available"); |
| 1747 | return; |
| 1748 | } |
| 1749 | |
| 1750 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, |
| 1751 | struct p2p_srv_bonjour, list) { |
| 1752 | if (wpabuf_tailroom(resp) < |
| 1753 | 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) |
| 1754 | return; |
| 1755 | /* Length (to be filled) */ |
| 1756 | len_pos = wpabuf_put(resp, 2); |
| 1757 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); |
| 1758 | wpabuf_put_u8(resp, srv_trans_id); |
| 1759 | /* Status Code */ |
| 1760 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 1761 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service", |
| 1762 | wpabuf_head(bsrv->resp), |
| 1763 | wpabuf_len(bsrv->resp)); |
| 1764 | /* Response Data */ |
| 1765 | wpabuf_put_buf(resp, bsrv->query); /* Key */ |
| 1766 | wpabuf_put_buf(resp, bsrv->resp); /* Value */ |
| 1767 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - |
| 1768 | 2); |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1773 | static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query, |
| 1774 | size_t query_len) |
| 1775 | { |
| 1776 | char str_rx[256], str_srv[256]; |
| 1777 | |
| 1778 | if (query_len < 3 || wpabuf_len(bsrv->query) < 3) |
| 1779 | return 0; /* Too short to include DNS Type and Version */ |
| 1780 | if (os_memcmp(query + query_len - 3, |
| 1781 | wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3, |
| 1782 | 3) != 0) |
| 1783 | return 0; /* Mismatch in DNS Type or Version */ |
| 1784 | if (query_len == wpabuf_len(bsrv->query) && |
| 1785 | os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0) |
| 1786 | return 1; /* Binary match */ |
| 1787 | |
| 1788 | if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3, |
| 1789 | 0)) |
| 1790 | return 0; /* Failed to uncompress query */ |
| 1791 | if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv), |
| 1792 | wpabuf_head(bsrv->query), |
| 1793 | wpabuf_len(bsrv->query) - 3, 0)) |
| 1794 | return 0; /* Failed to uncompress service */ |
| 1795 | |
| 1796 | return os_strcmp(str_rx, str_srv) == 0; |
| 1797 | } |
| 1798 | |
| 1799 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1800 | static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s, |
| 1801 | struct wpabuf *resp, u8 srv_trans_id, |
| 1802 | const u8 *query, size_t query_len) |
| 1803 | { |
| 1804 | struct p2p_srv_bonjour *bsrv; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1805 | u8 *len_pos; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1806 | int matches = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1807 | |
| 1808 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour", |
| 1809 | query, query_len); |
| 1810 | if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { |
| 1811 | wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available"); |
| 1812 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR, |
| 1813 | srv_trans_id); |
| 1814 | return; |
| 1815 | } |
| 1816 | |
| 1817 | if (query_len == 0) { |
| 1818 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); |
| 1819 | return; |
| 1820 | } |
| 1821 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1822 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, |
| 1823 | struct p2p_srv_bonjour, list) { |
| 1824 | if (!match_bonjour_query(bsrv, query, query_len)) |
| 1825 | continue; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1826 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1827 | if (wpabuf_tailroom(resp) < |
| 1828 | 5 + query_len + wpabuf_len(bsrv->resp)) |
| 1829 | return; |
| 1830 | |
| 1831 | matches++; |
| 1832 | |
| 1833 | /* Length (to be filled) */ |
| 1834 | len_pos = wpabuf_put(resp, 2); |
| 1835 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); |
| 1836 | wpabuf_put_u8(resp, srv_trans_id); |
| 1837 | |
| 1838 | /* Status Code */ |
| 1839 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 1840 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service", |
| 1841 | wpabuf_head(bsrv->resp), |
| 1842 | wpabuf_len(bsrv->resp)); |
| 1843 | |
| 1844 | /* Response Data */ |
| 1845 | wpabuf_put_data(resp, query, query_len); /* Key */ |
| 1846 | wpabuf_put_buf(resp, bsrv->resp); /* Value */ |
| 1847 | |
| 1848 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 1849 | } |
| 1850 | |
| 1851 | if (matches == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1852 | wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not " |
| 1853 | "available"); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1854 | if (wpabuf_tailroom(resp) < 5) |
| 1855 | return; |
| 1856 | |
| 1857 | /* Length (to be filled) */ |
| 1858 | len_pos = wpabuf_put(resp, 2); |
| 1859 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); |
| 1860 | wpabuf_put_u8(resp, srv_trans_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1861 | |
| 1862 | /* Status Code */ |
| 1863 | wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); |
| 1864 | /* Response Data: empty */ |
| 1865 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - |
| 1866 | 2); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1867 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | |
| 1871 | static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s, |
| 1872 | struct wpabuf *resp, u8 srv_trans_id) |
| 1873 | { |
| 1874 | struct p2p_srv_upnp *usrv; |
| 1875 | u8 *len_pos; |
| 1876 | |
| 1877 | wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services"); |
| 1878 | |
| 1879 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) { |
| 1880 | wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available"); |
| 1881 | return; |
| 1882 | } |
| 1883 | |
| 1884 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
| 1885 | struct p2p_srv_upnp, list) { |
| 1886 | if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service)) |
| 1887 | return; |
| 1888 | |
| 1889 | /* Length (to be filled) */ |
| 1890 | len_pos = wpabuf_put(resp, 2); |
| 1891 | wpabuf_put_u8(resp, P2P_SERV_UPNP); |
| 1892 | wpabuf_put_u8(resp, srv_trans_id); |
| 1893 | |
| 1894 | /* Status Code */ |
| 1895 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 1896 | /* Response Data */ |
| 1897 | wpabuf_put_u8(resp, usrv->version); |
| 1898 | wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s", |
| 1899 | usrv->service); |
| 1900 | wpabuf_put_str(resp, usrv->service); |
| 1901 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - |
| 1902 | 2); |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | |
| 1907 | static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s, |
| 1908 | struct wpabuf *resp, u8 srv_trans_id, |
| 1909 | const u8 *query, size_t query_len) |
| 1910 | { |
| 1911 | struct p2p_srv_upnp *usrv; |
| 1912 | u8 *len_pos; |
| 1913 | u8 version; |
| 1914 | char *str; |
| 1915 | int count = 0; |
| 1916 | |
| 1917 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP", |
| 1918 | query, query_len); |
| 1919 | |
| 1920 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) { |
| 1921 | wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available"); |
| 1922 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP, |
| 1923 | srv_trans_id); |
| 1924 | return; |
| 1925 | } |
| 1926 | |
| 1927 | if (query_len == 0) { |
| 1928 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); |
| 1929 | return; |
| 1930 | } |
| 1931 | |
| 1932 | if (wpabuf_tailroom(resp) < 5) |
| 1933 | return; |
| 1934 | |
| 1935 | /* Length (to be filled) */ |
| 1936 | len_pos = wpabuf_put(resp, 2); |
| 1937 | wpabuf_put_u8(resp, P2P_SERV_UPNP); |
| 1938 | wpabuf_put_u8(resp, srv_trans_id); |
| 1939 | |
| 1940 | version = query[0]; |
| 1941 | str = os_malloc(query_len); |
| 1942 | if (str == NULL) |
| 1943 | return; |
| 1944 | os_memcpy(str, query + 1, query_len - 1); |
| 1945 | str[query_len - 1] = '\0'; |
| 1946 | |
| 1947 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
| 1948 | struct p2p_srv_upnp, list) { |
| 1949 | if (version != usrv->version) |
| 1950 | continue; |
| 1951 | |
| 1952 | if (os_strcmp(str, "ssdp:all") != 0 && |
| 1953 | os_strstr(usrv->service, str) == NULL) |
| 1954 | continue; |
| 1955 | |
| 1956 | if (wpabuf_tailroom(resp) < 2) |
| 1957 | break; |
| 1958 | if (count == 0) { |
| 1959 | /* Status Code */ |
| 1960 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 1961 | /* Response Data */ |
| 1962 | wpabuf_put_u8(resp, version); |
| 1963 | } else |
| 1964 | wpabuf_put_u8(resp, ','); |
| 1965 | |
| 1966 | count++; |
| 1967 | |
| 1968 | wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s", |
| 1969 | usrv->service); |
| 1970 | if (wpabuf_tailroom(resp) < os_strlen(usrv->service)) |
| 1971 | break; |
| 1972 | wpabuf_put_str(resp, usrv->service); |
| 1973 | } |
| 1974 | os_free(str); |
| 1975 | |
| 1976 | if (count == 0) { |
| 1977 | wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not " |
| 1978 | "available"); |
| 1979 | /* Status Code */ |
| 1980 | wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); |
| 1981 | /* Response Data: empty */ |
| 1982 | } |
| 1983 | |
| 1984 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 1985 | } |
| 1986 | |
| 1987 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1988 | #ifdef CONFIG_WIFI_DISPLAY |
| 1989 | static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s, |
| 1990 | struct wpabuf *resp, u8 srv_trans_id, |
| 1991 | const u8 *query, size_t query_len) |
| 1992 | { |
| 1993 | const u8 *pos; |
| 1994 | u8 role; |
| 1995 | u8 *len_pos; |
| 1996 | |
| 1997 | wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len); |
| 1998 | |
| 1999 | if (!wpa_s->global->wifi_display) { |
| 2000 | wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available"); |
| 2001 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY, |
| 2002 | srv_trans_id); |
| 2003 | return; |
| 2004 | } |
| 2005 | |
| 2006 | if (query_len < 1) { |
| 2007 | wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device " |
| 2008 | "Role"); |
| 2009 | return; |
| 2010 | } |
| 2011 | |
| 2012 | if (wpabuf_tailroom(resp) < 5) |
| 2013 | return; |
| 2014 | |
| 2015 | pos = query; |
| 2016 | role = *pos++; |
| 2017 | wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role); |
| 2018 | |
| 2019 | /* TODO: role specific handling */ |
| 2020 | |
| 2021 | /* Length (to be filled) */ |
| 2022 | len_pos = wpabuf_put(resp, 2); |
| 2023 | wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY); |
| 2024 | wpabuf_put_u8(resp, srv_trans_id); |
| 2025 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */ |
| 2026 | |
| 2027 | while (pos < query + query_len) { |
| 2028 | if (*pos < MAX_WFD_SUBELEMS && |
| 2029 | wpa_s->global->wfd_subelem[*pos] && |
| 2030 | wpabuf_tailroom(resp) >= |
| 2031 | wpabuf_len(wpa_s->global->wfd_subelem[*pos])) { |
| 2032 | wpa_printf(MSG_DEBUG, "P2P: Add WSD response " |
| 2033 | "subelement %u", *pos); |
| 2034 | wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]); |
| 2035 | } |
| 2036 | pos++; |
| 2037 | } |
| 2038 | |
| 2039 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 2040 | } |
| 2041 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 2042 | |
| 2043 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2044 | void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token, |
| 2045 | u16 update_indic, const u8 *tlvs, size_t tlvs_len) |
| 2046 | { |
| 2047 | struct wpa_supplicant *wpa_s = ctx; |
| 2048 | const u8 *pos = tlvs; |
| 2049 | const u8 *end = tlvs + tlvs_len; |
| 2050 | const u8 *tlv_end; |
| 2051 | u16 slen; |
| 2052 | struct wpabuf *resp; |
| 2053 | u8 srv_proto, srv_trans_id; |
| 2054 | size_t buf_len; |
| 2055 | char *buf; |
| 2056 | |
| 2057 | wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs", |
| 2058 | tlvs, tlvs_len); |
| 2059 | buf_len = 2 * tlvs_len + 1; |
| 2060 | buf = os_malloc(buf_len); |
| 2061 | if (buf) { |
| 2062 | wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); |
| 2063 | wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d " |
| 2064 | MACSTR " %u %u %s", |
| 2065 | freq, MAC2STR(sa), dialog_token, update_indic, |
| 2066 | buf); |
| 2067 | os_free(buf); |
| 2068 | } |
| 2069 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2070 | if (wpa_s->p2p_sd_over_ctrl_iface) { |
| 2071 | wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token, |
| 2072 | update_indic, tlvs, tlvs_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2073 | return; /* to be processed by an external program */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2074 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2075 | |
| 2076 | resp = wpabuf_alloc(10000); |
| 2077 | if (resp == NULL) |
| 2078 | return; |
| 2079 | |
| 2080 | while (pos + 1 < end) { |
| 2081 | wpa_printf(MSG_DEBUG, "P2P: Service Request TLV"); |
| 2082 | slen = WPA_GET_LE16(pos); |
| 2083 | pos += 2; |
| 2084 | if (pos + slen > end || slen < 2) { |
| 2085 | wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data " |
| 2086 | "length"); |
| 2087 | wpabuf_free(resp); |
| 2088 | return; |
| 2089 | } |
| 2090 | tlv_end = pos + slen; |
| 2091 | |
| 2092 | srv_proto = *pos++; |
| 2093 | wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", |
| 2094 | srv_proto); |
| 2095 | srv_trans_id = *pos++; |
| 2096 | wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", |
| 2097 | srv_trans_id); |
| 2098 | |
| 2099 | wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data", |
| 2100 | pos, tlv_end - pos); |
| 2101 | |
| 2102 | |
| 2103 | if (wpa_s->force_long_sd) { |
| 2104 | wpa_printf(MSG_DEBUG, "P2P: SD test - force long " |
| 2105 | "response"); |
| 2106 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); |
| 2107 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); |
| 2108 | goto done; |
| 2109 | } |
| 2110 | |
| 2111 | switch (srv_proto) { |
| 2112 | case P2P_SERV_ALL_SERVICES: |
| 2113 | wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request " |
| 2114 | "for all services"); |
| 2115 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) && |
| 2116 | dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { |
| 2117 | wpa_printf(MSG_DEBUG, "P2P: No service " |
| 2118 | "discovery protocols available"); |
| 2119 | wpas_sd_add_proto_not_avail( |
| 2120 | resp, P2P_SERV_ALL_SERVICES, |
| 2121 | srv_trans_id); |
| 2122 | break; |
| 2123 | } |
| 2124 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); |
| 2125 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); |
| 2126 | break; |
| 2127 | case P2P_SERV_BONJOUR: |
| 2128 | wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id, |
| 2129 | pos, tlv_end - pos); |
| 2130 | break; |
| 2131 | case P2P_SERV_UPNP: |
| 2132 | wpas_sd_req_upnp(wpa_s, resp, srv_trans_id, |
| 2133 | pos, tlv_end - pos); |
| 2134 | break; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2135 | #ifdef CONFIG_WIFI_DISPLAY |
| 2136 | case P2P_SERV_WIFI_DISPLAY: |
| 2137 | wpas_sd_req_wfd(wpa_s, resp, srv_trans_id, |
| 2138 | pos, tlv_end - pos); |
| 2139 | break; |
| 2140 | #endif /* CONFIG_WIFI_DISPLAY */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2141 | default: |
| 2142 | wpa_printf(MSG_DEBUG, "P2P: Unavailable service " |
| 2143 | "protocol %u", srv_proto); |
| 2144 | wpas_sd_add_proto_not_avail(resp, srv_proto, |
| 2145 | srv_trans_id); |
| 2146 | break; |
| 2147 | } |
| 2148 | |
| 2149 | pos = tlv_end; |
| 2150 | } |
| 2151 | |
| 2152 | done: |
| 2153 | wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token, |
| 2154 | update_indic, tlvs, tlvs_len); |
| 2155 | |
| 2156 | wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp); |
| 2157 | |
| 2158 | wpabuf_free(resp); |
| 2159 | } |
| 2160 | |
| 2161 | |
| 2162 | void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic, |
| 2163 | const u8 *tlvs, size_t tlvs_len) |
| 2164 | { |
| 2165 | struct wpa_supplicant *wpa_s = ctx; |
| 2166 | const u8 *pos = tlvs; |
| 2167 | const u8 *end = tlvs + tlvs_len; |
| 2168 | const u8 *tlv_end; |
| 2169 | u16 slen; |
| 2170 | size_t buf_len; |
| 2171 | char *buf; |
| 2172 | |
| 2173 | wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs", |
| 2174 | tlvs, tlvs_len); |
| 2175 | if (tlvs_len > 1500) { |
| 2176 | /* TODO: better way for handling this */ |
| 2177 | wpa_msg_ctrl(wpa_s, MSG_INFO, |
| 2178 | P2P_EVENT_SERV_DISC_RESP MACSTR |
| 2179 | " %u <long response: %u bytes>", |
| 2180 | MAC2STR(sa), update_indic, |
| 2181 | (unsigned int) tlvs_len); |
| 2182 | } else { |
| 2183 | buf_len = 2 * tlvs_len + 1; |
| 2184 | buf = os_malloc(buf_len); |
| 2185 | if (buf) { |
| 2186 | wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); |
| 2187 | wpa_msg_ctrl(wpa_s, MSG_INFO, |
| 2188 | P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s", |
| 2189 | MAC2STR(sa), update_indic, buf); |
| 2190 | os_free(buf); |
| 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | while (pos < end) { |
| 2195 | u8 srv_proto, srv_trans_id, status; |
| 2196 | |
| 2197 | wpa_printf(MSG_DEBUG, "P2P: Service Response TLV"); |
| 2198 | slen = WPA_GET_LE16(pos); |
| 2199 | pos += 2; |
| 2200 | if (pos + slen > end || slen < 3) { |
| 2201 | wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data " |
| 2202 | "length"); |
| 2203 | return; |
| 2204 | } |
| 2205 | tlv_end = pos + slen; |
| 2206 | |
| 2207 | srv_proto = *pos++; |
| 2208 | wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", |
| 2209 | srv_proto); |
| 2210 | srv_trans_id = *pos++; |
| 2211 | wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", |
| 2212 | srv_trans_id); |
| 2213 | status = *pos++; |
| 2214 | wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u", |
| 2215 | status); |
| 2216 | |
| 2217 | wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data", |
| 2218 | pos, tlv_end - pos); |
| 2219 | |
| 2220 | pos = tlv_end; |
| 2221 | } |
| 2222 | |
| 2223 | wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len); |
| 2224 | } |
| 2225 | |
| 2226 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2227 | u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 2228 | const struct wpabuf *tlvs) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2229 | { |
| 2230 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2231 | return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2232 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2233 | return 0; |
| 2234 | return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2238 | u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 2239 | u8 version, const char *query) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2240 | { |
| 2241 | struct wpabuf *tlvs; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2242 | u64 ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2243 | |
| 2244 | tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query)); |
| 2245 | if (tlvs == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2246 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2247 | wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query)); |
| 2248 | wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */ |
| 2249 | wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */ |
| 2250 | wpabuf_put_u8(tlvs, version); |
| 2251 | wpabuf_put_str(tlvs, query); |
| 2252 | ret = wpas_p2p_sd_request(wpa_s, dst, tlvs); |
| 2253 | wpabuf_free(tlvs); |
| 2254 | return ret; |
| 2255 | } |
| 2256 | |
| 2257 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2258 | #ifdef CONFIG_WIFI_DISPLAY |
| 2259 | |
| 2260 | static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 2261 | const struct wpabuf *tlvs) |
| 2262 | { |
| 2263 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 2264 | return 0; |
| 2265 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 2266 | return 0; |
| 2267 | return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs); |
| 2268 | } |
| 2269 | |
| 2270 | |
| 2271 | #define MAX_WFD_SD_SUBELEMS 20 |
| 2272 | |
| 2273 | static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role, |
| 2274 | const char *subelems) |
| 2275 | { |
| 2276 | u8 *len; |
| 2277 | const char *pos; |
| 2278 | int val; |
| 2279 | int count = 0; |
| 2280 | |
| 2281 | len = wpabuf_put(tlvs, 2); |
| 2282 | wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */ |
| 2283 | wpabuf_put_u8(tlvs, id); /* Service Transaction ID */ |
| 2284 | |
| 2285 | wpabuf_put_u8(tlvs, role); |
| 2286 | |
| 2287 | pos = subelems; |
| 2288 | while (*pos) { |
| 2289 | val = atoi(pos); |
| 2290 | if (val >= 0 && val < 256) { |
| 2291 | wpabuf_put_u8(tlvs, val); |
| 2292 | count++; |
| 2293 | if (count == MAX_WFD_SD_SUBELEMS) |
| 2294 | break; |
| 2295 | } |
| 2296 | pos = os_strchr(pos + 1, ','); |
| 2297 | if (pos == NULL) |
| 2298 | break; |
| 2299 | pos++; |
| 2300 | } |
| 2301 | |
| 2302 | WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2); |
| 2303 | } |
| 2304 | |
| 2305 | |
| 2306 | u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s, |
| 2307 | const u8 *dst, const char *role) |
| 2308 | { |
| 2309 | struct wpabuf *tlvs; |
| 2310 | u64 ret; |
| 2311 | const char *subelems; |
| 2312 | u8 id = 1; |
| 2313 | |
| 2314 | subelems = os_strchr(role, ' '); |
| 2315 | if (subelems == NULL) |
| 2316 | return 0; |
| 2317 | subelems++; |
| 2318 | |
| 2319 | tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS)); |
| 2320 | if (tlvs == NULL) |
| 2321 | return 0; |
| 2322 | |
| 2323 | if (os_strstr(role, "[source]")) |
| 2324 | wfd_add_sd_req_role(tlvs, id++, 0x00, subelems); |
| 2325 | if (os_strstr(role, "[pri-sink]")) |
| 2326 | wfd_add_sd_req_role(tlvs, id++, 0x01, subelems); |
| 2327 | if (os_strstr(role, "[sec-sink]")) |
| 2328 | wfd_add_sd_req_role(tlvs, id++, 0x02, subelems); |
| 2329 | if (os_strstr(role, "[source+sink]")) |
| 2330 | wfd_add_sd_req_role(tlvs, id++, 0x03, subelems); |
| 2331 | |
| 2332 | ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs); |
| 2333 | wpabuf_free(tlvs); |
| 2334 | return ret; |
| 2335 | } |
| 2336 | |
| 2337 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 2338 | |
| 2339 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2340 | 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] | 2341 | { |
| 2342 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2343 | return wpa_drv_p2p_sd_cancel_request(wpa_s, req); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2344 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 2345 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2346 | return p2p_sd_cancel_request(wpa_s->global->p2p, |
| 2347 | (void *) (uintptr_t) req); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | |
| 2351 | void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq, |
| 2352 | const u8 *dst, u8 dialog_token, |
| 2353 | const struct wpabuf *resp_tlvs) |
| 2354 | { |
| 2355 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { |
| 2356 | wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token, |
| 2357 | resp_tlvs); |
| 2358 | return; |
| 2359 | } |
| 2360 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 2361 | return; |
| 2362 | p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token, |
| 2363 | resp_tlvs); |
| 2364 | } |
| 2365 | |
Dmitry Shmidt | eaf261d | 2013-08-14 15:30:08 -0700 | [diff] [blame] | 2366 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2367 | void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s) |
| 2368 | { |
| 2369 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { |
| 2370 | wpa_drv_p2p_service_update(wpa_s); |
| 2371 | return; |
| 2372 | } |
| 2373 | if (wpa_s->global->p2p) |
| 2374 | p2p_sd_service_update(wpa_s->global->p2p); |
| 2375 | } |
| 2376 | |
| 2377 | |
| 2378 | static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv) |
| 2379 | { |
| 2380 | dl_list_del(&bsrv->list); |
| 2381 | wpabuf_free(bsrv->query); |
| 2382 | wpabuf_free(bsrv->resp); |
| 2383 | os_free(bsrv); |
| 2384 | } |
| 2385 | |
| 2386 | |
| 2387 | static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv) |
| 2388 | { |
| 2389 | dl_list_del(&usrv->list); |
| 2390 | os_free(usrv->service); |
| 2391 | os_free(usrv); |
| 2392 | } |
| 2393 | |
| 2394 | |
| 2395 | void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s) |
| 2396 | { |
| 2397 | struct p2p_srv_bonjour *bsrv, *bn; |
| 2398 | struct p2p_srv_upnp *usrv, *un; |
| 2399 | |
| 2400 | dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour, |
| 2401 | struct p2p_srv_bonjour, list) |
| 2402 | wpas_p2p_srv_bonjour_free(bsrv); |
| 2403 | |
| 2404 | dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp, |
| 2405 | struct p2p_srv_upnp, list) |
| 2406 | wpas_p2p_srv_upnp_free(usrv); |
| 2407 | |
| 2408 | wpas_p2p_sd_service_update(wpa_s); |
| 2409 | } |
| 2410 | |
| 2411 | |
| 2412 | int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s, |
| 2413 | struct wpabuf *query, struct wpabuf *resp) |
| 2414 | { |
| 2415 | struct p2p_srv_bonjour *bsrv; |
| 2416 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2417 | bsrv = os_zalloc(sizeof(*bsrv)); |
| 2418 | if (bsrv == NULL) |
| 2419 | return -1; |
| 2420 | bsrv->query = query; |
| 2421 | bsrv->resp = resp; |
| 2422 | dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list); |
| 2423 | |
| 2424 | wpas_p2p_sd_service_update(wpa_s); |
| 2425 | return 0; |
| 2426 | } |
| 2427 | |
| 2428 | |
| 2429 | int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s, |
| 2430 | const struct wpabuf *query) |
| 2431 | { |
| 2432 | struct p2p_srv_bonjour *bsrv; |
| 2433 | |
| 2434 | bsrv = wpas_p2p_service_get_bonjour(wpa_s, query); |
| 2435 | if (bsrv == NULL) |
| 2436 | return -1; |
| 2437 | wpas_p2p_srv_bonjour_free(bsrv); |
| 2438 | wpas_p2p_sd_service_update(wpa_s); |
| 2439 | return 0; |
| 2440 | } |
| 2441 | |
| 2442 | |
| 2443 | int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version, |
| 2444 | const char *service) |
| 2445 | { |
| 2446 | struct p2p_srv_upnp *usrv; |
| 2447 | |
| 2448 | if (wpas_p2p_service_get_upnp(wpa_s, version, service)) |
| 2449 | return 0; /* Already listed */ |
| 2450 | usrv = os_zalloc(sizeof(*usrv)); |
| 2451 | if (usrv == NULL) |
| 2452 | return -1; |
| 2453 | usrv->version = version; |
| 2454 | usrv->service = os_strdup(service); |
| 2455 | if (usrv->service == NULL) { |
| 2456 | os_free(usrv); |
| 2457 | return -1; |
| 2458 | } |
| 2459 | dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list); |
| 2460 | |
| 2461 | wpas_p2p_sd_service_update(wpa_s); |
| 2462 | return 0; |
| 2463 | } |
| 2464 | |
| 2465 | |
| 2466 | int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version, |
| 2467 | const char *service) |
| 2468 | { |
| 2469 | struct p2p_srv_upnp *usrv; |
| 2470 | |
| 2471 | usrv = wpas_p2p_service_get_upnp(wpa_s, version, service); |
| 2472 | if (usrv == NULL) |
| 2473 | return -1; |
| 2474 | wpas_p2p_srv_upnp_free(usrv); |
| 2475 | wpas_p2p_sd_service_update(wpa_s); |
| 2476 | return 0; |
| 2477 | } |
| 2478 | |
| 2479 | |
| 2480 | static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s, |
| 2481 | const u8 *peer, const char *params, |
| 2482 | unsigned int generated_pin) |
| 2483 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2484 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR |
| 2485 | " %08d%s", MAC2STR(peer), generated_pin, params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | |
| 2489 | static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s, |
| 2490 | const u8 *peer, const char *params) |
| 2491 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2492 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR |
| 2493 | "%s", MAC2STR(peer), params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | |
| 2497 | void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods, |
| 2498 | const u8 *dev_addr, const u8 *pri_dev_type, |
| 2499 | const char *dev_name, u16 supp_config_methods, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2500 | u8 dev_capab, u8 group_capab, const u8 *group_id, |
| 2501 | size_t group_id_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2502 | { |
| 2503 | struct wpa_supplicant *wpa_s = ctx; |
| 2504 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2505 | char params[300]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2506 | u8 empty_dev_type[8]; |
| 2507 | unsigned int generated_pin = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2508 | struct wpa_supplicant *group = NULL; |
| 2509 | |
| 2510 | if (group_id) { |
| 2511 | for (group = wpa_s->global->ifaces; group; group = group->next) |
| 2512 | { |
| 2513 | struct wpa_ssid *s = group->current_ssid; |
| 2514 | if (s != NULL && |
| 2515 | s->mode == WPAS_MODE_P2P_GO && |
| 2516 | group_id_len - ETH_ALEN == s->ssid_len && |
| 2517 | os_memcmp(group_id + ETH_ALEN, s->ssid, |
| 2518 | s->ssid_len) == 0) |
| 2519 | break; |
| 2520 | } |
| 2521 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2522 | |
| 2523 | if (pri_dev_type == NULL) { |
| 2524 | os_memset(empty_dev_type, 0, sizeof(empty_dev_type)); |
| 2525 | pri_dev_type = empty_dev_type; |
| 2526 | } |
| 2527 | os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR |
| 2528 | " pri_dev_type=%s name='%s' config_methods=0x%x " |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2529 | "dev_capab=0x%x group_capab=0x%x%s%s", |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2530 | MAC2STR(dev_addr), |
| 2531 | wps_dev_type_bin2str(pri_dev_type, devtype, |
| 2532 | sizeof(devtype)), |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2533 | dev_name, supp_config_methods, dev_capab, group_capab, |
| 2534 | group ? " group=" : "", |
| 2535 | group ? group->ifname : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2536 | params[sizeof(params) - 1] = '\0'; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2537 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2538 | if (config_methods & WPS_CONFIG_DISPLAY) { |
| 2539 | generated_pin = wps_generate_pin(); |
| 2540 | wpas_prov_disc_local_display(wpa_s, peer, params, |
| 2541 | generated_pin); |
| 2542 | } else if (config_methods & WPS_CONFIG_KEYPAD) |
| 2543 | wpas_prov_disc_local_keypad(wpa_s, peer, params); |
| 2544 | else if (config_methods & WPS_CONFIG_PUSHBUTTON) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2545 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ |
| 2546 | MACSTR "%s", MAC2STR(peer), params); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2547 | |
| 2548 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */, |
| 2549 | P2P_PROV_DISC_SUCCESS, |
| 2550 | config_methods, generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | |
| 2554 | void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods) |
| 2555 | { |
| 2556 | struct wpa_supplicant *wpa_s = ctx; |
| 2557 | unsigned int generated_pin = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2558 | char params[20]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2559 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2560 | if (wpa_s->pending_pd_before_join && |
| 2561 | (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || |
| 2562 | os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { |
| 2563 | wpa_s->pending_pd_before_join = 0; |
| 2564 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " |
| 2565 | "join-existing-group operation"); |
| 2566 | wpas_p2p_join_start(wpa_s); |
| 2567 | return; |
| 2568 | } |
| 2569 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2570 | if (wpa_s->pending_pd_use == AUTO_PD_JOIN || |
| 2571 | wpa_s->pending_pd_use == AUTO_PD_GO_NEG) |
| 2572 | os_snprintf(params, sizeof(params), " peer_go=%d", |
| 2573 | wpa_s->pending_pd_use == AUTO_PD_JOIN); |
| 2574 | else |
| 2575 | params[0] = '\0'; |
| 2576 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2577 | if (config_methods & WPS_CONFIG_DISPLAY) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2578 | wpas_prov_disc_local_keypad(wpa_s, peer, params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2579 | else if (config_methods & WPS_CONFIG_KEYPAD) { |
| 2580 | generated_pin = wps_generate_pin(); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2581 | wpas_prov_disc_local_display(wpa_s, peer, params, |
| 2582 | generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2583 | } else if (config_methods & WPS_CONFIG_PUSHBUTTON) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2584 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP |
| 2585 | MACSTR "%s", MAC2STR(peer), params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2586 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2587 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, |
| 2588 | P2P_PROV_DISC_SUCCESS, |
| 2589 | config_methods, generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2593 | static void wpas_prov_disc_fail(void *ctx, const u8 *peer, |
| 2594 | enum p2p_prov_disc_status status) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2595 | { |
| 2596 | struct wpa_supplicant *wpa_s = ctx; |
| 2597 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2598 | if (wpa_s->p2p_fallback_to_go_neg) { |
| 2599 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto " |
| 2600 | "failed - fall back to GO Negotiation"); |
| 2601 | wpas_p2p_fallback_to_go_neg(wpa_s, 0); |
| 2602 | return; |
| 2603 | } |
| 2604 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2605 | if (status == P2P_PROV_DISC_TIMEOUT_JOIN) { |
Dmitry Shmidt | 2b38048 | 2012-09-13 10:52:13 -0700 | [diff] [blame] | 2606 | wpa_s->pending_pd_before_join = 0; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2607 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " |
| 2608 | "join-existing-group operation (no ACK for PD " |
| 2609 | "Req attempts)"); |
| 2610 | wpas_p2p_join_start(wpa_s); |
| 2611 | return; |
Dmitry Shmidt | 2b38048 | 2012-09-13 10:52:13 -0700 | [diff] [blame] | 2612 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2613 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2614 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE |
| 2615 | " p2p_dev_addr=" MACSTR " status=%d", |
| 2616 | MAC2STR(peer), status); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2617 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2618 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, |
| 2619 | status, 0, 0); |
| 2620 | } |
| 2621 | |
| 2622 | |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2623 | static int freq_included(const struct p2p_channels *channels, unsigned int freq) |
| 2624 | { |
| 2625 | if (channels == NULL) |
| 2626 | return 1; /* Assume no restrictions */ |
| 2627 | return p2p_channels_includes_freq(channels, freq); |
| 2628 | |
| 2629 | } |
| 2630 | |
| 2631 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2632 | static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid, |
| 2633 | const u8 *go_dev_addr, const u8 *ssid, |
| 2634 | size_t ssid_len, int *go, u8 *group_bssid, |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2635 | int *force_freq, int persistent_group, |
| 2636 | const struct p2p_channels *channels) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2637 | { |
| 2638 | struct wpa_supplicant *wpa_s = ctx; |
| 2639 | struct wpa_ssid *s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2640 | int res; |
| 2641 | struct wpa_supplicant *grp; |
| 2642 | |
| 2643 | if (!persistent_group) { |
| 2644 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR |
| 2645 | " to join an active group", MAC2STR(sa)); |
| 2646 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && |
| 2647 | (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN) |
| 2648 | == 0 || |
| 2649 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) { |
| 2650 | wpa_printf(MSG_DEBUG, "P2P: Accept previously " |
| 2651 | "authorized invitation"); |
| 2652 | goto accept_inv; |
| 2653 | } |
| 2654 | /* |
| 2655 | * Do not accept the invitation automatically; notify user and |
| 2656 | * request approval. |
| 2657 | */ |
| 2658 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 2659 | } |
| 2660 | |
| 2661 | grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go); |
| 2662 | if (grp) { |
| 2663 | wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already " |
| 2664 | "running persistent group"); |
| 2665 | if (*go) |
| 2666 | os_memcpy(group_bssid, grp->own_addr, ETH_ALEN); |
| 2667 | goto accept_inv; |
| 2668 | } |
| 2669 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2670 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && |
| 2671 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) { |
| 2672 | wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated " |
| 2673 | "invitation to re-invoke a persistent group"); |
| 2674 | } else if (!wpa_s->conf->persistent_reconnect) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2675 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 2676 | |
| 2677 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 2678 | if (s->disabled == 2 && |
| 2679 | os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 && |
| 2680 | s->ssid_len == ssid_len && |
| 2681 | os_memcmp(ssid, s->ssid, ssid_len) == 0) |
| 2682 | break; |
| 2683 | } |
| 2684 | |
| 2685 | if (!s) { |
| 2686 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR |
| 2687 | " requested reinvocation of an unknown group", |
| 2688 | MAC2STR(sa)); |
| 2689 | return P2P_SC_FAIL_UNKNOWN_GROUP; |
| 2690 | } |
| 2691 | |
| 2692 | if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) { |
| 2693 | *go = 1; |
| 2694 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { |
| 2695 | wpa_printf(MSG_DEBUG, "P2P: The only available " |
| 2696 | "interface is already in use - reject " |
| 2697 | "invitation"); |
| 2698 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; |
| 2699 | } |
| 2700 | os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN); |
| 2701 | } else if (s->mode == WPAS_MODE_P2P_GO) { |
| 2702 | *go = 1; |
| 2703 | if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0) |
| 2704 | { |
| 2705 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 2706 | "interface address for the group"); |
| 2707 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; |
| 2708 | } |
| 2709 | os_memcpy(group_bssid, wpa_s->pending_interface_addr, |
| 2710 | ETH_ALEN); |
| 2711 | } |
| 2712 | |
| 2713 | accept_inv: |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2714 | wpas_p2p_set_own_freq_preference(wpa_s, 0); |
| 2715 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 2716 | /* Get one of the frequencies currently in use */ |
| 2717 | if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) { |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 2718 | 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] | 2719 | wpas_p2p_set_own_freq_preference(wpa_s, res); |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 2720 | |
| 2721 | if (wpa_s->num_multichan_concurrent < 2 || |
| 2722 | wpas_p2p_num_unused_channels(wpa_s) < 1) { |
| 2723 | wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces"); |
| 2724 | *force_freq = res; |
| 2725 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2726 | } |
| 2727 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 2728 | if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 && |
| 2729 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2730 | if (*go == 0) { |
| 2731 | /* We are the client */ |
| 2732 | wpa_printf(MSG_DEBUG, "P2P: Peer was found to be " |
| 2733 | "running a GO but we are capable of MCC, " |
| 2734 | "figure out the best channel to use"); |
| 2735 | *force_freq = 0; |
| 2736 | } else if (!freq_included(channels, *force_freq)) { |
| 2737 | /* We are the GO, and *force_freq is not in the |
| 2738 | * intersection */ |
| 2739 | wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not " |
| 2740 | "in intersection but we are capable of MCC, " |
| 2741 | "figure out the best channel to use", |
| 2742 | *force_freq); |
| 2743 | *force_freq = 0; |
| 2744 | } |
| 2745 | } |
| 2746 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2747 | return P2P_SC_SUCCESS; |
| 2748 | } |
| 2749 | |
| 2750 | |
| 2751 | static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid, |
| 2752 | const u8 *ssid, size_t ssid_len, |
| 2753 | const u8 *go_dev_addr, u8 status, |
| 2754 | int op_freq) |
| 2755 | { |
| 2756 | struct wpa_supplicant *wpa_s = ctx; |
| 2757 | struct wpa_ssid *s; |
| 2758 | |
| 2759 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 2760 | if (s->disabled == 2 && |
| 2761 | s->ssid_len == ssid_len && |
| 2762 | os_memcmp(ssid, s->ssid, ssid_len) == 0) |
| 2763 | break; |
| 2764 | } |
| 2765 | |
| 2766 | if (status == P2P_SC_SUCCESS) { |
| 2767 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR |
| 2768 | " was accepted; op_freq=%d MHz", |
| 2769 | MAC2STR(sa), op_freq); |
| 2770 | if (s) { |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 2771 | int go = s->mode == WPAS_MODE_P2P_GO; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2772 | wpas_p2p_group_add_persistent( |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2773 | wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 2774 | go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2775 | } else if (bssid) { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2776 | wpa_s->user_initiated_pd = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2777 | wpas_p2p_join(wpa_s, bssid, go_dev_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2778 | wpa_s->p2p_wps_method, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2779 | } |
| 2780 | return; |
| 2781 | } |
| 2782 | |
| 2783 | if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { |
| 2784 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR |
| 2785 | " was rejected (status %u)", MAC2STR(sa), status); |
| 2786 | return; |
| 2787 | } |
| 2788 | |
| 2789 | if (!s) { |
| 2790 | if (bssid) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2791 | wpa_msg_global(wpa_s, MSG_INFO, |
| 2792 | P2P_EVENT_INVITATION_RECEIVED |
| 2793 | "sa=" MACSTR " go_dev_addr=" MACSTR |
| 2794 | " bssid=" MACSTR " unknown-network", |
| 2795 | MAC2STR(sa), MAC2STR(go_dev_addr), |
| 2796 | MAC2STR(bssid)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2797 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2798 | wpa_msg_global(wpa_s, MSG_INFO, |
| 2799 | P2P_EVENT_INVITATION_RECEIVED |
| 2800 | "sa=" MACSTR " go_dev_addr=" MACSTR |
| 2801 | " unknown-network", |
| 2802 | MAC2STR(sa), MAC2STR(go_dev_addr)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2803 | } |
| 2804 | return; |
| 2805 | } |
| 2806 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2807 | if (s->mode == WPAS_MODE_P2P_GO && op_freq) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2808 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED |
| 2809 | "sa=" MACSTR " persistent=%d freq=%d", |
| 2810 | MAC2STR(sa), s->id, op_freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2811 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2812 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED |
| 2813 | "sa=" MACSTR " persistent=%d", |
| 2814 | MAC2STR(sa), s->id); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2815 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2819 | static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s, |
| 2820 | struct wpa_ssid *ssid, |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2821 | const u8 *peer, int inv) |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2822 | { |
| 2823 | size_t i; |
| 2824 | |
| 2825 | if (ssid == NULL) |
| 2826 | return; |
| 2827 | |
| 2828 | for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) { |
| 2829 | if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer, |
| 2830 | ETH_ALEN) == 0) |
| 2831 | break; |
| 2832 | } |
| 2833 | if (i >= ssid->num_p2p_clients) { |
| 2834 | if (ssid->mode != WPAS_MODE_P2P_GO && |
| 2835 | os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) { |
| 2836 | wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d " |
| 2837 | "due to invitation result", ssid->id); |
| 2838 | wpas_notify_network_removed(wpa_s, ssid); |
| 2839 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 2840 | return; |
| 2841 | } |
| 2842 | return; /* Peer not found in client list */ |
| 2843 | } |
| 2844 | |
| 2845 | wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent " |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2846 | "group %d client list%s", |
| 2847 | MAC2STR(peer), ssid->id, |
| 2848 | inv ? " due to invitation result" : ""); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2849 | os_memmove(ssid->p2p_client_list + i * ETH_ALEN, |
| 2850 | ssid->p2p_client_list + (i + 1) * ETH_ALEN, |
| 2851 | (ssid->num_p2p_clients - i - 1) * ETH_ALEN); |
| 2852 | ssid->num_p2p_clients--; |
| 2853 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 2854 | if (wpa_s->parent->conf->update_config && |
| 2855 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 2856 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
| 2857 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
| 2858 | } |
| 2859 | |
| 2860 | |
| 2861 | static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s, |
| 2862 | const u8 *peer) |
| 2863 | { |
| 2864 | struct wpa_ssid *ssid; |
| 2865 | |
| 2866 | wpa_s = wpa_s->global->p2p_invite_group; |
| 2867 | if (wpa_s == NULL) |
| 2868 | return; /* No known invitation group */ |
| 2869 | ssid = wpa_s->current_ssid; |
| 2870 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || |
| 2871 | !ssid->p2p_persistent_group) |
| 2872 | return; /* Not operating as a GO in persistent group */ |
| 2873 | ssid = wpas_p2p_get_persistent(wpa_s->parent, peer, |
| 2874 | ssid->ssid, ssid->ssid_len); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2875 | wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2879 | static void wpas_invitation_result(void *ctx, int status, const u8 *bssid, |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2880 | const struct p2p_channels *channels, |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 2881 | const u8 *peer, int neg_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2882 | { |
| 2883 | struct wpa_supplicant *wpa_s = ctx; |
| 2884 | struct wpa_ssid *ssid; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 2885 | int freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2886 | |
| 2887 | if (bssid) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2888 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT |
| 2889 | "status=%d " MACSTR, |
| 2890 | status, MAC2STR(bssid)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2891 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2892 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT |
| 2893 | "status=%d ", status); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2894 | } |
| 2895 | wpas_notify_p2p_invitation_result(wpa_s, status, bssid); |
| 2896 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2897 | wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR, |
| 2898 | status, MAC2STR(peer)); |
| 2899 | if (wpa_s->pending_invite_ssid_id == -1) { |
| 2900 | if (status == P2P_SC_FAIL_UNKNOWN_GROUP) |
| 2901 | wpas_remove_persistent_client(wpa_s, peer); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2902 | return; /* Invitation to active group */ |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2903 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2904 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2905 | if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { |
| 2906 | wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another " |
| 2907 | "invitation exchange to indicate readiness for " |
| 2908 | "re-invocation"); |
| 2909 | } |
| 2910 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2911 | if (status != P2P_SC_SUCCESS) { |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2912 | if (status == P2P_SC_FAIL_UNKNOWN_GROUP) { |
| 2913 | ssid = wpa_config_get_network( |
| 2914 | wpa_s->conf, wpa_s->pending_invite_ssid_id); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2915 | wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2916 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2917 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 2918 | return; |
| 2919 | } |
| 2920 | |
| 2921 | ssid = wpa_config_get_network(wpa_s->conf, |
| 2922 | wpa_s->pending_invite_ssid_id); |
| 2923 | if (ssid == NULL) { |
| 2924 | wpa_printf(MSG_ERROR, "P2P: Could not find persistent group " |
| 2925 | "data matching with invitation"); |
| 2926 | return; |
| 2927 | } |
| 2928 | |
Jouni Malinen | 2c5b17d | 2012-10-13 21:52:46 -0700 | [diff] [blame] | 2929 | /* |
| 2930 | * The peer could have missed our ctrl::ack frame for Invitation |
| 2931 | * Response and continue retransmitting the frame. To reduce the |
| 2932 | * likelihood of the peer not getting successful TX status for the |
| 2933 | * Invitation Response frame, wait a short time here before starting |
| 2934 | * the persistent group so that we will remain on the current channel to |
| 2935 | * acknowledge any possible retransmission from the peer. |
| 2936 | */ |
Irfan Sheriff | 81931b8 | 2012-10-16 21:40:46 -0700 | [diff] [blame] | 2937 | #ifndef ANDROID_P2P |
Jouni Malinen | 2c5b17d | 2012-10-13 21:52:46 -0700 | [diff] [blame] | 2938 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before " |
| 2939 | "starting persistent group"); |
| 2940 | os_sleep(0, 50000); |
Irfan Sheriff | 81931b8 | 2012-10-16 21:40:46 -0700 | [diff] [blame] | 2941 | #else |
| 2942 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before " |
| 2943 | "starting persistent group"); |
| 2944 | os_sleep(0, 100000); |
| 2945 | #endif |
Jouni Malinen | 2c5b17d | 2012-10-13 21:52:46 -0700 | [diff] [blame] | 2946 | |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 2947 | freq = wpa_s->p2p_persistent_go_freq; |
| 2948 | if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO && |
| 2949 | freq_included(channels, neg_freq)) { |
| 2950 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode", |
| 2951 | neg_freq); |
| 2952 | freq = neg_freq; |
| 2953 | } |
| 2954 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2955 | wpas_p2p_group_add_persistent(wpa_s, ssid, |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 2956 | ssid->mode == WPAS_MODE_P2P_GO, |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 2957 | freq, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2958 | wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht, |
| 2959 | channels, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 2960 | ssid->mode == WPAS_MODE_P2P_GO ? |
| 2961 | P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : |
| 2962 | 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2966 | static int wpas_p2p_disallowed_freq(struct wpa_global *global, |
| 2967 | unsigned int freq) |
| 2968 | { |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 2969 | return freq_range_list_includes(&global->p2p_disallow_freq, freq); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | |
| 2973 | static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan) |
| 2974 | { |
| 2975 | reg->channel[reg->channels] = chan; |
| 2976 | reg->channels++; |
| 2977 | } |
| 2978 | |
| 2979 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2980 | static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2981 | struct p2p_channels *chan, |
| 2982 | struct p2p_channels *cli_chan) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2983 | { |
| 2984 | int i, cla = 0; |
| 2985 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2986 | os_memset(cli_chan, 0, sizeof(*cli_chan)); |
| 2987 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2988 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz " |
| 2989 | "band"); |
| 2990 | |
| 2991 | /* Operating class 81 - 2.4 GHz band channels 1..13 */ |
| 2992 | chan->reg_class[cla].reg_class = 81; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2993 | chan->reg_class[cla].channels = 0; |
| 2994 | for (i = 0; i < 11; i++) { |
| 2995 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5)) |
| 2996 | wpas_p2p_add_chan(&chan->reg_class[cla], i + 1); |
| 2997 | } |
| 2998 | if (chan->reg_class[cla].channels) |
| 2999 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3000 | |
| 3001 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz " |
| 3002 | "band"); |
| 3003 | |
| 3004 | /* Operating class 115 - 5 GHz, channels 36-48 */ |
| 3005 | chan->reg_class[cla].reg_class = 115; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3006 | chan->reg_class[cla].channels = 0; |
| 3007 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5)) |
| 3008 | wpas_p2p_add_chan(&chan->reg_class[cla], 36); |
| 3009 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5)) |
| 3010 | wpas_p2p_add_chan(&chan->reg_class[cla], 40); |
| 3011 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5)) |
| 3012 | wpas_p2p_add_chan(&chan->reg_class[cla], 44); |
| 3013 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5)) |
| 3014 | wpas_p2p_add_chan(&chan->reg_class[cla], 48); |
| 3015 | if (chan->reg_class[cla].channels) |
| 3016 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3017 | |
| 3018 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz " |
| 3019 | "band"); |
| 3020 | |
| 3021 | /* Operating class 124 - 5 GHz, channels 149,153,157,161 */ |
| 3022 | chan->reg_class[cla].reg_class = 124; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3023 | chan->reg_class[cla].channels = 0; |
| 3024 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5)) |
| 3025 | wpas_p2p_add_chan(&chan->reg_class[cla], 149); |
| 3026 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5)) |
| 3027 | wpas_p2p_add_chan(&chan->reg_class[cla], 153); |
| 3028 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5)) |
| 3029 | wpas_p2p_add_chan(&chan->reg_class[cla], 157); |
| 3030 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5)) |
| 3031 | wpas_p2p_add_chan(&chan->reg_class[cla], 161); |
| 3032 | if (chan->reg_class[cla].channels) |
| 3033 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3034 | |
| 3035 | chan->reg_classes = cla; |
| 3036 | return 0; |
| 3037 | } |
| 3038 | |
| 3039 | |
| 3040 | static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes, |
| 3041 | u16 num_modes, |
| 3042 | enum hostapd_hw_mode mode) |
| 3043 | { |
| 3044 | u16 i; |
| 3045 | |
| 3046 | for (i = 0; i < num_modes; i++) { |
| 3047 | if (modes[i].mode == mode) |
| 3048 | return &modes[i]; |
| 3049 | } |
| 3050 | |
| 3051 | return NULL; |
| 3052 | } |
| 3053 | |
| 3054 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3055 | enum chan_allowed { |
| 3056 | NOT_ALLOWED, PASSIVE_ONLY, ALLOWED |
| 3057 | }; |
| 3058 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3059 | static int has_channel(struct wpa_global *global, |
| 3060 | struct hostapd_hw_modes *mode, u8 chan, int *flags) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3061 | { |
| 3062 | int i; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3063 | unsigned int freq; |
| 3064 | |
| 3065 | freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) + |
| 3066 | chan * 5; |
| 3067 | if (wpas_p2p_disallowed_freq(global, freq)) |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3068 | return NOT_ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3069 | |
| 3070 | for (i = 0; i < mode->num_channels; i++) { |
| 3071 | if (mode->channels[i].chan == chan) { |
| 3072 | if (flags) |
| 3073 | *flags = mode->channels[i].flag; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3074 | if (mode->channels[i].flag & |
| 3075 | (HOSTAPD_CHAN_DISABLED | |
| 3076 | HOSTAPD_CHAN_RADAR)) |
| 3077 | return NOT_ALLOWED; |
| 3078 | if (mode->channels[i].flag & |
| 3079 | (HOSTAPD_CHAN_PASSIVE_SCAN | |
| 3080 | HOSTAPD_CHAN_NO_IBSS)) |
| 3081 | return PASSIVE_ONLY; |
| 3082 | return ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3083 | } |
| 3084 | } |
| 3085 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3086 | return NOT_ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | |
| 3090 | struct p2p_oper_class_map { |
| 3091 | enum hostapd_hw_mode mode; |
| 3092 | u8 op_class; |
| 3093 | u8 min_chan; |
| 3094 | u8 max_chan; |
| 3095 | u8 inc; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3096 | enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3097 | }; |
| 3098 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3099 | static struct p2p_oper_class_map op_class[] = { |
| 3100 | { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 }, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3101 | #if 0 /* Do not enable HT40 on 2 GHz for now */ |
| 3102 | { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS }, |
| 3103 | { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS }, |
| 3104 | #endif |
| 3105 | { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 }, |
| 3106 | { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 }, |
| 3107 | { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS }, |
| 3108 | { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS }, |
| 3109 | { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS }, |
| 3110 | { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS }, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3111 | |
| 3112 | /* |
| 3113 | * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center |
| 3114 | * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of |
| 3115 | * 80 MHz, but currently use the following definition for simplicity |
| 3116 | * (these center frequencies are not actual channels, which makes |
| 3117 | * has_channel() fail). wpas_p2p_verify_80mhz() should take care of |
| 3118 | * removing invalid channels. |
| 3119 | */ |
| 3120 | { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 }, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3121 | { -1, 0, 0, 0, 0, BW20 } |
| 3122 | }; |
| 3123 | |
| 3124 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3125 | static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s, |
| 3126 | struct hostapd_hw_modes *mode, |
| 3127 | u8 channel) |
| 3128 | { |
| 3129 | u8 center_channels[] = { 42, 58, 106, 122, 138, 155 }; |
| 3130 | unsigned int i; |
| 3131 | |
| 3132 | if (mode->mode != HOSTAPD_MODE_IEEE80211A) |
| 3133 | return 0; |
| 3134 | |
| 3135 | for (i = 0; i < ARRAY_SIZE(center_channels); i++) |
| 3136 | /* |
| 3137 | * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48), |
| 3138 | * so the center channel is 6 channels away from the start/end. |
| 3139 | */ |
| 3140 | if (channel >= center_channels[i] - 6 && |
| 3141 | channel <= center_channels[i] + 6) |
| 3142 | return center_channels[i]; |
| 3143 | |
| 3144 | return 0; |
| 3145 | } |
| 3146 | |
| 3147 | |
| 3148 | static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s, |
| 3149 | struct hostapd_hw_modes *mode, |
| 3150 | u8 channel, u8 bw) |
| 3151 | { |
| 3152 | u8 center_chan; |
| 3153 | int i, flags; |
| 3154 | enum chan_allowed res, ret = ALLOWED; |
| 3155 | |
| 3156 | center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel); |
| 3157 | if (!center_chan) |
| 3158 | return NOT_ALLOWED; |
| 3159 | if (center_chan >= 58 && center_chan <= 138) |
| 3160 | return NOT_ALLOWED; /* Do not allow DFS channels for P2P */ |
| 3161 | |
| 3162 | /* check all the channels are available */ |
| 3163 | for (i = 0; i < 4; i++) { |
| 3164 | int adj_chan = center_chan - 6 + i * 4; |
| 3165 | |
| 3166 | res = has_channel(wpa_s->global, mode, adj_chan, &flags); |
| 3167 | if (res == NOT_ALLOWED) |
| 3168 | return NOT_ALLOWED; |
| 3169 | if (res == PASSIVE_ONLY) |
| 3170 | ret = PASSIVE_ONLY; |
| 3171 | |
| 3172 | if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) |
| 3173 | return NOT_ALLOWED; |
| 3174 | if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) |
| 3175 | return NOT_ALLOWED; |
| 3176 | if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) |
| 3177 | return NOT_ALLOWED; |
| 3178 | if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)) |
| 3179 | return NOT_ALLOWED; |
| 3180 | } |
| 3181 | |
| 3182 | return ret; |
| 3183 | } |
| 3184 | |
| 3185 | |
| 3186 | static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s, |
| 3187 | struct hostapd_hw_modes *mode, |
| 3188 | u8 channel, u8 bw) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3189 | { |
| 3190 | int flag; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3191 | enum chan_allowed res, res2; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3192 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3193 | res2 = res = has_channel(wpa_s->global, mode, channel, &flag); |
| 3194 | if (bw == BW40MINUS) { |
| 3195 | if (!(flag & HOSTAPD_CHAN_HT40MINUS)) |
| 3196 | return NOT_ALLOWED; |
| 3197 | res2 = has_channel(wpa_s->global, mode, channel - 4, NULL); |
| 3198 | } else if (bw == BW40PLUS) { |
| 3199 | if (!(flag & HOSTAPD_CHAN_HT40PLUS)) |
| 3200 | return NOT_ALLOWED; |
| 3201 | res2 = has_channel(wpa_s->global, mode, channel + 4, NULL); |
| 3202 | } else if (bw == BW80) { |
| 3203 | res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw); |
| 3204 | } |
| 3205 | |
| 3206 | if (res == NOT_ALLOWED || res2 == NOT_ALLOWED) |
| 3207 | return NOT_ALLOWED; |
| 3208 | if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY) |
| 3209 | return PASSIVE_ONLY; |
| 3210 | return res; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3214 | static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3215 | struct p2p_channels *chan, |
| 3216 | struct p2p_channels *cli_chan) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3217 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3218 | struct hostapd_hw_modes *mode; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3219 | int cla, op, cli_cla; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3220 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3221 | if (wpa_s->hw.modes == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3222 | wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching " |
| 3223 | "of all supported channels; assume dualband " |
| 3224 | "support"); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3225 | return wpas_p2p_default_channels(wpa_s, chan, cli_chan); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3226 | } |
| 3227 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3228 | cla = cli_cla = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3229 | |
| 3230 | for (op = 0; op_class[op].op_class; op++) { |
| 3231 | struct p2p_oper_class_map *o = &op_class[op]; |
| 3232 | u8 ch; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3233 | struct p2p_reg_class *reg = NULL, *cli_reg = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3234 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3235 | 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] | 3236 | if (mode == NULL) |
| 3237 | continue; |
| 3238 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3239 | enum chan_allowed res; |
| 3240 | res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); |
| 3241 | if (res == ALLOWED) { |
| 3242 | if (reg == NULL) { |
| 3243 | wpa_printf(MSG_DEBUG, "P2P: Add operating class %u", |
| 3244 | o->op_class); |
| 3245 | reg = &chan->reg_class[cla]; |
| 3246 | cla++; |
| 3247 | reg->reg_class = o->op_class; |
| 3248 | } |
| 3249 | reg->channel[reg->channels] = ch; |
| 3250 | reg->channels++; |
| 3251 | } else if (res == PASSIVE_ONLY && |
| 3252 | wpa_s->conf->p2p_add_cli_chan) { |
| 3253 | if (cli_reg == NULL) { |
| 3254 | wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)", |
| 3255 | o->op_class); |
| 3256 | cli_reg = &cli_chan->reg_class[cli_cla]; |
| 3257 | cli_cla++; |
| 3258 | cli_reg->reg_class = o->op_class; |
| 3259 | } |
| 3260 | cli_reg->channel[cli_reg->channels] = ch; |
| 3261 | cli_reg->channels++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3262 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3263 | } |
| 3264 | if (reg) { |
| 3265 | wpa_hexdump(MSG_DEBUG, "P2P: Channels", |
| 3266 | reg->channel, reg->channels); |
| 3267 | } |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3268 | if (cli_reg) { |
| 3269 | wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)", |
| 3270 | cli_reg->channel, cli_reg->channels); |
| 3271 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | chan->reg_classes = cla; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3275 | cli_chan->reg_classes = cli_cla; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3276 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3277 | return 0; |
| 3278 | } |
| 3279 | |
| 3280 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3281 | int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s, |
| 3282 | struct hostapd_hw_modes *mode, u8 channel) |
| 3283 | { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3284 | int op; |
| 3285 | enum chan_allowed ret; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3286 | |
| 3287 | for (op = 0; op_class[op].op_class; op++) { |
| 3288 | struct p2p_oper_class_map *o = &op_class[op]; |
| 3289 | u8 ch; |
| 3290 | |
| 3291 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { |
| 3292 | if (o->mode != HOSTAPD_MODE_IEEE80211A || |
| 3293 | o->bw == BW20 || ch != channel) |
| 3294 | continue; |
| 3295 | ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3296 | if (ret == ALLOWED) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3297 | return (o->bw == BW40MINUS) ? -1 : 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3298 | } |
| 3299 | } |
| 3300 | return 0; |
| 3301 | } |
| 3302 | |
| 3303 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3304 | int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s, |
| 3305 | struct hostapd_hw_modes *mode, u8 channel) |
| 3306 | { |
| 3307 | if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80)) |
| 3308 | return 0; |
| 3309 | |
| 3310 | return wpas_p2p_get_center_80mhz(wpa_s, mode, channel); |
| 3311 | } |
| 3312 | |
| 3313 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3314 | static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf, |
| 3315 | size_t buf_len) |
| 3316 | { |
| 3317 | struct wpa_supplicant *wpa_s = ctx; |
| 3318 | |
| 3319 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3320 | if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0) |
| 3321 | break; |
| 3322 | } |
| 3323 | if (wpa_s == NULL) |
| 3324 | return -1; |
| 3325 | |
| 3326 | return wpa_drv_get_noa(wpa_s, buf, buf_len); |
| 3327 | } |
| 3328 | |
| 3329 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3330 | static int wpas_go_connected(void *ctx, const u8 *dev_addr) |
| 3331 | { |
| 3332 | struct wpa_supplicant *wpa_s = ctx; |
| 3333 | |
| 3334 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3335 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 3336 | if (ssid == NULL) |
| 3337 | continue; |
| 3338 | if (ssid->mode != WPAS_MODE_INFRA) |
| 3339 | continue; |
| 3340 | if (wpa_s->wpa_state != WPA_COMPLETED && |
| 3341 | wpa_s->wpa_state != WPA_GROUP_HANDSHAKE) |
| 3342 | continue; |
| 3343 | if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0) |
| 3344 | return 1; |
| 3345 | } |
| 3346 | |
| 3347 | return 0; |
| 3348 | } |
| 3349 | |
| 3350 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3351 | static void wpas_p2p_debug_print(void *ctx, int level, const char *msg) |
| 3352 | { |
| 3353 | struct wpa_supplicant *wpa_s = ctx; |
| 3354 | wpa_msg_global(wpa_s, level, "P2P: %s", msg); |
| 3355 | } |
| 3356 | |
| 3357 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3358 | int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s) |
| 3359 | { |
| 3360 | struct wpa_interface iface; |
| 3361 | struct wpa_supplicant *p2pdev_wpa_s; |
| 3362 | char ifname[100]; |
| 3363 | char force_name[100]; |
| 3364 | int ret; |
| 3365 | |
| 3366 | os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s", |
| 3367 | wpa_s->ifname); |
| 3368 | force_name[0] = '\0'; |
| 3369 | wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE; |
| 3370 | ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL, |
| 3371 | force_name, wpa_s->pending_interface_addr, NULL); |
| 3372 | if (ret < 0) { |
| 3373 | wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface"); |
| 3374 | return ret; |
| 3375 | } |
| 3376 | os_strlcpy(wpa_s->pending_interface_name, ifname, |
| 3377 | sizeof(wpa_s->pending_interface_name)); |
| 3378 | |
| 3379 | os_memset(&iface, 0, sizeof(iface)); |
| 3380 | iface.p2p_mgmt = 1; |
| 3381 | iface.ifname = wpa_s->pending_interface_name; |
| 3382 | iface.driver = wpa_s->driver->name; |
| 3383 | iface.driver_param = wpa_s->conf->driver_param; |
| 3384 | iface.confname = wpa_s->confname; |
| 3385 | p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface); |
| 3386 | if (!p2pdev_wpa_s) { |
| 3387 | wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface"); |
| 3388 | return -1; |
| 3389 | } |
| 3390 | p2pdev_wpa_s->parent = wpa_s; |
| 3391 | |
| 3392 | wpa_s->pending_interface_name[0] = '\0'; |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
| 3396 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3397 | /** |
| 3398 | * wpas_p2p_init - Initialize P2P module for %wpa_supplicant |
| 3399 | * @global: Pointer to global data from wpa_supplicant_init() |
| 3400 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 3401 | * Returns: 0 on success, -1 on failure |
| 3402 | */ |
| 3403 | int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s) |
| 3404 | { |
| 3405 | struct p2p_config p2p; |
| 3406 | unsigned int r; |
| 3407 | int i; |
| 3408 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3409 | if (wpa_s->conf->p2p_disabled) |
| 3410 | return 0; |
| 3411 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3412 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) |
| 3413 | return 0; |
| 3414 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3415 | if (global->p2p) |
| 3416 | return 0; |
| 3417 | |
| 3418 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { |
| 3419 | struct p2p_params params; |
| 3420 | |
| 3421 | wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management"); |
| 3422 | os_memset(¶ms, 0, sizeof(params)); |
| 3423 | params.dev_name = wpa_s->conf->device_name; |
| 3424 | os_memcpy(params.pri_dev_type, wpa_s->conf->device_type, |
| 3425 | WPS_DEV_TYPE_LEN); |
| 3426 | params.num_sec_dev_types = wpa_s->conf->num_sec_device_types; |
| 3427 | os_memcpy(params.sec_dev_type, |
| 3428 | wpa_s->conf->sec_device_type, |
| 3429 | params.num_sec_dev_types * WPS_DEV_TYPE_LEN); |
| 3430 | |
| 3431 | if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0) |
| 3432 | return -1; |
| 3433 | |
| 3434 | return 0; |
| 3435 | } |
| 3436 | |
| 3437 | os_memset(&p2p, 0, sizeof(p2p)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3438 | p2p.cb_ctx = wpa_s; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3439 | p2p.debug_print = wpas_p2p_debug_print; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3440 | p2p.p2p_scan = wpas_p2p_scan; |
| 3441 | p2p.send_action = wpas_send_action; |
| 3442 | p2p.send_action_done = wpas_send_action_done; |
| 3443 | p2p.go_neg_completed = wpas_go_neg_completed; |
| 3444 | p2p.go_neg_req_rx = wpas_go_neg_req_rx; |
| 3445 | p2p.dev_found = wpas_dev_found; |
| 3446 | p2p.dev_lost = wpas_dev_lost; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3447 | p2p.find_stopped = wpas_find_stopped; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3448 | p2p.start_listen = wpas_start_listen; |
| 3449 | p2p.stop_listen = wpas_stop_listen; |
| 3450 | p2p.send_probe_resp = wpas_send_probe_resp; |
| 3451 | p2p.sd_request = wpas_sd_request; |
| 3452 | p2p.sd_response = wpas_sd_response; |
| 3453 | p2p.prov_disc_req = wpas_prov_disc_req; |
| 3454 | p2p.prov_disc_resp = wpas_prov_disc_resp; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 3455 | p2p.prov_disc_fail = wpas_prov_disc_fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3456 | p2p.invitation_process = wpas_invitation_process; |
| 3457 | p2p.invitation_received = wpas_invitation_received; |
| 3458 | p2p.invitation_result = wpas_invitation_result; |
| 3459 | p2p.get_noa = wpas_get_noa; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3460 | p2p.go_connected = wpas_go_connected; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3461 | |
| 3462 | 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] | 3463 | 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] | 3464 | p2p.dev_name = wpa_s->conf->device_name; |
| 3465 | p2p.manufacturer = wpa_s->conf->manufacturer; |
| 3466 | p2p.model_name = wpa_s->conf->model_name; |
| 3467 | p2p.model_number = wpa_s->conf->model_number; |
| 3468 | p2p.serial_number = wpa_s->conf->serial_number; |
| 3469 | if (wpa_s->wps) { |
| 3470 | os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16); |
| 3471 | p2p.config_methods = wpa_s->wps->config_methods; |
| 3472 | } |
| 3473 | |
| 3474 | if (wpa_s->conf->p2p_listen_reg_class && |
| 3475 | wpa_s->conf->p2p_listen_channel) { |
| 3476 | p2p.reg_class = wpa_s->conf->p2p_listen_reg_class; |
| 3477 | p2p.channel = wpa_s->conf->p2p_listen_channel; |
| 3478 | } else { |
| 3479 | p2p.reg_class = 81; |
| 3480 | /* |
| 3481 | * Pick one of the social channels randomly as the listen |
| 3482 | * channel. |
| 3483 | */ |
| 3484 | os_get_random((u8 *) &r, sizeof(r)); |
| 3485 | p2p.channel = 1 + (r % 3) * 5; |
| 3486 | } |
| 3487 | wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel); |
| 3488 | |
| 3489 | if (wpa_s->conf->p2p_oper_reg_class && |
| 3490 | wpa_s->conf->p2p_oper_channel) { |
| 3491 | p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class; |
| 3492 | p2p.op_channel = wpa_s->conf->p2p_oper_channel; |
| 3493 | p2p.cfg_op_channel = 1; |
| 3494 | wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: " |
| 3495 | "%d:%d", p2p.op_reg_class, p2p.op_channel); |
| 3496 | |
| 3497 | } else { |
| 3498 | p2p.op_reg_class = 81; |
| 3499 | /* |
| 3500 | * Use random operation channel from (1, 6, 11) if no other |
| 3501 | * preference is indicated. |
| 3502 | */ |
| 3503 | os_get_random((u8 *) &r, sizeof(r)); |
| 3504 | p2p.op_channel = 1 + (r % 3) * 5; |
| 3505 | p2p.cfg_op_channel = 0; |
| 3506 | wpa_printf(MSG_DEBUG, "P2P: Random operating channel: " |
| 3507 | "%d:%d", p2p.op_reg_class, p2p.op_channel); |
| 3508 | } |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 3509 | |
| 3510 | if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) { |
| 3511 | p2p.pref_chan = wpa_s->conf->p2p_pref_chan; |
| 3512 | p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan; |
| 3513 | } |
| 3514 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3515 | if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) { |
| 3516 | os_memcpy(p2p.country, wpa_s->conf->country, 2); |
| 3517 | p2p.country[2] = 0x04; |
| 3518 | } else |
| 3519 | os_memcpy(p2p.country, "XX\x04", 3); |
| 3520 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3521 | if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3522 | wpa_printf(MSG_ERROR, "P2P: Failed to configure supported " |
| 3523 | "channel list"); |
| 3524 | return -1; |
| 3525 | } |
| 3526 | |
| 3527 | os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type, |
| 3528 | WPS_DEV_TYPE_LEN); |
| 3529 | |
| 3530 | p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types; |
| 3531 | os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type, |
| 3532 | p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN); |
| 3533 | |
| 3534 | p2p.concurrent_operations = !!(wpa_s->drv_flags & |
| 3535 | WPA_DRIVER_FLAGS_P2P_CONCURRENT); |
| 3536 | |
| 3537 | p2p.max_peers = 100; |
| 3538 | |
| 3539 | if (wpa_s->conf->p2p_ssid_postfix) { |
| 3540 | p2p.ssid_postfix_len = |
| 3541 | os_strlen(wpa_s->conf->p2p_ssid_postfix); |
| 3542 | if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix)) |
| 3543 | p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix); |
| 3544 | os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix, |
| 3545 | p2p.ssid_postfix_len); |
| 3546 | } |
| 3547 | |
| 3548 | p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss; |
| 3549 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3550 | p2p.max_listen = wpa_s->max_remain_on_chan; |
| 3551 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3552 | global->p2p = p2p_init(&p2p); |
| 3553 | if (global->p2p == NULL) |
| 3554 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3555 | global->p2p_init_wpa_s = wpa_s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3556 | |
| 3557 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { |
| 3558 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) |
| 3559 | continue; |
| 3560 | p2p_add_wps_vendor_extension( |
| 3561 | global->p2p, wpa_s->conf->wps_vendor_ext[i]); |
| 3562 | } |
| 3563 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3564 | p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq); |
| 3565 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3566 | return 0; |
| 3567 | } |
| 3568 | |
| 3569 | |
| 3570 | /** |
| 3571 | * wpas_p2p_deinit - Deinitialize per-interface P2P data |
| 3572 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 3573 | * |
| 3574 | * This function deinitialize per-interface P2P data. |
| 3575 | */ |
| 3576 | void wpas_p2p_deinit(struct wpa_supplicant *wpa_s) |
| 3577 | { |
| 3578 | if (wpa_s->driver && wpa_s->drv_priv) |
| 3579 | wpa_drv_probe_req_report(wpa_s, 0); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3580 | |
| 3581 | if (wpa_s->go_params) { |
| 3582 | /* Clear any stored provisioning info */ |
| 3583 | p2p_clear_provisioning_info( |
| 3584 | wpa_s->global->p2p, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3585 | wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3586 | } |
| 3587 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3588 | os_free(wpa_s->go_params); |
| 3589 | wpa_s->go_params = NULL; |
Dmitry Shmidt | 1cf4573 | 2013-04-29 17:36:45 -0700 | [diff] [blame] | 3590 | #ifdef ANDROID_P2P |
| 3591 | eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL); |
| 3592 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3593 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 3594 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 3595 | wpa_s->p2p_long_listen = 0; |
| 3596 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 3597 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); |
| 3598 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 3599 | |
| 3600 | /* TODO: remove group interface from the driver if this wpa_s instance |
| 3601 | * is on top of a P2P group interface */ |
| 3602 | } |
| 3603 | |
| 3604 | |
| 3605 | /** |
| 3606 | * wpas_p2p_deinit_global - Deinitialize global P2P module |
| 3607 | * @global: Pointer to global data from wpa_supplicant_init() |
| 3608 | * |
| 3609 | * This function deinitializes the global (per device) P2P module. |
| 3610 | */ |
| 3611 | void wpas_p2p_deinit_global(struct wpa_global *global) |
| 3612 | { |
| 3613 | struct wpa_supplicant *wpa_s, *tmp; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3614 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3615 | wpa_s = global->ifaces; |
| 3616 | if (wpa_s) |
| 3617 | wpas_p2p_service_flush(wpa_s); |
| 3618 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3619 | if (global->p2p == NULL) |
| 3620 | return; |
| 3621 | |
| 3622 | /* Remove remaining P2P group interfaces */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3623 | while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) |
| 3624 | wpa_s = wpa_s->next; |
| 3625 | while (wpa_s) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3626 | tmp = global->ifaces; |
| 3627 | while (tmp && |
| 3628 | (tmp == wpa_s || |
| 3629 | tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) { |
| 3630 | tmp = tmp->next; |
| 3631 | } |
| 3632 | if (tmp == NULL) |
| 3633 | break; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3634 | /* Disconnect from the P2P group and deinit the interface */ |
| 3635 | wpas_p2p_disconnect(tmp); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | /* |
| 3639 | * Deinit GO data on any possibly remaining interface (if main |
| 3640 | * interface is used as GO). |
| 3641 | */ |
| 3642 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3643 | if (wpa_s->ap_iface) |
| 3644 | wpas_p2p_group_deinit(wpa_s); |
| 3645 | } |
| 3646 | |
| 3647 | p2p_deinit(global->p2p); |
| 3648 | global->p2p = NULL; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3649 | global->p2p_init_wpa_s = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | |
| 3653 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s) |
| 3654 | { |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3655 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) && |
| 3656 | wpa_s->conf->p2p_no_group_iface) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3657 | return 0; /* separate interface disabled per configuration */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3658 | if (wpa_s->drv_flags & |
| 3659 | (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE | |
| 3660 | WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P)) |
| 3661 | return 1; /* P2P group requires a new interface in every case |
| 3662 | */ |
| 3663 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)) |
| 3664 | return 0; /* driver does not support concurrent operations */ |
| 3665 | if (wpa_s->global->ifaces->next) |
| 3666 | return 1; /* more that one interface already in use */ |
| 3667 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) |
| 3668 | return 1; /* this interface is already in use */ |
| 3669 | return 0; |
| 3670 | } |
| 3671 | |
| 3672 | |
| 3673 | static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s, |
| 3674 | const u8 *peer_addr, |
| 3675 | enum p2p_wps_method wps_method, |
| 3676 | int go_intent, const u8 *own_interface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3677 | unsigned int force_freq, int persistent_group, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3678 | struct wpa_ssid *ssid, unsigned int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3679 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3680 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
| 3681 | persistent_group = 2; |
| 3682 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3683 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { |
| 3684 | return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method, |
| 3685 | go_intent, own_interface_addr, |
| 3686 | force_freq, persistent_group); |
| 3687 | } |
| 3688 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3689 | /* |
| 3690 | * Increase GO config timeout if HT40 is used since it takes some time |
| 3691 | * to scan channels for coex purposes before the BSS can be started. |
| 3692 | */ |
| 3693 | p2p_set_config_timeout(wpa_s->global->p2p, |
| 3694 | wpa_s->p2p_go_ht40 ? 255 : 100, 20); |
| 3695 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3696 | return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method, |
| 3697 | go_intent, own_interface_addr, force_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3698 | persistent_group, ssid ? ssid->ssid : NULL, |
| 3699 | ssid ? ssid->ssid_len : 0, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3700 | wpa_s->p2p_pd_before_go_neg, pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | |
| 3704 | static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s, |
| 3705 | const u8 *peer_addr, |
| 3706 | enum p2p_wps_method wps_method, |
| 3707 | int go_intent, const u8 *own_interface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3708 | unsigned int force_freq, int persistent_group, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3709 | struct wpa_ssid *ssid, unsigned int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3710 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3711 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
| 3712 | persistent_group = 2; |
| 3713 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3714 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 3715 | return -1; |
| 3716 | |
| 3717 | return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method, |
| 3718 | go_intent, own_interface_addr, force_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3719 | persistent_group, ssid ? ssid->ssid : NULL, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3720 | ssid ? ssid->ssid_len : 0, pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | |
| 3724 | static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s) |
| 3725 | { |
| 3726 | wpa_s->p2p_join_scan_count++; |
| 3727 | wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d", |
| 3728 | wpa_s->p2p_join_scan_count); |
| 3729 | if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) { |
| 3730 | wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR |
| 3731 | " for join operationg - stop join attempt", |
| 3732 | MAC2STR(wpa_s->pending_join_iface_addr)); |
| 3733 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3734 | if (wpa_s->p2p_auto_pd) { |
| 3735 | wpa_s->p2p_auto_pd = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3736 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3737 | P2P_EVENT_PROV_DISC_FAILURE |
| 3738 | " p2p_dev_addr=" MACSTR " status=N/A", |
| 3739 | MAC2STR(wpa_s->pending_join_dev_addr)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3740 | return; |
| 3741 | } |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3742 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 3743 | P2P_EVENT_GROUP_FORMATION_FAILURE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3744 | } |
| 3745 | } |
| 3746 | |
| 3747 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3748 | static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq) |
| 3749 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3750 | int *freqs, res, num, i; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3751 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3752 | if (wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 3753 | /* Multiple channels are supported and not all are in use */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3754 | return 0; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3755 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3756 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3757 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 3758 | if (!freqs) |
| 3759 | return 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3760 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3761 | num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, |
| 3762 | wpa_s->num_multichan_concurrent); |
| 3763 | if (num < 0) { |
| 3764 | res = 1; |
| 3765 | goto exit_free; |
| 3766 | } |
| 3767 | |
| 3768 | for (i = 0; i < num; i++) { |
| 3769 | if (freqs[i] == freq) { |
| 3770 | wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used", |
| 3771 | freq); |
| 3772 | res = 0; |
| 3773 | goto exit_free; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3774 | } |
| 3775 | } |
| 3776 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3777 | res = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3778 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3779 | exit_free: |
| 3780 | os_free(freqs); |
| 3781 | return res; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3782 | } |
| 3783 | |
| 3784 | |
| 3785 | static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s, |
| 3786 | const u8 *peer_dev_addr) |
| 3787 | { |
| 3788 | struct wpa_bss *bss; |
| 3789 | int updated; |
| 3790 | |
| 3791 | bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr); |
| 3792 | if (bss == NULL) |
| 3793 | return -1; |
| 3794 | if (bss->last_update_idx < wpa_s->bss_update_idx) { |
| 3795 | wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the " |
| 3796 | "last scan"); |
| 3797 | return 0; |
| 3798 | } |
| 3799 | |
| 3800 | updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update); |
| 3801 | wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at " |
| 3802 | "%ld.%06ld (%supdated in last scan)", |
| 3803 | bss->last_update.sec, bss->last_update.usec, |
| 3804 | updated ? "": "not "); |
| 3805 | |
| 3806 | return updated; |
| 3807 | } |
| 3808 | |
| 3809 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3810 | static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s, |
| 3811 | struct wpa_scan_results *scan_res) |
| 3812 | { |
| 3813 | struct wpa_bss *bss; |
| 3814 | int freq; |
| 3815 | u8 iface_addr[ETH_ALEN]; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3816 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3817 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 3818 | |
| 3819 | if (wpa_s->global->p2p_disabled) |
| 3820 | return; |
| 3821 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3822 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin", |
| 3823 | scan_res ? (int) scan_res->num : -1, |
| 3824 | wpa_s->p2p_auto_join ? "auto_" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3825 | |
| 3826 | if (scan_res) |
| 3827 | wpas_p2p_scan_res_handler(wpa_s, scan_res); |
| 3828 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3829 | if (wpa_s->p2p_auto_pd) { |
| 3830 | int join = wpas_p2p_peer_go(wpa_s, |
| 3831 | wpa_s->pending_join_dev_addr); |
| 3832 | if (join == 0 && |
| 3833 | wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) { |
| 3834 | wpa_s->auto_pd_scan_retry++; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 3835 | bss = wpa_bss_get_bssid_latest( |
| 3836 | wpa_s, wpa_s->pending_join_dev_addr); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3837 | if (bss) { |
| 3838 | freq = bss->freq; |
| 3839 | wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for " |
| 3840 | "the peer " MACSTR " at %d MHz", |
| 3841 | wpa_s->auto_pd_scan_retry, |
| 3842 | MAC2STR(wpa_s-> |
| 3843 | pending_join_dev_addr), |
| 3844 | freq); |
| 3845 | wpas_p2p_join_scan_req(wpa_s, freq); |
| 3846 | return; |
| 3847 | } |
| 3848 | } |
| 3849 | |
| 3850 | if (join < 0) |
| 3851 | join = 0; |
| 3852 | |
| 3853 | wpa_s->p2p_auto_pd = 0; |
| 3854 | wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG; |
| 3855 | wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d", |
| 3856 | MAC2STR(wpa_s->pending_join_dev_addr), join); |
| 3857 | if (p2p_prov_disc_req(wpa_s->global->p2p, |
| 3858 | wpa_s->pending_join_dev_addr, |
| 3859 | wpa_s->pending_pd_config_methods, join, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3860 | 0, wpa_s->user_initiated_pd) < 0) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3861 | wpa_s->p2p_auto_pd = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3862 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3863 | P2P_EVENT_PROV_DISC_FAILURE |
| 3864 | " p2p_dev_addr=" MACSTR " status=N/A", |
| 3865 | MAC2STR(wpa_s->pending_join_dev_addr)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3866 | } |
| 3867 | return; |
| 3868 | } |
| 3869 | |
| 3870 | if (wpa_s->p2p_auto_join) { |
| 3871 | int join = wpas_p2p_peer_go(wpa_s, |
| 3872 | wpa_s->pending_join_dev_addr); |
| 3873 | if (join < 0) { |
| 3874 | wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be " |
| 3875 | "running a GO -> use GO Negotiation"); |
| 3876 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, |
| 3877 | wpa_s->p2p_pin, wpa_s->p2p_wps_method, |
| 3878 | wpa_s->p2p_persistent_group, 0, 0, 0, |
| 3879 | wpa_s->p2p_go_intent, |
| 3880 | wpa_s->p2p_connect_freq, |
| 3881 | wpa_s->p2p_persistent_id, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3882 | wpa_s->p2p_pd_before_go_neg, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3883 | wpa_s->p2p_go_ht40, |
| 3884 | wpa_s->p2p_go_vht); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3885 | return; |
| 3886 | } |
| 3887 | |
| 3888 | wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> " |
| 3889 | "try to join the group", join ? "" : |
| 3890 | " in older scan"); |
| 3891 | if (!join) |
| 3892 | wpa_s->p2p_fallback_to_go_neg = 1; |
| 3893 | } |
| 3894 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3895 | freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 3896 | wpa_s->pending_join_iface_addr); |
| 3897 | if (freq < 0 && |
| 3898 | p2p_get_interface_addr(wpa_s->global->p2p, |
| 3899 | wpa_s->pending_join_dev_addr, |
| 3900 | iface_addr) == 0 && |
| 3901 | os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0) |
| 3902 | { |
| 3903 | wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface " |
| 3904 | "address for join from " MACSTR " to " MACSTR |
| 3905 | " based on newly discovered P2P peer entry", |
| 3906 | MAC2STR(wpa_s->pending_join_iface_addr), |
| 3907 | MAC2STR(iface_addr)); |
| 3908 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, |
| 3909 | ETH_ALEN); |
| 3910 | |
| 3911 | freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 3912 | wpa_s->pending_join_iface_addr); |
| 3913 | } |
| 3914 | if (freq >= 0) { |
| 3915 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " |
| 3916 | "from P2P peer table: %d MHz", freq); |
| 3917 | } |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 3918 | bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3919 | if (bss) { |
| 3920 | freq = bss->freq; |
| 3921 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 3922 | "from BSS table: %d MHz (SSID %s)", freq, |
| 3923 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3924 | } |
| 3925 | if (freq > 0) { |
| 3926 | u16 method; |
| 3927 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3928 | if (wpas_check_freq_conflict(wpa_s, freq) > 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3929 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 3930 | P2P_EVENT_GROUP_FORMATION_FAILURE |
| 3931 | "reason=FREQ_CONFLICT"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3932 | return; |
| 3933 | } |
| 3934 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3935 | wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request " |
| 3936 | "prior to joining an existing group (GO " MACSTR |
| 3937 | " freq=%u MHz)", |
| 3938 | MAC2STR(wpa_s->pending_join_dev_addr), freq); |
| 3939 | wpa_s->pending_pd_before_join = 1; |
| 3940 | |
| 3941 | switch (wpa_s->pending_join_wps_method) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3942 | case WPS_PIN_DISPLAY: |
| 3943 | method = WPS_CONFIG_KEYPAD; |
| 3944 | break; |
| 3945 | case WPS_PIN_KEYPAD: |
| 3946 | method = WPS_CONFIG_DISPLAY; |
| 3947 | break; |
| 3948 | case WPS_PBC: |
| 3949 | method = WPS_CONFIG_PUSHBUTTON; |
| 3950 | break; |
| 3951 | default: |
| 3952 | method = 0; |
| 3953 | break; |
| 3954 | } |
| 3955 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3956 | if ((p2p_get_provisioning_info(wpa_s->global->p2p, |
| 3957 | wpa_s->pending_join_dev_addr) == |
| 3958 | method)) { |
| 3959 | /* |
| 3960 | * We have already performed provision discovery for |
| 3961 | * joining the group. Proceed directly to join |
| 3962 | * operation without duplicated provision discovery. */ |
| 3963 | wpa_printf(MSG_DEBUG, "P2P: Provision discovery " |
| 3964 | "with " MACSTR " already done - proceed to " |
| 3965 | "join", |
| 3966 | MAC2STR(wpa_s->pending_join_dev_addr)); |
| 3967 | wpa_s->pending_pd_before_join = 0; |
| 3968 | goto start; |
| 3969 | } |
| 3970 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3971 | if (p2p_prov_disc_req(wpa_s->global->p2p, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3972 | wpa_s->pending_join_dev_addr, method, 1, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3973 | freq, wpa_s->user_initiated_pd) < 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3974 | wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision " |
| 3975 | "Discovery Request before joining an " |
| 3976 | "existing group"); |
| 3977 | wpa_s->pending_pd_before_join = 0; |
| 3978 | goto start; |
| 3979 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3980 | return; |
| 3981 | } |
| 3982 | |
| 3983 | wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later"); |
| 3984 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 3985 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); |
| 3986 | wpas_p2p_check_join_scan_limit(wpa_s); |
| 3987 | return; |
| 3988 | |
| 3989 | start: |
| 3990 | /* Start join operation immediately */ |
| 3991 | wpas_p2p_join_start(wpa_s); |
| 3992 | } |
| 3993 | |
| 3994 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3995 | static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3996 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3997 | int ret; |
| 3998 | struct wpa_driver_scan_params params; |
| 3999 | struct wpabuf *wps_ie, *ies; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4000 | size_t ielen; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4001 | int freqs[2] = { 0, 0 }; |
Dmitry Shmidt | ad266fb | 2012-08-24 17:03:35 -0700 | [diff] [blame] | 4002 | #ifdef ANDROID_P2P |
| 4003 | int oper_freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4004 | |
Dmitry Shmidt | ad266fb | 2012-08-24 17:03:35 -0700 | [diff] [blame] | 4005 | /* If freq is not provided, check the operating freq of the GO and do a |
| 4006 | * a directed scan to save time |
| 4007 | */ |
| 4008 | if(!freq) { |
| 4009 | freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4010 | wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq; |
| 4011 | } |
| 4012 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4013 | os_memset(¶ms, 0, sizeof(params)); |
| 4014 | |
| 4015 | /* P2P Wildcard SSID */ |
| 4016 | params.num_ssids = 1; |
| 4017 | params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; |
| 4018 | params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; |
| 4019 | |
| 4020 | wpa_s->wps->dev.p2p = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4021 | wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev, |
| 4022 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0, |
| 4023 | NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4024 | if (wps_ie == NULL) { |
| 4025 | wpas_p2p_scan_res_join(wpa_s, NULL); |
| 4026 | return; |
| 4027 | } |
| 4028 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4029 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
| 4030 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4031 | if (ies == NULL) { |
| 4032 | wpabuf_free(wps_ie); |
| 4033 | wpas_p2p_scan_res_join(wpa_s, NULL); |
| 4034 | return; |
| 4035 | } |
| 4036 | wpabuf_put_buf(ies, wps_ie); |
| 4037 | wpabuf_free(wps_ie); |
| 4038 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 4039 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4040 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4041 | params.p2p_probe = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4042 | params.extra_ies = wpabuf_head(ies); |
| 4043 | params.extra_ies_len = wpabuf_len(ies); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4044 | if (freq > 0) { |
| 4045 | freqs[0] = freq; |
| 4046 | params.freqs = freqs; |
| 4047 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4048 | |
| 4049 | /* |
| 4050 | * Run a scan to update BSS table and start Provision Discovery once |
| 4051 | * the new scan results become available. |
| 4052 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4053 | ret = wpa_drv_scan(wpa_s, ¶ms); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4054 | if (!ret) { |
| 4055 | os_get_time(&wpa_s->scan_trigger_time); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4056 | wpa_s->scan_res_handler = wpas_p2p_scan_res_join; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4057 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4058 | |
| 4059 | wpabuf_free(ies); |
| 4060 | |
| 4061 | if (ret) { |
| 4062 | wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - " |
| 4063 | "try again later"); |
| 4064 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4065 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); |
| 4066 | wpas_p2p_check_join_scan_limit(wpa_s); |
| 4067 | } |
| 4068 | } |
| 4069 | |
| 4070 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4071 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx) |
| 4072 | { |
| 4073 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 4074 | wpas_p2p_join_scan_req(wpa_s, 0); |
| 4075 | } |
| 4076 | |
| 4077 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4078 | 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] | 4079 | const u8 *dev_addr, enum p2p_wps_method wps_method, |
| 4080 | int auto_join) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4081 | { |
| 4082 | wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface " |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4083 | MACSTR " dev " MACSTR ")%s", |
| 4084 | MAC2STR(iface_addr), MAC2STR(dev_addr), |
| 4085 | auto_join ? " (auto_join)" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4086 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4087 | wpa_s->p2p_auto_pd = 0; |
| 4088 | wpa_s->p2p_auto_join = !!auto_join; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4089 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN); |
| 4090 | os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN); |
| 4091 | wpa_s->pending_join_wps_method = wps_method; |
| 4092 | |
| 4093 | /* Make sure we are not running find during connection establishment */ |
| 4094 | wpas_p2p_stop_find(wpa_s); |
| 4095 | |
| 4096 | wpa_s->p2p_join_scan_count = 0; |
| 4097 | wpas_p2p_join_scan(wpa_s, NULL); |
| 4098 | return 0; |
| 4099 | } |
| 4100 | |
| 4101 | |
| 4102 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s) |
| 4103 | { |
| 4104 | struct wpa_supplicant *group; |
| 4105 | struct p2p_go_neg_results res; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4106 | struct wpa_bss *bss; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4107 | |
| 4108 | group = wpas_p2p_get_group_iface(wpa_s, 0, 0); |
| 4109 | if (group == NULL) |
| 4110 | return -1; |
| 4111 | if (group != wpa_s) { |
| 4112 | os_memcpy(group->p2p_pin, wpa_s->p2p_pin, |
| 4113 | sizeof(group->p2p_pin)); |
| 4114 | group->p2p_wps_method = wpa_s->p2p_wps_method; |
Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 4115 | } else { |
| 4116 | /* |
| 4117 | * Need to mark the current interface for p2p_group_formation |
| 4118 | * when a separate group interface is not used. This is needed |
| 4119 | * to allow p2p_cancel stop a pending p2p_connect-join. |
| 4120 | * wpas_p2p_init_group_interface() addresses this for the case |
| 4121 | * where a separate group interface is used. |
| 4122 | */ |
| 4123 | wpa_s->global->p2p_group_formation = wpa_s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | group->p2p_in_provisioning = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4127 | 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] | 4128 | |
| 4129 | os_memset(&res, 0, sizeof(res)); |
| 4130 | os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr, |
| 4131 | ETH_ALEN); |
| 4132 | res.wps_method = wpa_s->pending_join_wps_method; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4133 | bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4134 | if (bss) { |
| 4135 | res.freq = bss->freq; |
| 4136 | res.ssid_len = bss->ssid_len; |
| 4137 | os_memcpy(res.ssid, bss->ssid, bss->ssid_len); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 4138 | wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency " |
| 4139 | "from BSS table: %d MHz (SSID %s)", bss->freq, |
| 4140 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4141 | } |
| 4142 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 4143 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 4144 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to " |
| 4145 | "starting client"); |
| 4146 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 4147 | wpa_s->off_channel_freq = 0; |
| 4148 | wpa_s->roc_waiting_drv_freq = 0; |
| 4149 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4150 | wpas_start_wps_enrollee(group, &res); |
| 4151 | |
| 4152 | /* |
| 4153 | * Allow a longer timeout for join-a-running-group than normal 15 |
| 4154 | * second group formation timeout since the GO may not have authorized |
| 4155 | * our connection yet. |
| 4156 | */ |
| 4157 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 4158 | eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout, |
| 4159 | wpa_s, NULL); |
| 4160 | |
| 4161 | return 0; |
| 4162 | } |
| 4163 | |
| 4164 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4165 | 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] | 4166 | int *force_freq, int *pref_freq, int go) |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4167 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4168 | int *freqs, res; |
| 4169 | unsigned int freq_in_use = 0, num, i; |
| 4170 | |
| 4171 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 4172 | if (!freqs) |
| 4173 | return -1; |
| 4174 | |
| 4175 | num = get_shared_radio_freqs(wpa_s, freqs, |
| 4176 | wpa_s->num_multichan_concurrent); |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 4177 | wpa_printf(MSG_DEBUG, |
| 4178 | "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u", |
| 4179 | freq, wpa_s->num_multichan_concurrent, num); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4180 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4181 | if (freq > 0) { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4182 | int ret; |
| 4183 | if (go) |
| 4184 | ret = p2p_supported_freq(wpa_s->global->p2p, freq); |
| 4185 | else |
| 4186 | ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq); |
| 4187 | if (!ret) { |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4188 | wpa_printf(MSG_DEBUG, "P2P: The forced channel " |
| 4189 | "(%u MHz) is not supported for P2P uses", |
| 4190 | freq); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4191 | res = -3; |
| 4192 | goto exit_free; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4193 | } |
| 4194 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4195 | for (i = 0; i < num; i++) { |
| 4196 | if (freqs[i] == freq) |
| 4197 | freq_in_use = 1; |
| 4198 | } |
| 4199 | |
| 4200 | if (num == wpa_s->num_multichan_concurrent && !freq_in_use) { |
| 4201 | wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels", |
| 4202 | freq); |
| 4203 | res = -2; |
| 4204 | goto exit_free; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4205 | } |
| 4206 | wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " |
| 4207 | "requested channel (%u MHz)", freq); |
| 4208 | *force_freq = freq; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4209 | goto exit_ok; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4210 | } |
| 4211 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4212 | for (i = 0; i < num; i++) { |
| 4213 | if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i])) |
| 4214 | continue; |
| 4215 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4216 | if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4217 | wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using", |
| 4218 | freqs[i]); |
Dmitry Shmidt | 51a47d5 | 2013-09-10 10:52:57 -0700 | [diff] [blame] | 4219 | *pref_freq = freqs[i]; |
Dmitry Shmidt | 51a47d5 | 2013-09-10 10:52:57 -0700 | [diff] [blame] | 4220 | } else { |
| 4221 | 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] | 4222 | freqs[i]); |
Dmitry Shmidt | 51a47d5 | 2013-09-10 10:52:57 -0700 | [diff] [blame] | 4223 | *force_freq = freqs[i]; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4224 | } |
| 4225 | break; |
| 4226 | } |
| 4227 | |
| 4228 | if (i == num) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4229 | if (num < wpa_s->num_multichan_concurrent && num > 0) { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4230 | wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel"); |
| 4231 | *force_freq = 0; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4232 | } else if (num < wpa_s->num_multichan_concurrent) { |
| 4233 | wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel"); |
| 4234 | *force_freq = 0; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4235 | } else { |
| 4236 | wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group"); |
| 4237 | res = -2; |
| 4238 | goto exit_free; |
| 4239 | } |
| 4240 | } |
| 4241 | |
| 4242 | exit_ok: |
| 4243 | res = 0; |
| 4244 | exit_free: |
| 4245 | os_free(freqs); |
| 4246 | return res; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4247 | } |
| 4248 | |
| 4249 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4250 | /** |
| 4251 | * wpas_p2p_connect - Request P2P Group Formation to be started |
| 4252 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4253 | * @peer_addr: Address of the peer P2P Device |
| 4254 | * @pin: PIN to use during provisioning or %NULL to indicate PBC mode |
| 4255 | * @persistent_group: Whether to create a persistent group |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4256 | * @auto_join: Whether to select join vs. GO Negotiation automatically |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4257 | * @join: Whether to join an existing group (as a client) instead of starting |
| 4258 | * Group Owner negotiation; @peer_addr is BSSID in that case |
| 4259 | * @auth: Whether to only authorize the connection instead of doing that and |
| 4260 | * initiating Group Owner negotiation |
| 4261 | * @go_intent: GO Intent or -1 to use default |
| 4262 | * @freq: Frequency for the group or 0 for auto-selection |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4263 | * @persistent_id: Persistent group credentials to use for forcing GO |
| 4264 | * parameters or -1 to generate new values (SSID/passphrase) |
| 4265 | * @pd: Whether to send Provision Discovery prior to GO Negotiation as an |
| 4266 | * interoperability workaround when initiating group formation |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4267 | * @ht40: Start GO with 40 MHz channel width |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4268 | * @vht: Start GO with VHT support |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4269 | * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified |
| 4270 | * failure, -2 on failure due to channel not currently available, |
| 4271 | * -3 if forced channel is not supported |
| 4272 | */ |
| 4273 | int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
| 4274 | const char *pin, enum p2p_wps_method wps_method, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4275 | int persistent_group, int auto_join, int join, int auth, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4276 | int go_intent, int freq, int persistent_id, int pd, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4277 | int ht40, int vht) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4278 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4279 | int force_freq = 0, pref_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4280 | int ret = 0, res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4281 | enum wpa_driver_if_type iftype; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4282 | const u8 *if_addr; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4283 | struct wpa_ssid *ssid = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4284 | |
| 4285 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4286 | return -1; |
| 4287 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4288 | if (persistent_id >= 0) { |
| 4289 | ssid = wpa_config_get_network(wpa_s->conf, persistent_id); |
| 4290 | if (ssid == NULL || ssid->disabled != 2 || |
| 4291 | ssid->mode != WPAS_MODE_P2P_GO) |
| 4292 | return -1; |
| 4293 | } |
| 4294 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4295 | os_free(wpa_s->global->add_psk); |
| 4296 | wpa_s->global->add_psk = NULL; |
| 4297 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4298 | if (go_intent < 0) |
| 4299 | go_intent = wpa_s->conf->p2p_go_intent; |
| 4300 | |
| 4301 | if (!auth) |
| 4302 | wpa_s->p2p_long_listen = 0; |
| 4303 | |
| 4304 | wpa_s->p2p_wps_method = wps_method; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4305 | wpa_s->p2p_persistent_group = !!persistent_group; |
| 4306 | wpa_s->p2p_persistent_id = persistent_id; |
| 4307 | wpa_s->p2p_go_intent = go_intent; |
| 4308 | wpa_s->p2p_connect_freq = freq; |
| 4309 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 4310 | wpa_s->p2p_pd_before_go_neg = !!pd; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4311 | wpa_s->p2p_go_ht40 = !!ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4312 | wpa_s->p2p_go_vht = !!vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4313 | |
| 4314 | if (pin) |
| 4315 | os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin)); |
| 4316 | else if (wps_method == WPS_PIN_DISPLAY) { |
| 4317 | ret = wps_generate_pin(); |
| 4318 | os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d", |
| 4319 | ret); |
| 4320 | wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s", |
| 4321 | wpa_s->p2p_pin); |
| 4322 | } else |
| 4323 | wpa_s->p2p_pin[0] = '\0'; |
| 4324 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4325 | if (join || auto_join) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4326 | u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN]; |
| 4327 | if (auth) { |
| 4328 | wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to " |
| 4329 | "connect a running group from " MACSTR, |
| 4330 | MAC2STR(peer_addr)); |
| 4331 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); |
| 4332 | return ret; |
| 4333 | } |
| 4334 | os_memcpy(dev_addr, peer_addr, ETH_ALEN); |
| 4335 | if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr, |
| 4336 | iface_addr) < 0) { |
| 4337 | os_memcpy(iface_addr, peer_addr, ETH_ALEN); |
| 4338 | p2p_get_dev_addr(wpa_s->global->p2p, peer_addr, |
| 4339 | dev_addr); |
| 4340 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4341 | if (auto_join) { |
| 4342 | os_get_time(&wpa_s->p2p_auto_started); |
| 4343 | wpa_printf(MSG_DEBUG, "P2P: Auto join started at " |
| 4344 | "%ld.%06ld", |
| 4345 | wpa_s->p2p_auto_started.sec, |
| 4346 | wpa_s->p2p_auto_started.usec); |
| 4347 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4348 | wpa_s->user_initiated_pd = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4349 | if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method, |
| 4350 | auto_join) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4351 | return -1; |
| 4352 | return ret; |
| 4353 | } |
| 4354 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4355 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 4356 | go_intent == 15); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4357 | if (res) |
| 4358 | return res; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 4359 | wpas_p2p_set_own_freq_preference(wpa_s, |
| 4360 | force_freq ? force_freq : pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4361 | |
| 4362 | wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); |
| 4363 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4364 | if (wpa_s->create_p2p_iface) { |
| 4365 | /* Prepare to add a new interface for the group */ |
| 4366 | iftype = WPA_IF_P2P_GROUP; |
| 4367 | if (go_intent == 15) |
| 4368 | iftype = WPA_IF_P2P_GO; |
| 4369 | if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { |
| 4370 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 4371 | "interface for the group"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4372 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4373 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4374 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4375 | if_addr = wpa_s->pending_interface_addr; |
| 4376 | } else |
| 4377 | if_addr = wpa_s->own_addr; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4378 | |
| 4379 | if (auth) { |
| 4380 | if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4381 | go_intent, if_addr, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4382 | force_freq, persistent_group, ssid, |
| 4383 | pref_freq) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4384 | return -1; |
| 4385 | return ret; |
| 4386 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4387 | |
| 4388 | if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, |
| 4389 | go_intent, if_addr, force_freq, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4390 | persistent_group, ssid, pref_freq) < 0) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4391 | if (wpa_s->create_p2p_iface) |
| 4392 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4393 | return -1; |
| 4394 | } |
| 4395 | return ret; |
| 4396 | } |
| 4397 | |
| 4398 | |
| 4399 | /** |
| 4400 | * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start |
| 4401 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4402 | * @freq: Frequency of the channel in MHz |
| 4403 | * @duration: Duration of the stay on the channel in milliseconds |
| 4404 | * |
| 4405 | * This callback is called when the driver indicates that it has started the |
| 4406 | * requested remain-on-channel duration. |
| 4407 | */ |
| 4408 | void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s, |
| 4409 | unsigned int freq, unsigned int duration) |
| 4410 | { |
| 4411 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4412 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4413 | if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) { |
| 4414 | p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq, |
| 4415 | wpa_s->pending_listen_duration); |
| 4416 | wpa_s->pending_listen_freq = 0; |
| 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | |
| 4421 | static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, |
| 4422 | unsigned int timeout) |
| 4423 | { |
| 4424 | /* Limit maximum Listen state time based on driver limitation. */ |
| 4425 | if (timeout > wpa_s->max_remain_on_chan) |
| 4426 | timeout = wpa_s->max_remain_on_chan; |
| 4427 | |
| 4428 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 4429 | return wpa_drv_p2p_listen(wpa_s, timeout); |
| 4430 | |
| 4431 | return p2p_listen(wpa_s->global->p2p, timeout); |
| 4432 | } |
| 4433 | |
| 4434 | |
| 4435 | /** |
| 4436 | * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout |
| 4437 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4438 | * @freq: Frequency of the channel in MHz |
| 4439 | * |
| 4440 | * This callback is called when the driver indicates that a remain-on-channel |
| 4441 | * operation has been completed, i.e., the duration on the requested channel |
| 4442 | * has timed out. |
| 4443 | */ |
| 4444 | void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s, |
| 4445 | unsigned int freq) |
| 4446 | { |
| 4447 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback " |
| 4448 | "(p2p_long_listen=%d ms pending_action_tx=%p)", |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4449 | wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4450 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4451 | return; |
| 4452 | if (p2p_listen_end(wpa_s->global->p2p, freq) > 0) |
| 4453 | return; /* P2P module started a new operation */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4454 | if (offchannel_pending_action_tx(wpa_s)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4455 | return; |
| 4456 | if (wpa_s->p2p_long_listen > 0) |
| 4457 | wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan; |
| 4458 | if (wpa_s->p2p_long_listen > 0) { |
| 4459 | wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state"); |
| 4460 | wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4461 | } else { |
| 4462 | /* |
| 4463 | * When listen duration is over, stop listen & update p2p_state |
| 4464 | * to IDLE. |
| 4465 | */ |
| 4466 | p2p_stop_listen(wpa_s->global->p2p); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4467 | } |
| 4468 | } |
| 4469 | |
| 4470 | |
| 4471 | /** |
| 4472 | * wpas_p2p_group_remove - Remove a P2P group |
| 4473 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4474 | * @ifname: Network interface name of the group interface or "*" to remove all |
| 4475 | * groups |
| 4476 | * Returns: 0 on success, -1 on failure |
| 4477 | * |
| 4478 | * This function is used to remove a P2P group. This can be used to disconnect |
| 4479 | * from a group in which the local end is a P2P Client or to end a P2P Group in |
| 4480 | * case the local end is the Group Owner. If a virtual network interface was |
| 4481 | * created for this group, that interface will be removed. Otherwise, only the |
| 4482 | * configured P2P group network will be removed from the interface. |
| 4483 | */ |
| 4484 | int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname) |
| 4485 | { |
| 4486 | struct wpa_global *global = wpa_s->global; |
| 4487 | |
| 4488 | if (os_strcmp(ifname, "*") == 0) { |
| 4489 | struct wpa_supplicant *prev; |
| 4490 | wpa_s = global->ifaces; |
| 4491 | while (wpa_s) { |
| 4492 | prev = wpa_s; |
| 4493 | wpa_s = wpa_s->next; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 4494 | if (prev->p2p_group_interface != |
| 4495 | NOT_P2P_GROUP_INTERFACE || |
| 4496 | (prev->current_ssid && |
| 4497 | prev->current_ssid->p2p_group)) |
| 4498 | wpas_p2p_disconnect(prev); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4499 | } |
| 4500 | return 0; |
| 4501 | } |
| 4502 | |
| 4503 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 4504 | if (os_strcmp(wpa_s->ifname, ifname) == 0) |
| 4505 | break; |
| 4506 | } |
| 4507 | |
| 4508 | return wpas_p2p_disconnect(wpa_s); |
| 4509 | } |
| 4510 | |
| 4511 | |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4512 | static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq) |
| 4513 | { |
| 4514 | unsigned int r; |
| 4515 | |
| 4516 | if (freq == 2) { |
| 4517 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz " |
| 4518 | "band"); |
| 4519 | if (wpa_s->best_24_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4520 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 4521 | wpa_s->best_24_freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4522 | freq = wpa_s->best_24_freq; |
| 4523 | wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band " |
| 4524 | "channel: %d MHz", freq); |
| 4525 | } else { |
| 4526 | os_get_random((u8 *) &r, sizeof(r)); |
| 4527 | freq = 2412 + (r % 3) * 25; |
| 4528 | wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band " |
| 4529 | "channel: %d MHz", freq); |
| 4530 | } |
| 4531 | } |
| 4532 | |
| 4533 | if (freq == 5) { |
| 4534 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz " |
| 4535 | "band"); |
| 4536 | if (wpa_s->best_5_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4537 | p2p_supported_freq_go(wpa_s->global->p2p, |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4538 | wpa_s->best_5_freq)) { |
| 4539 | freq = wpa_s->best_5_freq; |
| 4540 | wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band " |
| 4541 | "channel: %d MHz", freq); |
| 4542 | } else { |
| 4543 | os_get_random((u8 *) &r, sizeof(r)); |
| 4544 | freq = 5180 + (r % 4) * 20; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4545 | if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4546 | wpa_printf(MSG_DEBUG, "P2P: Could not select " |
| 4547 | "5 GHz channel for P2P group"); |
| 4548 | return -1; |
| 4549 | } |
| 4550 | wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band " |
| 4551 | "channel: %d MHz", freq); |
| 4552 | } |
| 4553 | } |
| 4554 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4555 | if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4556 | wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO " |
| 4557 | "(%u MHz) is not supported for P2P uses", |
| 4558 | freq); |
| 4559 | return -1; |
| 4560 | } |
| 4561 | |
| 4562 | return freq; |
| 4563 | } |
| 4564 | |
| 4565 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4566 | static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s, |
| 4567 | struct p2p_go_neg_results *params, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4568 | int freq, int ht40, int vht, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4569 | const struct p2p_channels *channels) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4570 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4571 | int res, *freqs; |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 4572 | unsigned int pref_freq; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4573 | unsigned int num, i; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4574 | |
| 4575 | os_memset(params, 0, sizeof(*params)); |
| 4576 | params->role_go = 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4577 | params->ht40 = ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4578 | params->vht = vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4579 | if (freq) { |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4580 | if (!freq_included(channels, freq)) { |
| 4581 | wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not " |
| 4582 | "accepted", freq); |
| 4583 | return -1; |
| 4584 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4585 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced " |
| 4586 | "frequency %d MHz", freq); |
| 4587 | params->freq = freq; |
| 4588 | } else if (wpa_s->conf->p2p_oper_reg_class == 81 && |
| 4589 | wpa_s->conf->p2p_oper_channel >= 1 && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4590 | wpa_s->conf->p2p_oper_channel <= 11 && |
| 4591 | freq_included(channels, |
| 4592 | 2407 + 5 * wpa_s->conf->p2p_oper_channel)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4593 | params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel; |
| 4594 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
| 4595 | "frequency %d MHz", params->freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4596 | } else if ((wpa_s->conf->p2p_oper_reg_class == 115 || |
| 4597 | wpa_s->conf->p2p_oper_reg_class == 116 || |
| 4598 | wpa_s->conf->p2p_oper_reg_class == 117 || |
| 4599 | wpa_s->conf->p2p_oper_reg_class == 124 || |
| 4600 | wpa_s->conf->p2p_oper_reg_class == 126 || |
| 4601 | wpa_s->conf->p2p_oper_reg_class == 127) && |
| 4602 | freq_included(channels, |
| 4603 | 5000 + 5 * wpa_s->conf->p2p_oper_channel)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4604 | params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel; |
| 4605 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
| 4606 | "frequency %d MHz", params->freq); |
| 4607 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 4608 | wpa_s->best_overall_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4609 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 4610 | wpa_s->best_overall_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4611 | freq_included(channels, wpa_s->best_overall_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4612 | params->freq = wpa_s->best_overall_freq; |
| 4613 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall " |
| 4614 | "channel %d MHz", params->freq); |
| 4615 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 4616 | wpa_s->best_24_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4617 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 4618 | wpa_s->best_24_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4619 | freq_included(channels, wpa_s->best_24_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4620 | params->freq = wpa_s->best_24_freq; |
| 4621 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz " |
| 4622 | "channel %d MHz", params->freq); |
| 4623 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 4624 | wpa_s->best_5_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4625 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 4626 | wpa_s->best_5_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4627 | freq_included(channels, wpa_s->best_5_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4628 | params->freq = wpa_s->best_5_freq; |
| 4629 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz " |
| 4630 | "channel %d MHz", params->freq); |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 4631 | } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p, |
| 4632 | channels))) { |
| 4633 | params->freq = pref_freq; |
| 4634 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred " |
| 4635 | "channels", params->freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4636 | } else { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4637 | int chan; |
| 4638 | for (chan = 0; chan < 11; chan++) { |
| 4639 | params->freq = 2412 + chan * 5; |
| 4640 | if (!wpas_p2p_disallowed_freq(wpa_s->global, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4641 | params->freq) && |
| 4642 | freq_included(channels, params->freq)) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4643 | break; |
| 4644 | } |
| 4645 | if (chan == 11) { |
| 4646 | wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel " |
| 4647 | "allowed"); |
| 4648 | return -1; |
| 4649 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4650 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference " |
| 4651 | "known)", params->freq); |
| 4652 | } |
| 4653 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4654 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 4655 | if (!freqs) |
| 4656 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4657 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4658 | res = wpas_p2p_valid_oper_freqs(wpa_s, freqs, |
| 4659 | wpa_s->num_multichan_concurrent); |
| 4660 | if (res < 0) { |
| 4661 | os_free(freqs); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4662 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4663 | } |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4664 | num = res; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4665 | |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4666 | for (i = 0; i < num; i++) { |
| 4667 | if (freq && freqs[i] == freq) |
| 4668 | break; |
| 4669 | if (!freq && freq_included(channels, freqs[i])) { |
| 4670 | wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)", |
| 4671 | freqs[i]); |
| 4672 | params->freq = freqs[i]; |
| 4673 | break; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4674 | } |
| 4675 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4676 | |
| 4677 | if (i == num) { |
| 4678 | if (wpas_p2p_num_unused_channels(wpa_s) <= 0) { |
| 4679 | if (freq) |
| 4680 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq); |
| 4681 | else |
| 4682 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using"); |
| 4683 | os_free(freqs); |
| 4684 | return -1; |
| 4685 | } else if (num == 0) { |
| 4686 | wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels"); |
| 4687 | } else { |
| 4688 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels"); |
| 4689 | } |
| 4690 | } |
| 4691 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4692 | os_free(freqs); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4693 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | |
| 4697 | static struct wpa_supplicant * |
| 4698 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, |
| 4699 | int go) |
| 4700 | { |
| 4701 | struct wpa_supplicant *group_wpa_s; |
| 4702 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 4703 | if (!wpas_p2p_create_iface(wpa_s)) { |
| 4704 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group " |
| 4705 | "operations"); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 4706 | wpa_s->p2p_first_connection_timeout = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4707 | return wpa_s; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 4708 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4709 | |
| 4710 | 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] | 4711 | WPA_IF_P2P_CLIENT) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4712 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 4713 | "P2P: Failed to add group interface"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4714 | return NULL; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 4715 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4716 | group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go); |
| 4717 | if (group_wpa_s == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4718 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 4719 | "P2P: Failed to initialize group interface"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4720 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 4721 | return NULL; |
| 4722 | } |
| 4723 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 4724 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s", |
| 4725 | group_wpa_s->ifname); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 4726 | group_wpa_s->p2p_first_connection_timeout = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4727 | return group_wpa_s; |
| 4728 | } |
| 4729 | |
| 4730 | |
| 4731 | /** |
| 4732 | * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner |
| 4733 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4734 | * @persistent_group: Whether to create a persistent group |
| 4735 | * @freq: Frequency for the group or 0 to indicate no hardcoding |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4736 | * @ht40: Start GO with 40 MHz channel width |
| 4737 | * @vht: Start GO with VHT support |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4738 | * Returns: 0 on success, -1 on failure |
| 4739 | * |
| 4740 | * This function creates a new P2P group with the local end as the Group Owner, |
| 4741 | * i.e., without using Group Owner Negotiation. |
| 4742 | */ |
| 4743 | 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] | 4744 | int freq, int ht40, int vht) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4745 | { |
| 4746 | struct p2p_go_neg_results params; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4747 | |
| 4748 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4749 | return -1; |
| 4750 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4751 | os_free(wpa_s->global->add_psk); |
| 4752 | wpa_s->global->add_psk = NULL; |
| 4753 | |
Dmitry Shmidt | dca3979 | 2011-09-06 11:17:33 -0700 | [diff] [blame] | 4754 | /* Make sure we are not running find during connection establishment */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4755 | wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND"); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4756 | wpas_p2p_stop_find_oper(wpa_s); |
Dmitry Shmidt | dca3979 | 2011-09-06 11:17:33 -0700 | [diff] [blame] | 4757 | |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4758 | freq = wpas_p2p_select_go_freq(wpa_s, freq); |
| 4759 | if (freq < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4760 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4761 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4762 | 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] | 4763 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4764 | if (params.freq && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4765 | !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4766 | wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO " |
| 4767 | "(%u MHz) is not supported for P2P uses", |
| 4768 | params.freq); |
| 4769 | return -1; |
| 4770 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4771 | p2p_go_params(wpa_s->global->p2p, ¶ms); |
| 4772 | params.persistent_group = persistent_group; |
| 4773 | |
| 4774 | wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1); |
| 4775 | if (wpa_s == NULL) |
| 4776 | return -1; |
| 4777 | wpas_start_wps_go(wpa_s, ¶ms, 0); |
| 4778 | |
| 4779 | return 0; |
| 4780 | } |
| 4781 | |
| 4782 | |
| 4783 | static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s, |
| 4784 | struct wpa_ssid *params, int addr_allocated) |
| 4785 | { |
| 4786 | struct wpa_ssid *ssid; |
| 4787 | |
| 4788 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0); |
| 4789 | if (wpa_s == NULL) |
| 4790 | return -1; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4791 | wpa_s->p2p_last_4way_hs_fail = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4792 | |
| 4793 | wpa_supplicant_ap_deinit(wpa_s); |
| 4794 | |
| 4795 | ssid = wpa_config_add_network(wpa_s->conf); |
| 4796 | if (ssid == NULL) |
| 4797 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4798 | wpa_config_set_network_defaults(ssid); |
| 4799 | ssid->temporary = 1; |
| 4800 | ssid->proto = WPA_PROTO_RSN; |
| 4801 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; |
| 4802 | ssid->group_cipher = WPA_CIPHER_CCMP; |
| 4803 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; |
| 4804 | ssid->ssid = os_malloc(params->ssid_len); |
| 4805 | if (ssid->ssid == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4806 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 4807 | return -1; |
| 4808 | } |
| 4809 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); |
| 4810 | ssid->ssid_len = params->ssid_len; |
| 4811 | ssid->p2p_group = 1; |
| 4812 | ssid->export_keys = 1; |
| 4813 | if (params->psk_set) { |
| 4814 | os_memcpy(ssid->psk, params->psk, 32); |
| 4815 | ssid->psk_set = 1; |
| 4816 | } |
| 4817 | if (params->passphrase) |
| 4818 | ssid->passphrase = os_strdup(params->passphrase); |
| 4819 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4820 | wpa_s->show_group_started = 1; |
| 4821 | |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 4822 | wpa_supplicant_select_network(wpa_s, ssid); |
| 4823 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4824 | return 0; |
| 4825 | } |
| 4826 | |
| 4827 | |
| 4828 | int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s, |
| 4829 | struct wpa_ssid *ssid, int addr_allocated, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4830 | int freq, int ht40, int vht, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 4831 | const struct p2p_channels *channels, |
| 4832 | int connection_timeout) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4833 | { |
| 4834 | struct p2p_go_neg_results params; |
| 4835 | int go = 0; |
| 4836 | |
| 4837 | if (ssid->disabled != 2 || ssid->ssid == NULL) |
| 4838 | return -1; |
| 4839 | |
| 4840 | if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) && |
| 4841 | go == (ssid->mode == WPAS_MODE_P2P_GO)) { |
| 4842 | wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is " |
| 4843 | "already running"); |
| 4844 | return 0; |
| 4845 | } |
| 4846 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4847 | os_free(wpa_s->global->add_psk); |
| 4848 | wpa_s->global->add_psk = NULL; |
| 4849 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4850 | /* Make sure we are not running find during connection establishment */ |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4851 | wpas_p2p_stop_find_oper(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4852 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4853 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 4854 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4855 | if (ssid->mode == WPAS_MODE_INFRA) |
| 4856 | return wpas_start_p2p_client(wpa_s, ssid, addr_allocated); |
| 4857 | |
| 4858 | if (ssid->mode != WPAS_MODE_P2P_GO) |
| 4859 | return -1; |
| 4860 | |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4861 | freq = wpas_p2p_select_go_freq(wpa_s, freq); |
| 4862 | if (freq < 0) |
| 4863 | return -1; |
| 4864 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4865 | 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] | 4866 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4867 | |
| 4868 | params.role_go = 1; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4869 | params.psk_set = ssid->psk_set; |
| 4870 | if (params.psk_set) |
| 4871 | os_memcpy(params.psk, ssid->psk, sizeof(params.psk)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4872 | if (ssid->passphrase) { |
| 4873 | if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) { |
| 4874 | wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in " |
| 4875 | "persistent group"); |
| 4876 | return -1; |
| 4877 | } |
| 4878 | os_strlcpy(params.passphrase, ssid->passphrase, |
| 4879 | sizeof(params.passphrase)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4880 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4881 | os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len); |
| 4882 | params.ssid_len = ssid->ssid_len; |
| 4883 | params.persistent_group = 1; |
| 4884 | |
| 4885 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1); |
| 4886 | if (wpa_s == NULL) |
| 4887 | return -1; |
| 4888 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 4889 | wpa_s->p2p_first_connection_timeout = connection_timeout; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4890 | wpas_start_wps_go(wpa_s, ¶ms, 0); |
| 4891 | |
| 4892 | return 0; |
| 4893 | } |
| 4894 | |
| 4895 | |
| 4896 | static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies, |
| 4897 | struct wpabuf *proberesp_ies) |
| 4898 | { |
| 4899 | struct wpa_supplicant *wpa_s = ctx; |
| 4900 | if (wpa_s->ap_iface) { |
| 4901 | struct hostapd_data *hapd = wpa_s->ap_iface->bss[0]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4902 | if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) { |
| 4903 | wpabuf_free(beacon_ies); |
| 4904 | wpabuf_free(proberesp_ies); |
| 4905 | return; |
| 4906 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4907 | if (beacon_ies) { |
| 4908 | wpabuf_free(hapd->p2p_beacon_ie); |
| 4909 | hapd->p2p_beacon_ie = beacon_ies; |
| 4910 | } |
| 4911 | wpabuf_free(hapd->p2p_probe_resp_ie); |
| 4912 | hapd->p2p_probe_resp_ie = proberesp_ies; |
| 4913 | } else { |
| 4914 | wpabuf_free(beacon_ies); |
| 4915 | wpabuf_free(proberesp_ies); |
| 4916 | } |
| 4917 | wpa_supplicant_ap_update_beacon(wpa_s); |
| 4918 | } |
| 4919 | |
| 4920 | |
| 4921 | static void wpas_p2p_idle_update(void *ctx, int idle) |
| 4922 | { |
| 4923 | struct wpa_supplicant *wpa_s = ctx; |
| 4924 | if (!wpa_s->ap_iface) |
| 4925 | return; |
| 4926 | wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not "); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4927 | if (idle) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4928 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 4929 | else |
| 4930 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); |
| 4931 | } |
| 4932 | |
| 4933 | |
| 4934 | struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4935 | struct wpa_ssid *ssid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4936 | { |
| 4937 | struct p2p_group *group; |
| 4938 | struct p2p_group_config *cfg; |
| 4939 | |
| 4940 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 4941 | return NULL; |
| 4942 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4943 | return NULL; |
| 4944 | |
| 4945 | cfg = os_zalloc(sizeof(*cfg)); |
| 4946 | if (cfg == NULL) |
| 4947 | return NULL; |
| 4948 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4949 | if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4950 | cfg->persistent_group = 2; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4951 | else if (ssid->p2p_persistent_group) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4952 | cfg->persistent_group = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4953 | os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN); |
| 4954 | if (wpa_s->max_stations && |
| 4955 | wpa_s->max_stations < wpa_s->conf->max_num_sta) |
| 4956 | cfg->max_clients = wpa_s->max_stations; |
| 4957 | else |
| 4958 | cfg->max_clients = wpa_s->conf->max_num_sta; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4959 | os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len); |
| 4960 | cfg->ssid_len = ssid->ssid_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4961 | cfg->cb_ctx = wpa_s; |
| 4962 | cfg->ie_update = wpas_p2p_ie_update; |
| 4963 | cfg->idle_update = wpas_p2p_idle_update; |
| 4964 | |
| 4965 | group = p2p_group_init(wpa_s->global->p2p, cfg); |
| 4966 | if (group == NULL) |
| 4967 | os_free(cfg); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4968 | if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4969 | p2p_group_notif_formation_done(group); |
| 4970 | wpa_s->p2p_group = group; |
| 4971 | return group; |
| 4972 | } |
| 4973 | |
| 4974 | |
| 4975 | void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
| 4976 | int registrar) |
| 4977 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4978 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 4979 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4980 | if (!wpa_s->p2p_in_provisioning) { |
| 4981 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P " |
| 4982 | "provisioning not in progress"); |
| 4983 | return; |
| 4984 | } |
| 4985 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4986 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { |
| 4987 | u8 go_dev_addr[ETH_ALEN]; |
| 4988 | os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN); |
| 4989 | wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, |
| 4990 | ssid->ssid_len); |
| 4991 | /* Clear any stored provisioning info */ |
| 4992 | p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr); |
| 4993 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4994 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4995 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent, |
| 4996 | NULL); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 4997 | wpa_s->p2p_go_group_formation_completed = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4998 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { |
| 4999 | /* |
| 5000 | * Use a separate timeout for initial data connection to |
| 5001 | * complete to allow the group to be removed automatically if |
| 5002 | * something goes wrong in this step before the P2P group idle |
| 5003 | * timeout mechanism is taken into use. |
| 5004 | */ |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5005 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 5006 | "P2P: Re-start group formation timeout (%d seconds) as client for initial connection", |
| 5007 | P2P_MAX_INITIAL_CONN_WAIT); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5008 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, |
| 5009 | wpas_p2p_group_formation_timeout, |
| 5010 | wpa_s->parent, NULL); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5011 | } else if (ssid) { |
| 5012 | /* |
| 5013 | * Use a separate timeout for initial data connection to |
| 5014 | * complete to allow the group to be removed automatically if |
| 5015 | * the client does not complete data connection successfully. |
| 5016 | */ |
| 5017 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 5018 | "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection", |
| 5019 | P2P_MAX_INITIAL_CONN_WAIT_GO); |
| 5020 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0, |
| 5021 | wpas_p2p_group_formation_timeout, |
| 5022 | wpa_s->parent, NULL); |
| 5023 | /* |
| 5024 | * Complete group formation on first successful data connection |
| 5025 | */ |
| 5026 | wpa_s->p2p_go_group_formation_completed = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5027 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5028 | if (wpa_s->global->p2p) |
| 5029 | p2p_wps_success_cb(wpa_s->global->p2p, peer_addr); |
| 5030 | else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 5031 | wpa_drv_wps_success_cb(wpa_s, peer_addr); |
| 5032 | wpas_group_formation_completed(wpa_s, 1); |
| 5033 | } |
| 5034 | |
| 5035 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5036 | void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s, |
| 5037 | struct wps_event_fail *fail) |
| 5038 | { |
| 5039 | if (!wpa_s->p2p_in_provisioning) { |
| 5040 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P " |
| 5041 | "provisioning not in progress"); |
| 5042 | return; |
| 5043 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5044 | |
| 5045 | if (wpa_s->go_params) { |
| 5046 | p2p_clear_provisioning_info( |
| 5047 | wpa_s->global->p2p, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5048 | wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5049 | } |
| 5050 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5051 | wpas_notify_p2p_wps_failed(wpa_s, fail); |
| 5052 | } |
| 5053 | |
| 5054 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5055 | 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] | 5056 | const char *config_method, |
| 5057 | enum wpas_p2p_prov_disc_use use) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5058 | { |
| 5059 | u16 config_methods; |
| 5060 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5061 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 5062 | wpa_s->pending_pd_use = NORMAL_PD; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5063 | if (os_strncmp(config_method, "display", 7) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5064 | config_methods = WPS_CONFIG_DISPLAY; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5065 | else if (os_strncmp(config_method, "keypad", 6) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5066 | config_methods = WPS_CONFIG_KEYPAD; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5067 | else if (os_strncmp(config_method, "pbc", 3) == 0 || |
| 5068 | os_strncmp(config_method, "pushbutton", 10) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5069 | config_methods = WPS_CONFIG_PUSHBUTTON; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5070 | else { |
| 5071 | wpa_printf(MSG_DEBUG, "P2P: Unknown config method"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5072 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5073 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5074 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5075 | if (use == WPAS_P2P_PD_AUTO) { |
| 5076 | os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN); |
| 5077 | wpa_s->pending_pd_config_methods = config_methods; |
| 5078 | wpa_s->p2p_auto_pd = 1; |
| 5079 | wpa_s->p2p_auto_join = 0; |
| 5080 | wpa_s->pending_pd_before_join = 0; |
| 5081 | wpa_s->auto_pd_scan_retry = 0; |
| 5082 | wpas_p2p_stop_find(wpa_s); |
| 5083 | wpa_s->p2p_join_scan_count = 0; |
| 5084 | os_get_time(&wpa_s->p2p_auto_started); |
| 5085 | wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld", |
| 5086 | wpa_s->p2p_auto_started.sec, |
| 5087 | wpa_s->p2p_auto_started.usec); |
| 5088 | wpas_p2p_join_scan(wpa_s, NULL); |
| 5089 | return 0; |
| 5090 | } |
| 5091 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5092 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { |
| 5093 | return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5094 | config_methods, |
| 5095 | use == WPAS_P2P_PD_FOR_JOIN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5096 | } |
| 5097 | |
| 5098 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) |
| 5099 | return -1; |
| 5100 | |
| 5101 | return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5102 | config_methods, use == WPAS_P2P_PD_FOR_JOIN, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5103 | 0, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5104 | } |
| 5105 | |
| 5106 | |
| 5107 | int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, |
| 5108 | char *end) |
| 5109 | { |
| 5110 | return p2p_scan_result_text(ies, ies_len, buf, end); |
| 5111 | } |
| 5112 | |
| 5113 | |
| 5114 | static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s) |
| 5115 | { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5116 | if (!offchannel_pending_action_tx(wpa_s)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5117 | return; |
| 5118 | |
| 5119 | wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new " |
| 5120 | "operation request"); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5121 | offchannel_clear_pending_action_tx(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5122 | } |
| 5123 | |
| 5124 | |
| 5125 | int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout, |
| 5126 | enum p2p_discovery_type type, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5127 | unsigned int num_req_dev_types, const u8 *req_dev_types, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5128 | const u8 *dev_id, unsigned int search_delay) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5129 | { |
| 5130 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5131 | wpa_s->p2p_long_listen = 0; |
| 5132 | |
| 5133 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 5134 | return wpa_drv_p2p_find(wpa_s, timeout, type); |
| 5135 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5136 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL || |
| 5137 | wpa_s->p2p_in_provisioning) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5138 | return -1; |
| 5139 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5140 | wpa_supplicant_cancel_sched_scan(wpa_s); |
| 5141 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5142 | return p2p_find(wpa_s->global->p2p, timeout, type, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5143 | num_req_dev_types, req_dev_types, dev_id, |
| 5144 | search_delay); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5148 | static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5149 | { |
| 5150 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5151 | wpa_s->p2p_long_listen = 0; |
| 5152 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 5153 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
Jouni Malinen | dc7b713 | 2012-09-14 12:53:47 -0700 | [diff] [blame] | 5154 | wpa_s->global->p2p_cb_on_scan_complete = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5155 | |
| 5156 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { |
| 5157 | wpa_drv_p2p_stop_find(wpa_s); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5158 | return 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5159 | } |
| 5160 | |
| 5161 | if (wpa_s->global->p2p) |
| 5162 | p2p_stop_find(wpa_s->global->p2p); |
| 5163 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5164 | return 0; |
| 5165 | } |
| 5166 | |
| 5167 | |
| 5168 | void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s) |
| 5169 | { |
| 5170 | if (wpas_p2p_stop_find_oper(wpa_s) > 0) |
| 5171 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5172 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 5173 | } |
| 5174 | |
| 5175 | |
| 5176 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx) |
| 5177 | { |
| 5178 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 5179 | wpa_s->p2p_long_listen = 0; |
| 5180 | } |
| 5181 | |
| 5182 | |
| 5183 | int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout) |
| 5184 | { |
| 5185 | int res; |
| 5186 | |
| 5187 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5188 | return -1; |
| 5189 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5190 | wpa_supplicant_cancel_sched_scan(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5191 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5192 | |
| 5193 | if (timeout == 0) { |
| 5194 | /* |
| 5195 | * This is a request for unlimited Listen state. However, at |
| 5196 | * least for now, this is mapped to a Listen state for one |
| 5197 | * hour. |
| 5198 | */ |
| 5199 | timeout = 3600; |
| 5200 | } |
| 5201 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 5202 | wpa_s->p2p_long_listen = 0; |
| 5203 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5204 | /* |
| 5205 | * Stop previous find/listen operation to avoid trying to request a new |
| 5206 | * remain-on-channel operation while the driver is still running the |
| 5207 | * previous one. |
| 5208 | */ |
| 5209 | if (wpa_s->global->p2p) |
| 5210 | p2p_stop_find(wpa_s->global->p2p); |
| 5211 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5212 | res = wpas_p2p_listen_start(wpa_s, timeout * 1000); |
| 5213 | if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) { |
| 5214 | wpa_s->p2p_long_listen = timeout * 1000; |
| 5215 | eloop_register_timeout(timeout, 0, |
| 5216 | wpas_p2p_long_listen_timeout, |
| 5217 | wpa_s, NULL); |
| 5218 | } |
| 5219 | |
| 5220 | return res; |
| 5221 | } |
| 5222 | |
| 5223 | |
| 5224 | int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, |
| 5225 | u8 *buf, size_t len, int p2p_group) |
| 5226 | { |
| 5227 | struct wpabuf *p2p_ie; |
| 5228 | int ret; |
| 5229 | |
| 5230 | if (wpa_s->global->p2p_disabled) |
| 5231 | return -1; |
| 5232 | if (wpa_s->global->p2p == NULL) |
| 5233 | return -1; |
| 5234 | if (bss == NULL) |
| 5235 | return -1; |
| 5236 | |
| 5237 | p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); |
| 5238 | ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len, |
| 5239 | p2p_group, p2p_ie); |
| 5240 | wpabuf_free(p2p_ie); |
| 5241 | |
| 5242 | return ret; |
| 5243 | } |
| 5244 | |
| 5245 | |
| 5246 | 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] | 5247 | const u8 *dst, const u8 *bssid, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5248 | const u8 *ie, size_t ie_len, int ssi_signal) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5249 | { |
| 5250 | if (wpa_s->global->p2p_disabled) |
| 5251 | return 0; |
| 5252 | if (wpa_s->global->p2p == NULL) |
| 5253 | return 0; |
| 5254 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5255 | switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid, |
| 5256 | ie, ie_len)) { |
| 5257 | case P2P_PREQ_NOT_P2P: |
| 5258 | wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len, |
| 5259 | ssi_signal); |
| 5260 | /* fall through */ |
| 5261 | case P2P_PREQ_MALFORMED: |
| 5262 | case P2P_PREQ_NOT_LISTEN: |
| 5263 | case P2P_PREQ_NOT_PROCESSED: |
| 5264 | default: /* make gcc happy */ |
| 5265 | return 0; |
| 5266 | case P2P_PREQ_PROCESSED: |
| 5267 | return 1; |
| 5268 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | |
| 5272 | void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da, |
| 5273 | const u8 *sa, const u8 *bssid, |
| 5274 | u8 category, const u8 *data, size_t len, int freq) |
| 5275 | { |
| 5276 | if (wpa_s->global->p2p_disabled) |
| 5277 | return; |
| 5278 | if (wpa_s->global->p2p == NULL) |
| 5279 | return; |
| 5280 | |
| 5281 | p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len, |
| 5282 | freq); |
| 5283 | } |
| 5284 | |
| 5285 | |
| 5286 | void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies) |
| 5287 | { |
| 5288 | if (wpa_s->global->p2p_disabled) |
| 5289 | return; |
| 5290 | if (wpa_s->global->p2p == NULL) |
| 5291 | return; |
| 5292 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5293 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5294 | } |
| 5295 | |
| 5296 | |
| 5297 | void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s) |
| 5298 | { |
| 5299 | p2p_group_deinit(wpa_s->p2p_group); |
| 5300 | wpa_s->p2p_group = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5301 | |
| 5302 | wpa_s->ap_configured_cb = NULL; |
| 5303 | wpa_s->ap_configured_cb_ctx = NULL; |
| 5304 | wpa_s->ap_configured_cb_data = NULL; |
| 5305 | wpa_s->connect_without_scan = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | |
| 5309 | int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr) |
| 5310 | { |
| 5311 | wpa_s->p2p_long_listen = 0; |
| 5312 | |
| 5313 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 5314 | return wpa_drv_p2p_reject(wpa_s, addr); |
| 5315 | |
| 5316 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5317 | return -1; |
| 5318 | |
| 5319 | return p2p_reject(wpa_s->global->p2p, addr); |
| 5320 | } |
| 5321 | |
| 5322 | |
| 5323 | /* Invite to reinvoke a persistent group */ |
| 5324 | 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] | 5325 | struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5326 | int ht40, int vht, int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5327 | { |
| 5328 | enum p2p_invite_role role; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5329 | u8 *bssid = NULL; |
| 5330 | int force_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5331 | int res; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 5332 | int no_pref_freq_given = pref_freq == 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5333 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 5334 | wpa_s->global->p2p_invite_group = NULL; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5335 | if (peer_addr) |
| 5336 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); |
| 5337 | else |
| 5338 | os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5339 | |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5340 | wpa_s->p2p_persistent_go_freq = freq; |
| 5341 | wpa_s->p2p_go_ht40 = !!ht40; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5342 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 5343 | role = P2P_INVITE_ROLE_GO; |
| 5344 | if (peer_addr == NULL) { |
| 5345 | wpa_printf(MSG_DEBUG, "P2P: Missing peer " |
| 5346 | "address in invitation command"); |
| 5347 | return -1; |
| 5348 | } |
| 5349 | if (wpas_p2p_create_iface(wpa_s)) { |
| 5350 | if (wpas_p2p_add_group_interface(wpa_s, |
| 5351 | WPA_IF_P2P_GO) < 0) { |
| 5352 | wpa_printf(MSG_ERROR, "P2P: Failed to " |
| 5353 | "allocate a new interface for the " |
| 5354 | "group"); |
| 5355 | return -1; |
| 5356 | } |
| 5357 | bssid = wpa_s->pending_interface_addr; |
| 5358 | } else |
| 5359 | bssid = wpa_s->own_addr; |
| 5360 | } else { |
| 5361 | role = P2P_INVITE_ROLE_CLIENT; |
| 5362 | peer_addr = ssid->bssid; |
| 5363 | } |
| 5364 | wpa_s->pending_invite_ssid_id = ssid->id; |
| 5365 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5366 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 5367 | role == P2P_INVITE_ROLE_GO); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5368 | if (res) |
| 5369 | return res; |
Irfan Sheriff | af84a57 | 2012-09-22 16:59:30 -0700 | [diff] [blame] | 5370 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5371 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 5372 | return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid, |
| 5373 | ssid->ssid, ssid->ssid_len, |
| 5374 | go_dev_addr, 1); |
| 5375 | |
| 5376 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5377 | return -1; |
| 5378 | |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 5379 | if (wpa_s->parent->conf->p2p_ignore_shared_freq && |
| 5380 | no_pref_freq_given && pref_freq > 0 && |
| 5381 | wpa_s->num_multichan_concurrent > 1 && |
| 5382 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 5383 | wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration", |
| 5384 | pref_freq); |
| 5385 | pref_freq = 0; |
| 5386 | } |
| 5387 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5388 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5389 | ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, |
| 5390 | 1, pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5391 | } |
| 5392 | |
| 5393 | |
| 5394 | /* Invite to join an active group */ |
| 5395 | int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname, |
| 5396 | const u8 *peer_addr, const u8 *go_dev_addr) |
| 5397 | { |
| 5398 | struct wpa_global *global = wpa_s->global; |
| 5399 | enum p2p_invite_role role; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5400 | u8 *bssid = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5401 | struct wpa_ssid *ssid; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5402 | int persistent; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5403 | int freq = 0, force_freq = 0, pref_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5404 | int res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5405 | |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5406 | wpa_s->p2p_persistent_go_freq = 0; |
| 5407 | wpa_s->p2p_go_ht40 = 0; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5408 | wpa_s->p2p_go_vht = 0; |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5409 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5410 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 5411 | if (os_strcmp(wpa_s->ifname, ifname) == 0) |
| 5412 | break; |
| 5413 | } |
| 5414 | if (wpa_s == NULL) { |
| 5415 | wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname); |
| 5416 | return -1; |
| 5417 | } |
| 5418 | |
| 5419 | ssid = wpa_s->current_ssid; |
| 5420 | if (ssid == NULL) { |
| 5421 | wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for " |
| 5422 | "invitation"); |
| 5423 | return -1; |
| 5424 | } |
| 5425 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 5426 | wpa_s->global->p2p_invite_group = wpa_s; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5427 | persistent = ssid->p2p_persistent_group && |
| 5428 | wpas_p2p_get_persistent(wpa_s->parent, peer_addr, |
| 5429 | ssid->ssid, ssid->ssid_len); |
| 5430 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5431 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 5432 | role = P2P_INVITE_ROLE_ACTIVE_GO; |
| 5433 | bssid = wpa_s->own_addr; |
| 5434 | if (go_dev_addr == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5435 | go_dev_addr = wpa_s->global->p2p_dev_addr; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5436 | freq = ssid->frequency; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5437 | } else { |
| 5438 | role = P2P_INVITE_ROLE_CLIENT; |
| 5439 | if (wpa_s->wpa_state < WPA_ASSOCIATED) { |
| 5440 | wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot " |
| 5441 | "invite to current group"); |
| 5442 | return -1; |
| 5443 | } |
| 5444 | bssid = wpa_s->bssid; |
| 5445 | if (go_dev_addr == NULL && |
| 5446 | !is_zero_ether_addr(wpa_s->go_dev_addr)) |
| 5447 | go_dev_addr = wpa_s->go_dev_addr; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5448 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : |
| 5449 | (int) wpa_s->assoc_freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5450 | } |
| 5451 | wpa_s->parent->pending_invite_ssid_id = -1; |
| 5452 | |
| 5453 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 5454 | return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid, |
| 5455 | ssid->ssid, ssid->ssid_len, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5456 | go_dev_addr, persistent); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5457 | |
| 5458 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5459 | return -1; |
| 5460 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5461 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 5462 | role == P2P_INVITE_ROLE_ACTIVE_GO); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5463 | if (res) |
| 5464 | return res; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5465 | wpas_p2p_set_own_freq_preference(wpa_s, force_freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5466 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5467 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5468 | ssid->ssid, ssid->ssid_len, force_freq, |
| 5469 | go_dev_addr, persistent, pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5470 | } |
| 5471 | |
| 5472 | |
| 5473 | void wpas_p2p_completed(struct wpa_supplicant *wpa_s) |
| 5474 | { |
| 5475 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 5476 | const char *ssid_txt; |
| 5477 | u8 go_dev_addr[ETH_ALEN]; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5478 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5479 | int persistent; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5480 | int freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5481 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5482 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) { |
| 5483 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 5484 | wpa_s->parent, NULL); |
| 5485 | } |
| 5486 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5487 | if (!wpa_s->show_group_started || !ssid) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5488 | goto done; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5489 | |
| 5490 | wpa_s->show_group_started = 0; |
| 5491 | |
| 5492 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); |
| 5493 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 5494 | if (ssid->bssid_set) |
| 5495 | os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN); |
| 5496 | persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, |
| 5497 | ssid->ssid_len); |
| 5498 | os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN); |
| 5499 | |
| 5500 | if (wpa_s->global->p2p_group_formation == wpa_s) |
| 5501 | wpa_s->global->p2p_group_formation = NULL; |
| 5502 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5503 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : |
| 5504 | (int) wpa_s->assoc_freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5505 | if (ssid->passphrase == NULL && ssid->psk_set) { |
| 5506 | char psk[65]; |
| 5507 | wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5508 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 5509 | "%s client ssid=\"%s\" freq=%d psk=%s " |
| 5510 | "go_dev_addr=" MACSTR "%s", |
| 5511 | wpa_s->ifname, ssid_txt, freq, psk, |
| 5512 | MAC2STR(go_dev_addr), |
| 5513 | persistent ? " [PERSISTENT]" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5514 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5515 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 5516 | "%s client ssid=\"%s\" freq=%d " |
| 5517 | "passphrase=\"%s\" go_dev_addr=" MACSTR "%s", |
| 5518 | wpa_s->ifname, ssid_txt, freq, |
| 5519 | ssid->passphrase ? ssid->passphrase : "", |
| 5520 | MAC2STR(go_dev_addr), |
| 5521 | persistent ? " [PERSISTENT]" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5522 | } |
| 5523 | |
| 5524 | if (persistent) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5525 | network_id = wpas_p2p_store_persistent_group(wpa_s->parent, |
| 5526 | ssid, go_dev_addr); |
| 5527 | if (network_id < 0) |
| 5528 | network_id = ssid->id; |
| 5529 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5530 | |
| 5531 | done: |
Dmitry Shmidt | 37d4d6a | 2013-03-18 13:09:42 -0700 | [diff] [blame] | 5532 | wpas_p2p_continue_after_scan(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5533 | } |
| 5534 | |
| 5535 | |
| 5536 | int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1, |
| 5537 | u32 interval1, u32 duration2, u32 interval2) |
| 5538 | { |
| 5539 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 5540 | return -1; |
| 5541 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5542 | return -1; |
| 5543 | |
| 5544 | if (wpa_s->wpa_state < WPA_ASSOCIATED || |
| 5545 | wpa_s->current_ssid == NULL || |
| 5546 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA) |
| 5547 | return -1; |
| 5548 | |
| 5549 | return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid, |
| 5550 | wpa_s->own_addr, wpa_s->assoc_freq, |
| 5551 | duration1, interval1, duration2, interval2); |
| 5552 | } |
| 5553 | |
| 5554 | |
| 5555 | int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period, |
| 5556 | unsigned int interval) |
| 5557 | { |
| 5558 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 5559 | return -1; |
| 5560 | |
| 5561 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5562 | return -1; |
| 5563 | |
| 5564 | return p2p_ext_listen(wpa_s->global->p2p, period, interval); |
| 5565 | } |
| 5566 | |
| 5567 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5568 | static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s) |
| 5569 | { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5570 | if (wpa_s->current_ssid == NULL) { |
| 5571 | /* |
| 5572 | * current_ssid can be cleared when P2P client interface gets |
| 5573 | * disconnected, so assume this interface was used as P2P |
| 5574 | * client. |
| 5575 | */ |
| 5576 | return 1; |
| 5577 | } |
| 5578 | return wpa_s->current_ssid->p2p_group && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5579 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA; |
| 5580 | } |
| 5581 | |
| 5582 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5583 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx) |
| 5584 | { |
| 5585 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 5586 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5587 | 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] | 5588 | wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - " |
| 5589 | "disabled"); |
| 5590 | return; |
| 5591 | } |
| 5592 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5593 | wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate " |
| 5594 | "group"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5595 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5596 | } |
| 5597 | |
| 5598 | |
| 5599 | static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s) |
| 5600 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5601 | int timeout; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5602 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5603 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 5604 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
| 5605 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5606 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) |
| 5607 | return; |
| 5608 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5609 | timeout = wpa_s->conf->p2p_group_idle; |
| 5610 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA && |
| 5611 | (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE)) |
| 5612 | timeout = P2P_MAX_CLIENT_IDLE; |
| 5613 | |
| 5614 | if (timeout == 0) |
| 5615 | return; |
| 5616 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5617 | if (timeout < 0) { |
| 5618 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA) |
| 5619 | timeout = 0; /* special client mode no-timeout */ |
| 5620 | else |
| 5621 | return; |
| 5622 | } |
| 5623 | |
| 5624 | if (wpa_s->p2p_in_provisioning) { |
| 5625 | /* |
| 5626 | * Use the normal group formation timeout during the |
| 5627 | * provisioning phase to avoid terminating this process too |
| 5628 | * early due to group idle timeout. |
| 5629 | */ |
| 5630 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " |
| 5631 | "during provisioning"); |
| 5632 | return; |
| 5633 | } |
Deepthi Gowri | 9e4e8ac | 2013-04-16 11:57:05 +0530 | [diff] [blame] | 5634 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5635 | if (wpa_s->show_group_started) { |
| 5636 | /* |
| 5637 | * Use the normal group formation timeout between the end of |
| 5638 | * the provisioning phase and completion of 4-way handshake to |
| 5639 | * avoid terminating this process too early due to group idle |
| 5640 | * timeout. |
| 5641 | */ |
| 5642 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " |
| 5643 | "while waiting for initial 4-way handshake to " |
| 5644 | "complete"); |
| 5645 | return; |
| 5646 | } |
| 5647 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5648 | 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] | 5649 | timeout); |
| 5650 | eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout, |
| 5651 | wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5652 | } |
| 5653 | |
| 5654 | |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 5655 | /* Returns 1 if the interface was removed */ |
| 5656 | int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, |
| 5657 | u16 reason_code, const u8 *ie, size_t ie_len, |
| 5658 | int locally_generated) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5659 | { |
| 5660 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 5661 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5662 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 5663 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5664 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5665 | if (!locally_generated) |
| 5666 | p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, |
| 5667 | ie_len); |
| 5668 | |
| 5669 | if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated && |
| 5670 | wpa_s->current_ssid && |
| 5671 | wpa_s->current_ssid->p2p_group && |
| 5672 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA) { |
| 5673 | wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group " |
| 5674 | "session is ending"); |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 5675 | if (wpas_p2p_group_delete(wpa_s, |
| 5676 | P2P_GROUP_REMOVAL_GO_ENDING_SESSION) |
| 5677 | > 0) |
| 5678 | return 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5679 | } |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 5680 | |
| 5681 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | |
| 5685 | 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] | 5686 | u16 reason_code, const u8 *ie, size_t ie_len, |
| 5687 | int locally_generated) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5688 | { |
| 5689 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5690 | return; |
| 5691 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 5692 | return; |
| 5693 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5694 | if (!locally_generated) |
| 5695 | p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, |
| 5696 | ie_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5697 | } |
| 5698 | |
| 5699 | |
| 5700 | void wpas_p2p_update_config(struct wpa_supplicant *wpa_s) |
| 5701 | { |
| 5702 | struct p2p_data *p2p = wpa_s->global->p2p; |
| 5703 | |
| 5704 | if (p2p == NULL) |
| 5705 | return; |
| 5706 | |
| 5707 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) |
| 5708 | return; |
| 5709 | |
| 5710 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME) |
| 5711 | p2p_set_dev_name(p2p, wpa_s->conf->device_name); |
| 5712 | |
| 5713 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) |
| 5714 | p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type); |
| 5715 | |
| 5716 | if (wpa_s->wps && |
| 5717 | (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS)) |
| 5718 | p2p_set_config_methods(p2p, wpa_s->wps->config_methods); |
| 5719 | |
| 5720 | if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)) |
| 5721 | p2p_set_uuid(p2p, wpa_s->wps->uuid); |
| 5722 | |
| 5723 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) { |
| 5724 | p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer); |
| 5725 | p2p_set_model_name(p2p, wpa_s->conf->model_name); |
| 5726 | p2p_set_model_number(p2p, wpa_s->conf->model_number); |
| 5727 | p2p_set_serial_number(p2p, wpa_s->conf->serial_number); |
| 5728 | } |
| 5729 | |
| 5730 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) |
| 5731 | p2p_set_sec_dev_types(p2p, |
| 5732 | (void *) wpa_s->conf->sec_device_type, |
| 5733 | wpa_s->conf->num_sec_device_types); |
| 5734 | |
| 5735 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) { |
| 5736 | int i; |
| 5737 | p2p_remove_wps_vendor_extensions(p2p); |
| 5738 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { |
| 5739 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) |
| 5740 | continue; |
| 5741 | p2p_add_wps_vendor_extension( |
| 5742 | p2p, wpa_s->conf->wps_vendor_ext[i]); |
| 5743 | } |
| 5744 | } |
| 5745 | |
| 5746 | if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) && |
| 5747 | wpa_s->conf->country[0] && wpa_s->conf->country[1]) { |
| 5748 | char country[3]; |
| 5749 | country[0] = wpa_s->conf->country[0]; |
| 5750 | country[1] = wpa_s->conf->country[1]; |
| 5751 | country[2] = 0x04; |
| 5752 | p2p_set_country(p2p, country); |
| 5753 | } |
| 5754 | |
| 5755 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) { |
| 5756 | p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix, |
| 5757 | wpa_s->conf->p2p_ssid_postfix ? |
| 5758 | os_strlen(wpa_s->conf->p2p_ssid_postfix) : |
| 5759 | 0); |
| 5760 | } |
| 5761 | |
| 5762 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS) |
| 5763 | p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5764 | |
| 5765 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) { |
| 5766 | u8 reg_class, channel; |
| 5767 | int ret; |
| 5768 | unsigned int r; |
| 5769 | if (wpa_s->conf->p2p_listen_reg_class && |
| 5770 | wpa_s->conf->p2p_listen_channel) { |
| 5771 | reg_class = wpa_s->conf->p2p_listen_reg_class; |
| 5772 | channel = wpa_s->conf->p2p_listen_channel; |
| 5773 | } else { |
| 5774 | reg_class = 81; |
| 5775 | /* |
| 5776 | * Pick one of the social channels randomly as the |
| 5777 | * listen channel. |
| 5778 | */ |
| 5779 | os_get_random((u8 *) &r, sizeof(r)); |
| 5780 | channel = 1 + (r % 3) * 5; |
| 5781 | } |
| 5782 | ret = p2p_set_listen_channel(p2p, reg_class, channel); |
| 5783 | if (ret) |
| 5784 | wpa_printf(MSG_ERROR, "P2P: Own listen channel update " |
| 5785 | "failed: %d", ret); |
| 5786 | } |
| 5787 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) { |
| 5788 | u8 op_reg_class, op_channel, cfg_op_channel; |
| 5789 | int ret = 0; |
| 5790 | unsigned int r; |
| 5791 | if (wpa_s->conf->p2p_oper_reg_class && |
| 5792 | wpa_s->conf->p2p_oper_channel) { |
| 5793 | op_reg_class = wpa_s->conf->p2p_oper_reg_class; |
| 5794 | op_channel = wpa_s->conf->p2p_oper_channel; |
| 5795 | cfg_op_channel = 1; |
| 5796 | } else { |
| 5797 | op_reg_class = 81; |
| 5798 | /* |
| 5799 | * Use random operation channel from (1, 6, 11) |
| 5800 | *if no other preference is indicated. |
| 5801 | */ |
| 5802 | os_get_random((u8 *) &r, sizeof(r)); |
| 5803 | op_channel = 1 + (r % 3) * 5; |
| 5804 | cfg_op_channel = 0; |
| 5805 | } |
| 5806 | ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel, |
| 5807 | cfg_op_channel); |
| 5808 | if (ret) |
| 5809 | wpa_printf(MSG_ERROR, "P2P: Own oper channel update " |
| 5810 | "failed: %d", ret); |
| 5811 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5812 | |
| 5813 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) { |
| 5814 | if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan, |
| 5815 | wpa_s->conf->p2p_pref_chan) < 0) { |
| 5816 | wpa_printf(MSG_ERROR, "P2P: Preferred channel list " |
| 5817 | "update failed"); |
| 5818 | } |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5819 | |
| 5820 | if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) { |
| 5821 | wpa_printf(MSG_ERROR, "P2P: No GO channel list " |
| 5822 | "update failed"); |
| 5823 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5824 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5825 | } |
| 5826 | |
| 5827 | |
| 5828 | int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start, |
| 5829 | int duration) |
| 5830 | { |
| 5831 | if (!wpa_s->ap_iface) |
| 5832 | return -1; |
| 5833 | return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start, |
| 5834 | duration); |
| 5835 | } |
| 5836 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5837 | |
| 5838 | int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled) |
| 5839 | { |
| 5840 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5841 | return -1; |
| 5842 | if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) |
| 5843 | return -1; |
| 5844 | |
| 5845 | wpa_s->global->cross_connection = enabled; |
| 5846 | p2p_set_cross_connect(wpa_s->global->p2p, enabled); |
| 5847 | |
| 5848 | if (!enabled) { |
| 5849 | struct wpa_supplicant *iface; |
| 5850 | |
| 5851 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) |
| 5852 | { |
| 5853 | if (iface->cross_connect_enabled == 0) |
| 5854 | continue; |
| 5855 | |
| 5856 | iface->cross_connect_enabled = 0; |
| 5857 | iface->cross_connect_in_use = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5858 | wpa_msg_global(iface->parent, MSG_INFO, |
| 5859 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 5860 | iface->ifname, |
| 5861 | iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5862 | } |
| 5863 | } |
| 5864 | |
| 5865 | return 0; |
| 5866 | } |
| 5867 | |
| 5868 | |
| 5869 | static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink) |
| 5870 | { |
| 5871 | struct wpa_supplicant *iface; |
| 5872 | |
| 5873 | if (!uplink->global->cross_connection) |
| 5874 | return; |
| 5875 | |
| 5876 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { |
| 5877 | if (!iface->cross_connect_enabled) |
| 5878 | continue; |
| 5879 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != |
| 5880 | 0) |
| 5881 | continue; |
| 5882 | if (iface->ap_iface == NULL) |
| 5883 | continue; |
| 5884 | if (iface->cross_connect_in_use) |
| 5885 | continue; |
| 5886 | |
| 5887 | iface->cross_connect_in_use = 1; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5888 | wpa_msg_global(iface->parent, MSG_INFO, |
| 5889 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", |
| 5890 | iface->ifname, iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5891 | } |
| 5892 | } |
| 5893 | |
| 5894 | |
| 5895 | static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink) |
| 5896 | { |
| 5897 | struct wpa_supplicant *iface; |
| 5898 | |
| 5899 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { |
| 5900 | if (!iface->cross_connect_enabled) |
| 5901 | continue; |
| 5902 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != |
| 5903 | 0) |
| 5904 | continue; |
| 5905 | if (!iface->cross_connect_in_use) |
| 5906 | continue; |
| 5907 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5908 | wpa_msg_global(iface->parent, MSG_INFO, |
| 5909 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 5910 | iface->ifname, iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5911 | iface->cross_connect_in_use = 0; |
| 5912 | } |
| 5913 | } |
| 5914 | |
| 5915 | |
| 5916 | void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s) |
| 5917 | { |
| 5918 | if (wpa_s->ap_iface || wpa_s->current_ssid == NULL || |
| 5919 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA || |
| 5920 | wpa_s->cross_connect_disallowed) |
| 5921 | wpas_p2p_disable_cross_connect(wpa_s); |
| 5922 | else |
| 5923 | wpas_p2p_enable_cross_connect(wpa_s); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5924 | if (!wpa_s->ap_iface && |
| 5925 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 5926 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5927 | } |
| 5928 | |
| 5929 | |
| 5930 | void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s) |
| 5931 | { |
| 5932 | wpas_p2p_disable_cross_connect(wpa_s); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5933 | if (!wpa_s->ap_iface && |
| 5934 | !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout, |
| 5935 | wpa_s, NULL)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5936 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 5937 | } |
| 5938 | |
| 5939 | |
| 5940 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s) |
| 5941 | { |
| 5942 | struct wpa_supplicant *iface; |
| 5943 | |
| 5944 | if (!wpa_s->global->cross_connection) |
| 5945 | return; |
| 5946 | |
| 5947 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 5948 | if (iface == wpa_s) |
| 5949 | continue; |
| 5950 | if (iface->drv_flags & |
| 5951 | WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE) |
| 5952 | continue; |
| 5953 | if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) |
| 5954 | continue; |
| 5955 | |
| 5956 | wpa_s->cross_connect_enabled = 1; |
| 5957 | os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname, |
| 5958 | sizeof(wpa_s->cross_connect_uplink)); |
| 5959 | wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from " |
| 5960 | "%s to %s whenever uplink is available", |
| 5961 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
| 5962 | |
| 5963 | if (iface->ap_iface || iface->current_ssid == NULL || |
| 5964 | iface->current_ssid->mode != WPAS_MODE_INFRA || |
| 5965 | iface->cross_connect_disallowed || |
| 5966 | iface->wpa_state != WPA_COMPLETED) |
| 5967 | break; |
| 5968 | |
| 5969 | wpa_s->cross_connect_in_use = 1; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5970 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 5971 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", |
| 5972 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5973 | break; |
| 5974 | } |
| 5975 | } |
| 5976 | |
| 5977 | |
| 5978 | int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s) |
| 5979 | { |
| 5980 | if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT && |
| 5981 | !wpa_s->p2p_in_provisioning) |
| 5982 | return 0; /* not P2P client operation */ |
| 5983 | |
| 5984 | wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC " |
| 5985 | "session overlap"); |
| 5986 | if (wpa_s != wpa_s->parent) |
| 5987 | wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5988 | wpas_p2p_group_formation_failed(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5989 | return 1; |
| 5990 | } |
| 5991 | |
| 5992 | |
| 5993 | void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s) |
| 5994 | { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5995 | struct p2p_channels chan, cli_chan; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5996 | |
| 5997 | if (wpa_s->global == NULL || wpa_s->global->p2p == NULL) |
| 5998 | return; |
| 5999 | |
| 6000 | os_memset(&chan, 0, sizeof(chan)); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6001 | os_memset(&cli_chan, 0, sizeof(cli_chan)); |
| 6002 | if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6003 | wpa_printf(MSG_ERROR, "P2P: Failed to update supported " |
| 6004 | "channel list"); |
| 6005 | return; |
| 6006 | } |
| 6007 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6008 | p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6009 | } |
| 6010 | |
| 6011 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6012 | static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s, |
| 6013 | struct wpa_scan_results *scan_res) |
| 6014 | { |
| 6015 | wpa_printf(MSG_DEBUG, "P2P: Ignore scan results"); |
| 6016 | } |
| 6017 | |
| 6018 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6019 | int wpas_p2p_cancel(struct wpa_supplicant *wpa_s) |
| 6020 | { |
| 6021 | struct wpa_global *global = wpa_s->global; |
| 6022 | int found = 0; |
| 6023 | const u8 *peer; |
| 6024 | |
| 6025 | if (global->p2p == NULL) |
| 6026 | return -1; |
| 6027 | |
| 6028 | wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation"); |
| 6029 | |
| 6030 | if (wpa_s->pending_interface_name[0] && |
| 6031 | !is_zero_ether_addr(wpa_s->pending_interface_addr)) |
| 6032 | found = 1; |
| 6033 | |
| 6034 | peer = p2p_get_go_neg_peer(global->p2p); |
| 6035 | if (peer) { |
| 6036 | wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer " |
| 6037 | MACSTR, MAC2STR(peer)); |
| 6038 | p2p_unauthorize(global->p2p, peer); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6039 | found = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6040 | } |
| 6041 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6042 | if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) { |
| 6043 | wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join"); |
| 6044 | wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore; |
| 6045 | found = 1; |
| 6046 | } |
| 6047 | |
| 6048 | if (wpa_s->pending_pd_before_join) { |
| 6049 | wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join"); |
| 6050 | wpa_s->pending_pd_before_join = 0; |
| 6051 | found = 1; |
| 6052 | } |
| 6053 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6054 | wpas_p2p_stop_find(wpa_s); |
| 6055 | |
| 6056 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 6057 | if (wpa_s == global->p2p_group_formation && |
| 6058 | (wpa_s->p2p_in_provisioning || |
| 6059 | wpa_s->parent->pending_interface_type == |
| 6060 | WPA_IF_P2P_CLIENT)) { |
| 6061 | wpa_printf(MSG_DEBUG, "P2P: Interface %s in group " |
| 6062 | "formation found - cancelling", |
| 6063 | wpa_s->ifname); |
| 6064 | found = 1; |
| 6065 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6066 | wpa_s->parent, NULL); |
Jouni Malinen | adddfc4 | 2012-10-03 14:31:41 -0700 | [diff] [blame] | 6067 | if (wpa_s->p2p_in_provisioning) { |
| 6068 | wpas_group_formation_completed(wpa_s, 0); |
| 6069 | break; |
| 6070 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6071 | wpas_p2p_group_delete(wpa_s, |
| 6072 | P2P_GROUP_REMOVAL_REQUESTED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6073 | break; |
| 6074 | } |
| 6075 | } |
| 6076 | |
| 6077 | if (!found) { |
| 6078 | wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found"); |
| 6079 | return -1; |
| 6080 | } |
| 6081 | |
| 6082 | return 0; |
| 6083 | } |
| 6084 | |
| 6085 | |
| 6086 | void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s) |
| 6087 | { |
| 6088 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) |
| 6089 | return; |
| 6090 | |
| 6091 | wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not " |
| 6092 | "being available anymore"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6093 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6094 | } |
| 6095 | |
| 6096 | |
| 6097 | void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s, |
| 6098 | int freq_24, int freq_5, int freq_overall) |
| 6099 | { |
| 6100 | struct p2p_data *p2p = wpa_s->global->p2p; |
| 6101 | if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) |
| 6102 | return; |
| 6103 | p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall); |
| 6104 | } |
| 6105 | |
| 6106 | |
| 6107 | int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr) |
| 6108 | { |
| 6109 | u8 peer[ETH_ALEN]; |
| 6110 | struct p2p_data *p2p = wpa_s->global->p2p; |
| 6111 | |
| 6112 | if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) |
| 6113 | return -1; |
| 6114 | |
| 6115 | if (hwaddr_aton(addr, peer)) |
| 6116 | return -1; |
| 6117 | |
| 6118 | return p2p_unauthorize(p2p, peer); |
| 6119 | } |
| 6120 | |
| 6121 | |
| 6122 | /** |
| 6123 | * wpas_p2p_disconnect - Disconnect from a P2P Group |
| 6124 | * @wpa_s: Pointer to wpa_supplicant data |
| 6125 | * Returns: 0 on success, -1 on failure |
| 6126 | * |
| 6127 | * This can be used to disconnect from a group in which the local end is a P2P |
| 6128 | * Client or to end a P2P Group in case the local end is the Group Owner. If a |
| 6129 | * virtual network interface was created for this group, that interface will be |
| 6130 | * removed. Otherwise, only the configured P2P group network will be removed |
| 6131 | * from the interface. |
| 6132 | */ |
| 6133 | int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s) |
| 6134 | { |
| 6135 | |
| 6136 | if (wpa_s == NULL) |
| 6137 | return -1; |
| 6138 | |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6139 | return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ? |
| 6140 | -1 : 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6141 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6142 | |
| 6143 | |
| 6144 | int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s) |
| 6145 | { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6146 | int ret; |
| 6147 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6148 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6149 | return 0; |
| 6150 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6151 | ret = p2p_in_progress(wpa_s->global->p2p); |
| 6152 | if (ret == 0) { |
| 6153 | /* |
| 6154 | * Check whether there is an ongoing WPS provisioning step (or |
| 6155 | * other parts of group formation) on another interface since |
| 6156 | * p2p_in_progress() does not report this to avoid issues for |
| 6157 | * scans during such provisioning step. |
| 6158 | */ |
| 6159 | if (wpa_s->global->p2p_group_formation && |
| 6160 | wpa_s->global->p2p_group_formation != wpa_s) { |
| 6161 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) " |
| 6162 | "in group formation", |
| 6163 | wpa_s->global->p2p_group_formation->ifname); |
| 6164 | ret = 1; |
| 6165 | } |
| 6166 | } |
| 6167 | |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6168 | if (!ret && wpa_s->global->p2p_go_wait_client.sec) { |
| 6169 | struct os_time now; |
| 6170 | os_get_time(&now); |
| 6171 | if (now.sec > wpa_s->global->p2p_go_wait_client.sec + |
| 6172 | P2P_MAX_INITIAL_CONN_WAIT_GO) { |
| 6173 | /* Wait for the first client has expired */ |
| 6174 | wpa_s->global->p2p_go_wait_client.sec = 0; |
| 6175 | } else { |
| 6176 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation"); |
| 6177 | ret = 1; |
| 6178 | } |
| 6179 | } |
| 6180 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6181 | return ret; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6182 | } |
| 6183 | |
| 6184 | |
| 6185 | void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s, |
| 6186 | struct wpa_ssid *ssid) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6187 | { |
| 6188 | if (wpa_s->p2p_in_provisioning && ssid->p2p_group && |
| 6189 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6190 | wpa_s->parent, NULL) > 0) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6191 | /** |
| 6192 | * Remove the network by scheduling the group formation |
| 6193 | * timeout to happen immediately. The teardown code |
| 6194 | * needs to be scheduled to run asynch later so that we |
| 6195 | * don't delete data from under ourselves unexpectedly. |
| 6196 | * Calling wpas_p2p_group_formation_timeout directly |
| 6197 | * causes a series of crashes in WPS failure scenarios. |
| 6198 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6199 | wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to " |
| 6200 | "P2P group network getting removed"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6201 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 6202 | wpa_s->parent, NULL); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6203 | } |
| 6204 | } |
| 6205 | |
| 6206 | |
| 6207 | struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6208 | const u8 *addr, const u8 *ssid, |
| 6209 | size_t ssid_len) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6210 | { |
| 6211 | struct wpa_ssid *s; |
| 6212 | size_t i; |
| 6213 | |
| 6214 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 6215 | if (s->disabled != 2) |
| 6216 | continue; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6217 | if (ssid && |
| 6218 | (ssid_len != s->ssid_len || |
| 6219 | os_memcmp(ssid, s->ssid, ssid_len) != 0)) |
| 6220 | continue; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6221 | if (addr == NULL) { |
| 6222 | if (s->mode == WPAS_MODE_P2P_GO) |
| 6223 | return s; |
| 6224 | continue; |
| 6225 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6226 | if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0) |
| 6227 | return s; /* peer is GO in the persistent group */ |
| 6228 | if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL) |
| 6229 | continue; |
| 6230 | for (i = 0; i < s->num_p2p_clients; i++) { |
| 6231 | if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, |
| 6232 | addr, ETH_ALEN) == 0) |
| 6233 | return s; /* peer is P2P client in persistent |
| 6234 | * group */ |
| 6235 | } |
| 6236 | } |
| 6237 | |
| 6238 | return NULL; |
| 6239 | } |
| 6240 | |
| 6241 | |
| 6242 | void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s, |
| 6243 | const u8 *addr) |
| 6244 | { |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 6245 | if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6246 | wpa_s->parent, NULL) > 0) { |
| 6247 | /* |
| 6248 | * This can happen if WPS provisioning step is not terminated |
| 6249 | * cleanly (e.g., P2P Client does not send WSC_Done). Since the |
| 6250 | * peer was able to connect, there is no need to time out group |
| 6251 | * formation after this, though. In addition, this is used with |
| 6252 | * the initial connection wait on the GO as a separate formation |
| 6253 | * timeout and as such, expected to be hit after the initial WPS |
| 6254 | * provisioning step. |
| 6255 | */ |
| 6256 | wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection"); |
| 6257 | } |
| 6258 | if (!wpa_s->p2p_go_group_formation_completed) { |
| 6259 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection"); |
| 6260 | wpa_s->p2p_go_group_formation_completed = 1; |
| 6261 | wpa_s->global->p2p_group_formation = NULL; |
| 6262 | wpa_s->p2p_in_provisioning = 0; |
| 6263 | } |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6264 | wpa_s->global->p2p_go_wait_client.sec = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6265 | if (addr == NULL) |
| 6266 | return; |
| 6267 | wpas_p2p_add_persistent_group_client(wpa_s, addr); |
| 6268 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6269 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6270 | |
| 6271 | static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, |
| 6272 | int group_added) |
| 6273 | { |
| 6274 | struct wpa_supplicant *group = wpa_s; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6275 | if (wpa_s->global->p2p_group_formation) |
| 6276 | group = wpa_s->global->p2p_group_formation; |
| 6277 | wpa_s = wpa_s->parent; |
| 6278 | offchannel_send_action_done(wpa_s); |
| 6279 | if (group_added) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6280 | wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6281 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation"); |
| 6282 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin, |
| 6283 | wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0, |
| 6284 | 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq, |
| 6285 | wpa_s->p2p_persistent_id, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6286 | wpa_s->p2p_pd_before_go_neg, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6287 | wpa_s->p2p_go_ht40, |
| 6288 | wpa_s->p2p_go_vht); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6289 | } |
| 6290 | |
| 6291 | |
| 6292 | int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s) |
| 6293 | { |
| 6294 | if (!wpa_s->p2p_fallback_to_go_neg || |
| 6295 | wpa_s->p2p_in_provisioning <= 5) |
| 6296 | return 0; |
| 6297 | |
| 6298 | if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0) |
| 6299 | return 0; /* peer operating as a GO */ |
| 6300 | |
| 6301 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - " |
| 6302 | "fallback to GO Negotiation"); |
| 6303 | wpas_p2p_fallback_to_go_neg(wpa_s, 1); |
| 6304 | |
| 6305 | return 1; |
| 6306 | } |
| 6307 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6308 | |
| 6309 | unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s) |
| 6310 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6311 | struct wpa_supplicant *ifs; |
| 6312 | |
| 6313 | if (wpa_s->wpa_state > WPA_SCANNING) { |
| 6314 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to " |
| 6315 | "concurrent operation", |
| 6316 | P2P_CONCURRENT_SEARCH_DELAY); |
| 6317 | return P2P_CONCURRENT_SEARCH_DELAY; |
| 6318 | } |
| 6319 | |
Dmitry Shmidt | 01904cf | 2013-12-05 11:08:35 -0800 | [diff] [blame^] | 6320 | dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant, |
| 6321 | radio_list) { |
| 6322 | if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6323 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search " |
| 6324 | "delay due to concurrent operation on " |
| 6325 | "interface %s", |
| 6326 | P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname); |
| 6327 | return P2P_CONCURRENT_SEARCH_DELAY; |
| 6328 | } |
| 6329 | } |
| 6330 | |
| 6331 | return 0; |
| 6332 | } |
| 6333 | |
Dmitry Shmidt | 37d4d6a | 2013-03-18 13:09:42 -0700 | [diff] [blame] | 6334 | |
| 6335 | void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s) |
| 6336 | { |
| 6337 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not " |
| 6338 | "pending anymore (sta_scan_pending=%d " |
| 6339 | "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending, |
| 6340 | wpa_s->global->p2p_cb_on_scan_complete); |
| 6341 | wpa_s->sta_scan_pending = 0; |
| 6342 | |
| 6343 | if (!wpa_s->global->p2p_cb_on_scan_complete) |
| 6344 | return; |
| 6345 | wpa_s->global->p2p_cb_on_scan_complete = 0; |
| 6346 | |
| 6347 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6348 | return; |
| 6349 | |
| 6350 | if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) { |
| 6351 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation " |
| 6352 | "continued after successful connection"); |
| 6353 | p2p_increase_search_delay(wpa_s->global->p2p, |
| 6354 | wpas_p2p_search_delay(wpa_s)); |
| 6355 | } |
| 6356 | } |
| 6357 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6358 | |
| 6359 | static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s, |
| 6360 | struct wpa_ssid *s, const u8 *addr, |
| 6361 | int iface_addr) |
| 6362 | { |
| 6363 | struct psk_list_entry *psk, *tmp; |
| 6364 | int changed = 0; |
| 6365 | |
| 6366 | dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry, |
| 6367 | list) { |
| 6368 | if ((iface_addr && !psk->p2p && |
| 6369 | os_memcmp(addr, psk->addr, ETH_ALEN) == 0) || |
| 6370 | (!iface_addr && psk->p2p && |
| 6371 | os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) { |
| 6372 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 6373 | "P2P: Remove persistent group PSK list entry for " |
| 6374 | MACSTR " p2p=%u", |
| 6375 | MAC2STR(psk->addr), psk->p2p); |
| 6376 | dl_list_del(&psk->list); |
| 6377 | os_free(psk); |
| 6378 | changed++; |
| 6379 | } |
| 6380 | } |
| 6381 | |
| 6382 | return changed; |
| 6383 | } |
| 6384 | |
| 6385 | |
| 6386 | void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr, |
| 6387 | const u8 *p2p_dev_addr, |
| 6388 | const u8 *psk, size_t psk_len) |
| 6389 | { |
| 6390 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 6391 | struct wpa_ssid *persistent; |
| 6392 | struct psk_list_entry *p; |
| 6393 | |
| 6394 | if (psk_len != sizeof(p->psk)) |
| 6395 | return; |
| 6396 | |
| 6397 | if (p2p_dev_addr) { |
| 6398 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR |
| 6399 | " p2p_dev_addr=" MACSTR, |
| 6400 | MAC2STR(mac_addr), MAC2STR(p2p_dev_addr)); |
| 6401 | if (is_zero_ether_addr(p2p_dev_addr)) |
| 6402 | p2p_dev_addr = NULL; |
| 6403 | } else { |
| 6404 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR, |
| 6405 | MAC2STR(mac_addr)); |
| 6406 | } |
| 6407 | |
| 6408 | if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { |
| 6409 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation"); |
| 6410 | /* To be added to persistent group once created */ |
| 6411 | if (wpa_s->global->add_psk == NULL) { |
| 6412 | wpa_s->global->add_psk = os_zalloc(sizeof(*p)); |
| 6413 | if (wpa_s->global->add_psk == NULL) |
| 6414 | return; |
| 6415 | } |
| 6416 | p = wpa_s->global->add_psk; |
| 6417 | if (p2p_dev_addr) { |
| 6418 | p->p2p = 1; |
| 6419 | os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); |
| 6420 | } else { |
| 6421 | p->p2p = 0; |
| 6422 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
| 6423 | } |
| 6424 | os_memcpy(p->psk, psk, psk_len); |
| 6425 | return; |
| 6426 | } |
| 6427 | |
| 6428 | if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) { |
| 6429 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO"); |
| 6430 | return; |
| 6431 | } |
| 6432 | |
| 6433 | persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid, |
| 6434 | ssid->ssid_len); |
| 6435 | if (!persistent) { |
| 6436 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK"); |
| 6437 | return; |
| 6438 | } |
| 6439 | |
| 6440 | p = os_zalloc(sizeof(*p)); |
| 6441 | if (p == NULL) |
| 6442 | return; |
| 6443 | if (p2p_dev_addr) { |
| 6444 | p->p2p = 1; |
| 6445 | os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); |
| 6446 | } else { |
| 6447 | p->p2p = 0; |
| 6448 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
| 6449 | } |
| 6450 | os_memcpy(p->psk, psk, psk_len); |
| 6451 | |
| 6452 | if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) { |
| 6453 | struct psk_list_entry *last; |
| 6454 | last = dl_list_last(&persistent->psk_list, |
| 6455 | struct psk_list_entry, list); |
| 6456 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for " |
| 6457 | MACSTR " (p2p=%u) to make room for a new one", |
| 6458 | MAC2STR(last->addr), last->p2p); |
| 6459 | dl_list_del(&last->list); |
| 6460 | os_free(last); |
| 6461 | } |
| 6462 | |
| 6463 | wpas_p2p_remove_psk_entry(wpa_s->parent, persistent, |
| 6464 | p2p_dev_addr ? p2p_dev_addr : mac_addr, |
| 6465 | p2p_dev_addr == NULL); |
| 6466 | if (p2p_dev_addr) { |
| 6467 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr=" |
| 6468 | MACSTR, MAC2STR(p2p_dev_addr)); |
| 6469 | } else { |
| 6470 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR, |
| 6471 | MAC2STR(mac_addr)); |
| 6472 | } |
| 6473 | dl_list_add(&persistent->psk_list, &p->list); |
| 6474 | |
| 6475 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 6476 | if (wpa_s->parent->conf->update_config && |
| 6477 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 6478 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
| 6479 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
| 6480 | } |
| 6481 | |
| 6482 | |
| 6483 | static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s, |
| 6484 | struct wpa_ssid *s, const u8 *addr, |
| 6485 | int iface_addr) |
| 6486 | { |
| 6487 | int res; |
| 6488 | |
| 6489 | res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr); |
| 6490 | if (res > 0) { |
| 6491 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 6492 | if (wpa_s->conf->update_config && |
| 6493 | wpa_config_write(wpa_s->confname, wpa_s->conf)) |
| 6494 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 6495 | "P2P: Failed to update configuration"); |
| 6496 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
| 6497 | } |
| 6498 | } |
| 6499 | |
| 6500 | |
| 6501 | static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s, |
| 6502 | const u8 *peer, int iface_addr) |
| 6503 | { |
| 6504 | struct hostapd_data *hapd; |
| 6505 | struct hostapd_wpa_psk *psk, *prev, *rem; |
| 6506 | struct sta_info *sta; |
| 6507 | |
| 6508 | if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL || |
| 6509 | wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO) |
| 6510 | return; |
| 6511 | |
| 6512 | /* Remove per-station PSK entry */ |
| 6513 | hapd = wpa_s->ap_iface->bss[0]; |
| 6514 | prev = NULL; |
| 6515 | psk = hapd->conf->ssid.wpa_psk; |
| 6516 | while (psk) { |
| 6517 | if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) || |
| 6518 | (!iface_addr && |
| 6519 | os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) { |
| 6520 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for " |
| 6521 | MACSTR " iface_addr=%d", |
| 6522 | MAC2STR(peer), iface_addr); |
| 6523 | if (prev) |
| 6524 | prev->next = psk->next; |
| 6525 | else |
| 6526 | hapd->conf->ssid.wpa_psk = psk->next; |
| 6527 | rem = psk; |
| 6528 | psk = psk->next; |
| 6529 | os_free(rem); |
| 6530 | } else { |
| 6531 | prev = psk; |
| 6532 | psk = psk->next; |
| 6533 | } |
| 6534 | } |
| 6535 | |
| 6536 | /* Disconnect from group */ |
| 6537 | if (iface_addr) |
| 6538 | sta = ap_get_sta(hapd, peer); |
| 6539 | else |
| 6540 | sta = ap_get_sta_p2p(hapd, peer); |
| 6541 | if (sta) { |
| 6542 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR |
| 6543 | " (iface_addr=%d) from group", |
| 6544 | MAC2STR(peer), iface_addr); |
| 6545 | hostapd_drv_sta_deauth(hapd, sta->addr, |
| 6546 | WLAN_REASON_DEAUTH_LEAVING); |
| 6547 | ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING); |
| 6548 | } |
| 6549 | } |
| 6550 | |
| 6551 | |
| 6552 | void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer, |
| 6553 | int iface_addr) |
| 6554 | { |
| 6555 | struct wpa_ssid *s; |
| 6556 | struct wpa_supplicant *w; |
| 6557 | |
| 6558 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer)); |
| 6559 | |
| 6560 | /* Remove from any persistent group */ |
| 6561 | for (s = wpa_s->parent->conf->ssid; s; s = s->next) { |
| 6562 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) |
| 6563 | continue; |
| 6564 | if (!iface_addr) |
| 6565 | wpas_remove_persistent_peer(wpa_s, s, peer, 0); |
| 6566 | wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr); |
| 6567 | } |
| 6568 | |
| 6569 | /* Remove from any operating group */ |
| 6570 | for (w = wpa_s->global->ifaces; w; w = w->next) |
| 6571 | wpas_p2p_remove_client_go(w, peer, iface_addr); |
| 6572 | } |
| 6573 | |
| 6574 | |
| 6575 | static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx) |
| 6576 | { |
| 6577 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 6578 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE); |
| 6579 | } |
| 6580 | |
| 6581 | |
| 6582 | int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s) |
| 6583 | { |
| 6584 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 6585 | |
| 6586 | if (ssid == NULL || !ssid->p2p_group) |
| 6587 | return 0; |
| 6588 | |
| 6589 | if (wpa_s->p2p_last_4way_hs_fail && |
| 6590 | wpa_s->p2p_last_4way_hs_fail == ssid) { |
| 6591 | u8 go_dev_addr[ETH_ALEN]; |
| 6592 | struct wpa_ssid *persistent; |
| 6593 | |
| 6594 | if (wpas_p2p_persistent_group(wpa_s, go_dev_addr, |
| 6595 | ssid->ssid, |
| 6596 | ssid->ssid_len) <= 0) { |
| 6597 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group"); |
| 6598 | goto disconnect; |
| 6599 | } |
| 6600 | |
| 6601 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr=" |
| 6602 | MACSTR, MAC2STR(go_dev_addr)); |
| 6603 | persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr, |
| 6604 | ssid->ssid, |
| 6605 | ssid->ssid_len); |
| 6606 | if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) { |
| 6607 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored"); |
| 6608 | goto disconnect; |
| 6609 | } |
| 6610 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 6611 | P2P_EVENT_PERSISTENT_PSK_FAIL "%d", |
| 6612 | persistent->id); |
| 6613 | disconnect: |
| 6614 | wpa_s->p2p_last_4way_hs_fail = NULL; |
| 6615 | /* |
| 6616 | * Remove the group from a timeout to avoid issues with caller |
| 6617 | * continuing to use the interface if this is on a P2P group |
| 6618 | * interface. |
| 6619 | */ |
| 6620 | eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal, |
| 6621 | wpa_s, NULL); |
| 6622 | return 1; |
| 6623 | } |
| 6624 | |
| 6625 | wpa_s->p2p_last_4way_hs_fail = ssid; |
| 6626 | return 0; |
| 6627 | } |
| 6628 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6629 | #ifdef ANDROID_P2P |
Dmitry Shmidt | 1cf4573 | 2013-04-29 17:36:45 -0700 | [diff] [blame] | 6630 | static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx) |
| 6631 | { |
| 6632 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 6633 | |
| 6634 | wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group"); |
| 6635 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT); |
| 6636 | } |
| 6637 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 6638 | int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq, |
| 6639 | struct wpa_ssid *ssid) |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6640 | { |
| 6641 | struct wpa_supplicant *iface = NULL; |
| 6642 | struct p2p_data *p2p = wpa_s->global->p2p; |
| 6643 | |
| 6644 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
Jeff Johnson | 12b1cd9 | 2012-10-07 19:34:24 -0700 | [diff] [blame] | 6645 | if ((iface->current_ssid) && |
| 6646 | (iface->current_ssid->frequency != freq) && |
| 6647 | ((iface->p2p_group_interface) || |
| 6648 | (iface->current_ssid->p2p_group))) { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6649 | |
Jeff Johnson | 12b1cd9 | 2012-10-07 19:34:24 -0700 | [diff] [blame] | 6650 | if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) || |
| 6651 | (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) { |
| 6652 | /* Try to see whether we can move the GO. If it |
| 6653 | * is not possible, remove the GO interface |
| 6654 | */ |
| 6655 | if (wpa_drv_switch_channel(iface, freq) == 0) { |
| 6656 | wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq); |
| 6657 | iface->current_ssid->frequency = freq; |
| 6658 | continue; |
| 6659 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6660 | } |
| 6661 | |
| 6662 | /* If GO cannot be moved or if the conflicting interface is a |
| 6663 | * P2P Client, remove the interface depending up on the connection |
| 6664 | * priority */ |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 6665 | if(!wpas_is_p2p_prioritized(iface)) { |
Dmitry Shmidt | f4f5db3 | 2012-09-11 14:36:56 -0700 | [diff] [blame] | 6666 | /* STA connection has priority over existing |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6667 | * P2P connection. So remove the interface */ |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 6668 | wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel" |
Dmitry Shmidt | 687922c | 2012-03-26 14:02:32 -0700 | [diff] [blame] | 6669 | "concurrent mode frequency conflict"); |
Dmitry Shmidt | 1cf4573 | 2013-04-29 17:36:45 -0700 | [diff] [blame] | 6670 | eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict, |
| 6671 | iface, NULL); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 6672 | /* If connection in progress is p2p connection, do not proceed for the connection */ |
| 6673 | if (wpa_s == iface) |
| 6674 | return -1; |
| 6675 | else |
| 6676 | /* If connection in progress is STA connection, proceed for the connection */ |
| 6677 | return 0; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6678 | } else { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 6679 | /* P2p connection has priority, disable the STA network*/ |
| 6680 | wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid); |
| 6681 | wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT |
| 6682 | " id=%d", ssid->id); |
| 6683 | os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN); |
| 6684 | if (wpa_s == iface) { |
| 6685 | /* p2p connection is in progress, continue connecting...*/ |
| 6686 | return 0; |
| 6687 | } |
| 6688 | else { |
| 6689 | /* STA connection is in progress, do not allow to continue */ |
| 6690 | return -1; |
| 6691 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6692 | } |
| 6693 | } |
| 6694 | } |
| 6695 | return 0; |
| 6696 | } |
| 6697 | #endif |