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