| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * wpa_supplicant - P2P | 
|  | 3 | * Copyright (c) 2009-2010, Atheros Communications | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4 | * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi> | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5 | * | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6 | * This software may be distributed under the terms of the BSD license. | 
|  | 7 | * See README for more details. | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 8 | */ | 
|  | 9 |  | 
|  | 10 | #include "includes.h" | 
|  | 11 |  | 
|  | 12 | #include "common.h" | 
|  | 13 | #include "eloop.h" | 
|  | 14 | #include "common/ieee802_11_common.h" | 
|  | 15 | #include "common/ieee802_11_defs.h" | 
|  | 16 | #include "common/wpa_ctrl.h" | 
|  | 17 | #include "wps/wps_i.h" | 
|  | 18 | #include "p2p/p2p.h" | 
|  | 19 | #include "ap/hostapd.h" | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 20 | #include "ap/ap_config.h" | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 21 | #include "ap/sta_info.h" | 
|  | 22 | #include "ap/ap_drv_ops.h" | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 23 | #include "ap/wps_hostapd.h" | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 24 | #include "ap/p2p_hostapd.h" | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 25 | #include "ap/dfs.h" | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 26 | #include "eapol_supp/eapol_supp_sm.h" | 
|  | 27 | #include "rsn_supp/wpa.h" | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 28 | #include "wpa_supplicant_i.h" | 
|  | 29 | #include "driver_i.h" | 
|  | 30 | #include "ap.h" | 
|  | 31 | #include "config_ssid.h" | 
|  | 32 | #include "config.h" | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 33 | #include "notify.h" | 
|  | 34 | #include "scan.h" | 
|  | 35 | #include "bss.h" | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 36 | #include "offchannel.h" | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 37 | #include "wps_supplicant.h" | 
|  | 38 | #include "p2p_supplicant.h" | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 39 | #include "wifi_display.h" | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 40 |  | 
|  | 41 |  | 
|  | 42 | /* | 
|  | 43 | * How many times to try to scan to find the GO before giving up on join | 
|  | 44 | * request. | 
|  | 45 | */ | 
|  | 46 | #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10 | 
|  | 47 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 48 | #define P2P_AUTO_PD_SCAN_ATTEMPTS 5 | 
|  | 49 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 50 | /** | 
|  | 51 | * Defines time interval in seconds when a GO needs to evacuate a frequency that | 
|  | 52 | * it is currently using, but is no longer valid for P2P use cases. | 
|  | 53 | */ | 
|  | 54 | #define P2P_GO_FREQ_CHANGE_TIME 5 | 
|  | 55 |  | 
|  | 56 | /** | 
|  | 57 | * Defines CSA parameters which are used when GO evacuates the no longer valid | 
|  | 58 | * channel (and if the driver supports channel switch). | 
|  | 59 | */ | 
|  | 60 | #define P2P_GO_CSA_COUNT 7 | 
|  | 61 | #define P2P_GO_CSA_BLOCK_TX 0 | 
|  | 62 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 63 | #ifndef P2P_MAX_CLIENT_IDLE | 
|  | 64 | /* | 
|  | 65 | * How many seconds to try to reconnect to the GO when connection in P2P client | 
|  | 66 | * role has been lost. | 
|  | 67 | */ | 
|  | 68 | #define P2P_MAX_CLIENT_IDLE 10 | 
|  | 69 | #endif /* P2P_MAX_CLIENT_IDLE */ | 
|  | 70 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 71 | #ifndef P2P_MAX_INITIAL_CONN_WAIT | 
|  | 72 | /* | 
|  | 73 | * How many seconds to wait for initial 4-way handshake to get completed after | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 74 | * WPS provisioning step or after the re-invocation of a persistent group on a | 
|  | 75 | * P2P Client. | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 76 | */ | 
|  | 77 | #define P2P_MAX_INITIAL_CONN_WAIT 10 | 
|  | 78 | #endif /* P2P_MAX_INITIAL_CONN_WAIT */ | 
|  | 79 |  | 
| Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 80 | #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO | 
|  | 81 | /* | 
|  | 82 | * How many seconds to wait for initial 4-way handshake to get completed after | 
|  | 83 | * WPS provisioning step on the GO. This controls the extra time the P2P | 
|  | 84 | * operation is considered to be in progress (e.g., to delay other scans) after | 
|  | 85 | * WPS provisioning has been completed on the GO during group formation. | 
|  | 86 | */ | 
|  | 87 | #define P2P_MAX_INITIAL_CONN_WAIT_GO 10 | 
|  | 88 | #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */ | 
|  | 89 |  | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 90 | #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE | 
|  | 91 | /* | 
|  | 92 | * How many seconds to wait for initial 4-way handshake to get completed after | 
|  | 93 | * re-invocation of a persistent group on the GO when the client is expected | 
|  | 94 | * to connect automatically (no user interaction). | 
|  | 95 | */ | 
|  | 96 | #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15 | 
|  | 97 | #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */ | 
|  | 98 |  | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 99 | #define P2P_MGMT_DEVICE_PREFIX		"p2p-dev-" | 
|  | 100 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 101 | /* | 
|  | 102 | * How many seconds to wait to re-attempt to move GOs, in case previous attempt | 
|  | 103 | * was not possible. | 
|  | 104 | */ | 
|  | 105 | #define P2P_RECONSIDER_GO_MOVE_DELAY 30 | 
|  | 106 |  | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 107 | enum p2p_group_removal_reason { | 
|  | 108 | P2P_GROUP_REMOVAL_UNKNOWN, | 
|  | 109 | P2P_GROUP_REMOVAL_SILENT, | 
|  | 110 | P2P_GROUP_REMOVAL_FORMATION_FAILED, | 
|  | 111 | P2P_GROUP_REMOVAL_REQUESTED, | 
|  | 112 | P2P_GROUP_REMOVAL_IDLE_TIMEOUT, | 
|  | 113 | P2P_GROUP_REMOVAL_UNAVAILABLE, | 
|  | 114 | P2P_GROUP_REMOVAL_GO_ENDING_SESSION, | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 115 | P2P_GROUP_REMOVAL_PSK_FAILURE, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 116 | P2P_GROUP_REMOVAL_FREQ_CONFLICT, | 
|  | 117 | P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 118 | }; | 
|  | 119 |  | 
| Jouni Malinen | dc7b713 | 2012-09-14 12:53:47 -0700 | [diff] [blame] | 120 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 121 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx); | 
|  | 122 | static struct wpa_supplicant * | 
|  | 123 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, | 
|  | 124 | int go); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 125 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq, | 
|  | 126 | const u8 *ssid, size_t ssid_len); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 127 | static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq, | 
|  | 128 | int *force_freq, int *pref_freq, int go, | 
|  | 129 | unsigned int *pref_freq_list, | 
|  | 130 | unsigned int *num_pref_freq); | 
| Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 131 | static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq, | 
|  | 132 | const u8 *ssid, size_t ssid_len); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 133 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx); | 
|  | 134 | 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] | 135 | const u8 *dev_addr, enum p2p_wps_method wps_method, | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 136 | int auto_join, int freq, | 
|  | 137 | const u8 *ssid, size_t ssid_len); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 138 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s); | 
|  | 139 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s); | 
|  | 140 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx); | 
|  | 141 | 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] | 142 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, | 
|  | 143 | void *timeout_ctx); | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 144 | static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx); | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 145 | static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, | 
|  | 146 | int group_added); | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 147 | static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s); | 
| Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 148 | static void wpas_stop_listen(void *ctx); | 
| Dmitry Shmidt | 684785c | 2014-05-12 13:34:29 -0700 | [diff] [blame] | 149 | static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx); | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 150 | static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 151 | static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, | 
|  | 152 | enum wpa_driver_if_type type); | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 153 | static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s, | 
|  | 154 | int already_deleted); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 155 | static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s, | 
|  | 156 | struct wpa_used_freq_data *freqs, | 
|  | 157 | unsigned int num); | 
|  | 158 | static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx); | 
|  | 159 | static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq); | 
|  | 160 | static void | 
|  | 161 | wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s, | 
|  | 162 | struct wpa_used_freq_data *freqs, unsigned int num, | 
|  | 163 | enum wpas_p2p_channel_update_trig trig); | 
|  | 164 | static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 165 |  | 
|  | 166 |  | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 167 | /* | 
|  | 168 | * Get the number of concurrent channels that the HW can operate, but that are | 
|  | 169 | * currently not in use by any of the wpa_supplicant interfaces. | 
|  | 170 | */ | 
|  | 171 | static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s) | 
|  | 172 | { | 
|  | 173 | int *freqs; | 
| Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 174 | int num, unused; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 175 |  | 
|  | 176 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); | 
|  | 177 | if (!freqs) | 
|  | 178 | return -1; | 
|  | 179 |  | 
|  | 180 | num = get_shared_radio_freqs(wpa_s, freqs, | 
|  | 181 | wpa_s->num_multichan_concurrent); | 
|  | 182 | os_free(freqs); | 
|  | 183 |  | 
| Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 184 | unused = wpa_s->num_multichan_concurrent - num; | 
|  | 185 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused); | 
|  | 186 | return unused; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 187 | } | 
|  | 188 |  | 
|  | 189 |  | 
|  | 190 | /* | 
|  | 191 | * Get the frequencies that are currently in use by one or more of the virtual | 
|  | 192 | * interfaces, and that are also valid for P2P operation. | 
|  | 193 | */ | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 194 | static unsigned int | 
|  | 195 | wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s, | 
|  | 196 | struct wpa_used_freq_data *p2p_freqs, | 
|  | 197 | unsigned int len) | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 198 | { | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 199 | struct wpa_used_freq_data *freqs; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 200 | unsigned int num, i, j; | 
|  | 201 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 202 | freqs = os_calloc(wpa_s->num_multichan_concurrent, | 
|  | 203 | sizeof(struct wpa_used_freq_data)); | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 204 | if (!freqs) | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 205 | return 0; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 206 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 207 | num = get_shared_radio_freqs_data(wpa_s, freqs, | 
|  | 208 | wpa_s->num_multichan_concurrent); | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 209 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 210 | os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len); | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 211 |  | 
|  | 212 | for (i = 0, j = 0; i < num && j < len; i++) { | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 213 | if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq)) | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 214 | p2p_freqs[j++] = freqs[i]; | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | os_free(freqs); | 
|  | 218 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 219 | dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j); | 
| Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 220 |  | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 221 | return j; | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 |  | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 225 | static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s, | 
|  | 226 | int freq) | 
|  | 227 | { | 
|  | 228 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 229 | return; | 
| Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 230 |  | 
|  | 231 | /* Use the wpa_s used to control the P2P Device operation */ | 
|  | 232 | wpa_s = wpa_s->global->p2p_init_wpa_s; | 
|  | 233 |  | 
|  | 234 | if (wpa_s->conf->p2p_ignore_shared_freq && | 
| Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 235 | freq > 0 && wpa_s->num_multichan_concurrent > 1 && | 
|  | 236 | wpas_p2p_num_unused_channels(wpa_s) > 0) { | 
|  | 237 | wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration", | 
|  | 238 | freq); | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 239 | freq = 0; | 
| Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 240 | } | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 241 | p2p_set_own_freq_preference(wpa_s->global->p2p, freq); | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 245 | static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s, | 
|  | 246 | struct wpa_scan_results *scan_res) | 
|  | 247 | { | 
|  | 248 | size_t i; | 
|  | 249 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 250 | if (wpa_s->p2p_scan_work) { | 
|  | 251 | struct wpa_radio_work *work = wpa_s->p2p_scan_work; | 
|  | 252 | wpa_s->p2p_scan_work = NULL; | 
|  | 253 | radio_work_done(work); | 
|  | 254 | } | 
|  | 255 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 256 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 257 | return; | 
|  | 258 |  | 
|  | 259 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)", | 
|  | 260 | (int) scan_res->num); | 
|  | 261 |  | 
|  | 262 | for (i = 0; i < scan_res->num; i++) { | 
|  | 263 | struct wpa_scan_res *bss = scan_res->res[i]; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 264 | struct os_reltime time_tmp_age, entry_ts; | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 265 | const u8 *ies; | 
|  | 266 | size_t ies_len; | 
|  | 267 |  | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 268 | time_tmp_age.sec = bss->age / 1000; | 
|  | 269 | time_tmp_age.usec = (bss->age % 1000) * 1000; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 270 | os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts); | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 271 |  | 
|  | 272 | ies = (const u8 *) (bss + 1); | 
|  | 273 | ies_len = bss->ie_len; | 
|  | 274 | if (bss->beacon_ie_len > 0 && | 
|  | 275 | !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) && | 
|  | 276 | wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) { | 
|  | 277 | wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for " | 
|  | 278 | MACSTR, MAC2STR(bss->bssid)); | 
|  | 279 | ies = ies + ies_len; | 
|  | 280 | ies_len = bss->beacon_ie_len; | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 284 | if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid, | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 285 | bss->freq, &entry_ts, bss->level, | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 286 | ies, ies_len) > 0) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 287 | break; | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | p2p_scan_res_handled(wpa_s->global->p2p); | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 294 | static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit) | 
|  | 295 | { | 
|  | 296 | struct wpa_supplicant *wpa_s = work->wpa_s; | 
|  | 297 | struct wpa_driver_scan_params *params = work->ctx; | 
|  | 298 | int ret; | 
|  | 299 |  | 
|  | 300 | if (deinit) { | 
| Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 301 | if (!work->started) { | 
|  | 302 | wpa_scan_free_params(params); | 
|  | 303 | return; | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | wpa_s->p2p_scan_work = NULL; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 307 | return; | 
|  | 308 | } | 
|  | 309 |  | 
| Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 310 | if (wpa_s->clear_driver_scan_cache) { | 
|  | 311 | wpa_printf(MSG_DEBUG, | 
|  | 312 | "Request driver to clear scan cache due to local BSS flush"); | 
|  | 313 | params->only_new_results = 1; | 
|  | 314 | } | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 315 | ret = wpa_drv_scan(wpa_s, params); | 
| Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 316 | if (ret == 0) | 
|  | 317 | wpa_s->curr_scan_cookie = params->scan_cookie; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 318 | wpa_scan_free_params(params); | 
|  | 319 | work->ctx = NULL; | 
|  | 320 | if (ret) { | 
|  | 321 | radio_work_done(work); | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 322 | p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 323 | return; | 
|  | 324 | } | 
|  | 325 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 326 | p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 327 | os_get_reltime(&wpa_s->scan_trigger_time); | 
|  | 328 | wpa_s->scan_res_handler = wpas_p2p_scan_res_handler; | 
|  | 329 | wpa_s->own_scan_requested = 1; | 
| Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 330 | wpa_s->clear_driver_scan_cache = 0; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 331 | wpa_s->p2p_scan_work = work; | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 335 | static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s, | 
|  | 336 | int freq) | 
|  | 337 | { | 
|  | 338 | if (wpa_s->global->p2p_24ghz_social_channels && | 
|  | 339 | (freq == 2412 || freq == 2437 || freq == 2462)) { | 
|  | 340 | /* | 
|  | 341 | * Search all social channels regardless of whether these have | 
|  | 342 | * been disabled for P2P operating channel use to avoid missing | 
|  | 343 | * peers. | 
|  | 344 | */ | 
|  | 345 | return 1; | 
|  | 346 | } | 
|  | 347 | return p2p_supported_freq(wpa_s->global->p2p, freq); | 
|  | 348 | } | 
|  | 349 |  | 
|  | 350 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 351 | static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, | 
|  | 352 | unsigned int num_req_dev_types, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 353 | const u8 *req_dev_types, const u8 *dev_id, u16 pw_id) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 354 | { | 
|  | 355 | struct wpa_supplicant *wpa_s = ctx; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 356 | struct wpa_driver_scan_params *params = NULL; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 357 | struct wpabuf *wps_ie, *ies; | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 358 | unsigned int num_channels = 0; | 
|  | 359 | int social_channels_freq[] = { 2412, 2437, 2462, 60480 }; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 360 | size_t ielen; | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 361 | u8 *n, i; | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 362 | unsigned int bands; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 363 |  | 
|  | 364 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 365 | return -1; | 
|  | 366 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 367 | if (wpa_s->p2p_scan_work) { | 
|  | 368 | wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending"); | 
|  | 369 | return -1; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 370 | } | 
|  | 371 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 372 | params = os_zalloc(sizeof(*params)); | 
|  | 373 | if (params == NULL) | 
|  | 374 | return -1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 375 |  | 
|  | 376 | /* P2P Wildcard SSID */ | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 377 | params->num_ssids = 1; | 
|  | 378 | n = os_malloc(P2P_WILDCARD_SSID_LEN); | 
|  | 379 | if (n == NULL) | 
|  | 380 | goto fail; | 
|  | 381 | os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); | 
|  | 382 | params->ssids[0].ssid = n; | 
|  | 383 | params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 384 |  | 
|  | 385 | wpa_s->wps->dev.p2p = 1; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 386 | wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev, | 
|  | 387 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 388 | num_req_dev_types, req_dev_types); | 
|  | 389 | if (wps_ie == NULL) | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 390 | goto fail; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 391 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 392 | switch (type) { | 
|  | 393 | case P2P_SCAN_SOCIAL: | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 394 | params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1, | 
|  | 395 | sizeof(int)); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 396 | if (params->freqs == NULL) | 
|  | 397 | goto fail; | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 398 | for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) { | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 399 | if (wpas_p2p_search_social_channel( | 
|  | 400 | wpa_s, social_channels_freq[i])) | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 401 | params->freqs[num_channels++] = | 
|  | 402 | social_channels_freq[i]; | 
|  | 403 | } | 
|  | 404 | params->freqs[num_channels++] = 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 405 | break; | 
|  | 406 | case P2P_SCAN_FULL: | 
|  | 407 | break; | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 408 | case P2P_SCAN_SPECIFIC: | 
|  | 409 | params->freqs = os_calloc(2, sizeof(int)); | 
|  | 410 | if (params->freqs == NULL) | 
|  | 411 | goto fail; | 
|  | 412 | params->freqs[0] = freq; | 
|  | 413 | params->freqs[1] = 0; | 
|  | 414 | break; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 415 | case P2P_SCAN_SOCIAL_PLUS_ONE: | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 416 | params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2, | 
|  | 417 | sizeof(int)); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 418 | if (params->freqs == NULL) | 
|  | 419 | goto fail; | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 420 | for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) { | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 421 | if (wpas_p2p_search_social_channel( | 
|  | 422 | wpa_s, social_channels_freq[i])) | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 423 | params->freqs[num_channels++] = | 
|  | 424 | social_channels_freq[i]; | 
|  | 425 | } | 
|  | 426 | if (p2p_supported_freq(wpa_s->global->p2p, freq)) | 
|  | 427 | params->freqs[num_channels++] = freq; | 
|  | 428 | params->freqs[num_channels++] = 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 429 | break; | 
|  | 430 | } | 
|  | 431 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 432 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); | 
|  | 433 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); | 
|  | 434 | if (ies == NULL) { | 
|  | 435 | wpabuf_free(wps_ie); | 
|  | 436 | goto fail; | 
|  | 437 | } | 
|  | 438 | wpabuf_put_buf(ies, wps_ie); | 
|  | 439 | wpabuf_free(wps_ie); | 
|  | 440 |  | 
|  | 441 | bands = wpas_get_bands(wpa_s, params->freqs); | 
|  | 442 | p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands); | 
|  | 443 |  | 
|  | 444 | params->p2p_probe = 1; | 
|  | 445 | n = os_malloc(wpabuf_len(ies)); | 
|  | 446 | if (n == NULL) { | 
|  | 447 | wpabuf_free(ies); | 
|  | 448 | goto fail; | 
|  | 449 | } | 
|  | 450 | os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies)); | 
|  | 451 | params->extra_ies = n; | 
|  | 452 | params->extra_ies_len = wpabuf_len(ies); | 
|  | 453 | wpabuf_free(ies); | 
|  | 454 |  | 
| Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 455 | radio_remove_works(wpa_s, "p2p-scan", 0); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 456 | if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb, | 
|  | 457 | params) < 0) | 
|  | 458 | goto fail; | 
|  | 459 | return 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 460 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 461 | fail: | 
|  | 462 | wpa_scan_free_params(params); | 
|  | 463 | return -1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 464 | } | 
|  | 465 |  | 
|  | 466 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 467 | static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface) | 
|  | 468 | { | 
|  | 469 | switch (p2p_group_interface) { | 
|  | 470 | case P2P_GROUP_INTERFACE_PENDING: | 
|  | 471 | return WPA_IF_P2P_GROUP; | 
|  | 472 | case P2P_GROUP_INTERFACE_GO: | 
|  | 473 | return WPA_IF_P2P_GO; | 
|  | 474 | case P2P_GROUP_INTERFACE_CLIENT: | 
|  | 475 | return WPA_IF_P2P_CLIENT; | 
|  | 476 | } | 
|  | 477 |  | 
|  | 478 | return WPA_IF_P2P_GROUP; | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 |  | 
|  | 482 | static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s, | 
|  | 483 | const u8 *ssid, | 
|  | 484 | size_t ssid_len, int *go) | 
|  | 485 | { | 
|  | 486 | struct wpa_ssid *s; | 
|  | 487 |  | 
|  | 488 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
|  | 489 | for (s = wpa_s->conf->ssid; s; s = s->next) { | 
|  | 490 | if (s->disabled != 0 || !s->p2p_group || | 
|  | 491 | s->ssid_len != ssid_len || | 
|  | 492 | os_memcmp(ssid, s->ssid, ssid_len) != 0) | 
|  | 493 | continue; | 
|  | 494 | if (s->mode == WPAS_MODE_P2P_GO && | 
|  | 495 | s != wpa_s->current_ssid) | 
|  | 496 | continue; | 
|  | 497 | if (go) | 
|  | 498 | *go = s->mode == WPAS_MODE_P2P_GO; | 
|  | 499 | return wpa_s; | 
|  | 500 | } | 
|  | 501 | } | 
|  | 502 |  | 
|  | 503 | return NULL; | 
|  | 504 | } | 
|  | 505 |  | 
|  | 506 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 507 | static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx) | 
|  | 508 | { | 
|  | 509 | struct wpa_supplicant *wpa_s = eloop_ctx; | 
|  | 510 | wpa_printf(MSG_DEBUG, | 
|  | 511 | "P2P: Complete previously requested removal of %s", | 
|  | 512 | wpa_s->ifname); | 
|  | 513 | wpas_p2p_disconnect(wpa_s); | 
|  | 514 | } | 
|  | 515 |  | 
|  | 516 |  | 
|  | 517 | static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s, | 
|  | 518 | struct wpa_supplicant *calling_wpa_s) | 
|  | 519 | { | 
|  | 520 | if (calling_wpa_s == wpa_s && wpa_s && | 
|  | 521 | wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { | 
|  | 522 | /* | 
|  | 523 | * The calling wpa_s instance is going to be removed. Do that | 
|  | 524 | * from an eloop callback to keep the instance available until | 
|  | 525 | * the caller has returned. This my be needed, e.g., to provide | 
|  | 526 | * control interface responses on the per-interface socket. | 
|  | 527 | */ | 
|  | 528 | if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect, | 
|  | 529 | wpa_s, NULL) < 0) | 
|  | 530 | return -1; | 
|  | 531 | return 0; | 
|  | 532 | } | 
|  | 533 |  | 
|  | 534 | return wpas_p2p_disconnect(wpa_s); | 
|  | 535 | } | 
|  | 536 |  | 
|  | 537 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 538 | /* Determine total number of clients in active groups where we are the GO */ | 
|  | 539 | static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s) | 
|  | 540 | { | 
|  | 541 | unsigned int count = 0; | 
|  | 542 | struct wpa_ssid *s; | 
|  | 543 |  | 
|  | 544 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
|  | 545 | for (s = wpa_s->conf->ssid; s; s = s->next) { | 
|  | 546 | wpa_printf(MSG_DEBUG, | 
|  | 547 | "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d", | 
|  | 548 | wpa_s, s, s->disabled, s->p2p_group, | 
|  | 549 | s->mode); | 
|  | 550 | if (!s->disabled && s->p2p_group && | 
|  | 551 | s->mode == WPAS_MODE_P2P_GO) { | 
|  | 552 | count += p2p_get_group_num_members( | 
|  | 553 | wpa_s->p2p_group); | 
|  | 554 | } | 
|  | 555 | } | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | return count; | 
|  | 559 | } | 
|  | 560 |  | 
|  | 561 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 562 | static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s) | 
|  | 563 | { | 
|  | 564 | return !wpa_s->p2p_mgmt && wpa_s->current_ssid && | 
|  | 565 | !wpa_s->current_ssid->disabled && | 
|  | 566 | wpa_s->current_ssid->p2p_group && | 
|  | 567 | wpa_s->current_ssid->p2p_persistent_group; | 
|  | 568 | } | 
|  | 569 |  | 
|  | 570 |  | 
|  | 571 | static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s) | 
|  | 572 | { | 
|  | 573 | return p2p_is_active_persistent_group(wpa_s) && | 
|  | 574 | wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO; | 
|  | 575 | } | 
|  | 576 |  | 
|  | 577 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 578 | /* Find an interface for a P2P group where we are the GO */ | 
|  | 579 | static struct wpa_supplicant * | 
|  | 580 | wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s) | 
|  | 581 | { | 
|  | 582 | struct wpa_supplicant *save = NULL; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 583 |  | 
|  | 584 | if (!wpa_s) | 
|  | 585 | return NULL; | 
|  | 586 |  | 
|  | 587 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 588 | if (!p2p_is_active_persistent_go(wpa_s)) | 
|  | 589 | continue; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 590 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 591 | /* Prefer a group with connected clients */ | 
|  | 592 | if (p2p_get_group_num_members(wpa_s->p2p_group)) | 
|  | 593 | return wpa_s; | 
|  | 594 | save = wpa_s; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 595 | } | 
|  | 596 |  | 
|  | 597 | /* No group with connected clients, so pick the one without (if any) */ | 
|  | 598 | return save; | 
|  | 599 | } | 
|  | 600 |  | 
|  | 601 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 602 | static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s) | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 603 | { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 604 | return p2p_is_active_persistent_group(wpa_s) && | 
|  | 605 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA; | 
|  | 606 | } | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 607 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 608 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 609 | /* Find an interface for a P2P group where we are the P2P Client */ | 
|  | 610 | static struct wpa_supplicant * | 
|  | 611 | wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s) | 
|  | 612 | { | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 613 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 614 | if (p2p_is_active_persistent_cli(wpa_s)) | 
|  | 615 | return wpa_s; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 616 | } | 
|  | 617 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 618 | return NULL; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 619 | } | 
|  | 620 |  | 
|  | 621 |  | 
|  | 622 | /* Find a persistent group where we are the GO */ | 
|  | 623 | static struct wpa_ssid * | 
|  | 624 | wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s) | 
|  | 625 | { | 
|  | 626 | struct wpa_ssid *s; | 
|  | 627 |  | 
|  | 628 | for (s = wpa_s->conf->ssid; s; s = s->next) { | 
|  | 629 | if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO) | 
|  | 630 | return s; | 
|  | 631 | } | 
|  | 632 |  | 
|  | 633 | return NULL; | 
|  | 634 | } | 
|  | 635 |  | 
|  | 636 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 637 | static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role, | 
|  | 638 | unsigned int *force_freq, | 
|  | 639 | unsigned int *pref_freq) | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 640 | { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 641 | struct wpa_supplicant *wpa_s = ctx; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 642 | struct wpa_ssid *s; | 
|  | 643 | u8 conncap = P2PS_SETUP_NONE; | 
|  | 644 | unsigned int owned_members = 0; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 645 | struct wpa_supplicant *go_wpa_s, *cli_wpa_s; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 646 | struct wpa_ssid *persistent_go; | 
|  | 647 | int p2p_no_group_iface; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 648 | unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 649 |  | 
|  | 650 | wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role); | 
|  | 651 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 652 | if (force_freq) | 
|  | 653 | *force_freq = 0; | 
|  | 654 | if (pref_freq) | 
|  | 655 | *pref_freq = 0; | 
|  | 656 |  | 
|  | 657 | size = P2P_MAX_PREF_CHANNELS; | 
|  | 658 | if (force_freq && pref_freq && | 
|  | 659 | !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq, | 
|  | 660 | (int *) pref_freq, 0, pref_freq_list, &size)) | 
|  | 661 | wpas_p2p_set_own_freq_preference(wpa_s, | 
|  | 662 | *force_freq ? *force_freq : | 
|  | 663 | *pref_freq); | 
|  | 664 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 665 | /* | 
|  | 666 | * For non-concurrent capable devices: | 
|  | 667 | * If persistent_go, then no new. | 
|  | 668 | * If GO, then no client. | 
|  | 669 | * If client, then no GO. | 
|  | 670 | */ | 
|  | 671 | go_wpa_s = wpas_p2p_get_go_group(wpa_s); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 672 | if (go_wpa_s) | 
|  | 673 | owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 674 | persistent_go = wpas_p2p_get_persistent_go(wpa_s); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 675 | p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s); | 
|  | 676 | cli_wpa_s = wpas_p2p_get_cli_group(wpa_s); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 677 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 678 | wpa_printf(MSG_DEBUG, | 
|  | 679 | "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p", | 
|  | 680 | go_wpa_s, owned_members, cli_wpa_s, persistent_go); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 681 |  | 
|  | 682 | /* If not concurrent, restrict our choices */ | 
|  | 683 | if (p2p_no_group_iface) { | 
|  | 684 | wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface"); | 
|  | 685 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 686 | if (cli_wpa_s) | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 687 | return P2PS_SETUP_NONE; | 
|  | 688 |  | 
|  | 689 | if (go_wpa_s) { | 
|  | 690 | if (role == P2PS_SETUP_CLIENT || | 
|  | 691 | incoming == P2PS_SETUP_GROUP_OWNER || | 
|  | 692 | p2p_client_limit_reached(go_wpa_s->p2p_group)) | 
|  | 693 | return P2PS_SETUP_NONE; | 
|  | 694 |  | 
|  | 695 | return P2PS_SETUP_GROUP_OWNER; | 
|  | 696 | } | 
|  | 697 |  | 
|  | 698 | if (persistent_go) { | 
|  | 699 | if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) { | 
|  | 700 | if (!incoming) | 
|  | 701 | return P2PS_SETUP_GROUP_OWNER | | 
|  | 702 | P2PS_SETUP_CLIENT; | 
|  | 703 | if (incoming == P2PS_SETUP_NEW) { | 
|  | 704 | u8 r; | 
|  | 705 |  | 
|  | 706 | if (os_get_random(&r, sizeof(r)) < 0 || | 
|  | 707 | (r & 1)) | 
|  | 708 | return P2PS_SETUP_CLIENT; | 
|  | 709 | return P2PS_SETUP_GROUP_OWNER; | 
|  | 710 | } | 
|  | 711 | } | 
|  | 712 | } | 
|  | 713 | } | 
|  | 714 |  | 
|  | 715 | /* If a required role has been specified, handle it here */ | 
|  | 716 | if (role && role != P2PS_SETUP_NEW) { | 
|  | 717 | switch (incoming) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 718 | case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW: | 
|  | 719 | case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT: | 
|  | 720 | /* | 
|  | 721 | * Peer has an active GO, so if the role allows it and | 
|  | 722 | * we do not have any active roles, become client. | 
|  | 723 | */ | 
|  | 724 | if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s && | 
|  | 725 | !cli_wpa_s) | 
|  | 726 | return P2PS_SETUP_CLIENT; | 
|  | 727 |  | 
|  | 728 | /* fall through */ | 
|  | 729 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 730 | case P2PS_SETUP_NONE: | 
|  | 731 | case P2PS_SETUP_NEW: | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 732 | conncap = role; | 
|  | 733 | goto grp_owner; | 
|  | 734 |  | 
|  | 735 | case P2PS_SETUP_GROUP_OWNER: | 
|  | 736 | /* | 
|  | 737 | * Must be a complimentary role - cannot be a client to | 
|  | 738 | * more than one peer. | 
|  | 739 | */ | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 740 | if (incoming == role || cli_wpa_s) | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 741 | return P2PS_SETUP_NONE; | 
|  | 742 |  | 
|  | 743 | return P2PS_SETUP_CLIENT; | 
|  | 744 |  | 
|  | 745 | case P2PS_SETUP_CLIENT: | 
|  | 746 | /* Must be a complimentary role */ | 
|  | 747 | if (incoming != role) { | 
|  | 748 | conncap = P2PS_SETUP_GROUP_OWNER; | 
|  | 749 | goto grp_owner; | 
|  | 750 | } | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 751 | /* fall through */ | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 752 |  | 
|  | 753 | default: | 
|  | 754 | return P2PS_SETUP_NONE; | 
|  | 755 | } | 
|  | 756 | } | 
|  | 757 |  | 
|  | 758 | /* | 
|  | 759 | * For now, we only will support ownership of one group, and being a | 
|  | 760 | * client of one group. Therefore, if we have either an existing GO | 
|  | 761 | * group, or an existing client group, we will not do a new GO | 
|  | 762 | * negotiation, but rather try to re-use the existing groups. | 
|  | 763 | */ | 
|  | 764 | switch (incoming) { | 
|  | 765 | case P2PS_SETUP_NONE: | 
|  | 766 | case P2PS_SETUP_NEW: | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 767 | if (cli_wpa_s) | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 768 | conncap = P2PS_SETUP_GROUP_OWNER; | 
|  | 769 | else if (!owned_members) | 
|  | 770 | conncap = P2PS_SETUP_NEW; | 
|  | 771 | else if (incoming == P2PS_SETUP_NONE) | 
|  | 772 | conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT; | 
|  | 773 | else | 
|  | 774 | conncap = P2PS_SETUP_CLIENT; | 
|  | 775 | break; | 
|  | 776 |  | 
|  | 777 | case P2PS_SETUP_CLIENT: | 
|  | 778 | conncap = P2PS_SETUP_GROUP_OWNER; | 
|  | 779 | break; | 
|  | 780 |  | 
|  | 781 | case P2PS_SETUP_GROUP_OWNER: | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 782 | if (!cli_wpa_s) | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 783 | conncap = P2PS_SETUP_CLIENT; | 
|  | 784 | break; | 
|  | 785 |  | 
|  | 786 | case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW: | 
|  | 787 | case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT: | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 788 | if (cli_wpa_s) | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 789 | conncap = P2PS_SETUP_GROUP_OWNER; | 
|  | 790 | else { | 
|  | 791 | u8 r; | 
|  | 792 |  | 
|  | 793 | if (os_get_random(&r, sizeof(r)) < 0 || | 
|  | 794 | (r & 1)) | 
|  | 795 | conncap = P2PS_SETUP_CLIENT; | 
|  | 796 | else | 
|  | 797 | conncap = P2PS_SETUP_GROUP_OWNER; | 
|  | 798 | } | 
|  | 799 | break; | 
|  | 800 |  | 
|  | 801 | default: | 
|  | 802 | return P2PS_SETUP_NONE; | 
|  | 803 | } | 
|  | 804 |  | 
|  | 805 | grp_owner: | 
|  | 806 | if ((conncap & P2PS_SETUP_GROUP_OWNER) || | 
|  | 807 | (!incoming && (conncap & P2PS_SETUP_NEW))) { | 
|  | 808 | if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group)) | 
|  | 809 | conncap &= ~P2PS_SETUP_GROUP_OWNER; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 810 |  | 
|  | 811 | s = wpas_p2p_get_persistent_go(wpa_s); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 812 | if (!s && !go_wpa_s && p2p_no_group_iface) { | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 813 | p2p_set_intended_addr(wpa_s->global->p2p, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 814 | wpa_s->p2p_mgmt ? | 
|  | 815 | wpa_s->parent->own_addr : | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 816 | wpa_s->own_addr); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 817 | } else if (!s && !go_wpa_s) { | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 818 | if (wpas_p2p_add_group_interface(wpa_s, | 
| Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 819 | WPA_IF_P2P_GROUP) < 0) { | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 820 | wpa_printf(MSG_ERROR, | 
|  | 821 | "P2P: Failed to allocate a new interface for the group"); | 
|  | 822 | return P2PS_SETUP_NONE; | 
|  | 823 | } | 
|  | 824 | wpa_s->global->pending_group_iface_for_p2ps = 1; | 
|  | 825 | p2p_set_intended_addr(wpa_s->global->p2p, | 
|  | 826 | wpa_s->pending_interface_addr); | 
|  | 827 | } | 
|  | 828 | } | 
|  | 829 |  | 
|  | 830 | return conncap; | 
|  | 831 | } | 
|  | 832 |  | 
|  | 833 |  | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 834 | static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s, | 
|  | 835 | enum p2p_group_removal_reason removal_reason) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 836 | { | 
|  | 837 | struct wpa_ssid *ssid; | 
|  | 838 | char *gtype; | 
|  | 839 | const char *reason; | 
|  | 840 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 841 | ssid = wpa_s->current_ssid; | 
|  | 842 | if (ssid == NULL) { | 
|  | 843 | /* | 
|  | 844 | * The current SSID was not known, but there may still be a | 
| Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 845 | * pending P2P group interface waiting for provisioning or a | 
|  | 846 | * P2P group that is trying to reconnect. | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 847 | */ | 
|  | 848 | ssid = wpa_s->conf->ssid; | 
|  | 849 | while (ssid) { | 
| Jouni Malinen | 9d71283 | 2012-10-05 11:01:57 -0700 | [diff] [blame] | 850 | if (ssid->p2p_group && ssid->disabled != 2) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 851 | break; | 
|  | 852 | ssid = ssid->next; | 
|  | 853 | } | 
| Jouni Malinen | 5c44edb | 2012-08-31 21:35:32 +0300 | [diff] [blame] | 854 | if (ssid == NULL && | 
|  | 855 | wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) | 
|  | 856 | { | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 857 | wpa_printf(MSG_ERROR, "P2P: P2P group interface " | 
|  | 858 | "not found"); | 
|  | 859 | return -1; | 
|  | 860 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 861 | } | 
|  | 862 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO) | 
|  | 863 | gtype = "GO"; | 
|  | 864 | else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT || | 
|  | 865 | (ssid && ssid->mode == WPAS_MODE_INFRA)) { | 
|  | 866 | wpa_s->reassociate = 0; | 
|  | 867 | wpa_s->disconnected = 1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 868 | gtype = "client"; | 
|  | 869 | } else | 
|  | 870 | gtype = "GO"; | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 871 |  | 
|  | 872 | if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid) | 
|  | 873 | wpas_notify_p2p_group_removed(wpa_s, ssid, gtype); | 
|  | 874 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 875 | if (os_strcmp(gtype, "client") == 0) { | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 876 | wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING); | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 877 | if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal, | 
|  | 878 | wpa_s, NULL)) { | 
|  | 879 | wpa_printf(MSG_DEBUG, | 
|  | 880 | "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal"); | 
|  | 881 | removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE; | 
|  | 882 | eloop_cancel_timeout(wpas_p2p_psk_failure_removal, | 
|  | 883 | wpa_s, NULL); | 
|  | 884 | } | 
|  | 885 | } | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 886 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 887 | if (wpa_s->cross_connect_in_use) { | 
|  | 888 | wpa_s->cross_connect_in_use = 0; | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 889 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 890 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", | 
|  | 891 | wpa_s->ifname, wpa_s->cross_connect_uplink); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 892 | } | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 893 | switch (removal_reason) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 894 | case P2P_GROUP_REMOVAL_REQUESTED: | 
|  | 895 | reason = " reason=REQUESTED"; | 
|  | 896 | break; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 897 | case P2P_GROUP_REMOVAL_FORMATION_FAILED: | 
|  | 898 | reason = " reason=FORMATION_FAILED"; | 
|  | 899 | break; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 900 | case P2P_GROUP_REMOVAL_IDLE_TIMEOUT: | 
|  | 901 | reason = " reason=IDLE"; | 
|  | 902 | break; | 
|  | 903 | case P2P_GROUP_REMOVAL_UNAVAILABLE: | 
|  | 904 | reason = " reason=UNAVAILABLE"; | 
|  | 905 | break; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 906 | case P2P_GROUP_REMOVAL_GO_ENDING_SESSION: | 
|  | 907 | reason = " reason=GO_ENDING_SESSION"; | 
|  | 908 | break; | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 909 | case P2P_GROUP_REMOVAL_PSK_FAILURE: | 
|  | 910 | reason = " reason=PSK_FAILURE"; | 
|  | 911 | break; | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 912 | case P2P_GROUP_REMOVAL_FREQ_CONFLICT: | 
|  | 913 | reason = " reason=FREQ_CONFLICT"; | 
|  | 914 | break; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 915 | default: | 
|  | 916 | reason = ""; | 
|  | 917 | break; | 
|  | 918 | } | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 919 | if (removal_reason != P2P_GROUP_REMOVAL_SILENT) { | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 920 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 921 | P2P_EVENT_GROUP_REMOVED "%s %s%s", | 
|  | 922 | wpa_s->ifname, gtype, reason); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 923 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 924 |  | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 925 | if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0) | 
|  | 926 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout"); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 927 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) | 
|  | 928 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 929 | if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 930 | wpa_s->p2pdev, NULL) > 0) { | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 931 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation " | 
|  | 932 | "timeout"); | 
| Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 933 | wpa_s->p2p_in_provisioning = 0; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 934 | wpas_p2p_group_formation_failed(wpa_s, 1); | 
| Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 935 | } | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 936 |  | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 937 | wpa_s->p2p_in_invitation = 0; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 938 | eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL); | 
|  | 939 | eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL); | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 940 |  | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 941 | /* | 
|  | 942 | * Make sure wait for the first client does not remain active after the | 
|  | 943 | * group has been removed. | 
|  | 944 | */ | 
|  | 945 | wpa_s->global->p2p_go_wait_client.sec = 0; | 
|  | 946 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 947 | if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { | 
|  | 948 | struct wpa_global *global; | 
|  | 949 | char *ifname; | 
|  | 950 | enum wpa_driver_if_type type; | 
|  | 951 | wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s", | 
|  | 952 | wpa_s->ifname); | 
|  | 953 | global = wpa_s->global; | 
|  | 954 | ifname = os_strdup(wpa_s->ifname); | 
|  | 955 | type = wpas_p2p_if_type(wpa_s->p2p_group_interface); | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 956 | eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL); | 
| Dmitry Shmidt | e15c7b5 | 2011-08-03 15:04:35 -0700 | [diff] [blame] | 957 | wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 958 | wpa_s = global->ifaces; | 
|  | 959 | if (wpa_s && ifname) | 
|  | 960 | wpa_drv_if_remove(wpa_s, type, ifname); | 
|  | 961 | os_free(ifname); | 
| Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 962 | return 1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 963 | } | 
|  | 964 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 965 | /* | 
|  | 966 | * The primary interface was used for P2P group operations, so | 
|  | 967 | * need to reset its p2pdev. | 
|  | 968 | */ | 
|  | 969 | wpa_s->p2pdev = wpa_s->parent; | 
|  | 970 |  | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 971 | if (!wpa_s->p2p_go_group_formation_completed) { | 
|  | 972 | wpa_s->global->p2p_group_formation = NULL; | 
|  | 973 | wpa_s->p2p_in_provisioning = 0; | 
|  | 974 | } | 
|  | 975 |  | 
| Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 976 | wpa_s->show_group_started = 0; | 
|  | 977 | os_free(wpa_s->go_params); | 
|  | 978 | wpa_s->go_params = NULL; | 
|  | 979 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 980 | os_free(wpa_s->p2p_group_common_freqs); | 
|  | 981 | wpa_s->p2p_group_common_freqs = NULL; | 
|  | 982 | wpa_s->p2p_group_common_freqs_num = 0; | 
|  | 983 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 984 | wpa_s->waiting_presence_resp = 0; | 
|  | 985 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 986 | wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network"); | 
|  | 987 | if (ssid && (ssid->p2p_group || | 
|  | 988 | ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || | 
|  | 989 | (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) { | 
|  | 990 | int id = ssid->id; | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 991 | if (ssid == wpa_s->current_ssid) { | 
|  | 992 | wpa_sm_set_config(wpa_s->wpa, NULL); | 
|  | 993 | eapol_sm_notify_config(wpa_s->eapol, NULL, NULL); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 994 | wpa_s->current_ssid = NULL; | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 995 | } | 
|  | 996 | /* | 
|  | 997 | * Networks objects created during any P2P activities are not | 
|  | 998 | * exposed out as they might/will confuse certain non-P2P aware | 
|  | 999 | * applications since these network objects won't behave like | 
|  | 1000 | * regular ones. | 
|  | 1001 | * | 
|  | 1002 | * Likewise, we don't send out network removed signals for such | 
|  | 1003 | * network objects. | 
|  | 1004 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1005 | wpa_config_remove_network(wpa_s->conf, id); | 
|  | 1006 | wpa_supplicant_clear_status(wpa_s); | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1007 | wpa_supplicant_cancel_sched_scan(wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1008 | } else { | 
|  | 1009 | wpa_printf(MSG_DEBUG, "P2P: Temporary group network not " | 
|  | 1010 | "found"); | 
|  | 1011 | } | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1012 | if (wpa_s->ap_iface) | 
|  | 1013 | wpa_supplicant_ap_deinit(wpa_s); | 
|  | 1014 | else | 
|  | 1015 | wpa_drv_deinit_p2p_cli(wpa_s); | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1016 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1017 | os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN); | 
|  | 1018 |  | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1019 | return 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1020 | } | 
|  | 1021 |  | 
|  | 1022 |  | 
|  | 1023 | static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s, | 
|  | 1024 | u8 *go_dev_addr, | 
|  | 1025 | const u8 *ssid, size_t ssid_len) | 
|  | 1026 | { | 
|  | 1027 | struct wpa_bss *bss; | 
|  | 1028 | const u8 *bssid; | 
|  | 1029 | struct wpabuf *p2p; | 
|  | 1030 | u8 group_capab; | 
|  | 1031 | const u8 *addr; | 
|  | 1032 |  | 
|  | 1033 | if (wpa_s->go_params) | 
|  | 1034 | bssid = wpa_s->go_params->peer_interface_addr; | 
|  | 1035 | else | 
|  | 1036 | bssid = wpa_s->bssid; | 
|  | 1037 |  | 
|  | 1038 | bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1039 | if (bss == NULL && wpa_s->go_params && | 
|  | 1040 | !is_zero_ether_addr(wpa_s->go_params->peer_device_addr)) | 
|  | 1041 | bss = wpa_bss_get_p2p_dev_addr( | 
|  | 1042 | wpa_s, wpa_s->go_params->peer_device_addr); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1043 | if (bss == NULL) { | 
|  | 1044 | u8 iface_addr[ETH_ALEN]; | 
|  | 1045 | if (p2p_get_interface_addr(wpa_s->global->p2p, bssid, | 
|  | 1046 | iface_addr) == 0) | 
|  | 1047 | bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len); | 
|  | 1048 | } | 
|  | 1049 | if (bss == NULL) { | 
|  | 1050 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " | 
|  | 1051 | "group is persistent - BSS " MACSTR " not found", | 
|  | 1052 | MAC2STR(bssid)); | 
|  | 1053 | return 0; | 
|  | 1054 | } | 
|  | 1055 |  | 
|  | 1056 | p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 1057 | if (p2p == NULL) | 
|  | 1058 | p2p = wpa_bss_get_vendor_ie_multi_beacon(bss, | 
|  | 1059 | P2P_IE_VENDOR_TYPE); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1060 | if (p2p == NULL) { | 
|  | 1061 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " | 
|  | 1062 | "group is persistent - BSS " MACSTR | 
|  | 1063 | " did not include P2P IE", MAC2STR(bssid)); | 
|  | 1064 | wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs", | 
|  | 1065 | (u8 *) (bss + 1), bss->ie_len); | 
|  | 1066 | wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs", | 
|  | 1067 | ((u8 *) bss + 1) + bss->ie_len, | 
|  | 1068 | bss->beacon_ie_len); | 
|  | 1069 | return 0; | 
|  | 1070 | } | 
|  | 1071 |  | 
|  | 1072 | group_capab = p2p_get_group_capab(p2p); | 
|  | 1073 | addr = p2p_get_go_dev_addr(p2p); | 
|  | 1074 | wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: " | 
|  | 1075 | "group_capab=0x%x", group_capab); | 
|  | 1076 | if (addr) { | 
|  | 1077 | os_memcpy(go_dev_addr, addr, ETH_ALEN); | 
|  | 1078 | wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR, | 
|  | 1079 | MAC2STR(addr)); | 
|  | 1080 | } else | 
|  | 1081 | os_memset(go_dev_addr, 0, ETH_ALEN); | 
|  | 1082 | wpabuf_free(p2p); | 
|  | 1083 |  | 
|  | 1084 | wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x " | 
|  | 1085 | "go_dev_addr=" MACSTR, | 
|  | 1086 | MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr)); | 
|  | 1087 |  | 
| Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 1088 | return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1089 | } | 
|  | 1090 |  | 
|  | 1091 |  | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1092 | static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s, | 
|  | 1093 | struct wpa_ssid *ssid, | 
|  | 1094 | const u8 *go_dev_addr) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1095 | { | 
|  | 1096 | struct wpa_ssid *s; | 
|  | 1097 | int changed = 0; | 
|  | 1098 |  | 
|  | 1099 | wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent " | 
|  | 1100 | "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr)); | 
|  | 1101 | for (s = wpa_s->conf->ssid; s; s = s->next) { | 
|  | 1102 | if (s->disabled == 2 && | 
|  | 1103 | os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 && | 
|  | 1104 | s->ssid_len == ssid->ssid_len && | 
|  | 1105 | os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0) | 
|  | 1106 | break; | 
|  | 1107 | } | 
|  | 1108 |  | 
|  | 1109 | if (s) { | 
|  | 1110 | wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group " | 
|  | 1111 | "entry"); | 
|  | 1112 | if (ssid->passphrase && !s->passphrase) | 
|  | 1113 | changed = 1; | 
|  | 1114 | else if (ssid->passphrase && s->passphrase && | 
|  | 1115 | os_strcmp(ssid->passphrase, s->passphrase) != 0) | 
|  | 1116 | changed = 1; | 
|  | 1117 | } else { | 
|  | 1118 | wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group " | 
|  | 1119 | "entry"); | 
|  | 1120 | changed = 1; | 
|  | 1121 | s = wpa_config_add_network(wpa_s->conf); | 
|  | 1122 | if (s == NULL) | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1123 | return -1; | 
|  | 1124 |  | 
|  | 1125 | /* | 
|  | 1126 | * Instead of network_added we emit persistent_group_added | 
|  | 1127 | * notification. Also to keep the defense checks in | 
|  | 1128 | * persistent_group obj registration method, we set the | 
|  | 1129 | * relevant flags in s to designate it as a persistent group. | 
|  | 1130 | */ | 
|  | 1131 | s->p2p_group = 1; | 
|  | 1132 | s->p2p_persistent_group = 1; | 
|  | 1133 | wpas_notify_persistent_group_added(wpa_s, s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1134 | wpa_config_set_network_defaults(s); | 
|  | 1135 | } | 
|  | 1136 |  | 
|  | 1137 | s->p2p_group = 1; | 
|  | 1138 | s->p2p_persistent_group = 1; | 
|  | 1139 | s->disabled = 2; | 
|  | 1140 | s->bssid_set = 1; | 
|  | 1141 | os_memcpy(s->bssid, go_dev_addr, ETH_ALEN); | 
|  | 1142 | s->mode = ssid->mode; | 
|  | 1143 | s->auth_alg = WPA_AUTH_ALG_OPEN; | 
|  | 1144 | s->key_mgmt = WPA_KEY_MGMT_PSK; | 
|  | 1145 | s->proto = WPA_PROTO_RSN; | 
| Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1146 | s->pbss = ssid->pbss; | 
|  | 1147 | s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1148 | s->export_keys = 1; | 
|  | 1149 | if (ssid->passphrase) { | 
|  | 1150 | os_free(s->passphrase); | 
|  | 1151 | s->passphrase = os_strdup(ssid->passphrase); | 
|  | 1152 | } | 
|  | 1153 | if (ssid->psk_set) { | 
|  | 1154 | s->psk_set = 1; | 
|  | 1155 | os_memcpy(s->psk, ssid->psk, 32); | 
|  | 1156 | } | 
|  | 1157 | if (s->passphrase && !s->psk_set) | 
|  | 1158 | wpa_config_update_psk(s); | 
|  | 1159 | if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) { | 
|  | 1160 | os_free(s->ssid); | 
|  | 1161 | s->ssid = os_malloc(ssid->ssid_len); | 
|  | 1162 | } | 
|  | 1163 | if (s->ssid) { | 
|  | 1164 | s->ssid_len = ssid->ssid_len; | 
|  | 1165 | os_memcpy(s->ssid, ssid->ssid, s->ssid_len); | 
|  | 1166 | } | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1167 | if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) { | 
|  | 1168 | dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list); | 
|  | 1169 | wpa_s->global->add_psk = NULL; | 
|  | 1170 | changed = 1; | 
|  | 1171 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1172 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1173 | if (changed && wpa_s->conf->update_config && | 
|  | 1174 | wpa_config_write(wpa_s->confname, wpa_s->conf)) { | 
|  | 1175 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); | 
|  | 1176 | } | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1177 |  | 
|  | 1178 | return s->id; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1179 | } | 
|  | 1180 |  | 
|  | 1181 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1182 | static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s, | 
|  | 1183 | const u8 *addr) | 
|  | 1184 | { | 
|  | 1185 | struct wpa_ssid *ssid, *s; | 
|  | 1186 | u8 *n; | 
|  | 1187 | size_t i; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1188 | int found = 0; | 
| Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 1189 | struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1190 |  | 
|  | 1191 | ssid = wpa_s->current_ssid; | 
|  | 1192 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || | 
|  | 1193 | !ssid->p2p_persistent_group) | 
|  | 1194 | return; | 
|  | 1195 |  | 
| Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 1196 | for (s = p2p_wpa_s->conf->ssid; s; s = s->next) { | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1197 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) | 
|  | 1198 | continue; | 
|  | 1199 |  | 
|  | 1200 | if (s->ssid_len == ssid->ssid_len && | 
|  | 1201 | os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0) | 
|  | 1202 | break; | 
|  | 1203 | } | 
|  | 1204 |  | 
|  | 1205 | if (s == NULL) | 
|  | 1206 | return; | 
|  | 1207 |  | 
|  | 1208 | for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) { | 
| Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1209 | if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr, | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1210 | ETH_ALEN) != 0) | 
|  | 1211 | continue; | 
|  | 1212 |  | 
|  | 1213 | if (i == s->num_p2p_clients - 1) | 
|  | 1214 | return; /* already the most recent entry */ | 
|  | 1215 |  | 
|  | 1216 | /* move the entry to mark it most recent */ | 
| Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1217 | os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN, | 
|  | 1218 | s->p2p_client_list + (i + 1) * 2 * ETH_ALEN, | 
|  | 1219 | (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN); | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1220 | os_memcpy(s->p2p_client_list + | 
| Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1221 | (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr, | 
|  | 1222 | ETH_ALEN); | 
|  | 1223 | os_memset(s->p2p_client_list + | 
|  | 1224 | (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN, | 
|  | 1225 | 0xff, ETH_ALEN); | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1226 | found = 1; | 
|  | 1227 | break; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1228 | } | 
|  | 1229 |  | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1230 | if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) { | 
|  | 1231 | n = os_realloc_array(s->p2p_client_list, | 
| Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1232 | s->num_p2p_clients + 1, 2 * ETH_ALEN); | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1233 | if (n == NULL) | 
|  | 1234 | return; | 
| Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1235 | os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr, | 
|  | 1236 | ETH_ALEN); | 
|  | 1237 | os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN, | 
|  | 1238 | 0xff, ETH_ALEN); | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1239 | s->p2p_client_list = n; | 
|  | 1240 | s->num_p2p_clients++; | 
| Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 1241 | } else if (!found && s->p2p_client_list) { | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1242 | /* Not enough room for an additional entry - drop the oldest | 
|  | 1243 | * entry */ | 
|  | 1244 | os_memmove(s->p2p_client_list, | 
| Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1245 | s->p2p_client_list + 2 * ETH_ALEN, | 
|  | 1246 | (s->num_p2p_clients - 1) * 2 * ETH_ALEN); | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1247 | os_memcpy(s->p2p_client_list + | 
| Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1248 | (s->num_p2p_clients - 1) * 2 * ETH_ALEN, | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1249 | addr, ETH_ALEN); | 
| Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1250 | os_memset(s->p2p_client_list + | 
|  | 1251 | (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN, | 
|  | 1252 | 0xff, ETH_ALEN); | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1253 | } | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1254 |  | 
| Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 1255 | if (p2p_wpa_s->conf->update_config && | 
|  | 1256 | wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf)) | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1257 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1258 | } | 
|  | 1259 |  | 
|  | 1260 |  | 
| Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1261 | static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s, | 
|  | 1262 | int go, struct wpa_ssid *ssid, int freq, | 
|  | 1263 | const u8 *psk, const char *passphrase, | 
|  | 1264 | const u8 *go_dev_addr, int persistent, | 
|  | 1265 | const char *extra) | 
|  | 1266 | { | 
|  | 1267 | const char *ssid_txt; | 
|  | 1268 | char psk_txt[65]; | 
|  | 1269 |  | 
|  | 1270 | if (psk) | 
|  | 1271 | wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32); | 
|  | 1272 | else | 
|  | 1273 | psk_txt[0] = '\0'; | 
|  | 1274 |  | 
|  | 1275 | if (ssid) | 
|  | 1276 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); | 
|  | 1277 | else | 
|  | 1278 | ssid_txt = ""; | 
|  | 1279 |  | 
|  | 1280 | if (passphrase && passphrase[0] == '\0') | 
|  | 1281 | passphrase = NULL; | 
|  | 1282 |  | 
|  | 1283 | /* | 
|  | 1284 | * Include PSK/passphrase only in the control interface message and | 
|  | 1285 | * leave it out from the debug log entry. | 
|  | 1286 | */ | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1287 | wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1288 | P2P_EVENT_GROUP_STARTED | 
|  | 1289 | "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr=" | 
|  | 1290 | MACSTR "%s%s", | 
|  | 1291 | wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq, | 
|  | 1292 | psk ? " psk=" : "", psk_txt, | 
|  | 1293 | passphrase ? " passphrase=\"" : "", | 
|  | 1294 | passphrase ? passphrase : "", | 
|  | 1295 | passphrase ? "\"" : "", | 
|  | 1296 | MAC2STR(go_dev_addr), | 
|  | 1297 | persistent ? " [PERSISTENT]" : "", extra); | 
|  | 1298 | wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED | 
|  | 1299 | "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s", | 
|  | 1300 | wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq, | 
|  | 1301 | MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "", | 
|  | 1302 | extra); | 
|  | 1303 | } | 
|  | 1304 |  | 
|  | 1305 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1306 | static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s, | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 1307 | int success, int already_deleted) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1308 | { | 
|  | 1309 | struct wpa_ssid *ssid; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1310 | int client; | 
|  | 1311 | int persistent; | 
|  | 1312 | u8 go_dev_addr[ETH_ALEN]; | 
|  | 1313 |  | 
|  | 1314 | /* | 
|  | 1315 | * This callback is likely called for the main interface. Update wpa_s | 
|  | 1316 | * to use the group interface if a new interface was created for the | 
|  | 1317 | * group. | 
|  | 1318 | */ | 
|  | 1319 | if (wpa_s->global->p2p_group_formation) | 
|  | 1320 | wpa_s = wpa_s->global->p2p_group_formation; | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 1321 | if (wpa_s->p2p_go_group_formation_completed) { | 
|  | 1322 | wpa_s->global->p2p_group_formation = NULL; | 
|  | 1323 | wpa_s->p2p_in_provisioning = 0; | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1324 | } else if (wpa_s->p2p_in_provisioning && !success) { | 
|  | 1325 | wpa_msg(wpa_s, MSG_DEBUG, | 
|  | 1326 | "P2P: Stop provisioning state due to failure"); | 
|  | 1327 | wpa_s->p2p_in_provisioning = 0; | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 1328 | } | 
| Dmitry Shmidt | 21de214 | 2014-04-08 10:50:52 -0700 | [diff] [blame] | 1329 | wpa_s->p2p_in_invitation = 0; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1330 | wpa_s->group_formation_reported = 1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1331 |  | 
|  | 1332 | if (!success) { | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1333 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1334 | P2P_EVENT_GROUP_FORMATION_FAILURE); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1335 | wpas_notify_p2p_group_formation_failure(wpa_s, ""); | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 1336 | if (already_deleted) | 
|  | 1337 | return; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1338 | wpas_p2p_group_delete(wpa_s, | 
|  | 1339 | P2P_GROUP_REMOVAL_FORMATION_FAILED); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1340 | return; | 
|  | 1341 | } | 
|  | 1342 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1343 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1344 | P2P_EVENT_GROUP_FORMATION_SUCCESS); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1345 |  | 
|  | 1346 | ssid = wpa_s->current_ssid; | 
|  | 1347 | if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { | 
|  | 1348 | ssid->mode = WPAS_MODE_P2P_GO; | 
|  | 1349 | p2p_group_notif_formation_done(wpa_s->p2p_group); | 
|  | 1350 | wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL); | 
|  | 1351 | } | 
|  | 1352 |  | 
|  | 1353 | persistent = 0; | 
|  | 1354 | if (ssid) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1355 | client = ssid->mode == WPAS_MODE_INFRA; | 
|  | 1356 | if (ssid->mode == WPAS_MODE_P2P_GO) { | 
|  | 1357 | persistent = ssid->p2p_persistent_group; | 
| Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 1358 | os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr, | 
|  | 1359 | ETH_ALEN); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1360 | } else | 
|  | 1361 | persistent = wpas_p2p_persistent_group(wpa_s, | 
|  | 1362 | go_dev_addr, | 
|  | 1363 | ssid->ssid, | 
|  | 1364 | ssid->ssid_len); | 
|  | 1365 | } else { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1366 | client = wpa_s->p2p_group_interface == | 
|  | 1367 | P2P_GROUP_INTERFACE_CLIENT; | 
|  | 1368 | os_memset(go_dev_addr, 0, ETH_ALEN); | 
|  | 1369 | } | 
|  | 1370 |  | 
|  | 1371 | wpa_s->show_group_started = 0; | 
|  | 1372 | if (client) { | 
|  | 1373 | /* | 
|  | 1374 | * Indicate event only after successfully completed 4-way | 
|  | 1375 | * handshake, i.e., when the interface is ready for data | 
|  | 1376 | * packets. | 
|  | 1377 | */ | 
|  | 1378 | wpa_s->show_group_started = 1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1379 | } else { | 
| Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1380 | wpas_p2p_group_started(wpa_s, 1, ssid, | 
|  | 1381 | ssid ? ssid->frequency : 0, | 
|  | 1382 | ssid && ssid->passphrase == NULL && | 
|  | 1383 | ssid->psk_set ? ssid->psk : NULL, | 
|  | 1384 | ssid ? ssid->passphrase : NULL, | 
|  | 1385 | go_dev_addr, persistent, ""); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1386 | wpas_p2p_cross_connect_setup(wpa_s); | 
|  | 1387 | wpas_p2p_set_group_idle_timeout(wpa_s); | 
|  | 1388 | } | 
|  | 1389 |  | 
|  | 1390 | if (persistent) | 
| Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 1391 | wpas_p2p_store_persistent_group(wpa_s->p2pdev, | 
|  | 1392 | ssid, go_dev_addr); | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1393 | else { | 
|  | 1394 | os_free(wpa_s->global->add_psk); | 
|  | 1395 | wpa_s->global->add_psk = NULL; | 
|  | 1396 | } | 
| Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 1397 |  | 
| Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 1398 | if (!client) { | 
| Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 1399 | wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1400 | os_get_reltime(&wpa_s->global->p2p_go_wait_client); | 
| Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 1401 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1402 | } | 
|  | 1403 |  | 
|  | 1404 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1405 | struct send_action_work { | 
|  | 1406 | unsigned int freq; | 
|  | 1407 | u8 dst[ETH_ALEN]; | 
|  | 1408 | u8 src[ETH_ALEN]; | 
|  | 1409 | u8 bssid[ETH_ALEN]; | 
|  | 1410 | size_t len; | 
|  | 1411 | unsigned int wait_time; | 
|  | 1412 | u8 buf[0]; | 
|  | 1413 | }; | 
|  | 1414 |  | 
|  | 1415 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1416 | static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s) | 
|  | 1417 | { | 
|  | 1418 | struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx; | 
|  | 1419 |  | 
|  | 1420 | wpa_printf(MSG_DEBUG, | 
|  | 1421 | "P2P: Free Action frame radio work @%p (freq=%u dst=" | 
|  | 1422 | MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)", | 
|  | 1423 | wpa_s->p2p_send_action_work, awork->freq, | 
|  | 1424 | MAC2STR(awork->dst), MAC2STR(awork->src), | 
|  | 1425 | MAC2STR(awork->bssid), awork->wait_time); | 
|  | 1426 | wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame", | 
|  | 1427 | awork->buf, awork->len); | 
|  | 1428 | os_free(awork); | 
|  | 1429 | wpa_s->p2p_send_action_work->ctx = NULL; | 
|  | 1430 | radio_work_done(wpa_s->p2p_send_action_work); | 
|  | 1431 | wpa_s->p2p_send_action_work = NULL; | 
|  | 1432 | } | 
|  | 1433 |  | 
|  | 1434 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1435 | static void wpas_p2p_send_action_work_timeout(void *eloop_ctx, | 
|  | 1436 | void *timeout_ctx) | 
|  | 1437 | { | 
|  | 1438 | struct wpa_supplicant *wpa_s = eloop_ctx; | 
|  | 1439 |  | 
|  | 1440 | if (!wpa_s->p2p_send_action_work) | 
|  | 1441 | return; | 
|  | 1442 |  | 
|  | 1443 | wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out"); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1444 | wpas_p2p_free_send_action_work(wpa_s); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1445 | } | 
|  | 1446 |  | 
|  | 1447 |  | 
| Dmitry Shmidt | 0365883 | 2014-08-13 11:03:49 -0700 | [diff] [blame] | 1448 | static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1449 | { | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1450 | if (wpa_s->p2p_send_action_work) { | 
|  | 1451 | struct send_action_work *awork; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1452 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1453 | awork = wpa_s->p2p_send_action_work->ctx; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1454 | wpa_printf(MSG_DEBUG, | 
|  | 1455 | "P2P: Clear Action TX work @%p (wait_time=%u)", | 
|  | 1456 | wpa_s->p2p_send_action_work, awork->wait_time); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1457 | if (awork->wait_time == 0) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1458 | wpas_p2p_free_send_action_work(wpa_s); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1459 | } else { | 
|  | 1460 | /* | 
|  | 1461 | * In theory, this should not be needed, but number of | 
|  | 1462 | * places in the P2P code is still using non-zero wait | 
|  | 1463 | * time for the last Action frame in the sequence and | 
|  | 1464 | * some of these do not call send_action_done(). | 
|  | 1465 | */ | 
|  | 1466 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, | 
|  | 1467 | wpa_s, NULL); | 
|  | 1468 | eloop_register_timeout( | 
|  | 1469 | 0, awork->wait_time * 1000, | 
|  | 1470 | wpas_p2p_send_action_work_timeout, | 
|  | 1471 | wpa_s, NULL); | 
|  | 1472 | } | 
|  | 1473 | } | 
| Dmitry Shmidt | 0365883 | 2014-08-13 11:03:49 -0700 | [diff] [blame] | 1474 | } | 
|  | 1475 |  | 
|  | 1476 |  | 
|  | 1477 | static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s, | 
|  | 1478 | unsigned int freq, | 
|  | 1479 | const u8 *dst, const u8 *src, | 
|  | 1480 | const u8 *bssid, | 
|  | 1481 | const u8 *data, size_t data_len, | 
|  | 1482 | enum offchannel_send_action_result | 
|  | 1483 | result) | 
|  | 1484 | { | 
|  | 1485 | enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS; | 
|  | 1486 |  | 
|  | 1487 | wpas_p2p_action_tx_clear(wpa_s); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1488 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1489 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) | 
|  | 1490 | return; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1491 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1492 | switch (result) { | 
|  | 1493 | case OFFCHANNEL_SEND_ACTION_SUCCESS: | 
|  | 1494 | res = P2P_SEND_ACTION_SUCCESS; | 
|  | 1495 | break; | 
|  | 1496 | case OFFCHANNEL_SEND_ACTION_NO_ACK: | 
|  | 1497 | res = P2P_SEND_ACTION_NO_ACK; | 
|  | 1498 | break; | 
|  | 1499 | case OFFCHANNEL_SEND_ACTION_FAILED: | 
|  | 1500 | res = P2P_SEND_ACTION_FAILED; | 
|  | 1501 | break; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1502 | } | 
|  | 1503 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1504 | 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] | 1505 |  | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1506 | if (result != OFFCHANNEL_SEND_ACTION_SUCCESS && | 
|  | 1507 | wpa_s->pending_pd_before_join && | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1508 | (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1509 | os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) && | 
|  | 1510 | wpa_s->p2p_fallback_to_go_neg) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1511 | wpa_s->pending_pd_before_join = 0; | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1512 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req " | 
|  | 1513 | "during p2p_connect-auto"); | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1514 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1515 | P2P_EVENT_FALLBACK_TO_GO_NEG | 
|  | 1516 | "reason=no-ACK-to-PD-Req"); | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1517 | wpas_p2p_fallback_to_go_neg(wpa_s, 0); | 
|  | 1518 | return; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1519 | } | 
|  | 1520 | } | 
|  | 1521 |  | 
|  | 1522 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1523 | static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit) | 
|  | 1524 | { | 
|  | 1525 | struct wpa_supplicant *wpa_s = work->wpa_s; | 
|  | 1526 | struct send_action_work *awork = work->ctx; | 
|  | 1527 |  | 
|  | 1528 | if (deinit) { | 
| Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 1529 | if (work->started) { | 
|  | 1530 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, | 
|  | 1531 | wpa_s, NULL); | 
|  | 1532 | wpa_s->p2p_send_action_work = NULL; | 
|  | 1533 | offchannel_send_action_done(wpa_s); | 
|  | 1534 | } | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1535 | os_free(awork); | 
|  | 1536 | return; | 
|  | 1537 | } | 
|  | 1538 |  | 
|  | 1539 | if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src, | 
|  | 1540 | awork->bssid, awork->buf, awork->len, | 
|  | 1541 | awork->wait_time, | 
|  | 1542 | wpas_p2p_send_action_tx_status, 1) < 0) { | 
|  | 1543 | os_free(awork); | 
|  | 1544 | radio_work_done(work); | 
|  | 1545 | return; | 
|  | 1546 | } | 
|  | 1547 | wpa_s->p2p_send_action_work = work; | 
|  | 1548 | } | 
|  | 1549 |  | 
|  | 1550 |  | 
|  | 1551 | static int wpas_send_action_work(struct wpa_supplicant *wpa_s, | 
|  | 1552 | unsigned int freq, const u8 *dst, | 
|  | 1553 | const u8 *src, const u8 *bssid, const u8 *buf, | 
|  | 1554 | size_t len, unsigned int wait_time) | 
|  | 1555 | { | 
|  | 1556 | struct send_action_work *awork; | 
|  | 1557 |  | 
|  | 1558 | if (wpa_s->p2p_send_action_work) { | 
|  | 1559 | wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending"); | 
|  | 1560 | return -1; | 
|  | 1561 | } | 
|  | 1562 |  | 
|  | 1563 | awork = os_zalloc(sizeof(*awork) + len); | 
|  | 1564 | if (awork == NULL) | 
|  | 1565 | return -1; | 
|  | 1566 |  | 
|  | 1567 | awork->freq = freq; | 
|  | 1568 | os_memcpy(awork->dst, dst, ETH_ALEN); | 
|  | 1569 | os_memcpy(awork->src, src, ETH_ALEN); | 
|  | 1570 | os_memcpy(awork->bssid, bssid, ETH_ALEN); | 
|  | 1571 | awork->len = len; | 
|  | 1572 | awork->wait_time = wait_time; | 
|  | 1573 | os_memcpy(awork->buf, buf, len); | 
|  | 1574 |  | 
|  | 1575 | if (radio_add_work(wpa_s, freq, "p2p-send-action", 0, | 
|  | 1576 | wpas_send_action_cb, awork) < 0) { | 
|  | 1577 | os_free(awork); | 
|  | 1578 | return -1; | 
|  | 1579 | } | 
|  | 1580 |  | 
|  | 1581 | return 0; | 
|  | 1582 | } | 
|  | 1583 |  | 
|  | 1584 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1585 | static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst, | 
|  | 1586 | const u8 *src, const u8 *bssid, const u8 *buf, | 
|  | 1587 | size_t len, unsigned int wait_time) | 
|  | 1588 | { | 
|  | 1589 | struct wpa_supplicant *wpa_s = ctx; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1590 | int listen_freq = -1, send_freq = -1; | 
|  | 1591 |  | 
|  | 1592 | if (wpa_s->p2p_listen_work) | 
|  | 1593 | listen_freq = wpa_s->p2p_listen_work->freq; | 
|  | 1594 | if (wpa_s->p2p_send_action_work) | 
|  | 1595 | send_freq = wpa_s->p2p_send_action_work->freq; | 
|  | 1596 | if (listen_freq != (int) freq && send_freq != (int) freq) { | 
|  | 1597 | wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)", | 
|  | 1598 | listen_freq, send_freq); | 
|  | 1599 | return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf, | 
|  | 1600 | len, wait_time); | 
|  | 1601 | } | 
|  | 1602 |  | 
|  | 1603 | wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX"); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1604 | return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len, | 
|  | 1605 | wait_time, | 
|  | 1606 | wpas_p2p_send_action_tx_status, 1); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1607 | } | 
|  | 1608 |  | 
|  | 1609 |  | 
|  | 1610 | static void wpas_send_action_done(void *ctx) | 
|  | 1611 | { | 
|  | 1612 | struct wpa_supplicant *wpa_s = ctx; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1613 |  | 
|  | 1614 | if (wpa_s->p2p_send_action_work) { | 
|  | 1615 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, | 
|  | 1616 | wpa_s, NULL); | 
|  | 1617 | os_free(wpa_s->p2p_send_action_work->ctx); | 
|  | 1618 | radio_work_done(wpa_s->p2p_send_action_work); | 
|  | 1619 | wpa_s->p2p_send_action_work = NULL; | 
|  | 1620 | } | 
|  | 1621 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1622 | offchannel_send_action_done(wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1623 | } | 
|  | 1624 |  | 
|  | 1625 |  | 
|  | 1626 | static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s, | 
|  | 1627 | struct p2p_go_neg_results *params) | 
|  | 1628 | { | 
|  | 1629 | if (wpa_s->go_params == NULL) { | 
|  | 1630 | wpa_s->go_params = os_malloc(sizeof(*params)); | 
|  | 1631 | if (wpa_s->go_params == NULL) | 
|  | 1632 | return -1; | 
|  | 1633 | } | 
|  | 1634 | os_memcpy(wpa_s->go_params, params, sizeof(*params)); | 
|  | 1635 | return 0; | 
|  | 1636 | } | 
|  | 1637 |  | 
|  | 1638 |  | 
|  | 1639 | static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s, | 
|  | 1640 | struct p2p_go_neg_results *res) | 
|  | 1641 | { | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1642 | wpa_s->group_formation_reported = 0; | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1643 | wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR | 
|  | 1644 | " dev_addr " MACSTR " wps_method %d", | 
|  | 1645 | MAC2STR(res->peer_interface_addr), | 
|  | 1646 | MAC2STR(res->peer_device_addr), res->wps_method); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1647 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID", | 
|  | 1648 | res->ssid, res->ssid_len); | 
|  | 1649 | wpa_supplicant_ap_deinit(wpa_s); | 
|  | 1650 | wpas_copy_go_neg_results(wpa_s, res); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1651 | if (res->wps_method == WPS_PBC) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1652 | wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1653 | #ifdef CONFIG_WPS_NFC | 
|  | 1654 | } else if (res->wps_method == WPS_NFC) { | 
|  | 1655 | wpas_wps_start_nfc(wpa_s, res->peer_device_addr, | 
|  | 1656 | res->peer_interface_addr, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1657 | wpa_s->p2pdev->p2p_oob_dev_pw, | 
|  | 1658 | wpa_s->p2pdev->p2p_oob_dev_pw_id, 1, | 
|  | 1659 | wpa_s->p2pdev->p2p_oob_dev_pw_id == | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1660 | DEV_PW_NFC_CONNECTION_HANDOVER ? | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1661 | wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1662 | NULL, | 
|  | 1663 | NULL, 0, 0); | 
|  | 1664 | #endif /* CONFIG_WPS_NFC */ | 
|  | 1665 | } else { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1666 | u16 dev_pw_id = DEV_PW_DEFAULT; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1667 | if (wpa_s->p2p_wps_method == WPS_P2PS) | 
|  | 1668 | dev_pw_id = DEV_PW_P2PS_DEFAULT; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1669 | if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD) | 
|  | 1670 | dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED; | 
|  | 1671 | wpas_wps_start_pin(wpa_s, res->peer_interface_addr, | 
|  | 1672 | wpa_s->p2p_pin, 1, dev_pw_id); | 
|  | 1673 | } | 
|  | 1674 | } | 
|  | 1675 |  | 
|  | 1676 |  | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1677 | static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s, | 
|  | 1678 | struct wpa_ssid *ssid) | 
|  | 1679 | { | 
|  | 1680 | struct wpa_ssid *persistent; | 
|  | 1681 | struct psk_list_entry *psk; | 
|  | 1682 | struct hostapd_data *hapd; | 
|  | 1683 |  | 
|  | 1684 | if (!wpa_s->ap_iface) | 
|  | 1685 | return; | 
|  | 1686 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1687 | persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid, | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1688 | ssid->ssid_len); | 
|  | 1689 | if (persistent == NULL) | 
|  | 1690 | return; | 
|  | 1691 |  | 
|  | 1692 | hapd = wpa_s->ap_iface->bss[0]; | 
|  | 1693 |  | 
|  | 1694 | dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry, | 
|  | 1695 | list) { | 
|  | 1696 | struct hostapd_wpa_psk *hpsk; | 
|  | 1697 |  | 
|  | 1698 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for " | 
|  | 1699 | MACSTR " psk=%d", | 
|  | 1700 | MAC2STR(psk->addr), psk->p2p); | 
|  | 1701 | hpsk = os_zalloc(sizeof(*hpsk)); | 
|  | 1702 | if (hpsk == NULL) | 
|  | 1703 | break; | 
|  | 1704 | os_memcpy(hpsk->psk, psk->psk, PMK_LEN); | 
|  | 1705 | if (psk->p2p) | 
|  | 1706 | os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN); | 
|  | 1707 | else | 
|  | 1708 | os_memcpy(hpsk->addr, psk->addr, ETH_ALEN); | 
|  | 1709 | hpsk->next = hapd->conf->ssid.wpa_psk; | 
|  | 1710 | hapd->conf->ssid.wpa_psk = hpsk; | 
|  | 1711 | } | 
|  | 1712 | } | 
|  | 1713 |  | 
|  | 1714 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1715 | static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s) | 
|  | 1716 | { | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1717 | char buf[20 + P2P_MAX_CHANNELS * 6]; | 
|  | 1718 | char *pos, *end; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1719 | unsigned int i; | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1720 | int res; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1721 |  | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1722 | pos = buf; | 
|  | 1723 | end = pos + sizeof(buf); | 
|  | 1724 | for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) { | 
|  | 1725 | res = os_snprintf(pos, end - pos, " %d", | 
|  | 1726 | wpa_s->p2p_group_common_freqs[i]); | 
|  | 1727 | if (os_snprintf_error(end - pos, res)) | 
|  | 1728 | break; | 
|  | 1729 | pos += res; | 
|  | 1730 | } | 
|  | 1731 | *pos = '\0'; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1732 |  | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1733 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf); | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1734 | } | 
|  | 1735 |  | 
|  | 1736 |  | 
|  | 1737 | static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s, | 
|  | 1738 | struct p2p_go_neg_results *params) | 
|  | 1739 | { | 
|  | 1740 | unsigned int i, len = int_array_len(wpa_s->go_params->freq_list); | 
|  | 1741 |  | 
|  | 1742 | wpa_s->p2p_group_common_freqs_num = 0; | 
|  | 1743 | os_free(wpa_s->p2p_group_common_freqs); | 
|  | 1744 | wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int)); | 
|  | 1745 | if (!wpa_s->p2p_group_common_freqs) | 
|  | 1746 | return; | 
|  | 1747 |  | 
|  | 1748 | for (i = 0; i < len; i++) { | 
|  | 1749 | if (!wpa_s->go_params->freq_list[i]) | 
|  | 1750 | break; | 
|  | 1751 | wpa_s->p2p_group_common_freqs[i] = | 
|  | 1752 | wpa_s->go_params->freq_list[i]; | 
|  | 1753 | } | 
|  | 1754 | wpa_s->p2p_group_common_freqs_num = i; | 
|  | 1755 | } | 
|  | 1756 |  | 
|  | 1757 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1758 | static void p2p_config_write(struct wpa_supplicant *wpa_s) | 
|  | 1759 | { | 
|  | 1760 | #ifndef CONFIG_NO_CONFIG_WRITE | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1761 | if (wpa_s->p2pdev->conf->update_config && | 
|  | 1762 | wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf)) | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1763 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); | 
|  | 1764 | #endif /* CONFIG_NO_CONFIG_WRITE */ | 
|  | 1765 | } | 
|  | 1766 |  | 
|  | 1767 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1768 | static void p2p_go_configured(void *ctx, void *data) | 
|  | 1769 | { | 
|  | 1770 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 1771 | struct p2p_go_neg_results *params = data; | 
|  | 1772 | struct wpa_ssid *ssid; | 
|  | 1773 |  | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 1774 | wpa_s->ap_configured_cb = NULL; | 
|  | 1775 | wpa_s->ap_configured_cb_ctx = NULL; | 
|  | 1776 | wpa_s->ap_configured_cb_data = NULL; | 
|  | 1777 | if (!wpa_s->go_params) { | 
|  | 1778 | wpa_printf(MSG_ERROR, | 
|  | 1779 | "P2P: p2p_go_configured() called with wpa_s->go_params == NULL"); | 
|  | 1780 | return; | 
|  | 1781 | } | 
|  | 1782 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1783 | p2p_go_save_group_common_freqs(wpa_s, params); | 
|  | 1784 | p2p_go_dump_common_freqs(wpa_s); | 
|  | 1785 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1786 | ssid = wpa_s->current_ssid; | 
|  | 1787 | if (ssid && ssid->mode == WPAS_MODE_P2P_GO) { | 
|  | 1788 | wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning"); | 
|  | 1789 | if (wpa_s->global->p2p_group_formation == wpa_s) | 
|  | 1790 | wpa_s->global->p2p_group_formation = NULL; | 
| Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1791 | wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency, | 
|  | 1792 | params->passphrase[0] == '\0' ? | 
|  | 1793 | params->psk : NULL, | 
|  | 1794 | params->passphrase, | 
|  | 1795 | wpa_s->global->p2p_dev_addr, | 
|  | 1796 | params->persistent_group, ""); | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1797 | wpa_s->group_formation_reported = 1; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1798 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1799 | if (wpa_s->p2pdev->p2ps_method_config_any) { | 
|  | 1800 | if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1801 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 1802 | "P2PS: Setting default PIN for ANY"); | 
|  | 1803 | wpa_supplicant_ap_wps_pin(wpa_s, NULL, | 
|  | 1804 | "12345670", NULL, 0, | 
|  | 1805 | 0); | 
|  | 1806 | } else { | 
|  | 1807 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 1808 | "P2PS: Setting default PIN for " MACSTR, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1809 | MAC2STR(wpa_s->p2pdev->p2ps_join_addr)); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1810 | wpa_supplicant_ap_wps_pin( | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1811 | wpa_s, wpa_s->p2pdev->p2ps_join_addr, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1812 | "12345670", NULL, 0, 0); | 
|  | 1813 | } | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1814 | wpa_s->p2pdev->p2ps_method_config_any = 0; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1815 | } | 
|  | 1816 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1817 | os_get_reltime(&wpa_s->global->p2p_go_wait_client); | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1818 | if (params->persistent_group) { | 
| Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 1819 | wpas_p2p_store_persistent_group( | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1820 | wpa_s->p2pdev, ssid, | 
| Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 1821 | wpa_s->global->p2p_dev_addr); | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1822 | wpas_p2p_add_psk_list(wpa_s, ssid); | 
|  | 1823 | } | 
| Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 1824 |  | 
|  | 1825 | wpas_notify_p2p_group_started(wpa_s, ssid, | 
| Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 1826 | params->persistent_group, 0, | 
|  | 1827 | NULL); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1828 | wpas_p2p_cross_connect_setup(wpa_s); | 
|  | 1829 | wpas_p2p_set_group_idle_timeout(wpa_s); | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 1830 |  | 
|  | 1831 | if (wpa_s->p2p_first_connection_timeout) { | 
|  | 1832 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 1833 | "P2P: Start group formation timeout of %d seconds until first data connection on GO", | 
|  | 1834 | wpa_s->p2p_first_connection_timeout); | 
|  | 1835 | wpa_s->p2p_go_group_formation_completed = 0; | 
|  | 1836 | wpa_s->global->p2p_group_formation = wpa_s; | 
|  | 1837 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1838 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 1839 | eloop_register_timeout( | 
|  | 1840 | wpa_s->p2p_first_connection_timeout, 0, | 
|  | 1841 | wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1842 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 1843 | } | 
|  | 1844 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1845 | return; | 
|  | 1846 | } | 
|  | 1847 |  | 
|  | 1848 | wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning"); | 
|  | 1849 | if (wpa_supplicant_ap_mac_addr_filter(wpa_s, | 
|  | 1850 | params->peer_interface_addr)) { | 
|  | 1851 | wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address " | 
|  | 1852 | "filtering"); | 
|  | 1853 | return; | 
|  | 1854 | } | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1855 | if (params->wps_method == WPS_PBC) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1856 | wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr, | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1857 | params->peer_device_addr); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1858 | #ifdef CONFIG_WPS_NFC | 
|  | 1859 | } else if (params->wps_method == WPS_NFC) { | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1860 | if (wpa_s->p2pdev->p2p_oob_dev_pw_id != | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1861 | DEV_PW_NFC_CONNECTION_HANDOVER && | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1862 | !wpa_s->p2pdev->p2p_oob_dev_pw) { | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1863 | wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known"); | 
|  | 1864 | return; | 
|  | 1865 | } | 
|  | 1866 | wpas_ap_wps_add_nfc_pw( | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1867 | wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id, | 
|  | 1868 | wpa_s->p2pdev->p2p_oob_dev_pw, | 
|  | 1869 | wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ? | 
|  | 1870 | wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1871 | #endif /* CONFIG_WPS_NFC */ | 
|  | 1872 | } else if (wpa_s->p2p_pin[0]) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1873 | wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr, | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1874 | wpa_s->p2p_pin, NULL, 0, 0); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1875 | os_free(wpa_s->go_params); | 
|  | 1876 | wpa_s->go_params = NULL; | 
|  | 1877 | } | 
|  | 1878 |  | 
|  | 1879 |  | 
|  | 1880 | static void wpas_start_wps_go(struct wpa_supplicant *wpa_s, | 
|  | 1881 | struct p2p_go_neg_results *params, | 
|  | 1882 | int group_formation) | 
|  | 1883 | { | 
|  | 1884 | struct wpa_ssid *ssid; | 
|  | 1885 |  | 
| Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1886 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO"); | 
|  | 1887 | if (wpas_copy_go_neg_results(wpa_s, params) < 0) { | 
|  | 1888 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation " | 
|  | 1889 | "results"); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1890 | return; | 
| Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1891 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1892 |  | 
|  | 1893 | ssid = wpa_config_add_network(wpa_s->conf); | 
| Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1894 | if (ssid == NULL) { | 
|  | 1895 | 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] | 1896 | return; | 
| Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1897 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1898 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1899 | wpa_s->show_group_started = 0; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1900 | wpa_s->p2p_go_group_formation_completed = 0; | 
|  | 1901 | wpa_s->group_formation_reported = 0; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1902 | os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1903 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1904 | wpa_config_set_network_defaults(ssid); | 
|  | 1905 | ssid->temporary = 1; | 
|  | 1906 | ssid->p2p_group = 1; | 
| Dmitry Shmidt | aca489e | 2016-09-28 15:44:14 -0700 | [diff] [blame] | 1907 | ssid->p2p_persistent_group = !!params->persistent_group; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1908 | ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION : | 
|  | 1909 | WPAS_MODE_P2P_GO; | 
|  | 1910 | ssid->frequency = params->freq; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1911 | ssid->ht40 = params->ht40; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1912 | ssid->vht = params->vht; | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 1913 | ssid->max_oper_chwidth = params->max_oper_chwidth; | 
|  | 1914 | ssid->vht_center_freq2 = params->vht_center_freq2; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1915 | ssid->ssid = os_zalloc(params->ssid_len + 1); | 
|  | 1916 | if (ssid->ssid) { | 
|  | 1917 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); | 
|  | 1918 | ssid->ssid_len = params->ssid_len; | 
|  | 1919 | } | 
|  | 1920 | ssid->auth_alg = WPA_AUTH_ALG_OPEN; | 
|  | 1921 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; | 
|  | 1922 | ssid->proto = WPA_PROTO_RSN; | 
|  | 1923 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1924 | ssid->group_cipher = WPA_CIPHER_CCMP; | 
|  | 1925 | if (params->freq > 56160) { | 
|  | 1926 | /* | 
|  | 1927 | * Enable GCMP instead of CCMP as pairwise_cipher and | 
|  | 1928 | * group_cipher in 60 GHz. | 
|  | 1929 | */ | 
|  | 1930 | ssid->pairwise_cipher = WPA_CIPHER_GCMP; | 
|  | 1931 | ssid->group_cipher = WPA_CIPHER_GCMP; | 
| Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1932 | /* P2P GO in 60 GHz is always a PCP (PBSS) */ | 
|  | 1933 | ssid->pbss = 1; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1934 | } | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1935 | if (os_strlen(params->passphrase) > 0) { | 
|  | 1936 | ssid->passphrase = os_strdup(params->passphrase); | 
|  | 1937 | if (ssid->passphrase == NULL) { | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1938 | wpa_msg_global(wpa_s, MSG_ERROR, | 
|  | 1939 | "P2P: Failed to copy passphrase for GO"); | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1940 | wpa_config_remove_network(wpa_s->conf, ssid->id); | 
|  | 1941 | return; | 
|  | 1942 | } | 
|  | 1943 | } else | 
|  | 1944 | ssid->passphrase = NULL; | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1945 | ssid->psk_set = params->psk_set; | 
|  | 1946 | if (ssid->psk_set) | 
|  | 1947 | os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk)); | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1948 | else if (ssid->passphrase) | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1949 | wpa_config_update_psk(ssid); | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1950 | ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1951 |  | 
|  | 1952 | wpa_s->ap_configured_cb = p2p_go_configured; | 
|  | 1953 | wpa_s->ap_configured_cb_ctx = wpa_s; | 
|  | 1954 | wpa_s->ap_configured_cb_data = wpa_s->go_params; | 
| Dmitry Shmidt | 6dc03bd | 2014-05-16 10:40:13 -0700 | [diff] [blame] | 1955 | wpa_s->scan_req = NORMAL_SCAN_REQ; | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1956 | wpa_s->connect_without_scan = ssid; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1957 | wpa_s->reassociate = 1; | 
|  | 1958 | wpa_s->disconnected = 0; | 
| Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1959 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to " | 
|  | 1960 | "start GO)"); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1961 | wpa_supplicant_req_scan(wpa_s, 0, 0); | 
|  | 1962 | } | 
|  | 1963 |  | 
|  | 1964 |  | 
|  | 1965 | static void wpas_p2p_clone_config(struct wpa_supplicant *dst, | 
|  | 1966 | const struct wpa_supplicant *src) | 
|  | 1967 | { | 
|  | 1968 | struct wpa_config *d; | 
|  | 1969 | const struct wpa_config *s; | 
|  | 1970 |  | 
|  | 1971 | d = dst->conf; | 
|  | 1972 | s = src->conf; | 
|  | 1973 |  | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1974 | #define C(n)                            \ | 
|  | 1975 | do {                                    \ | 
|  | 1976 | if (s->n && !d->n)              \ | 
|  | 1977 | d->n = os_strdup(s->n); \ | 
|  | 1978 | } while (0) | 
|  | 1979 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1980 | C(device_name); | 
|  | 1981 | C(manufacturer); | 
|  | 1982 | C(model_name); | 
|  | 1983 | C(model_number); | 
|  | 1984 | C(serial_number); | 
|  | 1985 | C(config_methods); | 
|  | 1986 | #undef C | 
|  | 1987 |  | 
|  | 1988 | os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN); | 
|  | 1989 | os_memcpy(d->sec_device_type, s->sec_device_type, | 
|  | 1990 | sizeof(d->sec_device_type)); | 
|  | 1991 | d->num_sec_device_types = s->num_sec_device_types; | 
|  | 1992 |  | 
|  | 1993 | d->p2p_group_idle = s->p2p_group_idle; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1994 | d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1995 | d->p2p_intra_bss = s->p2p_intra_bss; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1996 | d->persistent_reconnect = s->persistent_reconnect; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1997 | d->max_num_sta = s->max_num_sta; | 
|  | 1998 | d->pbc_in_m1 = s->pbc_in_m1; | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1999 | d->ignore_old_scan_res = s->ignore_old_scan_res; | 
| Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 2000 | d->beacon_int = s->beacon_int; | 
| Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 2001 | d->dtim_period = s->dtim_period; | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2002 | d->p2p_go_ctwindow = s->p2p_go_ctwindow; | 
| Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 2003 | d->disassoc_low_ack = s->disassoc_low_ack; | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 2004 | d->disable_scan_offload = s->disable_scan_offload; | 
| Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2005 | d->passive_scan = s->passive_scan; | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2006 |  | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2007 | if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey && | 
|  | 2008 | !d->wps_nfc_pw_from_config) { | 
|  | 2009 | wpabuf_free(d->wps_nfc_dh_privkey); | 
|  | 2010 | wpabuf_free(d->wps_nfc_dh_pubkey); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2011 | d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey); | 
|  | 2012 | d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey); | 
|  | 2013 | } | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 2014 | d->p2p_cli_probe = s->p2p_cli_probe; | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2015 | d->go_interworking = s->go_interworking; | 
|  | 2016 | d->go_access_network_type = s->go_access_network_type; | 
|  | 2017 | d->go_internet = s->go_internet; | 
|  | 2018 | d->go_venue_group = s->go_venue_group; | 
|  | 2019 | d->go_venue_type = s->go_venue_type; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2020 | } | 
|  | 2021 |  | 
|  | 2022 |  | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 2023 | static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s, | 
|  | 2024 | char *ifname, size_t len) | 
|  | 2025 | { | 
|  | 2026 | char *ifname_ptr = wpa_s->ifname; | 
|  | 2027 |  | 
|  | 2028 | if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX, | 
|  | 2029 | os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) { | 
|  | 2030 | ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1; | 
|  | 2031 | } | 
|  | 2032 |  | 
|  | 2033 | os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx); | 
|  | 2034 | if (os_strlen(ifname) >= IFNAMSIZ && | 
|  | 2035 | os_strlen(wpa_s->ifname) < IFNAMSIZ) { | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2036 | int res; | 
|  | 2037 |  | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 2038 | /* Try to avoid going over the IFNAMSIZ length limit */ | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2039 | res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx); | 
|  | 2040 | if (os_snprintf_error(len, res) && len) | 
|  | 2041 | ifname[len - 1] = '\0'; | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 2042 | } | 
|  | 2043 | } | 
|  | 2044 |  | 
|  | 2045 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2046 | static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, | 
|  | 2047 | enum wpa_driver_if_type type) | 
|  | 2048 | { | 
|  | 2049 | char ifname[120], force_ifname[120]; | 
|  | 2050 |  | 
|  | 2051 | if (wpa_s->pending_interface_name[0]) { | 
|  | 2052 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists " | 
|  | 2053 | "- skip creation of a new one"); | 
|  | 2054 | if (is_zero_ether_addr(wpa_s->pending_interface_addr)) { | 
|  | 2055 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual address " | 
|  | 2056 | "unknown?! ifname='%s'", | 
|  | 2057 | wpa_s->pending_interface_name); | 
|  | 2058 | return -1; | 
|  | 2059 | } | 
|  | 2060 | return 0; | 
|  | 2061 | } | 
|  | 2062 |  | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 2063 | wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname)); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2064 | force_ifname[0] = '\0'; | 
|  | 2065 |  | 
|  | 2066 | wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group", | 
|  | 2067 | ifname); | 
|  | 2068 | wpa_s->p2p_group_idx++; | 
|  | 2069 |  | 
|  | 2070 | wpa_s->pending_interface_type = type; | 
|  | 2071 | if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname, | 
|  | 2072 | wpa_s->pending_interface_addr, NULL) < 0) { | 
|  | 2073 | wpa_printf(MSG_ERROR, "P2P: Failed to create new group " | 
|  | 2074 | "interface"); | 
|  | 2075 | return -1; | 
|  | 2076 | } | 
|  | 2077 |  | 
| Jimmy Chen | 36c2199 | 2018-11-29 16:46:43 +0800 | [diff] [blame] | 2078 | if (wpa_s->conf->p2p_interface_random_mac_addr) { | 
|  | 2079 | if (random_mac_addr(wpa_s->pending_interface_addr) < 0) { | 
|  | 2080 | wpa_printf(MSG_ERROR, "P2P: Failed to generate random MAC address " | 
|  | 2081 | "for the group interface"); | 
|  | 2082 | return -1; | 
|  | 2083 | } | 
|  | 2084 | wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR " for the group", | 
|  | 2085 | MAC2STR(wpa_s->pending_interface_addr)); | 
|  | 2086 | } | 
|  | 2087 |  | 
|  | 2088 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2089 | if (force_ifname[0]) { | 
|  | 2090 | wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s", | 
|  | 2091 | force_ifname); | 
|  | 2092 | os_strlcpy(wpa_s->pending_interface_name, force_ifname, | 
|  | 2093 | sizeof(wpa_s->pending_interface_name)); | 
|  | 2094 | } else | 
|  | 2095 | os_strlcpy(wpa_s->pending_interface_name, ifname, | 
|  | 2096 | sizeof(wpa_s->pending_interface_name)); | 
|  | 2097 | wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr " | 
|  | 2098 | MACSTR, wpa_s->pending_interface_name, | 
|  | 2099 | MAC2STR(wpa_s->pending_interface_addr)); | 
|  | 2100 |  | 
|  | 2101 | return 0; | 
|  | 2102 | } | 
|  | 2103 |  | 
|  | 2104 |  | 
|  | 2105 | static void wpas_p2p_remove_pending_group_interface( | 
|  | 2106 | struct wpa_supplicant *wpa_s) | 
|  | 2107 | { | 
|  | 2108 | if (!wpa_s->pending_interface_name[0] || | 
|  | 2109 | is_zero_ether_addr(wpa_s->pending_interface_addr)) | 
|  | 2110 | return; /* No pending virtual interface */ | 
|  | 2111 |  | 
|  | 2112 | wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s", | 
|  | 2113 | wpa_s->pending_interface_name); | 
|  | 2114 | wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type, | 
|  | 2115 | wpa_s->pending_interface_name); | 
|  | 2116 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); | 
|  | 2117 | wpa_s->pending_interface_name[0] = '\0'; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2118 | wpa_s->global->pending_group_iface_for_p2ps = 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2119 | } | 
|  | 2120 |  | 
|  | 2121 |  | 
|  | 2122 | static struct wpa_supplicant * | 
|  | 2123 | wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go) | 
|  | 2124 | { | 
|  | 2125 | struct wpa_interface iface; | 
|  | 2126 | struct wpa_supplicant *group_wpa_s; | 
|  | 2127 |  | 
|  | 2128 | if (!wpa_s->pending_interface_name[0]) { | 
|  | 2129 | wpa_printf(MSG_ERROR, "P2P: No pending group interface"); | 
|  | 2130 | if (!wpas_p2p_create_iface(wpa_s)) | 
|  | 2131 | return NULL; | 
|  | 2132 | /* | 
|  | 2133 | * Something has forced us to remove the pending interface; try | 
|  | 2134 | * to create a new one and hope for the best that we will get | 
|  | 2135 | * the same local address. | 
|  | 2136 | */ | 
|  | 2137 | if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : | 
|  | 2138 | WPA_IF_P2P_CLIENT) < 0) | 
|  | 2139 | return NULL; | 
|  | 2140 | } | 
|  | 2141 |  | 
|  | 2142 | os_memset(&iface, 0, sizeof(iface)); | 
|  | 2143 | iface.ifname = wpa_s->pending_interface_name; | 
|  | 2144 | iface.driver = wpa_s->driver->name; | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 2145 | if (wpa_s->conf->ctrl_interface == NULL && | 
|  | 2146 | wpa_s->parent != wpa_s && | 
|  | 2147 | wpa_s->p2p_mgmt && | 
|  | 2148 | (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) | 
|  | 2149 | iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface; | 
|  | 2150 | else | 
|  | 2151 | iface.ctrl_interface = wpa_s->conf->ctrl_interface; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2152 | iface.driver_param = wpa_s->conf->driver_param; | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 2153 | group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2154 | if (group_wpa_s == NULL) { | 
|  | 2155 | wpa_printf(MSG_ERROR, "P2P: Failed to create new " | 
|  | 2156 | "wpa_supplicant interface"); | 
|  | 2157 | return NULL; | 
|  | 2158 | } | 
|  | 2159 | wpa_s->pending_interface_name[0] = '\0'; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2160 | group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO : | 
|  | 2161 | P2P_GROUP_INTERFACE_CLIENT; | 
|  | 2162 | wpa_s->global->p2p_group_formation = group_wpa_s; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2163 | wpa_s->global->pending_group_iface_for_p2ps = 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2164 |  | 
|  | 2165 | wpas_p2p_clone_config(group_wpa_s, wpa_s); | 
|  | 2166 |  | 
| Jimmy Chen | 36c2199 | 2018-11-29 16:46:43 +0800 | [diff] [blame] | 2167 | if (wpa_s->conf->p2p_interface_random_mac_addr) { | 
|  | 2168 | if (wpa_drv_set_mac_addr(group_wpa_s, wpa_s->pending_interface_addr) < 0) { | 
|  | 2169 | wpa_msg(group_wpa_s, MSG_INFO, | 
|  | 2170 | "Failed to set random MAC address"); | 
|  | 2171 | wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s, 0); | 
|  | 2172 | return NULL; | 
|  | 2173 | } | 
|  | 2174 |  | 
|  | 2175 | if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) { | 
|  | 2176 | wpa_msg(group_wpa_s, MSG_INFO, | 
|  | 2177 | "Could not update MAC address information"); | 
|  | 2178 | wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s, 0); | 
|  | 2179 | return NULL; | 
|  | 2180 | } | 
|  | 2181 |  | 
|  | 2182 | wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR " for the group", | 
|  | 2183 | MAC2STR(wpa_s->pending_interface_addr)); | 
|  | 2184 | } | 
|  | 2185 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2186 | return group_wpa_s; | 
|  | 2187 | } | 
|  | 2188 |  | 
|  | 2189 |  | 
|  | 2190 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, | 
|  | 2191 | void *timeout_ctx) | 
|  | 2192 | { | 
|  | 2193 | struct wpa_supplicant *wpa_s = eloop_ctx; | 
|  | 2194 | wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out"); | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 2195 | wpas_p2p_group_formation_failed(wpa_s, 0); | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 2196 | } | 
|  | 2197 |  | 
|  | 2198 |  | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 2199 | static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s, | 
|  | 2200 | int already_deleted) | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 2201 | { | 
|  | 2202 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2203 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2204 | if (wpa_s->global->p2p) | 
|  | 2205 | p2p_group_formation_failed(wpa_s->global->p2p); | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 2206 | wpas_group_formation_completed(wpa_s, 0, already_deleted); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2207 | } | 
|  | 2208 |  | 
|  | 2209 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2210 | static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s) | 
|  | 2211 | { | 
|  | 2212 | wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure"); | 
|  | 2213 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2214 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2215 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2216 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2217 | wpa_s->global->p2p_fail_on_wps_complete = 0; | 
|  | 2218 | } | 
|  | 2219 |  | 
|  | 2220 |  | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2221 | void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s) | 
|  | 2222 | { | 
|  | 2223 | if (wpa_s->global->p2p_group_formation != wpa_s) | 
|  | 2224 | return; | 
|  | 2225 | /* Speed up group formation timeout since this cannot succeed */ | 
|  | 2226 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2227 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2228 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2229 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2230 | } | 
|  | 2231 |  | 
|  | 2232 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2233 | static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2234 | { | 
|  | 2235 | struct wpa_supplicant *wpa_s = ctx; | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2236 | struct wpa_supplicant *group_wpa_s; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2237 |  | 
|  | 2238 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { | 
|  | 2239 | wpa_drv_cancel_remain_on_channel(wpa_s); | 
|  | 2240 | wpa_s->off_channel_freq = 0; | 
|  | 2241 | wpa_s->roc_waiting_drv_freq = 0; | 
|  | 2242 | } | 
|  | 2243 |  | 
|  | 2244 | if (res->status) { | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2245 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 2246 | P2P_EVENT_GO_NEG_FAILURE "status=%d", | 
|  | 2247 | res->status); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2248 | wpas_notify_p2p_go_neg_completed(wpa_s, res); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2249 | wpas_p2p_remove_pending_group_interface(wpa_s); | 
|  | 2250 | return; | 
|  | 2251 | } | 
|  | 2252 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2253 | if (!res->role_go) { | 
|  | 2254 | /* Inform driver of the operating channel of GO. */ | 
|  | 2255 | wpa_drv_set_prob_oper_freq(wpa_s, res->freq); | 
|  | 2256 | } | 
|  | 2257 |  | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2258 | if (wpa_s->p2p_go_ht40) | 
|  | 2259 | res->ht40 = 1; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2260 | if (wpa_s->p2p_go_vht) | 
|  | 2261 | res->vht = 1; | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2262 | res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth; | 
|  | 2263 | res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2264 |  | 
| Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 2265 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s " | 
|  | 2266 | "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR | 
|  | 2267 | " wps_method=%s", | 
|  | 2268 | res->role_go ? "GO" : "client", res->freq, res->ht40, | 
|  | 2269 | MAC2STR(res->peer_device_addr), | 
|  | 2270 | MAC2STR(res->peer_interface_addr), | 
|  | 2271 | p2p_wps_method_text(res->wps_method)); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2272 | wpas_notify_p2p_go_neg_completed(wpa_s, res); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2273 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2274 | if (res->role_go && wpa_s->p2p_persistent_id >= 0) { | 
|  | 2275 | struct wpa_ssid *ssid; | 
|  | 2276 | ssid = wpa_config_get_network(wpa_s->conf, | 
|  | 2277 | wpa_s->p2p_persistent_id); | 
|  | 2278 | if (ssid && ssid->disabled == 2 && | 
|  | 2279 | ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) { | 
|  | 2280 | size_t len = os_strlen(ssid->passphrase); | 
|  | 2281 | wpa_printf(MSG_DEBUG, "P2P: Override passphrase based " | 
|  | 2282 | "on requested persistent group"); | 
|  | 2283 | os_memcpy(res->passphrase, ssid->passphrase, len); | 
|  | 2284 | res->passphrase[len] = '\0'; | 
|  | 2285 | } | 
|  | 2286 | } | 
|  | 2287 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2288 | if (wpa_s->create_p2p_iface) { | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2289 | group_wpa_s = | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2290 | wpas_p2p_init_group_interface(wpa_s, res->role_go); | 
|  | 2291 | if (group_wpa_s == NULL) { | 
|  | 2292 | wpas_p2p_remove_pending_group_interface(wpa_s); | 
| Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 2293 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, | 
|  | 2294 | wpa_s, NULL); | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 2295 | wpas_p2p_group_formation_failed(wpa_s, 1); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2296 | return; | 
|  | 2297 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2298 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); | 
|  | 2299 | wpa_s->pending_interface_name[0] = '\0'; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2300 | } else { | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2301 | group_wpa_s = wpa_s->parent; | 
|  | 2302 | wpa_s->global->p2p_group_formation = group_wpa_s; | 
|  | 2303 | if (group_wpa_s != wpa_s) | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2304 | wpas_p2p_clone_config(group_wpa_s, wpa_s); | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2305 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2306 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2307 | group_wpa_s->p2p_in_provisioning = 1; | 
|  | 2308 | group_wpa_s->p2pdev = wpa_s; | 
|  | 2309 | if (group_wpa_s != wpa_s) { | 
|  | 2310 | os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin, | 
|  | 2311 | sizeof(group_wpa_s->p2p_pin)); | 
|  | 2312 | group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method; | 
|  | 2313 | } | 
|  | 2314 | if (res->role_go) { | 
|  | 2315 | wpas_start_wps_go(group_wpa_s, res, 1); | 
|  | 2316 | } else { | 
|  | 2317 | os_get_reltime(&group_wpa_s->scan_min_time); | 
|  | 2318 | wpas_start_wps_enrollee(group_wpa_s, res); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2319 | } | 
|  | 2320 |  | 
|  | 2321 | wpa_s->p2p_long_listen = 0; | 
|  | 2322 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); | 
|  | 2323 |  | 
|  | 2324 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); | 
|  | 2325 | eloop_register_timeout(15 + res->peer_config_timeout / 100, | 
|  | 2326 | (res->peer_config_timeout % 100) * 10000, | 
|  | 2327 | wpas_p2p_group_formation_timeout, wpa_s, NULL); | 
|  | 2328 | } | 
|  | 2329 |  | 
|  | 2330 |  | 
| Dmitry Shmidt | 7a53dbb | 2015-06-11 13:13:53 -0700 | [diff] [blame] | 2331 | static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id, | 
|  | 2332 | u8 go_intent) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2333 | { | 
|  | 2334 | struct wpa_supplicant *wpa_s = ctx; | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2335 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR | 
| Dmitry Shmidt | 7a53dbb | 2015-06-11 13:13:53 -0700 | [diff] [blame] | 2336 | " dev_passwd_id=%u go_intent=%u", MAC2STR(src), | 
|  | 2337 | dev_passwd_id, go_intent); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2338 |  | 
| Dmitry Shmidt | 7a53dbb | 2015-06-11 13:13:53 -0700 | [diff] [blame] | 2339 | wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2340 | } | 
|  | 2341 |  | 
|  | 2342 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2343 | static void wpas_dev_found(void *ctx, const u8 *addr, | 
|  | 2344 | const struct p2p_peer_info *info, | 
|  | 2345 | int new_device) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2346 | { | 
| Roshan Pius | fd2fd66 | 2017-01-23 13:41:57 -0800 | [diff] [blame] | 2347 | u8 *wfd_dev_info = NULL; | 
|  | 2348 | u8 wfd_dev_info_len = 0; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2349 | #ifndef CONFIG_NO_STDOUT_DEBUG | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2350 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 2351 | char devtype[WPS_DEV_TYPE_BUFSIZE]; | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 2352 | char *wfd_dev_info_hex = NULL; | 
|  | 2353 |  | 
| Irfan Sheriff | 8367dc9 | 2012-09-09 17:08:19 -0700 | [diff] [blame] | 2354 | #ifdef CONFIG_WIFI_DISPLAY | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 2355 | wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems, | 
|  | 2356 | WFD_SUBELEM_DEVICE_INFO); | 
| Roshan Pius | fd2fd66 | 2017-01-23 13:41:57 -0800 | [diff] [blame] | 2357 | if (wfd_dev_info_hex) { | 
|  | 2358 | wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2; | 
|  | 2359 | wfd_dev_info = os_zalloc(wfd_dev_info_len); | 
|  | 2360 | // Only used for notification, so not handling error. | 
|  | 2361 | hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len); | 
|  | 2362 | } | 
| Irfan Sheriff | 8367dc9 | 2012-09-09 17:08:19 -0700 | [diff] [blame] | 2363 | #endif /* CONFIG_WIFI_DISPLAY */ | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 2364 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2365 | if (info->p2ps_instance) { | 
|  | 2366 | char str[256]; | 
|  | 2367 | const u8 *buf = wpabuf_head(info->p2ps_instance); | 
|  | 2368 | size_t len = wpabuf_len(info->p2ps_instance); | 
|  | 2369 |  | 
|  | 2370 | while (len) { | 
|  | 2371 | u32 id; | 
|  | 2372 | u16 methods; | 
|  | 2373 | u8 str_len; | 
|  | 2374 |  | 
|  | 2375 | if (len < 4 + 2 + 1) | 
|  | 2376 | break; | 
|  | 2377 | id = WPA_GET_LE32(buf); | 
|  | 2378 | buf += sizeof(u32); | 
|  | 2379 | methods = WPA_GET_BE16(buf); | 
|  | 2380 | buf += sizeof(u16); | 
|  | 2381 | str_len = *buf++; | 
|  | 2382 | if (str_len > len - 4 - 2 - 1) | 
|  | 2383 | break; | 
|  | 2384 | os_memcpy(str, buf, str_len); | 
|  | 2385 | str[str_len] = '\0'; | 
|  | 2386 | buf += str_len; | 
|  | 2387 | len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8); | 
|  | 2388 |  | 
|  | 2389 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 2390 | P2P_EVENT_DEVICE_FOUND MACSTR | 
|  | 2391 | " p2p_dev_addr=" MACSTR | 
|  | 2392 | " pri_dev_type=%s name='%s'" | 
|  | 2393 | " config_methods=0x%x" | 
|  | 2394 | " dev_capab=0x%x" | 
|  | 2395 | " group_capab=0x%x" | 
|  | 2396 | " adv_id=%x asp_svc=%s%s", | 
|  | 2397 | MAC2STR(addr), | 
|  | 2398 | MAC2STR(info->p2p_device_addr), | 
|  | 2399 | wps_dev_type_bin2str( | 
|  | 2400 | info->pri_dev_type, | 
|  | 2401 | devtype, sizeof(devtype)), | 
|  | 2402 | info->device_name, methods, | 
|  | 2403 | info->dev_capab, info->group_capab, | 
|  | 2404 | id, str, | 
|  | 2405 | info->vendor_elems ? | 
|  | 2406 | " vendor_elems=1" : ""); | 
|  | 2407 | } | 
|  | 2408 | goto done; | 
|  | 2409 | } | 
|  | 2410 |  | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2411 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR | 
|  | 2412 | " p2p_dev_addr=" MACSTR | 
|  | 2413 | " pri_dev_type=%s name='%s' config_methods=0x%x " | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2414 | "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d", | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2415 | MAC2STR(addr), MAC2STR(info->p2p_device_addr), | 
|  | 2416 | wps_dev_type_bin2str(info->pri_dev_type, devtype, | 
|  | 2417 | sizeof(devtype)), | 
|  | 2418 | info->device_name, info->config_methods, | 
|  | 2419 | info->dev_capab, info->group_capab, | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 2420 | wfd_dev_info_hex ? " wfd_dev_info=0x" : "", | 
| Dmitry Shmidt | 2e67f06 | 2014-07-16 09:55:28 -0700 | [diff] [blame] | 2421 | wfd_dev_info_hex ? wfd_dev_info_hex : "", | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2422 | info->vendor_elems ? " vendor_elems=1" : "", | 
|  | 2423 | new_device); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2424 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2425 | done: | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 2426 | os_free(wfd_dev_info_hex); | 
| Dmitry Shmidt | 9767226 | 2014-02-03 13:02:54 -0800 | [diff] [blame] | 2427 | #endif /* CONFIG_NO_STDOUT_DEBUG */ | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 2428 |  | 
| Roshan Pius | fd2fd66 | 2017-01-23 13:41:57 -0800 | [diff] [blame] | 2429 | wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info, | 
|  | 2430 | wfd_dev_info_len, new_device); | 
|  | 2431 | os_free(wfd_dev_info); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2432 | } | 
|  | 2433 |  | 
|  | 2434 |  | 
|  | 2435 | static void wpas_dev_lost(void *ctx, const u8 *dev_addr) | 
|  | 2436 | { | 
|  | 2437 | struct wpa_supplicant *wpa_s = ctx; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2438 |  | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2439 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST | 
|  | 2440 | "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr)); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2441 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2442 | wpas_notify_p2p_device_lost(wpa_s, dev_addr); | 
|  | 2443 | } | 
|  | 2444 |  | 
|  | 2445 |  | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2446 | static void wpas_find_stopped(void *ctx) | 
|  | 2447 | { | 
|  | 2448 | struct wpa_supplicant *wpa_s = ctx; | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2449 |  | 
|  | 2450 | if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0) | 
|  | 2451 | wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed"); | 
|  | 2452 |  | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2453 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED); | 
| Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 2454 | wpas_notify_p2p_find_stopped(wpa_s); | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2455 | } | 
|  | 2456 |  | 
|  | 2457 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2458 | struct wpas_p2p_listen_work { | 
|  | 2459 | unsigned int freq; | 
|  | 2460 | unsigned int duration; | 
|  | 2461 | struct wpabuf *probe_resp_ie; | 
|  | 2462 | }; | 
|  | 2463 |  | 
|  | 2464 |  | 
|  | 2465 | static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork) | 
|  | 2466 | { | 
|  | 2467 | if (lwork == NULL) | 
|  | 2468 | return; | 
|  | 2469 | wpabuf_free(lwork->probe_resp_ie); | 
|  | 2470 | os_free(lwork); | 
|  | 2471 | } | 
|  | 2472 |  | 
|  | 2473 |  | 
|  | 2474 | static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s) | 
|  | 2475 | { | 
|  | 2476 | struct wpas_p2p_listen_work *lwork; | 
|  | 2477 |  | 
|  | 2478 | if (!wpa_s->p2p_listen_work) | 
|  | 2479 | return; | 
|  | 2480 |  | 
|  | 2481 | lwork = wpa_s->p2p_listen_work->ctx; | 
|  | 2482 | wpas_p2p_listen_work_free(lwork); | 
|  | 2483 | radio_work_done(wpa_s->p2p_listen_work); | 
|  | 2484 | wpa_s->p2p_listen_work = NULL; | 
|  | 2485 | } | 
|  | 2486 |  | 
|  | 2487 |  | 
|  | 2488 | static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit) | 
|  | 2489 | { | 
|  | 2490 | struct wpa_supplicant *wpa_s = work->wpa_s; | 
|  | 2491 | struct wpas_p2p_listen_work *lwork = work->ctx; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2492 | unsigned int duration; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2493 |  | 
|  | 2494 | if (deinit) { | 
| Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 2495 | if (work->started) { | 
|  | 2496 | wpa_s->p2p_listen_work = NULL; | 
|  | 2497 | wpas_stop_listen(wpa_s); | 
|  | 2498 | } | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2499 | wpas_p2p_listen_work_free(lwork); | 
|  | 2500 | return; | 
|  | 2501 | } | 
|  | 2502 |  | 
|  | 2503 | wpa_s->p2p_listen_work = work; | 
|  | 2504 |  | 
|  | 2505 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL); | 
|  | 2506 |  | 
|  | 2507 | if (wpa_drv_probe_req_report(wpa_s, 1) < 0) { | 
|  | 2508 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to " | 
|  | 2509 | "report received Probe Request frames"); | 
|  | 2510 | wpas_p2p_listen_work_done(wpa_s); | 
|  | 2511 | return; | 
|  | 2512 | } | 
|  | 2513 |  | 
|  | 2514 | wpa_s->pending_listen_freq = lwork->freq; | 
|  | 2515 | wpa_s->pending_listen_duration = lwork->duration; | 
|  | 2516 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2517 | duration = lwork->duration; | 
|  | 2518 | #ifdef CONFIG_TESTING_OPTIONS | 
|  | 2519 | if (wpa_s->extra_roc_dur) { | 
|  | 2520 | wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u", | 
|  | 2521 | duration, duration + wpa_s->extra_roc_dur); | 
|  | 2522 | duration += wpa_s->extra_roc_dur; | 
|  | 2523 | } | 
|  | 2524 | #endif /* CONFIG_TESTING_OPTIONS */ | 
|  | 2525 |  | 
|  | 2526 | if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) { | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2527 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver " | 
|  | 2528 | "to remain on channel (%u MHz) for Listen " | 
|  | 2529 | "state", lwork->freq); | 
|  | 2530 | wpas_p2p_listen_work_done(wpa_s); | 
|  | 2531 | wpa_s->pending_listen_freq = 0; | 
|  | 2532 | return; | 
|  | 2533 | } | 
|  | 2534 | wpa_s->off_channel_freq = 0; | 
|  | 2535 | wpa_s->roc_waiting_drv_freq = lwork->freq; | 
|  | 2536 | } | 
|  | 2537 |  | 
|  | 2538 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2539 | static int wpas_start_listen(void *ctx, unsigned int freq, | 
|  | 2540 | unsigned int duration, | 
|  | 2541 | const struct wpabuf *probe_resp_ie) | 
|  | 2542 | { | 
|  | 2543 | struct wpa_supplicant *wpa_s = ctx; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2544 | struct wpas_p2p_listen_work *lwork; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2545 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2546 | if (wpa_s->p2p_listen_work) { | 
|  | 2547 | wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists"); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2548 | return -1; | 
|  | 2549 | } | 
|  | 2550 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2551 | lwork = os_zalloc(sizeof(*lwork)); | 
|  | 2552 | if (lwork == NULL) | 
|  | 2553 | return -1; | 
|  | 2554 | lwork->freq = freq; | 
|  | 2555 | lwork->duration = duration; | 
|  | 2556 | if (probe_resp_ie) { | 
|  | 2557 | lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie); | 
|  | 2558 | if (lwork->probe_resp_ie == NULL) { | 
|  | 2559 | wpas_p2p_listen_work_free(lwork); | 
|  | 2560 | return -1; | 
|  | 2561 | } | 
|  | 2562 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2563 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2564 | if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb, | 
|  | 2565 | lwork) < 0) { | 
|  | 2566 | wpas_p2p_listen_work_free(lwork); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2567 | return -1; | 
|  | 2568 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2569 |  | 
|  | 2570 | return 0; | 
|  | 2571 | } | 
|  | 2572 |  | 
|  | 2573 |  | 
|  | 2574 | static void wpas_stop_listen(void *ctx) | 
|  | 2575 | { | 
|  | 2576 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 2577 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { | 
|  | 2578 | wpa_drv_cancel_remain_on_channel(wpa_s); | 
|  | 2579 | wpa_s->off_channel_freq = 0; | 
|  | 2580 | wpa_s->roc_waiting_drv_freq = 0; | 
|  | 2581 | } | 
|  | 2582 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL); | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 2583 |  | 
|  | 2584 | /* | 
|  | 2585 | * Don't cancel Probe Request RX reporting for a connected P2P Client | 
|  | 2586 | * handling Probe Request frames. | 
|  | 2587 | */ | 
|  | 2588 | if (!wpa_s->p2p_cli_probe) | 
|  | 2589 | wpa_drv_probe_req_report(wpa_s, 0); | 
|  | 2590 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2591 | wpas_p2p_listen_work_done(wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2592 | } | 
|  | 2593 |  | 
|  | 2594 |  | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 2595 | static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf, | 
|  | 2596 | unsigned int freq) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2597 | { | 
|  | 2598 | struct wpa_supplicant *wpa_s = ctx; | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 2599 | return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1, | 
|  | 2600 | freq); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2601 | } | 
|  | 2602 |  | 
|  | 2603 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2604 | static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s, | 
|  | 2605 | const u8 *peer, const char *params, | 
|  | 2606 | unsigned int generated_pin) | 
|  | 2607 | { | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2608 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR | 
|  | 2609 | " %08d%s", MAC2STR(peer), generated_pin, params); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2610 | } | 
|  | 2611 |  | 
|  | 2612 |  | 
|  | 2613 | static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s, | 
|  | 2614 | const u8 *peer, const char *params) | 
|  | 2615 | { | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2616 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR | 
|  | 2617 | "%s", MAC2STR(peer), params); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2618 | } | 
|  | 2619 |  | 
|  | 2620 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2621 | static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods, | 
|  | 2622 | const u8 *dev_addr, const u8 *pri_dev_type, | 
|  | 2623 | const char *dev_name, u16 supp_config_methods, | 
|  | 2624 | u8 dev_capab, u8 group_capab, const u8 *group_id, | 
|  | 2625 | size_t group_id_len) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2626 | { | 
|  | 2627 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 2628 | char devtype[WPS_DEV_TYPE_BUFSIZE]; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2629 | char params[300]; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2630 | u8 empty_dev_type[8]; | 
|  | 2631 | unsigned int generated_pin = 0; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2632 | struct wpa_supplicant *group = NULL; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2633 | int res; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2634 |  | 
|  | 2635 | if (group_id) { | 
|  | 2636 | for (group = wpa_s->global->ifaces; group; group = group->next) | 
|  | 2637 | { | 
|  | 2638 | struct wpa_ssid *s = group->current_ssid; | 
|  | 2639 | if (s != NULL && | 
|  | 2640 | s->mode == WPAS_MODE_P2P_GO && | 
|  | 2641 | group_id_len - ETH_ALEN == s->ssid_len && | 
|  | 2642 | os_memcmp(group_id + ETH_ALEN, s->ssid, | 
|  | 2643 | s->ssid_len) == 0) | 
|  | 2644 | break; | 
|  | 2645 | } | 
|  | 2646 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2647 |  | 
|  | 2648 | if (pri_dev_type == NULL) { | 
|  | 2649 | os_memset(empty_dev_type, 0, sizeof(empty_dev_type)); | 
|  | 2650 | pri_dev_type = empty_dev_type; | 
|  | 2651 | } | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2652 | res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR | 
|  | 2653 | " pri_dev_type=%s name='%s' config_methods=0x%x " | 
|  | 2654 | "dev_capab=0x%x group_capab=0x%x%s%s", | 
|  | 2655 | MAC2STR(dev_addr), | 
|  | 2656 | wps_dev_type_bin2str(pri_dev_type, devtype, | 
|  | 2657 | sizeof(devtype)), | 
|  | 2658 | dev_name, supp_config_methods, dev_capab, group_capab, | 
|  | 2659 | group ? " group=" : "", | 
|  | 2660 | group ? group->ifname : ""); | 
|  | 2661 | if (os_snprintf_error(sizeof(params), res)) | 
|  | 2662 | wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated"); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2663 | params[sizeof(params) - 1] = '\0'; | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2664 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2665 | if (config_methods & WPS_CONFIG_DISPLAY) { | 
| Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 2666 | if (wps_generate_pin(&generated_pin) < 0) { | 
|  | 2667 | wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN"); | 
|  | 2668 | wpas_notify_p2p_provision_discovery( | 
|  | 2669 | wpa_s, peer, 0 /* response */, | 
|  | 2670 | P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0); | 
|  | 2671 | return; | 
|  | 2672 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2673 | wpas_prov_disc_local_display(wpa_s, peer, params, | 
|  | 2674 | generated_pin); | 
|  | 2675 | } else if (config_methods & WPS_CONFIG_KEYPAD) | 
|  | 2676 | wpas_prov_disc_local_keypad(wpa_s, peer, params); | 
|  | 2677 | else if (config_methods & WPS_CONFIG_PUSHBUTTON) | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2678 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ | 
|  | 2679 | MACSTR "%s", MAC2STR(peer), params); | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2680 |  | 
|  | 2681 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */, | 
|  | 2682 | P2P_PROV_DISC_SUCCESS, | 
|  | 2683 | config_methods, generated_pin); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2684 | } | 
|  | 2685 |  | 
|  | 2686 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2687 | static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2688 | { | 
|  | 2689 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 2690 | unsigned int generated_pin = 0; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2691 | char params[20]; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2692 |  | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2693 | if (wpa_s->pending_pd_before_join && | 
|  | 2694 | (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || | 
|  | 2695 | os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { | 
|  | 2696 | wpa_s->pending_pd_before_join = 0; | 
|  | 2697 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " | 
|  | 2698 | "join-existing-group operation"); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2699 | wpas_p2p_join_start(wpa_s, 0, NULL, 0); | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2700 | return; | 
|  | 2701 | } | 
|  | 2702 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2703 | if (wpa_s->pending_pd_use == AUTO_PD_JOIN || | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2704 | wpa_s->pending_pd_use == AUTO_PD_GO_NEG) { | 
|  | 2705 | int res; | 
|  | 2706 |  | 
|  | 2707 | res = os_snprintf(params, sizeof(params), " peer_go=%d", | 
|  | 2708 | wpa_s->pending_pd_use == AUTO_PD_JOIN); | 
|  | 2709 | if (os_snprintf_error(sizeof(params), res)) | 
|  | 2710 | params[sizeof(params) - 1] = '\0'; | 
|  | 2711 | } else | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2712 | params[0] = '\0'; | 
|  | 2713 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2714 | if (config_methods & WPS_CONFIG_DISPLAY) | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2715 | wpas_prov_disc_local_keypad(wpa_s, peer, params); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2716 | else if (config_methods & WPS_CONFIG_KEYPAD) { | 
| Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 2717 | if (wps_generate_pin(&generated_pin) < 0) { | 
|  | 2718 | wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN"); | 
|  | 2719 | wpas_notify_p2p_provision_discovery( | 
|  | 2720 | wpa_s, peer, 0 /* response */, | 
|  | 2721 | P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0); | 
|  | 2722 | return; | 
|  | 2723 | } | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2724 | wpas_prov_disc_local_display(wpa_s, peer, params, | 
|  | 2725 | generated_pin); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2726 | } else if (config_methods & WPS_CONFIG_PUSHBUTTON) | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2727 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP | 
|  | 2728 | MACSTR "%s", MAC2STR(peer), params); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2729 |  | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2730 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, | 
|  | 2731 | P2P_PROV_DISC_SUCCESS, | 
|  | 2732 | config_methods, generated_pin); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2733 | } | 
|  | 2734 |  | 
|  | 2735 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2736 | static void wpas_prov_disc_fail(void *ctx, const u8 *peer, | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2737 | enum p2p_prov_disc_status status, | 
|  | 2738 | u32 adv_id, const u8 *adv_mac, | 
|  | 2739 | const char *deferred_session_resp) | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2740 | { | 
|  | 2741 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 2742 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2743 | if (wpa_s->p2p_fallback_to_go_neg) { | 
|  | 2744 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto " | 
|  | 2745 | "failed - fall back to GO Negotiation"); | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2746 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2747 | P2P_EVENT_FALLBACK_TO_GO_NEG | 
|  | 2748 | "reason=PD-failed"); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2749 | wpas_p2p_fallback_to_go_neg(wpa_s, 0); | 
|  | 2750 | return; | 
|  | 2751 | } | 
|  | 2752 |  | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2753 | if (status == P2P_PROV_DISC_TIMEOUT_JOIN) { | 
| Dmitry Shmidt | 2b38048 | 2012-09-13 10:52:13 -0700 | [diff] [blame] | 2754 | wpa_s->pending_pd_before_join = 0; | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2755 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " | 
|  | 2756 | "join-existing-group operation (no ACK for PD " | 
|  | 2757 | "Req attempts)"); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2758 | wpas_p2p_join_start(wpa_s, 0, NULL, 0); | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2759 | return; | 
| Dmitry Shmidt | 2b38048 | 2012-09-13 10:52:13 -0700 | [diff] [blame] | 2760 | } | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2761 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2762 | if (adv_id && adv_mac && deferred_session_resp) { | 
|  | 2763 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE | 
|  | 2764 | " p2p_dev_addr=" MACSTR " status=%d adv_id=%x" | 
|  | 2765 | " deferred_session_resp='%s'", | 
|  | 2766 | MAC2STR(peer), status, adv_id, | 
|  | 2767 | deferred_session_resp); | 
|  | 2768 | } else if (adv_id && adv_mac) { | 
|  | 2769 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE | 
|  | 2770 | " p2p_dev_addr=" MACSTR " status=%d adv_id=%x", | 
|  | 2771 | MAC2STR(peer), status, adv_id); | 
|  | 2772 | } else { | 
|  | 2773 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE | 
|  | 2774 | " p2p_dev_addr=" MACSTR " status=%d", | 
|  | 2775 | MAC2STR(peer), status); | 
|  | 2776 | } | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2777 |  | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2778 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, | 
|  | 2779 | status, 0, 0); | 
|  | 2780 | } | 
|  | 2781 |  | 
|  | 2782 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2783 | static int freq_included(struct wpa_supplicant *wpa_s, | 
|  | 2784 | const struct p2p_channels *channels, | 
|  | 2785 | unsigned int freq) | 
| Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2786 | { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2787 | if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) && | 
|  | 2788 | wpas_p2p_go_is_peer_freq(wpa_s, freq)) | 
|  | 2789 | return 1; | 
|  | 2790 | return 0; | 
|  | 2791 | } | 
| Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2792 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2793 |  | 
|  | 2794 | static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s) | 
|  | 2795 | { | 
|  | 2796 | unsigned int num = P2P_MAX_CHANNELS; | 
|  | 2797 | int *common_freqs; | 
|  | 2798 | int ret; | 
|  | 2799 |  | 
|  | 2800 | p2p_go_dump_common_freqs(wpa_s); | 
|  | 2801 | common_freqs = os_calloc(num, sizeof(int)); | 
|  | 2802 | if (!common_freqs) | 
|  | 2803 | return; | 
|  | 2804 |  | 
|  | 2805 | ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num); | 
|  | 2806 | if (ret < 0) { | 
|  | 2807 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 2808 | "P2P: Failed to get group common freqs"); | 
|  | 2809 | os_free(common_freqs); | 
|  | 2810 | return; | 
|  | 2811 | } | 
|  | 2812 |  | 
|  | 2813 | os_free(wpa_s->p2p_group_common_freqs); | 
|  | 2814 | wpa_s->p2p_group_common_freqs = common_freqs; | 
|  | 2815 | wpa_s->p2p_group_common_freqs_num = num; | 
|  | 2816 | p2p_go_dump_common_freqs(wpa_s); | 
|  | 2817 | } | 
|  | 2818 |  | 
|  | 2819 |  | 
|  | 2820 | /* | 
|  | 2821 | * Check if the given frequency is one of the possible operating frequencies | 
|  | 2822 | * set after the completion of the GO Negotiation. | 
|  | 2823 | */ | 
|  | 2824 | static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq) | 
|  | 2825 | { | 
|  | 2826 | unsigned int i; | 
|  | 2827 |  | 
|  | 2828 | p2p_go_dump_common_freqs(wpa_s); | 
|  | 2829 |  | 
|  | 2830 | /* assume no restrictions */ | 
|  | 2831 | if (!wpa_s->p2p_group_common_freqs_num) | 
|  | 2832 | return 1; | 
|  | 2833 |  | 
|  | 2834 | for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) { | 
|  | 2835 | if (wpa_s->p2p_group_common_freqs[i] == freq) | 
|  | 2836 | return 1; | 
|  | 2837 | } | 
|  | 2838 | return 0; | 
|  | 2839 | } | 
|  | 2840 |  | 
|  | 2841 |  | 
|  | 2842 | static int wpas_sta_check_ecsa(struct hostapd_data *hapd, | 
|  | 2843 | struct sta_info *sta, void *ctx) | 
|  | 2844 | { | 
|  | 2845 | int *ecsa_support = ctx; | 
|  | 2846 |  | 
|  | 2847 | *ecsa_support &= sta->ecsa_supported; | 
|  | 2848 |  | 
|  | 2849 | return 0; | 
|  | 2850 | } | 
|  | 2851 |  | 
|  | 2852 |  | 
|  | 2853 | /* Check if all the peers support eCSA */ | 
|  | 2854 | static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s) | 
|  | 2855 | { | 
|  | 2856 | int ecsa_support = 1; | 
|  | 2857 |  | 
|  | 2858 | ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa, | 
|  | 2859 | &ecsa_support); | 
|  | 2860 |  | 
|  | 2861 | return ecsa_support; | 
| Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2862 | } | 
|  | 2863 |  | 
|  | 2864 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2865 | /** | 
|  | 2866 | * Pick the best frequency to use from all the currently used frequencies. | 
|  | 2867 | */ | 
|  | 2868 | static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s, | 
|  | 2869 | struct wpa_used_freq_data *freqs, | 
|  | 2870 | unsigned int num) | 
|  | 2871 | { | 
|  | 2872 | unsigned int i, c; | 
|  | 2873 |  | 
|  | 2874 | /* find a candidate freq that is supported by P2P */ | 
|  | 2875 | for (c = 0; c < num; c++) | 
|  | 2876 | if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq)) | 
|  | 2877 | break; | 
|  | 2878 |  | 
|  | 2879 | if (c == num) | 
|  | 2880 | return 0; | 
|  | 2881 |  | 
|  | 2882 | /* once we have a candidate, try to find a 'better' one */ | 
|  | 2883 | for (i = c + 1; i < num; i++) { | 
|  | 2884 | if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq)) | 
|  | 2885 | continue; | 
|  | 2886 |  | 
|  | 2887 | /* | 
|  | 2888 | * 1. Infrastructure station interfaces have higher preference. | 
|  | 2889 | * 2. P2P Clients have higher preference. | 
|  | 2890 | * 3. All others. | 
|  | 2891 | */ | 
|  | 2892 | if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) { | 
|  | 2893 | c = i; | 
|  | 2894 | break; | 
|  | 2895 | } | 
|  | 2896 |  | 
|  | 2897 | if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT)) | 
|  | 2898 | c = i; | 
|  | 2899 | } | 
|  | 2900 | return freqs[c].freq; | 
|  | 2901 | } | 
|  | 2902 |  | 
|  | 2903 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2904 | static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid, | 
|  | 2905 | const u8 *go_dev_addr, const u8 *ssid, | 
|  | 2906 | size_t ssid_len, int *go, u8 *group_bssid, | 
| Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2907 | int *force_freq, int persistent_group, | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2908 | const struct p2p_channels *channels, | 
|  | 2909 | int dev_pw_id) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2910 | { | 
|  | 2911 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 2912 | struct wpa_ssid *s; | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2913 | struct wpa_used_freq_data *freqs; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2914 | struct wpa_supplicant *grp; | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2915 | int best_freq; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2916 |  | 
|  | 2917 | if (!persistent_group) { | 
|  | 2918 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR | 
| Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 2919 | " to join an active group (SSID: %s)", | 
|  | 2920 | MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len)); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2921 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && | 
|  | 2922 | (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN) | 
|  | 2923 | == 0 || | 
|  | 2924 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) { | 
|  | 2925 | wpa_printf(MSG_DEBUG, "P2P: Accept previously " | 
|  | 2926 | "authorized invitation"); | 
|  | 2927 | goto accept_inv; | 
|  | 2928 | } | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2929 |  | 
|  | 2930 | #ifdef CONFIG_WPS_NFC | 
| Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2931 | if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled && | 
|  | 2932 | dev_pw_id == wpa_s->p2p_oob_dev_pw_id) { | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2933 | wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag"); | 
| Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2934 | wpa_s->p2p_wps_method = WPS_NFC; | 
|  | 2935 | wpa_s->pending_join_wps_method = WPS_NFC; | 
|  | 2936 | os_memcpy(wpa_s->pending_join_dev_addr, | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2937 | go_dev_addr, ETH_ALEN); | 
| Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2938 | os_memcpy(wpa_s->pending_join_iface_addr, | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2939 | bssid, ETH_ALEN); | 
|  | 2940 | goto accept_inv; | 
|  | 2941 | } | 
|  | 2942 | #endif /* CONFIG_WPS_NFC */ | 
|  | 2943 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2944 | /* | 
|  | 2945 | * Do not accept the invitation automatically; notify user and | 
|  | 2946 | * request approval. | 
|  | 2947 | */ | 
|  | 2948 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; | 
|  | 2949 | } | 
|  | 2950 |  | 
|  | 2951 | grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go); | 
|  | 2952 | if (grp) { | 
|  | 2953 | wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already " | 
|  | 2954 | "running persistent group"); | 
|  | 2955 | if (*go) | 
|  | 2956 | os_memcpy(group_bssid, grp->own_addr, ETH_ALEN); | 
|  | 2957 | goto accept_inv; | 
|  | 2958 | } | 
|  | 2959 |  | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2960 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && | 
|  | 2961 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) { | 
|  | 2962 | wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated " | 
|  | 2963 | "invitation to re-invoke a persistent group"); | 
| Dmitry Shmidt | 76cd2cc | 2014-05-27 12:56:04 -0700 | [diff] [blame] | 2964 | os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN); | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2965 | } else if (!wpa_s->conf->persistent_reconnect) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2966 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; | 
|  | 2967 |  | 
|  | 2968 | for (s = wpa_s->conf->ssid; s; s = s->next) { | 
|  | 2969 | if (s->disabled == 2 && | 
|  | 2970 | os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 && | 
|  | 2971 | s->ssid_len == ssid_len && | 
|  | 2972 | os_memcmp(ssid, s->ssid, ssid_len) == 0) | 
|  | 2973 | break; | 
|  | 2974 | } | 
|  | 2975 |  | 
|  | 2976 | if (!s) { | 
|  | 2977 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR | 
|  | 2978 | " requested reinvocation of an unknown group", | 
|  | 2979 | MAC2STR(sa)); | 
|  | 2980 | return P2P_SC_FAIL_UNKNOWN_GROUP; | 
|  | 2981 | } | 
|  | 2982 |  | 
|  | 2983 | if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) { | 
|  | 2984 | *go = 1; | 
|  | 2985 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { | 
|  | 2986 | wpa_printf(MSG_DEBUG, "P2P: The only available " | 
|  | 2987 | "interface is already in use - reject " | 
|  | 2988 | "invitation"); | 
|  | 2989 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; | 
|  | 2990 | } | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2991 | if (wpa_s->p2p_mgmt) | 
|  | 2992 | os_memcpy(group_bssid, wpa_s->parent->own_addr, | 
|  | 2993 | ETH_ALEN); | 
|  | 2994 | else | 
|  | 2995 | os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2996 | } else if (s->mode == WPAS_MODE_P2P_GO) { | 
|  | 2997 | *go = 1; | 
|  | 2998 | if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0) | 
|  | 2999 | { | 
|  | 3000 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " | 
|  | 3001 | "interface address for the group"); | 
|  | 3002 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; | 
|  | 3003 | } | 
|  | 3004 | os_memcpy(group_bssid, wpa_s->pending_interface_addr, | 
|  | 3005 | ETH_ALEN); | 
|  | 3006 | } | 
|  | 3007 |  | 
|  | 3008 | accept_inv: | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3009 | wpas_p2p_set_own_freq_preference(wpa_s, 0); | 
|  | 3010 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 3011 | best_freq = 0; | 
|  | 3012 | freqs = os_calloc(wpa_s->num_multichan_concurrent, | 
|  | 3013 | sizeof(struct wpa_used_freq_data)); | 
|  | 3014 | if (freqs) { | 
|  | 3015 | int num_channels = wpa_s->num_multichan_concurrent; | 
|  | 3016 | int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels); | 
|  | 3017 | best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num); | 
|  | 3018 | os_free(freqs); | 
|  | 3019 | } | 
|  | 3020 |  | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3021 | /* Get one of the frequencies currently in use */ | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 3022 | if (best_freq > 0) { | 
| Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 3023 | wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces"); | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 3024 | wpas_p2p_set_own_freq_preference(wpa_s, best_freq); | 
| Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 3025 |  | 
|  | 3026 | if (wpa_s->num_multichan_concurrent < 2 || | 
|  | 3027 | wpas_p2p_num_unused_channels(wpa_s) < 1) { | 
|  | 3028 | wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces"); | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 3029 | *force_freq = best_freq; | 
| Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 3030 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3031 | } | 
|  | 3032 |  | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3033 | if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 && | 
|  | 3034 | wpas_p2p_num_unused_channels(wpa_s) > 0) { | 
| Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 3035 | if (*go == 0) { | 
|  | 3036 | /* We are the client */ | 
|  | 3037 | wpa_printf(MSG_DEBUG, "P2P: Peer was found to be " | 
|  | 3038 | "running a GO but we are capable of MCC, " | 
|  | 3039 | "figure out the best channel to use"); | 
|  | 3040 | *force_freq = 0; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3041 | } else if (!freq_included(wpa_s, channels, *force_freq)) { | 
| Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 3042 | /* We are the GO, and *force_freq is not in the | 
|  | 3043 | * intersection */ | 
|  | 3044 | wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not " | 
|  | 3045 | "in intersection but we are capable of MCC, " | 
|  | 3046 | "figure out the best channel to use", | 
|  | 3047 | *force_freq); | 
|  | 3048 | *force_freq = 0; | 
|  | 3049 | } | 
|  | 3050 | } | 
|  | 3051 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3052 | return P2P_SC_SUCCESS; | 
|  | 3053 | } | 
|  | 3054 |  | 
|  | 3055 |  | 
|  | 3056 | static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid, | 
|  | 3057 | const u8 *ssid, size_t ssid_len, | 
|  | 3058 | const u8 *go_dev_addr, u8 status, | 
|  | 3059 | int op_freq) | 
|  | 3060 | { | 
|  | 3061 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 3062 | struct wpa_ssid *s; | 
|  | 3063 |  | 
|  | 3064 | for (s = wpa_s->conf->ssid; s; s = s->next) { | 
|  | 3065 | if (s->disabled == 2 && | 
|  | 3066 | s->ssid_len == ssid_len && | 
|  | 3067 | os_memcmp(ssid, s->ssid, ssid_len) == 0) | 
|  | 3068 | break; | 
|  | 3069 | } | 
|  | 3070 |  | 
|  | 3071 | if (status == P2P_SC_SUCCESS) { | 
|  | 3072 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR | 
| Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 3073 | " was accepted; op_freq=%d MHz, SSID=%s", | 
|  | 3074 | MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len)); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3075 | if (s) { | 
| Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 3076 | int go = s->mode == WPAS_MODE_P2P_GO; | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3077 | if (go) { | 
|  | 3078 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 3079 | P2P_EVENT_INVITATION_ACCEPTED | 
|  | 3080 | "sa=" MACSTR | 
|  | 3081 | " persistent=%d freq=%d", | 
|  | 3082 | MAC2STR(sa), s->id, op_freq); | 
|  | 3083 | } else { | 
|  | 3084 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 3085 | P2P_EVENT_INVITATION_ACCEPTED | 
|  | 3086 | "sa=" MACSTR | 
|  | 3087 | " persistent=%d", | 
|  | 3088 | MAC2STR(sa), s->id); | 
|  | 3089 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3090 | wpas_p2p_group_add_persistent( | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 3091 | wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, NULL, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3092 | go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, | 
|  | 3093 | 1); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3094 | } else if (bssid) { | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3095 | wpa_s->user_initiated_pd = 0; | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3096 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 3097 | P2P_EVENT_INVITATION_ACCEPTED | 
|  | 3098 | "sa=" MACSTR " go_dev_addr=" MACSTR | 
|  | 3099 | " bssid=" MACSTR " unknown-network", | 
|  | 3100 | MAC2STR(sa), MAC2STR(go_dev_addr), | 
|  | 3101 | MAC2STR(bssid)); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3102 | wpas_p2p_join(wpa_s, bssid, go_dev_addr, | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 3103 | wpa_s->p2p_wps_method, 0, op_freq, | 
| Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 3104 | ssid, ssid_len); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3105 | } | 
|  | 3106 | return; | 
|  | 3107 | } | 
|  | 3108 |  | 
|  | 3109 | if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { | 
|  | 3110 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR | 
|  | 3111 | " was rejected (status %u)", MAC2STR(sa), status); | 
|  | 3112 | return; | 
|  | 3113 | } | 
|  | 3114 |  | 
|  | 3115 | if (!s) { | 
|  | 3116 | if (bssid) { | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3117 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 3118 | P2P_EVENT_INVITATION_RECEIVED | 
|  | 3119 | "sa=" MACSTR " go_dev_addr=" MACSTR | 
|  | 3120 | " bssid=" MACSTR " unknown-network", | 
|  | 3121 | MAC2STR(sa), MAC2STR(go_dev_addr), | 
|  | 3122 | MAC2STR(bssid)); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3123 | } else { | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3124 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 3125 | P2P_EVENT_INVITATION_RECEIVED | 
|  | 3126 | "sa=" MACSTR " go_dev_addr=" MACSTR | 
|  | 3127 | " unknown-network", | 
|  | 3128 | MAC2STR(sa), MAC2STR(go_dev_addr)); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3129 | } | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3130 | wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, | 
|  | 3131 | bssid, 0, op_freq); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3132 | return; | 
|  | 3133 | } | 
|  | 3134 |  | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3135 | if (s->mode == WPAS_MODE_P2P_GO && op_freq) { | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3136 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED | 
|  | 3137 | "sa=" MACSTR " persistent=%d freq=%d", | 
|  | 3138 | MAC2STR(sa), s->id, op_freq); | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3139 | } else { | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3140 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED | 
|  | 3141 | "sa=" MACSTR " persistent=%d", | 
|  | 3142 | MAC2STR(sa), s->id); | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3143 | } | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3144 | wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid, | 
|  | 3145 | s->id, op_freq); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3146 | } | 
|  | 3147 |  | 
|  | 3148 |  | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3149 | static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s, | 
|  | 3150 | struct wpa_ssid *ssid, | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3151 | const u8 *peer, int inv) | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3152 | { | 
|  | 3153 | size_t i; | 
| Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 3154 | struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s; | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3155 |  | 
|  | 3156 | if (ssid == NULL) | 
|  | 3157 | return; | 
|  | 3158 |  | 
|  | 3159 | for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) { | 
| Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 3160 | if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer, | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3161 | ETH_ALEN) == 0) | 
|  | 3162 | break; | 
|  | 3163 | } | 
| Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 3164 | if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) { | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3165 | if (ssid->mode != WPAS_MODE_P2P_GO && | 
|  | 3166 | os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) { | 
|  | 3167 | wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d " | 
|  | 3168 | "due to invitation result", ssid->id); | 
|  | 3169 | wpas_notify_network_removed(wpa_s, ssid); | 
|  | 3170 | wpa_config_remove_network(wpa_s->conf, ssid->id); | 
|  | 3171 | return; | 
|  | 3172 | } | 
|  | 3173 | return; /* Peer not found in client list */ | 
|  | 3174 | } | 
|  | 3175 |  | 
|  | 3176 | wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent " | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3177 | "group %d client list%s", | 
|  | 3178 | MAC2STR(peer), ssid->id, | 
|  | 3179 | inv ? " due to invitation result" : ""); | 
| Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 3180 | os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN, | 
|  | 3181 | ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN, | 
|  | 3182 | (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN); | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3183 | ssid->num_p2p_clients--; | 
| Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 3184 | if (p2p_wpa_s->conf->update_config && | 
|  | 3185 | wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf)) | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3186 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3187 | } | 
|  | 3188 |  | 
|  | 3189 |  | 
|  | 3190 | static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s, | 
|  | 3191 | const u8 *peer) | 
|  | 3192 | { | 
|  | 3193 | struct wpa_ssid *ssid; | 
|  | 3194 |  | 
|  | 3195 | wpa_s = wpa_s->global->p2p_invite_group; | 
|  | 3196 | if (wpa_s == NULL) | 
|  | 3197 | return; /* No known invitation group */ | 
|  | 3198 | ssid = wpa_s->current_ssid; | 
|  | 3199 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || | 
|  | 3200 | !ssid->p2p_persistent_group) | 
|  | 3201 | return; /* Not operating as a GO in persistent group */ | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 3202 | ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer, | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3203 | ssid->ssid, ssid->ssid_len); | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3204 | wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3205 | } | 
|  | 3206 |  | 
|  | 3207 |  | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3208 | static void wpas_invitation_result(void *ctx, int status, const u8 *bssid, | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3209 | const struct p2p_channels *channels, | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3210 | const u8 *peer, int neg_freq, | 
|  | 3211 | int peer_oper_freq) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3212 | { | 
|  | 3213 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 3214 | struct wpa_ssid *ssid; | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3215 | int freq; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3216 |  | 
|  | 3217 | if (bssid) { | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3218 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT | 
|  | 3219 | "status=%d " MACSTR, | 
|  | 3220 | status, MAC2STR(bssid)); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3221 | } else { | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3222 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT | 
|  | 3223 | "status=%d ", status); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3224 | } | 
|  | 3225 | wpas_notify_p2p_invitation_result(wpa_s, status, bssid); | 
|  | 3226 |  | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3227 | wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR, | 
|  | 3228 | status, MAC2STR(peer)); | 
|  | 3229 | if (wpa_s->pending_invite_ssid_id == -1) { | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 3230 | struct wpa_supplicant *group_if = | 
|  | 3231 | wpa_s->global->p2p_invite_group; | 
|  | 3232 |  | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3233 | if (status == P2P_SC_FAIL_UNKNOWN_GROUP) | 
|  | 3234 | wpas_remove_persistent_client(wpa_s, peer); | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 3235 |  | 
|  | 3236 | /* | 
|  | 3237 | * Invitation to an active group. If this is successful and we | 
|  | 3238 | * are the GO, set the client wait to postpone some concurrent | 
|  | 3239 | * operations and to allow provisioning and connection to happen | 
|  | 3240 | * more quickly. | 
|  | 3241 | */ | 
|  | 3242 | if (status == P2P_SC_SUCCESS && | 
|  | 3243 | group_if && group_if->current_ssid && | 
|  | 3244 | group_if->current_ssid->mode == WPAS_MODE_P2P_GO) { | 
|  | 3245 | os_get_reltime(&wpa_s->global->p2p_go_wait_client); | 
|  | 3246 | #ifdef CONFIG_TESTING_OPTIONS | 
|  | 3247 | if (group_if->p2p_go_csa_on_inv) { | 
|  | 3248 | wpa_printf(MSG_DEBUG, | 
|  | 3249 | "Testing: force P2P GO CSA after invitation"); | 
|  | 3250 | eloop_cancel_timeout( | 
|  | 3251 | wpas_p2p_reconsider_moving_go, | 
|  | 3252 | wpa_s, NULL); | 
|  | 3253 | eloop_register_timeout( | 
|  | 3254 | 0, 50000, | 
|  | 3255 | wpas_p2p_reconsider_moving_go, | 
|  | 3256 | wpa_s, NULL); | 
|  | 3257 | } | 
|  | 3258 | #endif /* CONFIG_TESTING_OPTIONS */ | 
|  | 3259 | } | 
|  | 3260 | return; | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3261 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3262 |  | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3263 | if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { | 
|  | 3264 | wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another " | 
|  | 3265 | "invitation exchange to indicate readiness for " | 
|  | 3266 | "re-invocation"); | 
|  | 3267 | } | 
|  | 3268 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3269 | if (status != P2P_SC_SUCCESS) { | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3270 | if (status == P2P_SC_FAIL_UNKNOWN_GROUP) { | 
|  | 3271 | ssid = wpa_config_get_network( | 
|  | 3272 | wpa_s->conf, wpa_s->pending_invite_ssid_id); | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3273 | wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3274 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3275 | wpas_p2p_remove_pending_group_interface(wpa_s); | 
|  | 3276 | return; | 
|  | 3277 | } | 
|  | 3278 |  | 
|  | 3279 | ssid = wpa_config_get_network(wpa_s->conf, | 
|  | 3280 | wpa_s->pending_invite_ssid_id); | 
|  | 3281 | if (ssid == NULL) { | 
|  | 3282 | wpa_printf(MSG_ERROR, "P2P: Could not find persistent group " | 
|  | 3283 | "data matching with invitation"); | 
|  | 3284 | return; | 
|  | 3285 | } | 
|  | 3286 |  | 
| Jouni Malinen | 2c5b17d | 2012-10-13 21:52:46 -0700 | [diff] [blame] | 3287 | /* | 
|  | 3288 | * The peer could have missed our ctrl::ack frame for Invitation | 
|  | 3289 | * Response and continue retransmitting the frame. To reduce the | 
|  | 3290 | * likelihood of the peer not getting successful TX status for the | 
|  | 3291 | * Invitation Response frame, wait a short time here before starting | 
|  | 3292 | * the persistent group so that we will remain on the current channel to | 
|  | 3293 | * acknowledge any possible retransmission from the peer. | 
|  | 3294 | */ | 
|  | 3295 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before " | 
|  | 3296 | "starting persistent group"); | 
|  | 3297 | os_sleep(0, 50000); | 
|  | 3298 |  | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3299 | if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO && | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3300 | freq_included(wpa_s, channels, neg_freq)) | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3301 | freq = neg_freq; | 
|  | 3302 | else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO && | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3303 | freq_included(wpa_s, channels, peer_oper_freq)) | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3304 | freq = peer_oper_freq; | 
|  | 3305 | else | 
|  | 3306 | freq = 0; | 
|  | 3307 |  | 
|  | 3308 | wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s", | 
|  | 3309 | freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3310 | wpas_p2p_group_add_persistent(wpa_s, ssid, | 
| Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 3311 | ssid->mode == WPAS_MODE_P2P_GO, | 
| Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 3312 | wpa_s->p2p_persistent_go_freq, | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3313 | freq, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 3314 | wpa_s->p2p_go_vht_center_freq2, | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3315 | wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 3316 | wpa_s->p2p_go_max_oper_chwidth, | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3317 | channels, | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 3318 | ssid->mode == WPAS_MODE_P2P_GO ? | 
|  | 3319 | P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3320 | 0, 1); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3321 | } | 
|  | 3322 |  | 
|  | 3323 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3324 | static int wpas_p2p_disallowed_freq(struct wpa_global *global, | 
|  | 3325 | unsigned int freq) | 
|  | 3326 | { | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 3327 | if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq)) | 
|  | 3328 | return 1; | 
| Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 3329 | return freq_range_list_includes(&global->p2p_disallow_freq, freq); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3330 | } | 
|  | 3331 |  | 
|  | 3332 |  | 
|  | 3333 | static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan) | 
|  | 3334 | { | 
|  | 3335 | reg->channel[reg->channels] = chan; | 
|  | 3336 | reg->channels++; | 
|  | 3337 | } | 
|  | 3338 |  | 
|  | 3339 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3340 | static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s, | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3341 | struct p2p_channels *chan, | 
|  | 3342 | struct p2p_channels *cli_chan) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3343 | { | 
|  | 3344 | int i, cla = 0; | 
|  | 3345 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3346 | wpa_s->global->p2p_24ghz_social_channels = 1; | 
|  | 3347 |  | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3348 | os_memset(cli_chan, 0, sizeof(*cli_chan)); | 
|  | 3349 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3350 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz " | 
|  | 3351 | "band"); | 
|  | 3352 |  | 
|  | 3353 | /* Operating class 81 - 2.4 GHz band channels 1..13 */ | 
|  | 3354 | chan->reg_class[cla].reg_class = 81; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3355 | chan->reg_class[cla].channels = 0; | 
|  | 3356 | for (i = 0; i < 11; i++) { | 
|  | 3357 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5)) | 
|  | 3358 | wpas_p2p_add_chan(&chan->reg_class[cla], i + 1); | 
|  | 3359 | } | 
|  | 3360 | if (chan->reg_class[cla].channels) | 
|  | 3361 | cla++; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3362 |  | 
|  | 3363 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz " | 
|  | 3364 | "band"); | 
|  | 3365 |  | 
|  | 3366 | /* Operating class 115 - 5 GHz, channels 36-48 */ | 
|  | 3367 | chan->reg_class[cla].reg_class = 115; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3368 | chan->reg_class[cla].channels = 0; | 
|  | 3369 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5)) | 
|  | 3370 | wpas_p2p_add_chan(&chan->reg_class[cla], 36); | 
|  | 3371 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5)) | 
|  | 3372 | wpas_p2p_add_chan(&chan->reg_class[cla], 40); | 
|  | 3373 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5)) | 
|  | 3374 | wpas_p2p_add_chan(&chan->reg_class[cla], 44); | 
|  | 3375 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5)) | 
|  | 3376 | wpas_p2p_add_chan(&chan->reg_class[cla], 48); | 
|  | 3377 | if (chan->reg_class[cla].channels) | 
|  | 3378 | cla++; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3379 |  | 
|  | 3380 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz " | 
|  | 3381 | "band"); | 
|  | 3382 |  | 
|  | 3383 | /* Operating class 124 - 5 GHz, channels 149,153,157,161 */ | 
|  | 3384 | chan->reg_class[cla].reg_class = 124; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3385 | chan->reg_class[cla].channels = 0; | 
|  | 3386 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5)) | 
|  | 3387 | wpas_p2p_add_chan(&chan->reg_class[cla], 149); | 
|  | 3388 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5)) | 
|  | 3389 | wpas_p2p_add_chan(&chan->reg_class[cla], 153); | 
|  | 3390 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5)) | 
|  | 3391 | wpas_p2p_add_chan(&chan->reg_class[cla], 157); | 
|  | 3392 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5)) | 
|  | 3393 | wpas_p2p_add_chan(&chan->reg_class[cla], 161); | 
|  | 3394 | if (chan->reg_class[cla].channels) | 
|  | 3395 | cla++; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3396 |  | 
|  | 3397 | chan->reg_classes = cla; | 
|  | 3398 | return 0; | 
|  | 3399 | } | 
|  | 3400 |  | 
|  | 3401 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3402 | static int has_channel(struct wpa_global *global, | 
|  | 3403 | struct hostapd_hw_modes *mode, u8 chan, int *flags) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3404 | { | 
|  | 3405 | int i; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3406 | unsigned int freq; | 
|  | 3407 |  | 
|  | 3408 | freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) + | 
|  | 3409 | chan * 5; | 
|  | 3410 | if (wpas_p2p_disallowed_freq(global, freq)) | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3411 | return NOT_ALLOWED; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3412 |  | 
|  | 3413 | for (i = 0; i < mode->num_channels; i++) { | 
|  | 3414 | if (mode->channels[i].chan == chan) { | 
|  | 3415 | if (flags) | 
|  | 3416 | *flags = mode->channels[i].flag; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3417 | if (mode->channels[i].flag & | 
|  | 3418 | (HOSTAPD_CHAN_DISABLED | | 
|  | 3419 | HOSTAPD_CHAN_RADAR)) | 
|  | 3420 | return NOT_ALLOWED; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3421 | if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR) | 
|  | 3422 | return NO_IR; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3423 | return ALLOWED; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3424 | } | 
|  | 3425 | } | 
|  | 3426 |  | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3427 | return NOT_ALLOWED; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3428 | } | 
|  | 3429 |  | 
|  | 3430 |  | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3431 | static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s, | 
|  | 3432 | struct hostapd_hw_modes *mode, | 
|  | 3433 | u8 channel) | 
|  | 3434 | { | 
|  | 3435 | u8 center_channels[] = { 42, 58, 106, 122, 138, 155 }; | 
| Dmitry Shmidt | aca489e | 2016-09-28 15:44:14 -0700 | [diff] [blame] | 3436 | size_t i; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3437 |  | 
|  | 3438 | if (mode->mode != HOSTAPD_MODE_IEEE80211A) | 
|  | 3439 | return 0; | 
|  | 3440 |  | 
|  | 3441 | for (i = 0; i < ARRAY_SIZE(center_channels); i++) | 
|  | 3442 | /* | 
|  | 3443 | * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48), | 
|  | 3444 | * so the center channel is 6 channels away from the start/end. | 
|  | 3445 | */ | 
|  | 3446 | if (channel >= center_channels[i] - 6 && | 
|  | 3447 | channel <= center_channels[i] + 6) | 
|  | 3448 | return center_channels[i]; | 
|  | 3449 |  | 
|  | 3450 | return 0; | 
|  | 3451 | } | 
|  | 3452 |  | 
|  | 3453 |  | 
|  | 3454 | static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s, | 
|  | 3455 | struct hostapd_hw_modes *mode, | 
|  | 3456 | u8 channel, u8 bw) | 
|  | 3457 | { | 
|  | 3458 | u8 center_chan; | 
|  | 3459 | int i, flags; | 
|  | 3460 | enum chan_allowed res, ret = ALLOWED; | 
|  | 3461 |  | 
|  | 3462 | center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel); | 
|  | 3463 | if (!center_chan) | 
|  | 3464 | return NOT_ALLOWED; | 
|  | 3465 | if (center_chan >= 58 && center_chan <= 138) | 
|  | 3466 | return NOT_ALLOWED; /* Do not allow DFS channels for P2P */ | 
|  | 3467 |  | 
|  | 3468 | /* check all the channels are available */ | 
|  | 3469 | for (i = 0; i < 4; i++) { | 
|  | 3470 | int adj_chan = center_chan - 6 + i * 4; | 
|  | 3471 |  | 
|  | 3472 | res = has_channel(wpa_s->global, mode, adj_chan, &flags); | 
|  | 3473 | if (res == NOT_ALLOWED) | 
|  | 3474 | return NOT_ALLOWED; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3475 | if (res == NO_IR) | 
|  | 3476 | ret = NO_IR; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3477 |  | 
|  | 3478 | if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) | 
|  | 3479 | return NOT_ALLOWED; | 
|  | 3480 | if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) | 
|  | 3481 | return NOT_ALLOWED; | 
|  | 3482 | if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) | 
|  | 3483 | return NOT_ALLOWED; | 
|  | 3484 | if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)) | 
|  | 3485 | return NOT_ALLOWED; | 
|  | 3486 | } | 
|  | 3487 |  | 
|  | 3488 | return ret; | 
|  | 3489 | } | 
|  | 3490 |  | 
|  | 3491 |  | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 3492 | static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s, | 
|  | 3493 | struct hostapd_hw_modes *mode, | 
|  | 3494 | u8 channel) | 
|  | 3495 | { | 
|  | 3496 | u8 center_channels[] = { 50, 114 }; | 
|  | 3497 | unsigned int i; | 
|  | 3498 |  | 
|  | 3499 | if (mode->mode != HOSTAPD_MODE_IEEE80211A) | 
|  | 3500 | return 0; | 
|  | 3501 |  | 
|  | 3502 | for (i = 0; i < ARRAY_SIZE(center_channels); i++) | 
|  | 3503 | /* | 
|  | 3504 | * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64), | 
|  | 3505 | * so the center channel is 14 channels away from the start/end. | 
|  | 3506 | */ | 
|  | 3507 | if (channel >= center_channels[i] - 14 && | 
|  | 3508 | channel <= center_channels[i] + 14) | 
|  | 3509 | return center_channels[i]; | 
|  | 3510 |  | 
|  | 3511 | return 0; | 
|  | 3512 | } | 
|  | 3513 |  | 
|  | 3514 |  | 
|  | 3515 | static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s, | 
|  | 3516 | struct hostapd_hw_modes *mode, | 
|  | 3517 | u8 channel, u8 bw) | 
|  | 3518 | { | 
|  | 3519 | u8 center_chan; | 
|  | 3520 | int i, flags; | 
|  | 3521 | enum chan_allowed res, ret = ALLOWED; | 
|  | 3522 |  | 
|  | 3523 | center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel); | 
|  | 3524 | if (!center_chan) | 
|  | 3525 | return NOT_ALLOWED; | 
|  | 3526 | /* VHT 160 MHz uses DFS channels in most countries. */ | 
|  | 3527 |  | 
|  | 3528 | /* Check all the channels are available */ | 
|  | 3529 | for (i = 0; i < 8; i++) { | 
|  | 3530 | int adj_chan = center_chan - 14 + i * 4; | 
|  | 3531 |  | 
|  | 3532 | res = has_channel(wpa_s->global, mode, adj_chan, &flags); | 
|  | 3533 | if (res == NOT_ALLOWED) | 
|  | 3534 | return NOT_ALLOWED; | 
|  | 3535 |  | 
|  | 3536 | if (res == NO_IR) | 
|  | 3537 | ret = NO_IR; | 
|  | 3538 |  | 
|  | 3539 | if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150)) | 
|  | 3540 | return NOT_ALLOWED; | 
|  | 3541 | if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130)) | 
|  | 3542 | return NOT_ALLOWED; | 
|  | 3543 | if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110)) | 
|  | 3544 | return NOT_ALLOWED; | 
|  | 3545 | if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90)) | 
|  | 3546 | return NOT_ALLOWED; | 
|  | 3547 | if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70)) | 
|  | 3548 | return NOT_ALLOWED; | 
|  | 3549 | if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50)) | 
|  | 3550 | return NOT_ALLOWED; | 
|  | 3551 | if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30)) | 
|  | 3552 | return NOT_ALLOWED; | 
|  | 3553 | if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10)) | 
|  | 3554 | return NOT_ALLOWED; | 
|  | 3555 | } | 
|  | 3556 |  | 
|  | 3557 | return ret; | 
|  | 3558 | } | 
|  | 3559 |  | 
|  | 3560 |  | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3561 | static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s, | 
|  | 3562 | struct hostapd_hw_modes *mode, | 
|  | 3563 | u8 channel, u8 bw) | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3564 | { | 
| Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 3565 | int flag = 0; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3566 | enum chan_allowed res, res2; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3567 |  | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3568 | res2 = res = has_channel(wpa_s->global, mode, channel, &flag); | 
|  | 3569 | if (bw == BW40MINUS) { | 
|  | 3570 | if (!(flag & HOSTAPD_CHAN_HT40MINUS)) | 
|  | 3571 | return NOT_ALLOWED; | 
|  | 3572 | res2 = has_channel(wpa_s->global, mode, channel - 4, NULL); | 
|  | 3573 | } else if (bw == BW40PLUS) { | 
|  | 3574 | if (!(flag & HOSTAPD_CHAN_HT40PLUS)) | 
|  | 3575 | return NOT_ALLOWED; | 
|  | 3576 | res2 = has_channel(wpa_s->global, mode, channel + 4, NULL); | 
|  | 3577 | } else if (bw == BW80) { | 
|  | 3578 | res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw); | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 3579 | } else if (bw == BW160) { | 
|  | 3580 | res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw); | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3581 | } | 
|  | 3582 |  | 
|  | 3583 | if (res == NOT_ALLOWED || res2 == NOT_ALLOWED) | 
|  | 3584 | return NOT_ALLOWED; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3585 | if (res == NO_IR || res2 == NO_IR) | 
|  | 3586 | return NO_IR; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3587 | return res; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3588 | } | 
|  | 3589 |  | 
|  | 3590 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3591 | static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s, | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3592 | struct p2p_channels *chan, | 
|  | 3593 | struct p2p_channels *cli_chan) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3594 | { | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3595 | struct hostapd_hw_modes *mode; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3596 | int cla, op, cli_cla; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3597 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3598 | if (wpa_s->hw.modes == NULL) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3599 | wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching " | 
|  | 3600 | "of all supported channels; assume dualband " | 
|  | 3601 | "support"); | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3602 | return wpas_p2p_default_channels(wpa_s, chan, cli_chan); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3603 | } | 
|  | 3604 |  | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3605 | cla = cli_cla = 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3606 |  | 
| Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 3607 | for (op = 0; global_op_class[op].op_class; op++) { | 
|  | 3608 | const struct oper_class_map *o = &global_op_class[op]; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3609 | u8 ch; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3610 | struct p2p_reg_class *reg = NULL, *cli_reg = NULL; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3611 |  | 
| Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 3612 | if (o->p2p == NO_P2P_SUPP) | 
|  | 3613 | continue; | 
|  | 3614 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3615 | 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] | 3616 | if (mode == NULL) | 
|  | 3617 | continue; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3618 | if (mode->mode == HOSTAPD_MODE_IEEE80211G) | 
|  | 3619 | wpa_s->global->p2p_24ghz_social_channels = 1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3620 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3621 | enum chan_allowed res; | 
|  | 3622 | res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); | 
|  | 3623 | if (res == ALLOWED) { | 
|  | 3624 | if (reg == NULL) { | 
|  | 3625 | wpa_printf(MSG_DEBUG, "P2P: Add operating class %u", | 
|  | 3626 | o->op_class); | 
|  | 3627 | reg = &chan->reg_class[cla]; | 
|  | 3628 | cla++; | 
|  | 3629 | reg->reg_class = o->op_class; | 
|  | 3630 | } | 
|  | 3631 | reg->channel[reg->channels] = ch; | 
|  | 3632 | reg->channels++; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3633 | } else if (res == NO_IR && | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3634 | wpa_s->conf->p2p_add_cli_chan) { | 
|  | 3635 | if (cli_reg == NULL) { | 
|  | 3636 | wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)", | 
|  | 3637 | o->op_class); | 
|  | 3638 | cli_reg = &cli_chan->reg_class[cli_cla]; | 
|  | 3639 | cli_cla++; | 
|  | 3640 | cli_reg->reg_class = o->op_class; | 
|  | 3641 | } | 
|  | 3642 | cli_reg->channel[cli_reg->channels] = ch; | 
|  | 3643 | cli_reg->channels++; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3644 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3645 | } | 
|  | 3646 | if (reg) { | 
|  | 3647 | wpa_hexdump(MSG_DEBUG, "P2P: Channels", | 
|  | 3648 | reg->channel, reg->channels); | 
|  | 3649 | } | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3650 | if (cli_reg) { | 
|  | 3651 | wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)", | 
|  | 3652 | cli_reg->channel, cli_reg->channels); | 
|  | 3653 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3654 | } | 
|  | 3655 |  | 
|  | 3656 | chan->reg_classes = cla; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3657 | cli_chan->reg_classes = cli_cla; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3658 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3659 | return 0; | 
|  | 3660 | } | 
|  | 3661 |  | 
|  | 3662 |  | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3663 | int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s, | 
|  | 3664 | struct hostapd_hw_modes *mode, u8 channel) | 
|  | 3665 | { | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3666 | int op; | 
|  | 3667 | enum chan_allowed ret; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3668 |  | 
| Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 3669 | for (op = 0; global_op_class[op].op_class; op++) { | 
|  | 3670 | const struct oper_class_map *o = &global_op_class[op]; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3671 | u8 ch; | 
|  | 3672 |  | 
| Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 3673 | if (o->p2p == NO_P2P_SUPP) | 
|  | 3674 | continue; | 
|  | 3675 |  | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3676 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { | 
|  | 3677 | if (o->mode != HOSTAPD_MODE_IEEE80211A || | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 3678 | (o->bw != BW40PLUS && o->bw != BW40MINUS) || | 
|  | 3679 | ch != channel) | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3680 | continue; | 
|  | 3681 | ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3682 | if (ret == ALLOWED) | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3683 | return (o->bw == BW40MINUS) ? -1 : 1; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3684 | } | 
|  | 3685 | } | 
|  | 3686 | return 0; | 
|  | 3687 | } | 
|  | 3688 |  | 
|  | 3689 |  | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3690 | int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s, | 
|  | 3691 | struct hostapd_hw_modes *mode, u8 channel) | 
|  | 3692 | { | 
|  | 3693 | if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80)) | 
|  | 3694 | return 0; | 
|  | 3695 |  | 
|  | 3696 | return wpas_p2p_get_center_80mhz(wpa_s, mode, channel); | 
|  | 3697 | } | 
|  | 3698 |  | 
|  | 3699 |  | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 3700 | int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s, | 
|  | 3701 | struct hostapd_hw_modes *mode, u8 channel) | 
|  | 3702 | { | 
|  | 3703 | if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160)) | 
|  | 3704 | return 0; | 
|  | 3705 | return wpas_p2p_get_center_160mhz(wpa_s, mode, channel); | 
|  | 3706 | } | 
|  | 3707 |  | 
|  | 3708 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3709 | static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf, | 
|  | 3710 | size_t buf_len) | 
|  | 3711 | { | 
|  | 3712 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 3713 |  | 
|  | 3714 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
|  | 3715 | if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0) | 
|  | 3716 | break; | 
|  | 3717 | } | 
|  | 3718 | if (wpa_s == NULL) | 
|  | 3719 | return -1; | 
|  | 3720 |  | 
|  | 3721 | return wpa_drv_get_noa(wpa_s, buf, buf_len); | 
|  | 3722 | } | 
|  | 3723 |  | 
|  | 3724 |  | 
| Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3725 | struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s, | 
|  | 3726 | const u8 *ssid, size_t ssid_len) | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3727 | { | 
| Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3728 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
|  | 3729 | struct wpa_ssid *s = wpa_s->current_ssid; | 
|  | 3730 | if (s == NULL) | 
|  | 3731 | continue; | 
|  | 3732 | if (s->mode != WPAS_MODE_P2P_GO && | 
|  | 3733 | s->mode != WPAS_MODE_AP && | 
|  | 3734 | s->mode != WPAS_MODE_P2P_GROUP_FORMATION) | 
|  | 3735 | continue; | 
|  | 3736 | if (s->ssid_len != ssid_len || | 
| Dmitry Shmidt | 0365883 | 2014-08-13 11:03:49 -0700 | [diff] [blame] | 3737 | os_memcmp(ssid, s->ssid, ssid_len) != 0) | 
| Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3738 | continue; | 
|  | 3739 | return wpa_s; | 
|  | 3740 | } | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3741 |  | 
| Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3742 | return NULL; | 
|  | 3743 |  | 
|  | 3744 | } | 
|  | 3745 |  | 
|  | 3746 |  | 
|  | 3747 | struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s, | 
|  | 3748 | const u8 *peer_dev_addr) | 
|  | 3749 | { | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3750 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
|  | 3751 | struct wpa_ssid *ssid = wpa_s->current_ssid; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3752 | if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group)) | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3753 | continue; | 
| Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3754 | if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0) | 
|  | 3755 | return wpa_s; | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3756 | } | 
|  | 3757 |  | 
| Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3758 | return NULL; | 
|  | 3759 | } | 
|  | 3760 |  | 
|  | 3761 |  | 
|  | 3762 | static int wpas_go_connected(void *ctx, const u8 *dev_addr) | 
|  | 3763 | { | 
|  | 3764 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 3765 |  | 
|  | 3766 | return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL; | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3767 | } | 
|  | 3768 |  | 
|  | 3769 |  | 
| Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 3770 | static int wpas_is_concurrent_session_active(void *ctx) | 
|  | 3771 | { | 
|  | 3772 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 3773 | struct wpa_supplicant *ifs; | 
|  | 3774 |  | 
|  | 3775 | for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) { | 
|  | 3776 | if (ifs == wpa_s) | 
|  | 3777 | continue; | 
|  | 3778 | if (ifs->wpa_state > WPA_ASSOCIATED) | 
|  | 3779 | return 1; | 
|  | 3780 | } | 
|  | 3781 | return 0; | 
|  | 3782 | } | 
|  | 3783 |  | 
|  | 3784 |  | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3785 | static void wpas_p2p_debug_print(void *ctx, int level, const char *msg) | 
|  | 3786 | { | 
|  | 3787 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 3788 | wpa_msg_global(wpa_s, level, "P2P: %s", msg); | 
|  | 3789 | } | 
|  | 3790 |  | 
|  | 3791 |  | 
| Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 3792 | int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s, | 
|  | 3793 | const char *conf_p2p_dev) | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3794 | { | 
|  | 3795 | struct wpa_interface iface; | 
|  | 3796 | struct wpa_supplicant *p2pdev_wpa_s; | 
|  | 3797 | char ifname[100]; | 
|  | 3798 | char force_name[100]; | 
|  | 3799 | int ret; | 
|  | 3800 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3801 | ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s", | 
|  | 3802 | wpa_s->ifname); | 
|  | 3803 | if (os_snprintf_error(sizeof(ifname), ret)) | 
|  | 3804 | return -1; | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3805 | force_name[0] = '\0'; | 
|  | 3806 | wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE; | 
|  | 3807 | ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL, | 
|  | 3808 | force_name, wpa_s->pending_interface_addr, NULL); | 
|  | 3809 | if (ret < 0) { | 
|  | 3810 | wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface"); | 
|  | 3811 | return ret; | 
|  | 3812 | } | 
|  | 3813 | os_strlcpy(wpa_s->pending_interface_name, ifname, | 
|  | 3814 | sizeof(wpa_s->pending_interface_name)); | 
|  | 3815 |  | 
|  | 3816 | os_memset(&iface, 0, sizeof(iface)); | 
|  | 3817 | iface.p2p_mgmt = 1; | 
|  | 3818 | iface.ifname = wpa_s->pending_interface_name; | 
|  | 3819 | iface.driver = wpa_s->driver->name; | 
|  | 3820 | iface.driver_param = wpa_s->conf->driver_param; | 
| Dmitry Shmidt | 2ac5f60 | 2014-03-07 10:08:21 -0800 | [diff] [blame] | 3821 |  | 
|  | 3822 | /* | 
|  | 3823 | * If a P2P Device configuration file was given, use it as the interface | 
|  | 3824 | * configuration file (instead of using parent's configuration file. | 
|  | 3825 | */ | 
| Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 3826 | if (conf_p2p_dev) { | 
|  | 3827 | iface.confname = conf_p2p_dev; | 
| Dmitry Shmidt | 2ac5f60 | 2014-03-07 10:08:21 -0800 | [diff] [blame] | 3828 | iface.ctrl_interface = NULL; | 
|  | 3829 | } else { | 
|  | 3830 | iface.confname = wpa_s->confname; | 
|  | 3831 | iface.ctrl_interface = wpa_s->conf->ctrl_interface; | 
|  | 3832 | } | 
| Dmitry Shmidt | 2ac5f60 | 2014-03-07 10:08:21 -0800 | [diff] [blame] | 3833 |  | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 3834 | p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s); | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3835 | if (!p2pdev_wpa_s) { | 
|  | 3836 | wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface"); | 
|  | 3837 | return -1; | 
|  | 3838 | } | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3839 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 3840 | p2pdev_wpa_s->p2pdev = p2pdev_wpa_s; | 
| Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3841 | wpa_s->pending_interface_name[0] = '\0'; | 
|  | 3842 | return 0; | 
|  | 3843 | } | 
|  | 3844 |  | 
|  | 3845 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3846 | static void wpas_presence_resp(void *ctx, const u8 *src, u8 status, | 
|  | 3847 | const u8 *noa, size_t noa_len) | 
|  | 3848 | { | 
|  | 3849 | struct wpa_supplicant *wpa_s, *intf = ctx; | 
|  | 3850 | char hex[100]; | 
|  | 3851 |  | 
|  | 3852 | for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
|  | 3853 | if (wpa_s->waiting_presence_resp) | 
|  | 3854 | break; | 
|  | 3855 | } | 
|  | 3856 | if (!wpa_s) { | 
|  | 3857 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response"); | 
|  | 3858 | return; | 
|  | 3859 | } | 
|  | 3860 | wpa_s->waiting_presence_resp = 0; | 
|  | 3861 |  | 
|  | 3862 | wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len); | 
|  | 3863 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR | 
|  | 3864 | " status=%u noa=%s", MAC2STR(src), status, hex); | 
|  | 3865 | } | 
|  | 3866 |  | 
|  | 3867 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3868 | static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid, | 
|  | 3869 | size_t ssid_len, u8 *go_dev_addr, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3870 | u8 *ret_ssid, size_t *ret_ssid_len, | 
|  | 3871 | u8 *intended_iface_addr) | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3872 | { | 
|  | 3873 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 3874 | struct wpa_ssid *s; | 
|  | 3875 |  | 
|  | 3876 | s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len); | 
|  | 3877 | if (s) { | 
|  | 3878 | os_memcpy(ret_ssid, s->ssid, s->ssid_len); | 
|  | 3879 | *ret_ssid_len = s->ssid_len; | 
|  | 3880 | os_memcpy(go_dev_addr, s->bssid, ETH_ALEN); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3881 |  | 
|  | 3882 | if (s->mode != WPAS_MODE_P2P_GO) { | 
|  | 3883 | os_memset(intended_iface_addr, 0, ETH_ALEN); | 
|  | 3884 | } else if (wpas_p2p_create_iface(wpa_s)) { | 
|  | 3885 | if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO)) | 
|  | 3886 | return 0; | 
|  | 3887 |  | 
|  | 3888 | os_memcpy(intended_iface_addr, | 
|  | 3889 | wpa_s->pending_interface_addr, ETH_ALEN); | 
|  | 3890 | } else { | 
|  | 3891 | os_memcpy(intended_iface_addr, wpa_s->own_addr, | 
|  | 3892 | ETH_ALEN); | 
|  | 3893 | } | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3894 | return 1; | 
|  | 3895 | } | 
|  | 3896 |  | 
|  | 3897 | return 0; | 
|  | 3898 | } | 
|  | 3899 |  | 
|  | 3900 |  | 
|  | 3901 | static int wpas_get_go_info(void *ctx, u8 *intended_addr, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3902 | u8 *ssid, size_t *ssid_len, int *group_iface, | 
|  | 3903 | unsigned int *freq) | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3904 | { | 
|  | 3905 | struct wpa_supplicant *wpa_s = ctx; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3906 | struct wpa_supplicant *go; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3907 | struct wpa_ssid *s; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3908 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3909 | /* | 
|  | 3910 | * group_iface will be set to 1 only if a dedicated interface for P2P | 
|  | 3911 | * role is required. First, we try to reuse an active GO. However, | 
|  | 3912 | * if it is not present, we will try to reactivate an existing | 
|  | 3913 | * persistent group and set group_iface to 1, so the caller will know | 
|  | 3914 | * that the pending interface should be used. | 
|  | 3915 | */ | 
|  | 3916 | *group_iface = 0; | 
|  | 3917 |  | 
|  | 3918 | if (freq) | 
|  | 3919 | *freq = 0; | 
|  | 3920 |  | 
|  | 3921 | go = wpas_p2p_get_go_group(wpa_s); | 
|  | 3922 | if (!go) { | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3923 | s = wpas_p2p_get_persistent_go(wpa_s); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3924 | *group_iface = wpas_p2p_create_iface(wpa_s); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3925 | if (s) | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3926 | os_memcpy(intended_addr, s->bssid, ETH_ALEN); | 
|  | 3927 | else | 
|  | 3928 | return 0; | 
|  | 3929 | } else { | 
|  | 3930 | s = go->current_ssid; | 
|  | 3931 | os_memcpy(intended_addr, go->own_addr, ETH_ALEN); | 
|  | 3932 | if (freq) | 
|  | 3933 | *freq = go->assoc_freq; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3934 | } | 
|  | 3935 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3936 | os_memcpy(ssid, s->ssid, s->ssid_len); | 
|  | 3937 | *ssid_len = s->ssid_len; | 
|  | 3938 |  | 
|  | 3939 | return 1; | 
|  | 3940 | } | 
|  | 3941 |  | 
|  | 3942 |  | 
|  | 3943 | static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go, | 
|  | 3944 | const u8 *ssid, size_t ssid_len) | 
|  | 3945 | { | 
|  | 3946 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 3947 | struct wpa_ssid *s; | 
|  | 3948 | int save_config = 0; | 
|  | 3949 | size_t i; | 
|  | 3950 |  | 
|  | 3951 | /* Start with our first choice of Persistent Groups */ | 
|  | 3952 | while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) { | 
|  | 3953 | if (go && ssid && ssid_len && | 
|  | 3954 | s->ssid_len == ssid_len && | 
|  | 3955 | os_memcmp(go, s->bssid, ETH_ALEN) == 0 && | 
|  | 3956 | os_memcmp(ssid, s->ssid, ssid_len) == 0) | 
|  | 3957 | break; | 
|  | 3958 |  | 
|  | 3959 | /* Remove stale persistent group */ | 
|  | 3960 | if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) { | 
|  | 3961 | wpa_config_remove_network(wpa_s->conf, s->id); | 
|  | 3962 | save_config = 1; | 
|  | 3963 | continue; | 
|  | 3964 | } | 
|  | 3965 |  | 
|  | 3966 | for (i = 0; i < s->num_p2p_clients; i++) { | 
|  | 3967 | if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, | 
|  | 3968 | peer, ETH_ALEN) != 0) | 
|  | 3969 | continue; | 
|  | 3970 |  | 
|  | 3971 | os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN, | 
|  | 3972 | s->p2p_client_list + (i + 1) * 2 * ETH_ALEN, | 
|  | 3973 | (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN); | 
|  | 3974 | break; | 
|  | 3975 | } | 
|  | 3976 | s->num_p2p_clients--; | 
|  | 3977 | save_config = 1; | 
|  | 3978 | } | 
|  | 3979 |  | 
|  | 3980 | if (save_config) | 
|  | 3981 | p2p_config_write(wpa_s); | 
|  | 3982 |  | 
|  | 3983 | /* Return TRUE if valid SSID remains */ | 
|  | 3984 | return s != NULL; | 
|  | 3985 | } | 
|  | 3986 |  | 
|  | 3987 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3988 | static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len, | 
|  | 3989 | const u8 *feat_cap, size_t feat_cap_len) | 
|  | 3990 | { | 
|  | 3991 | static const char pref[] = " feature_cap="; | 
|  | 3992 | int ret; | 
|  | 3993 |  | 
|  | 3994 | buf[0] = '\0'; | 
|  | 3995 |  | 
|  | 3996 | /* | 
|  | 3997 | * We expect a feature capability to contain at least one byte to be | 
|  | 3998 | * reported. The string buffer provided by the caller function is | 
|  | 3999 | * expected to be big enough to contain all bytes of the attribute for | 
|  | 4000 | * known specifications. This function truncates the reported bytes if | 
|  | 4001 | * the feature capability data exceeds the string buffer size. | 
|  | 4002 | */ | 
|  | 4003 | if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2) | 
|  | 4004 | return; | 
|  | 4005 |  | 
|  | 4006 | os_memcpy(buf, pref, sizeof(pref)); | 
|  | 4007 | ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1], | 
|  | 4008 | buf_len - sizeof(pref) + 1, | 
|  | 4009 | feat_cap, feat_cap_len); | 
|  | 4010 |  | 
|  | 4011 | if (ret != (2 * (int) feat_cap_len)) | 
|  | 4012 | wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated"); | 
|  | 4013 | } | 
|  | 4014 |  | 
|  | 4015 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4016 | static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev, | 
|  | 4017 | const u8 *adv_mac, const u8 *ses_mac, | 
|  | 4018 | const u8 *grp_mac, u32 adv_id, u32 ses_id, | 
|  | 4019 | u8 conncap, int passwd_id, | 
|  | 4020 | const u8 *persist_ssid, | 
|  | 4021 | size_t persist_ssid_size, int response_done, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4022 | int prov_start, const char *session_info, | 
|  | 4023 | const u8 *feat_cap, size_t feat_cap_len, | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 4024 | unsigned int freq, | 
|  | 4025 | const u8 *group_ssid, size_t group_ssid_len) | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4026 | { | 
|  | 4027 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 4028 | u8 mac[ETH_ALEN]; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4029 | struct wpa_ssid *persistent_go, *stale, *s = NULL; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4030 | int save_config = 0; | 
|  | 4031 | struct wpa_supplicant *go_wpa_s; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4032 | char feat_cap_str[256]; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4033 |  | 
|  | 4034 | if (!dev) | 
|  | 4035 | return; | 
|  | 4036 |  | 
|  | 4037 | os_memset(mac, 0, ETH_ALEN); | 
|  | 4038 | if (!adv_mac) | 
|  | 4039 | adv_mac = mac; | 
|  | 4040 | if (!ses_mac) | 
|  | 4041 | ses_mac = mac; | 
|  | 4042 | if (!grp_mac) | 
|  | 4043 | grp_mac = mac; | 
|  | 4044 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4045 | wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str), | 
|  | 4046 | feat_cap, feat_cap_len); | 
|  | 4047 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4048 | if (prov_start) { | 
|  | 4049 | if (session_info == NULL) { | 
|  | 4050 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 4051 | P2P_EVENT_P2PS_PROVISION_START MACSTR | 
|  | 4052 | " adv_id=%x conncap=%x" | 
|  | 4053 | " adv_mac=" MACSTR | 
|  | 4054 | " session=%x mac=" MACSTR | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4055 | " dev_passwd_id=%d%s", | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4056 | MAC2STR(dev), adv_id, conncap, | 
|  | 4057 | MAC2STR(adv_mac), | 
|  | 4058 | ses_id, MAC2STR(ses_mac), | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4059 | passwd_id, feat_cap_str); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4060 | } else { | 
|  | 4061 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 4062 | P2P_EVENT_P2PS_PROVISION_START MACSTR | 
|  | 4063 | " adv_id=%x conncap=%x" | 
|  | 4064 | " adv_mac=" MACSTR | 
|  | 4065 | " session=%x mac=" MACSTR | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4066 | " dev_passwd_id=%d info='%s'%s", | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4067 | MAC2STR(dev), adv_id, conncap, | 
|  | 4068 | MAC2STR(adv_mac), | 
|  | 4069 | ses_id, MAC2STR(ses_mac), | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4070 | passwd_id, session_info, feat_cap_str); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4071 | } | 
|  | 4072 | return; | 
|  | 4073 | } | 
|  | 4074 |  | 
|  | 4075 | go_wpa_s = wpas_p2p_get_go_group(wpa_s); | 
|  | 4076 | persistent_go = wpas_p2p_get_persistent_go(wpa_s); | 
|  | 4077 |  | 
|  | 4078 | if (status && status != P2P_SC_SUCCESS_DEFERRED) { | 
|  | 4079 | if (go_wpa_s && !p2p_group_go_member_count(wpa_s)) | 
|  | 4080 | wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname); | 
|  | 4081 |  | 
|  | 4082 | if (persistent_go && !persistent_go->num_p2p_clients) { | 
|  | 4083 | /* remove empty persistent GO */ | 
|  | 4084 | wpa_config_remove_network(wpa_s->conf, | 
|  | 4085 | persistent_go->id); | 
|  | 4086 | } | 
|  | 4087 |  | 
|  | 4088 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 4089 | P2P_EVENT_P2PS_PROVISION_DONE MACSTR | 
|  | 4090 | " status=%d" | 
|  | 4091 | " adv_id=%x adv_mac=" MACSTR | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4092 | " session=%x mac=" MACSTR "%s", | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4093 | MAC2STR(dev), status, | 
|  | 4094 | adv_id, MAC2STR(adv_mac), | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4095 | ses_id, MAC2STR(ses_mac), feat_cap_str); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4096 | return; | 
|  | 4097 | } | 
|  | 4098 |  | 
|  | 4099 | /* Clean up stale persistent groups with this device */ | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4100 | if (persist_ssid && persist_ssid_size) | 
|  | 4101 | s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid, | 
|  | 4102 | persist_ssid_size); | 
|  | 4103 |  | 
|  | 4104 | if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO && | 
|  | 4105 | is_zero_ether_addr(grp_mac)) { | 
|  | 4106 | wpa_dbg(wpa_s, MSG_ERROR, | 
|  | 4107 | "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address"); | 
|  | 4108 | return; | 
|  | 4109 | } | 
|  | 4110 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4111 | for (;;) { | 
|  | 4112 | stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0); | 
|  | 4113 | if (!stale) | 
|  | 4114 | break; | 
|  | 4115 |  | 
|  | 4116 | if (s && s->ssid_len == stale->ssid_len && | 
|  | 4117 | os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 && | 
|  | 4118 | os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0) | 
|  | 4119 | break; | 
|  | 4120 |  | 
|  | 4121 | /* Remove stale persistent group */ | 
|  | 4122 | if (stale->mode != WPAS_MODE_P2P_GO || | 
|  | 4123 | stale->num_p2p_clients <= 1) { | 
|  | 4124 | wpa_config_remove_network(wpa_s->conf, stale->id); | 
|  | 4125 | } else { | 
|  | 4126 | size_t i; | 
|  | 4127 |  | 
|  | 4128 | for (i = 0; i < stale->num_p2p_clients; i++) { | 
|  | 4129 | if (os_memcmp(stale->p2p_client_list + | 
|  | 4130 | i * ETH_ALEN, | 
|  | 4131 | dev, ETH_ALEN) == 0) { | 
|  | 4132 | os_memmove(stale->p2p_client_list + | 
|  | 4133 | i * ETH_ALEN, | 
|  | 4134 | stale->p2p_client_list + | 
|  | 4135 | (i + 1) * ETH_ALEN, | 
|  | 4136 | (stale->num_p2p_clients - | 
|  | 4137 | i - 1) * ETH_ALEN); | 
|  | 4138 | break; | 
|  | 4139 | } | 
|  | 4140 | } | 
|  | 4141 | stale->num_p2p_clients--; | 
|  | 4142 | } | 
|  | 4143 | save_config = 1; | 
|  | 4144 | } | 
|  | 4145 |  | 
|  | 4146 | if (save_config) | 
|  | 4147 | p2p_config_write(wpa_s); | 
|  | 4148 |  | 
|  | 4149 | if (s) { | 
|  | 4150 | if (go_wpa_s && !p2p_group_go_member_count(wpa_s)) | 
|  | 4151 | wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname); | 
|  | 4152 |  | 
|  | 4153 | if (persistent_go && s != persistent_go && | 
|  | 4154 | !persistent_go->num_p2p_clients) { | 
|  | 4155 | /* remove empty persistent GO */ | 
|  | 4156 | wpa_config_remove_network(wpa_s->conf, | 
|  | 4157 | persistent_go->id); | 
|  | 4158 | /* Save config */ | 
|  | 4159 | } | 
|  | 4160 |  | 
|  | 4161 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 4162 | P2P_EVENT_P2PS_PROVISION_DONE MACSTR | 
|  | 4163 | " status=%d" | 
|  | 4164 | " adv_id=%x adv_mac=" MACSTR | 
|  | 4165 | " session=%x mac=" MACSTR | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4166 | " persist=%d%s", | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4167 | MAC2STR(dev), status, | 
|  | 4168 | adv_id, MAC2STR(adv_mac), | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4169 | ses_id, MAC2STR(ses_mac), s->id, feat_cap_str); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4170 | return; | 
|  | 4171 | } | 
|  | 4172 |  | 
|  | 4173 | if (conncap == P2PS_SETUP_GROUP_OWNER) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4174 | /* | 
|  | 4175 | * We need to copy the interface name. Simply saving a | 
|  | 4176 | * pointer isn't enough, since if we use pending_interface_name | 
|  | 4177 | * it will be overwritten when the group is added. | 
|  | 4178 | */ | 
|  | 4179 | char go_ifname[100]; | 
|  | 4180 |  | 
|  | 4181 | go_ifname[0] = '\0'; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4182 | if (!go_wpa_s) { | 
|  | 4183 | wpa_s->global->pending_p2ps_group = 1; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4184 | wpa_s->global->pending_p2ps_group_freq = freq; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4185 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4186 | if (!wpas_p2p_create_iface(wpa_s)) | 
|  | 4187 | os_memcpy(go_ifname, wpa_s->ifname, | 
|  | 4188 | sizeof(go_ifname)); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4189 | else if (wpa_s->pending_interface_name[0]) | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4190 | os_memcpy(go_ifname, | 
|  | 4191 | wpa_s->pending_interface_name, | 
|  | 4192 | sizeof(go_ifname)); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4193 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4194 | if (!go_ifname[0]) { | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4195 | wpas_p2ps_prov_complete( | 
|  | 4196 | wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP, | 
|  | 4197 | dev, adv_mac, ses_mac, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4198 | grp_mac, adv_id, ses_id, 0, 0, | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 4199 | NULL, 0, 0, 0, NULL, NULL, 0, 0, | 
|  | 4200 | NULL, 0); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4201 | return; | 
|  | 4202 | } | 
|  | 4203 |  | 
|  | 4204 | /* If PD Resp complete, start up the GO */ | 
|  | 4205 | if (response_done && persistent_go) { | 
|  | 4206 | wpas_p2p_group_add_persistent( | 
|  | 4207 | wpa_s, persistent_go, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 4208 | 0, 0, freq, 0, 0, 0, 0, NULL, | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4209 | persistent_go->mode == | 
|  | 4210 | WPAS_MODE_P2P_GO ? | 
|  | 4211 | P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4212 | 0, 0); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4213 | } else if (response_done) { | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 4214 | wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4215 | } | 
|  | 4216 |  | 
|  | 4217 | if (passwd_id == DEV_PW_P2PS_DEFAULT) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4218 | os_memcpy(wpa_s->p2ps_join_addr, grp_mac, | 
|  | 4219 | ETH_ALEN); | 
|  | 4220 | wpa_s->p2ps_method_config_any = 1; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4221 | } | 
|  | 4222 | } else if (passwd_id == DEV_PW_P2PS_DEFAULT) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4223 | os_memcpy(go_ifname, go_wpa_s->ifname, | 
|  | 4224 | sizeof(go_ifname)); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4225 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4226 | if (is_zero_ether_addr(grp_mac)) { | 
|  | 4227 | wpa_dbg(go_wpa_s, MSG_DEBUG, | 
|  | 4228 | "P2P: Setting PIN-1 for ANY"); | 
|  | 4229 | wpa_supplicant_ap_wps_pin(go_wpa_s, NULL, | 
|  | 4230 | "12345670", NULL, 0, | 
|  | 4231 | 0); | 
|  | 4232 | } else { | 
|  | 4233 | wpa_dbg(go_wpa_s, MSG_DEBUG, | 
|  | 4234 | "P2P: Setting PIN-1 for " MACSTR, | 
|  | 4235 | MAC2STR(grp_mac)); | 
|  | 4236 | wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac, | 
|  | 4237 | "12345670", NULL, 0, | 
|  | 4238 | 0); | 
|  | 4239 | } | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4240 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4241 | os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN); | 
|  | 4242 | wpa_s->p2ps_method_config_any = 1; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4243 | } | 
|  | 4244 |  | 
|  | 4245 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 4246 | P2P_EVENT_P2PS_PROVISION_DONE MACSTR | 
|  | 4247 | " status=%d conncap=%x" | 
|  | 4248 | " adv_id=%x adv_mac=" MACSTR | 
|  | 4249 | " session=%x mac=" MACSTR | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4250 | " dev_passwd_id=%d go=%s%s", | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4251 | MAC2STR(dev), status, conncap, | 
|  | 4252 | adv_id, MAC2STR(adv_mac), | 
|  | 4253 | ses_id, MAC2STR(ses_mac), | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4254 | passwd_id, go_ifname, feat_cap_str); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4255 | return; | 
|  | 4256 | } | 
|  | 4257 |  | 
|  | 4258 | if (go_wpa_s && !p2p_group_go_member_count(wpa_s)) | 
|  | 4259 | wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname); | 
|  | 4260 |  | 
|  | 4261 | if (persistent_go && !persistent_go->num_p2p_clients) { | 
|  | 4262 | /* remove empty persistent GO */ | 
|  | 4263 | wpa_config_remove_network(wpa_s->conf, persistent_go->id); | 
|  | 4264 | } | 
|  | 4265 |  | 
|  | 4266 | if (conncap == P2PS_SETUP_CLIENT) { | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 4267 | char ssid_hex[32 * 2 + 1]; | 
|  | 4268 |  | 
|  | 4269 | if (group_ssid) | 
|  | 4270 | wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex), | 
|  | 4271 | group_ssid, group_ssid_len); | 
|  | 4272 | else | 
|  | 4273 | ssid_hex[0] = '\0'; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4274 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 4275 | P2P_EVENT_P2PS_PROVISION_DONE MACSTR | 
|  | 4276 | " status=%d conncap=%x" | 
|  | 4277 | " adv_id=%x adv_mac=" MACSTR | 
|  | 4278 | " session=%x mac=" MACSTR | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 4279 | " dev_passwd_id=%d join=" MACSTR "%s%s%s", | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4280 | MAC2STR(dev), status, conncap, | 
|  | 4281 | adv_id, MAC2STR(adv_mac), | 
|  | 4282 | ses_id, MAC2STR(ses_mac), | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 4283 | passwd_id, MAC2STR(grp_mac), feat_cap_str, | 
|  | 4284 | group_ssid ? " group_ssid=" : "", ssid_hex); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4285 | } else { | 
|  | 4286 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 4287 | P2P_EVENT_P2PS_PROVISION_DONE MACSTR | 
|  | 4288 | " status=%d conncap=%x" | 
|  | 4289 | " adv_id=%x adv_mac=" MACSTR | 
|  | 4290 | " session=%x mac=" MACSTR | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4291 | " dev_passwd_id=%d%s", | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4292 | MAC2STR(dev), status, conncap, | 
|  | 4293 | adv_id, MAC2STR(adv_mac), | 
|  | 4294 | ses_id, MAC2STR(ses_mac), | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4295 | passwd_id, feat_cap_str); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4296 | } | 
|  | 4297 | } | 
|  | 4298 |  | 
|  | 4299 |  | 
| Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 4300 | static int _wpas_p2p_in_progress(void *ctx) | 
|  | 4301 | { | 
|  | 4302 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 4303 | return wpas_p2p_in_progress(wpa_s); | 
|  | 4304 | } | 
|  | 4305 |  | 
|  | 4306 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4307 | static int wpas_prov_disc_resp_cb(void *ctx) | 
|  | 4308 | { | 
|  | 4309 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 4310 | struct wpa_ssid *persistent_go; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4311 | unsigned int freq; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4312 |  | 
|  | 4313 | if (!wpa_s->global->pending_p2ps_group) | 
|  | 4314 | return 0; | 
|  | 4315 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4316 | freq = wpa_s->global->pending_p2ps_group_freq; | 
|  | 4317 | wpa_s->global->pending_p2ps_group_freq = 0; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4318 | wpa_s->global->pending_p2ps_group = 0; | 
|  | 4319 |  | 
|  | 4320 | if (wpas_p2p_get_go_group(wpa_s)) | 
|  | 4321 | return 0; | 
|  | 4322 | persistent_go = wpas_p2p_get_persistent_go(wpa_s); | 
|  | 4323 |  | 
|  | 4324 | if (persistent_go) { | 
|  | 4325 | wpas_p2p_group_add_persistent( | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 4326 | wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, NULL, | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4327 | persistent_go->mode == WPAS_MODE_P2P_GO ? | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4328 | P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4329 | } else { | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 4330 | wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4331 | } | 
|  | 4332 |  | 
|  | 4333 | return 1; | 
|  | 4334 | } | 
|  | 4335 |  | 
|  | 4336 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4337 | static int wpas_p2p_get_pref_freq_list(void *ctx, int go, | 
|  | 4338 | unsigned int *len, | 
|  | 4339 | unsigned int *freq_list) | 
|  | 4340 | { | 
|  | 4341 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 4342 |  | 
|  | 4343 | return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO : | 
|  | 4344 | WPA_IF_P2P_CLIENT, len, freq_list); | 
|  | 4345 | } | 
|  | 4346 |  | 
| Jimmy Chen | f887c7b | 2018-11-13 15:19:57 +0800 | [diff] [blame] | 4347 | int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s) | 
|  | 4348 | { | 
|  | 4349 | u8 addr[ETH_ALEN] = {0}; | 
|  | 4350 |  | 
|  | 4351 | if (wpa_s->conf->p2p_device_random_mac_addr == 0) | 
|  | 4352 | return 0; | 
|  | 4353 |  | 
|  | 4354 | if (wpa_s->conf->ssid == NULL) { | 
|  | 4355 | if (random_mac_addr(addr) < 0) { | 
|  | 4356 | wpa_msg(wpa_s, MSG_INFO, | 
|  | 4357 | "Failed to generate random MAC address"); | 
|  | 4358 | return -EINVAL; | 
|  | 4359 | } | 
|  | 4360 |  | 
|  | 4361 | // store generated MAC address. | 
|  | 4362 | if (wpa_s->conf->p2p_device_persistent_mac_addr) | 
|  | 4363 | os_free(wpa_s->conf->p2p_device_persistent_mac_addr); | 
|  | 4364 | size_t mac_addr_str_len = sizeof("00:00:00:00:00:00"); | 
|  | 4365 | wpa_s->conf->p2p_device_persistent_mac_addr = | 
|  | 4366 | os_zalloc(mac_addr_str_len + 1); | 
|  | 4367 | os_snprintf(wpa_s->conf->p2p_device_persistent_mac_addr, | 
|  | 4368 | mac_addr_str_len, MACSTR, MAC2STR(addr)); | 
|  | 4369 | } else { | 
|  | 4370 | // If there are existing saved groups, restore last MAC address. | 
|  | 4371 | // if there is no last used MAC address, the last one is factory MAC. | 
|  | 4372 | if (!wpa_s->conf->p2p_device_persistent_mac_addr) | 
|  | 4373 | return 0; | 
|  | 4374 |  | 
|  | 4375 | if (hwaddr_aton(wpa_s->conf->p2p_device_persistent_mac_addr, addr) < 0) | 
|  | 4376 | return -EINVAL; | 
|  | 4377 | wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address."); | 
|  | 4378 | } | 
|  | 4379 |  | 
|  | 4380 | if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) { | 
|  | 4381 | wpa_msg(wpa_s, MSG_INFO, | 
|  | 4382 | "Failed to set random MAC address"); | 
|  | 4383 | return -EINVAL; | 
|  | 4384 | } | 
|  | 4385 |  | 
|  | 4386 | if (wpa_supplicant_update_mac_addr(wpa_s) < 0) { | 
|  | 4387 | wpa_msg(wpa_s, MSG_INFO, | 
|  | 4388 | "Could not update MAC address information"); | 
|  | 4389 | return -EINVAL; | 
|  | 4390 | } | 
|  | 4391 |  | 
|  | 4392 | wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR, | 
|  | 4393 | MAC2STR(addr)); | 
|  | 4394 |  | 
|  | 4395 | return 0; | 
|  | 4396 | } | 
|  | 4397 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4398 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4399 | /** | 
|  | 4400 | * wpas_p2p_init - Initialize P2P module for %wpa_supplicant | 
|  | 4401 | * @global: Pointer to global data from wpa_supplicant_init() | 
|  | 4402 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | 
|  | 4403 | * Returns: 0 on success, -1 on failure | 
|  | 4404 | */ | 
|  | 4405 | int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s) | 
|  | 4406 | { | 
|  | 4407 | struct p2p_config p2p; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4408 | int i; | 
|  | 4409 |  | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4410 | if (wpa_s->conf->p2p_disabled) | 
|  | 4411 | return 0; | 
|  | 4412 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4413 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) | 
|  | 4414 | return 0; | 
|  | 4415 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4416 | if (global->p2p) | 
|  | 4417 | return 0; | 
|  | 4418 |  | 
| Jimmy Chen | f887c7b | 2018-11-13 15:19:57 +0800 | [diff] [blame] | 4419 | if (wpas_p2p_mac_setup(wpa_s) < 0) { | 
|  | 4420 | wpa_msg(wpa_s, MSG_ERROR, | 
|  | 4421 | "Failed to initialize P2P random MAC address."); | 
|  | 4422 | return -1; | 
|  | 4423 | } | 
|  | 4424 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4425 | os_memset(&p2p, 0, sizeof(p2p)); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4426 | p2p.cb_ctx = wpa_s; | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4427 | p2p.debug_print = wpas_p2p_debug_print; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4428 | p2p.p2p_scan = wpas_p2p_scan; | 
|  | 4429 | p2p.send_action = wpas_send_action; | 
|  | 4430 | p2p.send_action_done = wpas_send_action_done; | 
|  | 4431 | p2p.go_neg_completed = wpas_go_neg_completed; | 
|  | 4432 | p2p.go_neg_req_rx = wpas_go_neg_req_rx; | 
|  | 4433 | p2p.dev_found = wpas_dev_found; | 
|  | 4434 | p2p.dev_lost = wpas_dev_lost; | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4435 | p2p.find_stopped = wpas_find_stopped; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4436 | p2p.start_listen = wpas_start_listen; | 
|  | 4437 | p2p.stop_listen = wpas_stop_listen; | 
|  | 4438 | p2p.send_probe_resp = wpas_send_probe_resp; | 
|  | 4439 | p2p.sd_request = wpas_sd_request; | 
|  | 4440 | p2p.sd_response = wpas_sd_response; | 
|  | 4441 | p2p.prov_disc_req = wpas_prov_disc_req; | 
|  | 4442 | p2p.prov_disc_resp = wpas_prov_disc_resp; | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 4443 | p2p.prov_disc_fail = wpas_prov_disc_fail; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4444 | p2p.invitation_process = wpas_invitation_process; | 
|  | 4445 | p2p.invitation_received = wpas_invitation_received; | 
|  | 4446 | p2p.invitation_result = wpas_invitation_result; | 
|  | 4447 | p2p.get_noa = wpas_get_noa; | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 4448 | p2p.go_connected = wpas_go_connected; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4449 | p2p.presence_resp = wpas_presence_resp; | 
| Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 4450 | p2p.is_concurrent_session_active = wpas_is_concurrent_session_active; | 
| Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 4451 | p2p.is_p2p_in_progress = _wpas_p2p_in_progress; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4452 | p2p.get_persistent_group = wpas_get_persistent_group; | 
|  | 4453 | p2p.get_go_info = wpas_get_go_info; | 
|  | 4454 | p2p.remove_stale_groups = wpas_remove_stale_groups; | 
|  | 4455 | p2p.p2ps_prov_complete = wpas_p2ps_prov_complete; | 
|  | 4456 | p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb; | 
|  | 4457 | p2p.p2ps_group_capability = p2ps_group_capability; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4458 | p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4459 |  | 
|  | 4460 | 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] | 4461 | 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] | 4462 | p2p.dev_name = wpa_s->conf->device_name; | 
|  | 4463 | p2p.manufacturer = wpa_s->conf->manufacturer; | 
|  | 4464 | p2p.model_name = wpa_s->conf->model_name; | 
|  | 4465 | p2p.model_number = wpa_s->conf->model_number; | 
|  | 4466 | p2p.serial_number = wpa_s->conf->serial_number; | 
|  | 4467 | if (wpa_s->wps) { | 
|  | 4468 | os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16); | 
|  | 4469 | p2p.config_methods = wpa_s->wps->config_methods; | 
|  | 4470 | } | 
|  | 4471 |  | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 4472 | if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) { | 
|  | 4473 | wpa_printf(MSG_ERROR, | 
|  | 4474 | "P2P: Failed to configure supported channel list"); | 
|  | 4475 | return -1; | 
|  | 4476 | } | 
|  | 4477 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4478 | if (wpa_s->conf->p2p_listen_reg_class && | 
|  | 4479 | wpa_s->conf->p2p_listen_channel) { | 
|  | 4480 | p2p.reg_class = wpa_s->conf->p2p_listen_reg_class; | 
|  | 4481 | p2p.channel = wpa_s->conf->p2p_listen_channel; | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4482 | p2p.channel_forced = 1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4483 | } else { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4484 | /* | 
|  | 4485 | * Pick one of the social channels randomly as the listen | 
|  | 4486 | * channel. | 
|  | 4487 | */ | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 4488 | if (p2p_config_get_random_social(&p2p, &p2p.reg_class, | 
|  | 4489 | &p2p.channel) != 0) { | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 4490 | wpa_printf(MSG_INFO, | 
|  | 4491 | "P2P: No social channels supported by the driver - do not enable P2P"); | 
|  | 4492 | return 0; | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 4493 | } | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4494 | p2p.channel_forced = 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4495 | } | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 4496 | wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d", | 
|  | 4497 | p2p.reg_class, p2p.channel); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4498 |  | 
|  | 4499 | if (wpa_s->conf->p2p_oper_reg_class && | 
|  | 4500 | wpa_s->conf->p2p_oper_channel) { | 
|  | 4501 | p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class; | 
|  | 4502 | p2p.op_channel = wpa_s->conf->p2p_oper_channel; | 
|  | 4503 | p2p.cfg_op_channel = 1; | 
|  | 4504 | wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: " | 
|  | 4505 | "%d:%d", p2p.op_reg_class, p2p.op_channel); | 
|  | 4506 |  | 
|  | 4507 | } else { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4508 | /* | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 4509 | * Use random operation channel from 2.4 GHz band social | 
|  | 4510 | * channels (1, 6, 11) or band 60 GHz social channel (2) if no | 
|  | 4511 | * other preference is indicated. | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4512 | */ | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 4513 | if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class, | 
|  | 4514 | &p2p.op_channel) != 0) { | 
|  | 4515 | wpa_printf(MSG_ERROR, | 
|  | 4516 | "P2P: Failed to select random social channel as operation channel"); | 
|  | 4517 | return -1; | 
|  | 4518 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4519 | p2p.cfg_op_channel = 0; | 
|  | 4520 | wpa_printf(MSG_DEBUG, "P2P: Random operating channel: " | 
|  | 4521 | "%d:%d", p2p.op_reg_class, p2p.op_channel); | 
|  | 4522 | } | 
| Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 4523 |  | 
|  | 4524 | if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) { | 
|  | 4525 | p2p.pref_chan = wpa_s->conf->p2p_pref_chan; | 
|  | 4526 | p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan; | 
|  | 4527 | } | 
|  | 4528 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4529 | if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) { | 
|  | 4530 | os_memcpy(p2p.country, wpa_s->conf->country, 2); | 
|  | 4531 | p2p.country[2] = 0x04; | 
|  | 4532 | } else | 
|  | 4533 | os_memcpy(p2p.country, "XX\x04", 3); | 
|  | 4534 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4535 | os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type, | 
|  | 4536 | WPS_DEV_TYPE_LEN); | 
|  | 4537 |  | 
|  | 4538 | p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types; | 
|  | 4539 | os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type, | 
|  | 4540 | p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN); | 
|  | 4541 |  | 
|  | 4542 | p2p.concurrent_operations = !!(wpa_s->drv_flags & | 
|  | 4543 | WPA_DRIVER_FLAGS_P2P_CONCURRENT); | 
|  | 4544 |  | 
|  | 4545 | p2p.max_peers = 100; | 
|  | 4546 |  | 
|  | 4547 | if (wpa_s->conf->p2p_ssid_postfix) { | 
|  | 4548 | p2p.ssid_postfix_len = | 
|  | 4549 | os_strlen(wpa_s->conf->p2p_ssid_postfix); | 
|  | 4550 | if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix)) | 
|  | 4551 | p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix); | 
|  | 4552 | os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix, | 
|  | 4553 | p2p.ssid_postfix_len); | 
|  | 4554 | } | 
|  | 4555 |  | 
|  | 4556 | p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss; | 
|  | 4557 |  | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4558 | p2p.max_listen = wpa_s->max_remain_on_chan; | 
|  | 4559 |  | 
| Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 4560 | if (wpa_s->conf->p2p_passphrase_len >= 8 && | 
|  | 4561 | wpa_s->conf->p2p_passphrase_len <= 63) | 
|  | 4562 | p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len; | 
|  | 4563 | else | 
|  | 4564 | p2p.passphrase_len = 8; | 
|  | 4565 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4566 | global->p2p = p2p_init(&p2p); | 
|  | 4567 | if (global->p2p == NULL) | 
|  | 4568 | return -1; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4569 | global->p2p_init_wpa_s = wpa_s; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4570 |  | 
|  | 4571 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { | 
|  | 4572 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) | 
|  | 4573 | continue; | 
|  | 4574 | p2p_add_wps_vendor_extension( | 
|  | 4575 | global->p2p, wpa_s->conf->wps_vendor_ext[i]); | 
|  | 4576 | } | 
|  | 4577 |  | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4578 | p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq); | 
|  | 4579 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4580 | return 0; | 
|  | 4581 | } | 
|  | 4582 |  | 
|  | 4583 |  | 
|  | 4584 | /** | 
|  | 4585 | * wpas_p2p_deinit - Deinitialize per-interface P2P data | 
|  | 4586 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | 
|  | 4587 | * | 
|  | 4588 | * This function deinitialize per-interface P2P data. | 
|  | 4589 | */ | 
|  | 4590 | void wpas_p2p_deinit(struct wpa_supplicant *wpa_s) | 
|  | 4591 | { | 
|  | 4592 | if (wpa_s->driver && wpa_s->drv_priv) | 
|  | 4593 | wpa_drv_probe_req_report(wpa_s, 0); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4594 |  | 
|  | 4595 | if (wpa_s->go_params) { | 
|  | 4596 | /* Clear any stored provisioning info */ | 
|  | 4597 | p2p_clear_provisioning_info( | 
|  | 4598 | wpa_s->global->p2p, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4599 | wpa_s->go_params->peer_device_addr); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4600 | } | 
|  | 4601 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4602 | os_free(wpa_s->go_params); | 
|  | 4603 | wpa_s->go_params = NULL; | 
| Dmitry Shmidt | 684785c | 2014-05-12 13:34:29 -0700 | [diff] [blame] | 4604 | eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4605 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); | 
|  | 4606 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); | 
|  | 4607 | wpa_s->p2p_long_listen = 0; | 
|  | 4608 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); | 
|  | 4609 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); | 
|  | 4610 | wpas_p2p_remove_pending_group_interface(wpa_s); | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 4611 | eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4612 | wpas_p2p_listen_work_done(wpa_s); | 
|  | 4613 | if (wpa_s->p2p_send_action_work) { | 
|  | 4614 | os_free(wpa_s->p2p_send_action_work->ctx); | 
|  | 4615 | radio_work_done(wpa_s->p2p_send_action_work); | 
|  | 4616 | wpa_s->p2p_send_action_work = NULL; | 
|  | 4617 | } | 
|  | 4618 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4619 |  | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4620 | wpabuf_free(wpa_s->p2p_oob_dev_pw); | 
|  | 4621 | wpa_s->p2p_oob_dev_pw = NULL; | 
|  | 4622 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4623 | os_free(wpa_s->p2p_group_common_freqs); | 
|  | 4624 | wpa_s->p2p_group_common_freqs = NULL; | 
|  | 4625 | wpa_s->p2p_group_common_freqs_num = 0; | 
|  | 4626 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4627 | /* TODO: remove group interface from the driver if this wpa_s instance | 
|  | 4628 | * is on top of a P2P group interface */ | 
|  | 4629 | } | 
|  | 4630 |  | 
|  | 4631 |  | 
|  | 4632 | /** | 
|  | 4633 | * wpas_p2p_deinit_global - Deinitialize global P2P module | 
|  | 4634 | * @global: Pointer to global data from wpa_supplicant_init() | 
|  | 4635 | * | 
|  | 4636 | * This function deinitializes the global (per device) P2P module. | 
|  | 4637 | */ | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4638 | static void wpas_p2p_deinit_global(struct wpa_global *global) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4639 | { | 
|  | 4640 | struct wpa_supplicant *wpa_s, *tmp; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4641 |  | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4642 | wpa_s = global->ifaces; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4643 |  | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 4644 | wpas_p2p_service_flush(global->p2p_init_wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4645 |  | 
|  | 4646 | /* Remove remaining P2P group interfaces */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4647 | while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) | 
|  | 4648 | wpa_s = wpa_s->next; | 
|  | 4649 | while (wpa_s) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4650 | tmp = global->ifaces; | 
|  | 4651 | while (tmp && | 
|  | 4652 | (tmp == wpa_s || | 
|  | 4653 | tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) { | 
|  | 4654 | tmp = tmp->next; | 
|  | 4655 | } | 
|  | 4656 | if (tmp == NULL) | 
|  | 4657 | break; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4658 | /* Disconnect from the P2P group and deinit the interface */ | 
|  | 4659 | wpas_p2p_disconnect(tmp); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4660 | } | 
|  | 4661 |  | 
|  | 4662 | /* | 
|  | 4663 | * Deinit GO data on any possibly remaining interface (if main | 
|  | 4664 | * interface is used as GO). | 
|  | 4665 | */ | 
|  | 4666 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
|  | 4667 | if (wpa_s->ap_iface) | 
|  | 4668 | wpas_p2p_group_deinit(wpa_s); | 
|  | 4669 | } | 
|  | 4670 |  | 
|  | 4671 | p2p_deinit(global->p2p); | 
|  | 4672 | global->p2p = NULL; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4673 | global->p2p_init_wpa_s = NULL; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4674 | } | 
|  | 4675 |  | 
|  | 4676 |  | 
|  | 4677 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s) | 
|  | 4678 | { | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 4679 | if (wpa_s->conf->p2p_no_group_iface) | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4680 | return 0; /* separate interface disabled per configuration */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4681 | if (wpa_s->drv_flags & | 
|  | 4682 | (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE | | 
|  | 4683 | WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P)) | 
|  | 4684 | return 1; /* P2P group requires a new interface in every case | 
|  | 4685 | */ | 
|  | 4686 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)) | 
|  | 4687 | return 0; /* driver does not support concurrent operations */ | 
|  | 4688 | if (wpa_s->global->ifaces->next) | 
|  | 4689 | return 1; /* more that one interface already in use */ | 
|  | 4690 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) | 
|  | 4691 | return 1; /* this interface is already in use */ | 
|  | 4692 | return 0; | 
|  | 4693 | } | 
|  | 4694 |  | 
|  | 4695 |  | 
|  | 4696 | static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s, | 
|  | 4697 | const u8 *peer_addr, | 
|  | 4698 | enum p2p_wps_method wps_method, | 
|  | 4699 | int go_intent, const u8 *own_interface_addr, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4700 | unsigned int force_freq, int persistent_group, | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4701 | struct wpa_ssid *ssid, unsigned int pref_freq) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4702 | { | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4703 | if (persistent_group && wpa_s->conf->persistent_reconnect) | 
|  | 4704 | persistent_group = 2; | 
|  | 4705 |  | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4706 | /* | 
|  | 4707 | * Increase GO config timeout if HT40 is used since it takes some time | 
|  | 4708 | * to scan channels for coex purposes before the BSS can be started. | 
|  | 4709 | */ | 
|  | 4710 | p2p_set_config_timeout(wpa_s->global->p2p, | 
|  | 4711 | wpa_s->p2p_go_ht40 ? 255 : 100, 20); | 
|  | 4712 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4713 | return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method, | 
|  | 4714 | go_intent, own_interface_addr, force_freq, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4715 | persistent_group, ssid ? ssid->ssid : NULL, | 
|  | 4716 | ssid ? ssid->ssid_len : 0, | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4717 | wpa_s->p2p_pd_before_go_neg, pref_freq, | 
|  | 4718 | wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id : | 
|  | 4719 | 0); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4720 | } | 
|  | 4721 |  | 
|  | 4722 |  | 
|  | 4723 | static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s, | 
|  | 4724 | const u8 *peer_addr, | 
|  | 4725 | enum p2p_wps_method wps_method, | 
|  | 4726 | int go_intent, const u8 *own_interface_addr, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4727 | unsigned int force_freq, int persistent_group, | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4728 | struct wpa_ssid *ssid, unsigned int pref_freq) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4729 | { | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4730 | if (persistent_group && wpa_s->conf->persistent_reconnect) | 
|  | 4731 | persistent_group = 2; | 
|  | 4732 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4733 | return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method, | 
|  | 4734 | go_intent, own_interface_addr, force_freq, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4735 | persistent_group, ssid ? ssid->ssid : NULL, | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4736 | ssid ? ssid->ssid_len : 0, pref_freq, | 
|  | 4737 | wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id : | 
|  | 4738 | 0); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4739 | } | 
|  | 4740 |  | 
|  | 4741 |  | 
|  | 4742 | static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s) | 
|  | 4743 | { | 
|  | 4744 | wpa_s->p2p_join_scan_count++; | 
|  | 4745 | wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d", | 
|  | 4746 | wpa_s->p2p_join_scan_count); | 
|  | 4747 | if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) { | 
|  | 4748 | wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR | 
|  | 4749 | " for join operationg - stop join attempt", | 
|  | 4750 | MAC2STR(wpa_s->pending_join_iface_addr)); | 
|  | 4751 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4752 | if (wpa_s->p2p_auto_pd) { | 
|  | 4753 | wpa_s->p2p_auto_pd = 0; | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4754 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 4755 | P2P_EVENT_PROV_DISC_FAILURE | 
|  | 4756 | " p2p_dev_addr=" MACSTR " status=N/A", | 
|  | 4757 | MAC2STR(wpa_s->pending_join_dev_addr)); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4758 | return; | 
|  | 4759 | } | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 4760 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4761 | P2P_EVENT_GROUP_FORMATION_FAILURE); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4762 | wpas_notify_p2p_group_formation_failure(wpa_s, ""); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4763 | } | 
|  | 4764 | } | 
|  | 4765 |  | 
|  | 4766 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4767 | static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq) | 
|  | 4768 | { | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4769 | int res; | 
|  | 4770 | unsigned int num, i; | 
|  | 4771 | struct wpa_used_freq_data *freqs; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4772 |  | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4773 | if (wpas_p2p_num_unused_channels(wpa_s) > 0) { | 
|  | 4774 | /* Multiple channels are supported and not all are in use */ | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4775 | return 0; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4776 | } | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4777 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4778 | freqs = os_calloc(wpa_s->num_multichan_concurrent, | 
|  | 4779 | sizeof(struct wpa_used_freq_data)); | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4780 | if (!freqs) | 
|  | 4781 | return 1; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4782 |  | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4783 | num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, | 
|  | 4784 | wpa_s->num_multichan_concurrent); | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4785 |  | 
|  | 4786 | for (i = 0; i < num; i++) { | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4787 | if (freqs[i].freq == freq) { | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4788 | wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used", | 
|  | 4789 | freq); | 
|  | 4790 | res = 0; | 
|  | 4791 | goto exit_free; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4792 | } | 
|  | 4793 | } | 
|  | 4794 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4795 | wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies"); | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4796 | res = 1; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4797 |  | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4798 | exit_free: | 
|  | 4799 | os_free(freqs); | 
|  | 4800 | return res; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4801 | } | 
|  | 4802 |  | 
|  | 4803 |  | 
|  | 4804 | static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s, | 
|  | 4805 | const u8 *peer_dev_addr) | 
|  | 4806 | { | 
|  | 4807 | struct wpa_bss *bss; | 
|  | 4808 | int updated; | 
|  | 4809 |  | 
|  | 4810 | bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr); | 
|  | 4811 | if (bss == NULL) | 
|  | 4812 | return -1; | 
|  | 4813 | if (bss->last_update_idx < wpa_s->bss_update_idx) { | 
|  | 4814 | wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the " | 
|  | 4815 | "last scan"); | 
|  | 4816 | return 0; | 
|  | 4817 | } | 
|  | 4818 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4819 | updated = os_reltime_before(&wpa_s->p2p_auto_started, | 
|  | 4820 | &bss->last_update); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4821 | wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at " | 
|  | 4822 | "%ld.%06ld (%supdated in last scan)", | 
|  | 4823 | bss->last_update.sec, bss->last_update.usec, | 
|  | 4824 | updated ? "": "not "); | 
|  | 4825 |  | 
|  | 4826 | return updated; | 
|  | 4827 | } | 
|  | 4828 |  | 
|  | 4829 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4830 | static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s, | 
|  | 4831 | struct wpa_scan_results *scan_res) | 
|  | 4832 | { | 
| Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4833 | struct wpa_bss *bss = NULL; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4834 | int freq; | 
|  | 4835 | u8 iface_addr[ETH_ALEN]; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4836 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4837 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); | 
|  | 4838 |  | 
|  | 4839 | if (wpa_s->global->p2p_disabled) | 
|  | 4840 | return; | 
|  | 4841 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4842 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin", | 
|  | 4843 | scan_res ? (int) scan_res->num : -1, | 
|  | 4844 | wpa_s->p2p_auto_join ? "auto_" : ""); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4845 |  | 
|  | 4846 | if (scan_res) | 
|  | 4847 | wpas_p2p_scan_res_handler(wpa_s, scan_res); | 
|  | 4848 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4849 | if (wpa_s->p2p_auto_pd) { | 
|  | 4850 | int join = wpas_p2p_peer_go(wpa_s, | 
|  | 4851 | wpa_s->pending_join_dev_addr); | 
|  | 4852 | if (join == 0 && | 
|  | 4853 | wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) { | 
|  | 4854 | wpa_s->auto_pd_scan_retry++; | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4855 | bss = wpa_bss_get_bssid_latest( | 
|  | 4856 | wpa_s, wpa_s->pending_join_dev_addr); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4857 | if (bss) { | 
|  | 4858 | freq = bss->freq; | 
|  | 4859 | wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for " | 
|  | 4860 | "the peer " MACSTR " at %d MHz", | 
|  | 4861 | wpa_s->auto_pd_scan_retry, | 
|  | 4862 | MAC2STR(wpa_s-> | 
|  | 4863 | pending_join_dev_addr), | 
|  | 4864 | freq); | 
| Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4865 | wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4866 | return; | 
|  | 4867 | } | 
|  | 4868 | } | 
|  | 4869 |  | 
|  | 4870 | if (join < 0) | 
|  | 4871 | join = 0; | 
|  | 4872 |  | 
|  | 4873 | wpa_s->p2p_auto_pd = 0; | 
|  | 4874 | wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG; | 
|  | 4875 | wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d", | 
|  | 4876 | MAC2STR(wpa_s->pending_join_dev_addr), join); | 
|  | 4877 | if (p2p_prov_disc_req(wpa_s->global->p2p, | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4878 | wpa_s->pending_join_dev_addr, NULL, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4879 | wpa_s->pending_pd_config_methods, join, | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4880 | 0, wpa_s->user_initiated_pd) < 0) { | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4881 | wpa_s->p2p_auto_pd = 0; | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4882 | wpa_msg_global(wpa_s, MSG_INFO, | 
|  | 4883 | P2P_EVENT_PROV_DISC_FAILURE | 
|  | 4884 | " p2p_dev_addr=" MACSTR " status=N/A", | 
|  | 4885 | MAC2STR(wpa_s->pending_join_dev_addr)); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4886 | } | 
|  | 4887 | return; | 
|  | 4888 | } | 
|  | 4889 |  | 
|  | 4890 | if (wpa_s->p2p_auto_join) { | 
|  | 4891 | int join = wpas_p2p_peer_go(wpa_s, | 
|  | 4892 | wpa_s->pending_join_dev_addr); | 
|  | 4893 | if (join < 0) { | 
|  | 4894 | wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be " | 
|  | 4895 | "running a GO -> use GO Negotiation"); | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 4896 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 4897 | P2P_EVENT_FALLBACK_TO_GO_NEG | 
|  | 4898 | "reason=peer-not-running-GO"); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4899 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, | 
|  | 4900 | wpa_s->p2p_pin, wpa_s->p2p_wps_method, | 
|  | 4901 | wpa_s->p2p_persistent_group, 0, 0, 0, | 
|  | 4902 | wpa_s->p2p_go_intent, | 
|  | 4903 | wpa_s->p2p_connect_freq, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 4904 | wpa_s->p2p_go_vht_center_freq2, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4905 | wpa_s->p2p_persistent_id, | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4906 | wpa_s->p2p_pd_before_go_neg, | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4907 | wpa_s->p2p_go_ht40, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 4908 | wpa_s->p2p_go_vht, | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 4909 | wpa_s->p2p_go_max_oper_chwidth, | 
|  | 4910 | NULL, 0); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4911 | return; | 
|  | 4912 | } | 
|  | 4913 |  | 
|  | 4914 | wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> " | 
|  | 4915 | "try to join the group", join ? "" : | 
|  | 4916 | " in older scan"); | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 4917 | if (!join) { | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 4918 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 4919 | P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4920 | wpa_s->p2p_fallback_to_go_neg = 1; | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 4921 | } | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4922 | } | 
|  | 4923 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4924 | freq = p2p_get_oper_freq(wpa_s->global->p2p, | 
|  | 4925 | wpa_s->pending_join_iface_addr); | 
|  | 4926 | if (freq < 0 && | 
|  | 4927 | p2p_get_interface_addr(wpa_s->global->p2p, | 
|  | 4928 | wpa_s->pending_join_dev_addr, | 
|  | 4929 | iface_addr) == 0 && | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 4930 | os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0 | 
|  | 4931 | && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4932 | wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface " | 
|  | 4933 | "address for join from " MACSTR " to " MACSTR | 
|  | 4934 | " based on newly discovered P2P peer entry", | 
|  | 4935 | MAC2STR(wpa_s->pending_join_iface_addr), | 
|  | 4936 | MAC2STR(iface_addr)); | 
|  | 4937 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, | 
|  | 4938 | ETH_ALEN); | 
|  | 4939 |  | 
|  | 4940 | freq = p2p_get_oper_freq(wpa_s->global->p2p, | 
|  | 4941 | wpa_s->pending_join_iface_addr); | 
|  | 4942 | } | 
|  | 4943 | if (freq >= 0) { | 
|  | 4944 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " | 
|  | 4945 | "from P2P peer table: %d MHz", freq); | 
|  | 4946 | } | 
| Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4947 | if (wpa_s->p2p_join_ssid_len) { | 
|  | 4948 | wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID " | 
|  | 4949 | MACSTR " and SSID %s", | 
|  | 4950 | MAC2STR(wpa_s->pending_join_iface_addr), | 
|  | 4951 | wpa_ssid_txt(wpa_s->p2p_join_ssid, | 
|  | 4952 | wpa_s->p2p_join_ssid_len)); | 
|  | 4953 | bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr, | 
|  | 4954 | wpa_s->p2p_join_ssid, | 
|  | 4955 | wpa_s->p2p_join_ssid_len); | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 4956 | } else if (!bss) { | 
| Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4957 | wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID " | 
|  | 4958 | MACSTR, MAC2STR(wpa_s->pending_join_iface_addr)); | 
|  | 4959 | bss = wpa_bss_get_bssid_latest(wpa_s, | 
|  | 4960 | wpa_s->pending_join_iface_addr); | 
|  | 4961 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4962 | if (bss) { | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 4963 | u8 dev_addr[ETH_ALEN]; | 
|  | 4964 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4965 | freq = bss->freq; | 
|  | 4966 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " | 
| Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 4967 | "from BSS table: %d MHz (SSID %s)", freq, | 
|  | 4968 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 4969 | if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len, | 
|  | 4970 | dev_addr) == 0 && | 
|  | 4971 | os_memcmp(wpa_s->pending_join_dev_addr, | 
|  | 4972 | wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 && | 
|  | 4973 | os_memcmp(dev_addr, wpa_s->pending_join_dev_addr, | 
|  | 4974 | ETH_ALEN) != 0) { | 
|  | 4975 | wpa_printf(MSG_DEBUG, | 
|  | 4976 | "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")", | 
|  | 4977 | MAC2STR(dev_addr), | 
|  | 4978 | MAC2STR(wpa_s->pending_join_dev_addr)); | 
|  | 4979 | os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, | 
|  | 4980 | ETH_ALEN); | 
|  | 4981 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4982 | } | 
|  | 4983 | if (freq > 0) { | 
|  | 4984 | u16 method; | 
|  | 4985 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4986 | if (wpas_check_freq_conflict(wpa_s, freq) > 0) { | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 4987 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4988 | P2P_EVENT_GROUP_FORMATION_FAILURE | 
|  | 4989 | "reason=FREQ_CONFLICT"); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4990 | wpas_notify_p2p_group_formation_failure( | 
|  | 4991 | wpa_s, "FREQ_CONFLICT"); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4992 | return; | 
|  | 4993 | } | 
|  | 4994 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4995 | wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request " | 
|  | 4996 | "prior to joining an existing group (GO " MACSTR | 
|  | 4997 | " freq=%u MHz)", | 
|  | 4998 | MAC2STR(wpa_s->pending_join_dev_addr), freq); | 
|  | 4999 | wpa_s->pending_pd_before_join = 1; | 
|  | 5000 |  | 
|  | 5001 | switch (wpa_s->pending_join_wps_method) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5002 | case WPS_PIN_DISPLAY: | 
|  | 5003 | method = WPS_CONFIG_KEYPAD; | 
|  | 5004 | break; | 
|  | 5005 | case WPS_PIN_KEYPAD: | 
|  | 5006 | method = WPS_CONFIG_DISPLAY; | 
|  | 5007 | break; | 
|  | 5008 | case WPS_PBC: | 
|  | 5009 | method = WPS_CONFIG_PUSHBUTTON; | 
|  | 5010 | break; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5011 | case WPS_P2PS: | 
|  | 5012 | method = WPS_CONFIG_P2PS; | 
|  | 5013 | break; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5014 | default: | 
|  | 5015 | method = 0; | 
|  | 5016 | break; | 
|  | 5017 | } | 
|  | 5018 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5019 | if ((p2p_get_provisioning_info(wpa_s->global->p2p, | 
|  | 5020 | wpa_s->pending_join_dev_addr) == | 
|  | 5021 | method)) { | 
|  | 5022 | /* | 
|  | 5023 | * We have already performed provision discovery for | 
|  | 5024 | * joining the group. Proceed directly to join | 
|  | 5025 | * operation without duplicated provision discovery. */ | 
|  | 5026 | wpa_printf(MSG_DEBUG, "P2P: Provision discovery " | 
|  | 5027 | "with " MACSTR " already done - proceed to " | 
|  | 5028 | "join", | 
|  | 5029 | MAC2STR(wpa_s->pending_join_dev_addr)); | 
|  | 5030 | wpa_s->pending_pd_before_join = 0; | 
|  | 5031 | goto start; | 
|  | 5032 | } | 
|  | 5033 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5034 | if (p2p_prov_disc_req(wpa_s->global->p2p, | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5035 | wpa_s->pending_join_dev_addr, | 
|  | 5036 | NULL, method, 1, | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5037 | freq, wpa_s->user_initiated_pd) < 0) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5038 | wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision " | 
|  | 5039 | "Discovery Request before joining an " | 
|  | 5040 | "existing group"); | 
|  | 5041 | wpa_s->pending_pd_before_join = 0; | 
|  | 5042 | goto start; | 
|  | 5043 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5044 | return; | 
|  | 5045 | } | 
|  | 5046 |  | 
|  | 5047 | wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later"); | 
|  | 5048 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); | 
|  | 5049 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); | 
|  | 5050 | wpas_p2p_check_join_scan_limit(wpa_s); | 
|  | 5051 | return; | 
|  | 5052 |  | 
|  | 5053 | start: | 
|  | 5054 | /* Start join operation immediately */ | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 5055 | wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid, | 
|  | 5056 | wpa_s->p2p_join_ssid_len); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5057 | } | 
|  | 5058 |  | 
|  | 5059 |  | 
| Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 5060 | static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq, | 
|  | 5061 | const u8 *ssid, size_t ssid_len) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5062 | { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5063 | int ret; | 
|  | 5064 | struct wpa_driver_scan_params params; | 
|  | 5065 | struct wpabuf *wps_ie, *ies; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5066 | size_t ielen; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5067 | int freqs[2] = { 0, 0 }; | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 5068 | unsigned int bands; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5069 |  | 
|  | 5070 | os_memset(¶ms, 0, sizeof(params)); | 
|  | 5071 |  | 
|  | 5072 | /* P2P Wildcard SSID */ | 
|  | 5073 | params.num_ssids = 1; | 
| Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 5074 | if (ssid && ssid_len) { | 
|  | 5075 | params.ssids[0].ssid = ssid; | 
|  | 5076 | params.ssids[0].ssid_len = ssid_len; | 
|  | 5077 | os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len); | 
|  | 5078 | wpa_s->p2p_join_ssid_len = ssid_len; | 
|  | 5079 | } else { | 
|  | 5080 | params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; | 
|  | 5081 | params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; | 
|  | 5082 | wpa_s->p2p_join_ssid_len = 0; | 
|  | 5083 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5084 |  | 
|  | 5085 | wpa_s->wps->dev.p2p = 1; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5086 | wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev, | 
|  | 5087 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0, | 
|  | 5088 | NULL); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5089 | if (wps_ie == NULL) { | 
|  | 5090 | wpas_p2p_scan_res_join(wpa_s, NULL); | 
|  | 5091 | return; | 
|  | 5092 | } | 
|  | 5093 |  | 
| Dmitry Shmidt | 9e3f8ee | 2014-01-17 10:52:01 -0800 | [diff] [blame] | 5094 | if (!freq) { | 
|  | 5095 | int oper_freq; | 
|  | 5096 | /* | 
|  | 5097 | * If freq is not provided, check the operating freq of the GO | 
|  | 5098 | * and use a single channel scan on if possible. | 
|  | 5099 | */ | 
|  | 5100 | oper_freq = p2p_get_oper_freq(wpa_s->global->p2p, | 
|  | 5101 | wpa_s->pending_join_iface_addr); | 
|  | 5102 | if (oper_freq > 0) | 
|  | 5103 | freq = oper_freq; | 
|  | 5104 | } | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5105 | if (freq > 0) { | 
|  | 5106 | freqs[0] = freq; | 
|  | 5107 | params.freqs = freqs; | 
|  | 5108 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5109 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 5110 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); | 
|  | 5111 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); | 
|  | 5112 | if (ies == NULL) { | 
|  | 5113 | wpabuf_free(wps_ie); | 
|  | 5114 | wpas_p2p_scan_res_join(wpa_s, NULL); | 
|  | 5115 | return; | 
|  | 5116 | } | 
|  | 5117 | wpabuf_put_buf(ies, wps_ie); | 
|  | 5118 | wpabuf_free(wps_ie); | 
|  | 5119 |  | 
|  | 5120 | bands = wpas_get_bands(wpa_s, freqs); | 
|  | 5121 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands); | 
|  | 5122 |  | 
|  | 5123 | params.p2p_probe = 1; | 
|  | 5124 | params.extra_ies = wpabuf_head(ies); | 
|  | 5125 | params.extra_ies_len = wpabuf_len(ies); | 
|  | 5126 |  | 
| Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 5127 | if (wpa_s->clear_driver_scan_cache) { | 
|  | 5128 | wpa_printf(MSG_DEBUG, | 
|  | 5129 | "Request driver to clear scan cache due to local BSS flush"); | 
|  | 5130 | params.only_new_results = 1; | 
|  | 5131 | } | 
|  | 5132 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5133 | /* | 
|  | 5134 | * Run a scan to update BSS table and start Provision Discovery once | 
|  | 5135 | * the new scan results become available. | 
|  | 5136 | */ | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5137 | ret = wpa_drv_scan(wpa_s, ¶ms); | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 5138 | if (!ret) { | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5139 | os_get_reltime(&wpa_s->scan_trigger_time); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5140 | wpa_s->scan_res_handler = wpas_p2p_scan_res_join; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5141 | wpa_s->own_scan_requested = 1; | 
| Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 5142 | wpa_s->clear_driver_scan_cache = 0; | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 5143 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5144 |  | 
|  | 5145 | wpabuf_free(ies); | 
|  | 5146 |  | 
|  | 5147 | if (ret) { | 
|  | 5148 | wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - " | 
|  | 5149 | "try again later"); | 
|  | 5150 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); | 
|  | 5151 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); | 
|  | 5152 | wpas_p2p_check_join_scan_limit(wpa_s); | 
|  | 5153 | } | 
|  | 5154 | } | 
|  | 5155 |  | 
|  | 5156 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5157 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx) | 
|  | 5158 | { | 
|  | 5159 | struct wpa_supplicant *wpa_s = eloop_ctx; | 
| Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 5160 | wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5161 | } | 
|  | 5162 |  | 
|  | 5163 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5164 | 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] | 5165 | const u8 *dev_addr, enum p2p_wps_method wps_method, | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5166 | int auto_join, int op_freq, | 
|  | 5167 | const u8 *ssid, size_t ssid_len) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5168 | { | 
|  | 5169 | wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface " | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5170 | MACSTR " dev " MACSTR " op_freq=%d)%s", | 
|  | 5171 | MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5172 | auto_join ? " (auto_join)" : ""); | 
| Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 5173 | if (ssid && ssid_len) { | 
|  | 5174 | wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s", | 
|  | 5175 | wpa_ssid_txt(ssid, ssid_len)); | 
|  | 5176 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5177 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5178 | wpa_s->p2p_auto_pd = 0; | 
|  | 5179 | wpa_s->p2p_auto_join = !!auto_join; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5180 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN); | 
|  | 5181 | os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN); | 
|  | 5182 | wpa_s->pending_join_wps_method = wps_method; | 
|  | 5183 |  | 
|  | 5184 | /* Make sure we are not running find during connection establishment */ | 
|  | 5185 | wpas_p2p_stop_find(wpa_s); | 
|  | 5186 |  | 
|  | 5187 | wpa_s->p2p_join_scan_count = 0; | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5188 | wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5189 | return 0; | 
|  | 5190 | } | 
|  | 5191 |  | 
|  | 5192 |  | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5193 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq, | 
|  | 5194 | const u8 *ssid, size_t ssid_len) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5195 | { | 
|  | 5196 | struct wpa_supplicant *group; | 
|  | 5197 | struct p2p_go_neg_results res; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5198 | struct wpa_bss *bss; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5199 |  | 
|  | 5200 | group = wpas_p2p_get_group_iface(wpa_s, 0, 0); | 
|  | 5201 | if (group == NULL) | 
|  | 5202 | return -1; | 
|  | 5203 | if (group != wpa_s) { | 
|  | 5204 | os_memcpy(group->p2p_pin, wpa_s->p2p_pin, | 
|  | 5205 | sizeof(group->p2p_pin)); | 
|  | 5206 | group->p2p_wps_method = wpa_s->p2p_wps_method; | 
| Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 5207 | } else { | 
|  | 5208 | /* | 
|  | 5209 | * Need to mark the current interface for p2p_group_formation | 
|  | 5210 | * when a separate group interface is not used. This is needed | 
|  | 5211 | * to allow p2p_cancel stop a pending p2p_connect-join. | 
|  | 5212 | * wpas_p2p_init_group_interface() addresses this for the case | 
|  | 5213 | * where a separate group interface is used. | 
|  | 5214 | */ | 
|  | 5215 | wpa_s->global->p2p_group_formation = wpa_s; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5216 | } | 
|  | 5217 |  | 
|  | 5218 | group->p2p_in_provisioning = 1; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5219 | 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] | 5220 |  | 
|  | 5221 | os_memset(&res, 0, sizeof(res)); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5222 | os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5223 | os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr, | 
|  | 5224 | ETH_ALEN); | 
|  | 5225 | res.wps_method = wpa_s->pending_join_wps_method; | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5226 | if (freq && ssid && ssid_len) { | 
|  | 5227 | res.freq = freq; | 
|  | 5228 | res.ssid_len = ssid_len; | 
|  | 5229 | os_memcpy(res.ssid, ssid, ssid_len); | 
|  | 5230 | } else { | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 5231 | if (ssid && ssid_len) { | 
|  | 5232 | bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr, | 
|  | 5233 | ssid, ssid_len); | 
|  | 5234 | } else { | 
|  | 5235 | bss = wpa_bss_get_bssid_latest( | 
|  | 5236 | wpa_s, wpa_s->pending_join_iface_addr); | 
|  | 5237 | } | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5238 | if (bss) { | 
|  | 5239 | res.freq = bss->freq; | 
|  | 5240 | res.ssid_len = bss->ssid_len; | 
|  | 5241 | os_memcpy(res.ssid, bss->ssid, bss->ssid_len); | 
|  | 5242 | wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)", | 
|  | 5243 | bss->freq, | 
|  | 5244 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 5245 | } else if (ssid && ssid_len) { | 
|  | 5246 | res.ssid_len = ssid_len; | 
|  | 5247 | os_memcpy(res.ssid, ssid, ssid_len); | 
|  | 5248 | wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)", | 
|  | 5249 | wpa_ssid_txt(ssid, ssid_len)); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5250 | } | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5251 | } | 
|  | 5252 |  | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5253 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { | 
|  | 5254 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to " | 
|  | 5255 | "starting client"); | 
|  | 5256 | wpa_drv_cancel_remain_on_channel(wpa_s); | 
|  | 5257 | wpa_s->off_channel_freq = 0; | 
|  | 5258 | wpa_s->roc_waiting_drv_freq = 0; | 
|  | 5259 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5260 | wpas_start_wps_enrollee(group, &res); | 
|  | 5261 |  | 
|  | 5262 | /* | 
|  | 5263 | * Allow a longer timeout for join-a-running-group than normal 15 | 
|  | 5264 | * second group formation timeout since the GO may not have authorized | 
|  | 5265 | * our connection yet. | 
|  | 5266 | */ | 
|  | 5267 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); | 
|  | 5268 | eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout, | 
|  | 5269 | wpa_s, NULL); | 
|  | 5270 |  | 
|  | 5271 | return 0; | 
|  | 5272 | } | 
|  | 5273 |  | 
|  | 5274 |  | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5275 | static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5276 | int *force_freq, int *pref_freq, int go, | 
|  | 5277 | unsigned int *pref_freq_list, | 
|  | 5278 | unsigned int *num_pref_freq) | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5279 | { | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5280 | struct wpa_used_freq_data *freqs; | 
|  | 5281 | int res, best_freq, num_unused; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5282 | unsigned int freq_in_use = 0, num, i, max_pref_freq; | 
|  | 5283 |  | 
|  | 5284 | max_pref_freq = *num_pref_freq; | 
|  | 5285 | *num_pref_freq = 0; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5286 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5287 | freqs = os_calloc(wpa_s->num_multichan_concurrent, | 
|  | 5288 | sizeof(struct wpa_used_freq_data)); | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5289 | if (!freqs) | 
|  | 5290 | return -1; | 
|  | 5291 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5292 | num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, | 
|  | 5293 | wpa_s->num_multichan_concurrent); | 
|  | 5294 |  | 
|  | 5295 | /* | 
|  | 5296 | * It is possible that the total number of used frequencies is bigger | 
|  | 5297 | * than the number of frequencies used for P2P, so get the system wide | 
|  | 5298 | * number of unused frequencies. | 
|  | 5299 | */ | 
|  | 5300 | num_unused = wpas_p2p_num_unused_channels(wpa_s); | 
|  | 5301 |  | 
| Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 5302 | wpa_printf(MSG_DEBUG, | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5303 | "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d", | 
|  | 5304 | freq, wpa_s->num_multichan_concurrent, num, num_unused); | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5305 |  | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5306 | if (freq > 0) { | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5307 | int ret; | 
|  | 5308 | if (go) | 
|  | 5309 | ret = p2p_supported_freq(wpa_s->global->p2p, freq); | 
|  | 5310 | else | 
|  | 5311 | ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq); | 
|  | 5312 | if (!ret) { | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5313 | if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 5314 | ieee80211_is_dfs(freq, wpa_s->hw.modes, | 
|  | 5315 | wpa_s->hw.num_modes)) { | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5316 | /* | 
|  | 5317 | * If freq is a DFS channel and DFS is offloaded | 
|  | 5318 | * to the driver, allow P2P GO to use it. | 
|  | 5319 | */ | 
|  | 5320 | wpa_printf(MSG_DEBUG, | 
|  | 5321 | "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver", | 
|  | 5322 | freq); | 
|  | 5323 | } else { | 
|  | 5324 | wpa_printf(MSG_DEBUG, | 
|  | 5325 | "P2P: The forced channel (%u MHz) is not supported for P2P uses", | 
|  | 5326 | freq); | 
|  | 5327 | res = -3; | 
|  | 5328 | goto exit_free; | 
|  | 5329 | } | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5330 | } | 
|  | 5331 |  | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5332 | for (i = 0; i < num; i++) { | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5333 | if (freqs[i].freq == freq) | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5334 | freq_in_use = 1; | 
|  | 5335 | } | 
|  | 5336 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5337 | if (num_unused <= 0 && !freq_in_use) { | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5338 | wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels", | 
|  | 5339 | freq); | 
|  | 5340 | res = -2; | 
|  | 5341 | goto exit_free; | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5342 | } | 
|  | 5343 | wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " | 
|  | 5344 | "requested channel (%u MHz)", freq); | 
|  | 5345 | *force_freq = freq; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5346 | goto exit_ok; | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5347 | } | 
|  | 5348 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5349 | best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num); | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5350 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5351 | if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) { | 
|  | 5352 | enum wpa_driver_if_type iface_type; | 
|  | 5353 |  | 
|  | 5354 | if (go) | 
|  | 5355 | iface_type = WPA_IF_P2P_GO; | 
|  | 5356 | else | 
|  | 5357 | iface_type = WPA_IF_P2P_CLIENT; | 
|  | 5358 |  | 
|  | 5359 | wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d", | 
|  | 5360 | best_freq, go); | 
|  | 5361 |  | 
|  | 5362 | res = wpa_drv_get_pref_freq_list(wpa_s, iface_type, | 
|  | 5363 | &max_pref_freq, | 
|  | 5364 | pref_freq_list); | 
|  | 5365 | if (!res && max_pref_freq > 0) { | 
|  | 5366 | *num_pref_freq = max_pref_freq; | 
|  | 5367 | i = 0; | 
| Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 5368 | while (i < *num_pref_freq && | 
|  | 5369 | (!p2p_supported_freq(wpa_s->global->p2p, | 
| Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 5370 | pref_freq_list[i]) || | 
|  | 5371 | wpas_p2p_disallowed_freq(wpa_s->global, | 
| Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 5372 | pref_freq_list[i]))) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5373 | wpa_printf(MSG_DEBUG, | 
|  | 5374 | "P2P: preferred_freq_list[%d]=%d is disallowed", | 
|  | 5375 | i, pref_freq_list[i]); | 
|  | 5376 | i++; | 
|  | 5377 | } | 
|  | 5378 | if (i != *num_pref_freq) { | 
|  | 5379 | best_freq = pref_freq_list[i]; | 
|  | 5380 | wpa_printf(MSG_DEBUG, | 
|  | 5381 | "P2P: Using preferred_freq_list[%d]=%d", | 
|  | 5382 | i, best_freq); | 
|  | 5383 | } else { | 
|  | 5384 | wpa_printf(MSG_DEBUG, | 
|  | 5385 | "P2P: All driver preferred frequencies are disallowed for P2P use"); | 
|  | 5386 | *num_pref_freq = 0; | 
|  | 5387 | } | 
|  | 5388 | } else { | 
|  | 5389 | wpa_printf(MSG_DEBUG, | 
|  | 5390 | "P2P: No preferred frequency list available"); | 
|  | 5391 | } | 
|  | 5392 | } | 
|  | 5393 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5394 | /* We have a candidate frequency to use */ | 
|  | 5395 | if (best_freq > 0) { | 
|  | 5396 | if (*pref_freq == 0 && num_unused > 0) { | 
| Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 5397 | wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using", | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5398 | best_freq); | 
|  | 5399 | *pref_freq = best_freq; | 
| Dmitry Shmidt | 51a47d5 | 2013-09-10 10:52:57 -0700 | [diff] [blame] | 5400 | } else { | 
|  | 5401 | wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use", | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5402 | best_freq); | 
|  | 5403 | *force_freq = best_freq; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5404 | } | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5405 | } else if (num_unused > 0) { | 
|  | 5406 | wpa_printf(MSG_DEBUG, | 
|  | 5407 | "P2P: Current operating channels are not available for P2P. Try to use another channel"); | 
|  | 5408 | *force_freq = 0; | 
|  | 5409 | } else { | 
|  | 5410 | wpa_printf(MSG_DEBUG, | 
|  | 5411 | "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group"); | 
|  | 5412 | res = -2; | 
|  | 5413 | goto exit_free; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5414 | } | 
|  | 5415 |  | 
|  | 5416 | exit_ok: | 
|  | 5417 | res = 0; | 
|  | 5418 | exit_free: | 
|  | 5419 | os_free(freqs); | 
|  | 5420 | return res; | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5421 | } | 
|  | 5422 |  | 
|  | 5423 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5424 | /** | 
|  | 5425 | * wpas_p2p_connect - Request P2P Group Formation to be started | 
|  | 5426 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | 
|  | 5427 | * @peer_addr: Address of the peer P2P Device | 
|  | 5428 | * @pin: PIN to use during provisioning or %NULL to indicate PBC mode | 
|  | 5429 | * @persistent_group: Whether to create a persistent group | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5430 | * @auto_join: Whether to select join vs. GO Negotiation automatically | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5431 | * @join: Whether to join an existing group (as a client) instead of starting | 
|  | 5432 | *	Group Owner negotiation; @peer_addr is BSSID in that case | 
|  | 5433 | * @auth: Whether to only authorize the connection instead of doing that and | 
|  | 5434 | *	initiating Group Owner negotiation | 
|  | 5435 | * @go_intent: GO Intent or -1 to use default | 
|  | 5436 | * @freq: Frequency for the group or 0 for auto-selection | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 5437 | * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5438 | * @persistent_id: Persistent group credentials to use for forcing GO | 
|  | 5439 | *	parameters or -1 to generate new values (SSID/passphrase) | 
|  | 5440 | * @pd: Whether to send Provision Discovery prior to GO Negotiation as an | 
|  | 5441 | *	interoperability workaround when initiating group formation | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5442 | * @ht40: Start GO with 40 MHz channel width | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5443 | * @vht:  Start GO with VHT support | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 5444 | * @vht_chwidth: Channel width supported by GO operating with VHT support | 
|  | 5445 | *	(VHT_CHANWIDTH_*). | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 5446 | * @group_ssid: Specific Group SSID for join or %NULL if not set | 
|  | 5447 | * @group_ssid_len: Length of @group_ssid in octets | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5448 | * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified | 
|  | 5449 | *	failure, -2 on failure due to channel not currently available, | 
|  | 5450 | *	-3 if forced channel is not supported | 
|  | 5451 | */ | 
|  | 5452 | int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr, | 
|  | 5453 | const char *pin, enum p2p_wps_method wps_method, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5454 | int persistent_group, int auto_join, int join, int auth, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 5455 | int go_intent, int freq, unsigned int vht_center_freq2, | 
|  | 5456 | int persistent_id, int pd, int ht40, int vht, | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 5457 | unsigned int vht_chwidth, const u8 *group_ssid, | 
|  | 5458 | size_t group_ssid_len) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5459 | { | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5460 | int force_freq = 0, pref_freq = 0; | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5461 | int ret = 0, res; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5462 | enum wpa_driver_if_type iftype; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5463 | const u8 *if_addr; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5464 | struct wpa_ssid *ssid = NULL; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5465 | unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5466 |  | 
|  | 5467 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 5468 | return -1; | 
|  | 5469 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5470 | if (persistent_id >= 0) { | 
|  | 5471 | ssid = wpa_config_get_network(wpa_s->conf, persistent_id); | 
|  | 5472 | if (ssid == NULL || ssid->disabled != 2 || | 
|  | 5473 | ssid->mode != WPAS_MODE_P2P_GO) | 
|  | 5474 | return -1; | 
|  | 5475 | } | 
|  | 5476 |  | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5477 | os_free(wpa_s->global->add_psk); | 
|  | 5478 | wpa_s->global->add_psk = NULL; | 
|  | 5479 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5480 | wpa_s->global->p2p_fail_on_wps_complete = 0; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5481 | wpa_s->global->pending_p2ps_group = 0; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5482 | wpa_s->global->pending_p2ps_group_freq = 0; | 
|  | 5483 | wpa_s->p2ps_method_config_any = 0; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5484 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5485 | if (go_intent < 0) | 
|  | 5486 | go_intent = wpa_s->conf->p2p_go_intent; | 
|  | 5487 |  | 
|  | 5488 | if (!auth) | 
|  | 5489 | wpa_s->p2p_long_listen = 0; | 
|  | 5490 |  | 
|  | 5491 | wpa_s->p2p_wps_method = wps_method; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5492 | wpa_s->p2p_persistent_group = !!persistent_group; | 
|  | 5493 | wpa_s->p2p_persistent_id = persistent_id; | 
|  | 5494 | wpa_s->p2p_go_intent = go_intent; | 
|  | 5495 | wpa_s->p2p_connect_freq = freq; | 
|  | 5496 | wpa_s->p2p_fallback_to_go_neg = 0; | 
|  | 5497 | wpa_s->p2p_pd_before_go_neg = !!pd; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5498 | wpa_s->p2p_go_ht40 = !!ht40; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5499 | wpa_s->p2p_go_vht = !!vht; | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 5500 | wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2; | 
|  | 5501 | wpa_s->p2p_go_max_oper_chwidth = vht_chwidth; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5502 |  | 
|  | 5503 | if (pin) | 
|  | 5504 | os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin)); | 
|  | 5505 | else if (wps_method == WPS_PIN_DISPLAY) { | 
| Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 5506 | if (wps_generate_pin((unsigned int *) &ret) < 0) | 
|  | 5507 | return -1; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5508 | res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), | 
|  | 5509 | "%08d", ret); | 
|  | 5510 | if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res)) | 
|  | 5511 | wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0'; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5512 | wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s", | 
|  | 5513 | wpa_s->p2p_pin); | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 5514 | } else if (wps_method == WPS_P2PS) { | 
|  | 5515 | /* Force the P2Ps default PIN to be used */ | 
|  | 5516 | os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin)); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5517 | } else | 
|  | 5518 | wpa_s->p2p_pin[0] = '\0'; | 
|  | 5519 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5520 | if (join || auto_join) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5521 | u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN]; | 
|  | 5522 | if (auth) { | 
|  | 5523 | wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to " | 
|  | 5524 | "connect a running group from " MACSTR, | 
|  | 5525 | MAC2STR(peer_addr)); | 
|  | 5526 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); | 
|  | 5527 | return ret; | 
|  | 5528 | } | 
|  | 5529 | os_memcpy(dev_addr, peer_addr, ETH_ALEN); | 
|  | 5530 | if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr, | 
|  | 5531 | iface_addr) < 0) { | 
|  | 5532 | os_memcpy(iface_addr, peer_addr, ETH_ALEN); | 
|  | 5533 | p2p_get_dev_addr(wpa_s->global->p2p, peer_addr, | 
|  | 5534 | dev_addr); | 
|  | 5535 | } | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5536 | if (auto_join) { | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5537 | os_get_reltime(&wpa_s->p2p_auto_started); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5538 | wpa_printf(MSG_DEBUG, "P2P: Auto join started at " | 
|  | 5539 | "%ld.%06ld", | 
|  | 5540 | wpa_s->p2p_auto_started.sec, | 
|  | 5541 | wpa_s->p2p_auto_started.usec); | 
|  | 5542 | } | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5543 | wpa_s->user_initiated_pd = 1; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5544 | if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method, | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 5545 | auto_join, freq, | 
|  | 5546 | group_ssid, group_ssid_len) < 0) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5547 | return -1; | 
|  | 5548 | return ret; | 
|  | 5549 | } | 
|  | 5550 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5551 | size = P2P_MAX_PREF_CHANNELS; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5552 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5553 | go_intent == 15, pref_freq_list, &size); | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5554 | if (res) | 
|  | 5555 | return res; | 
| Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 5556 | wpas_p2p_set_own_freq_preference(wpa_s, | 
|  | 5557 | force_freq ? force_freq : pref_freq); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5558 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5559 | p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size); | 
|  | 5560 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5561 | wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); | 
|  | 5562 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5563 | if (wpa_s->create_p2p_iface) { | 
|  | 5564 | /* Prepare to add a new interface for the group */ | 
|  | 5565 | iftype = WPA_IF_P2P_GROUP; | 
|  | 5566 | if (go_intent == 15) | 
|  | 5567 | iftype = WPA_IF_P2P_GO; | 
|  | 5568 | if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { | 
|  | 5569 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " | 
|  | 5570 | "interface for the group"); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5571 | return -1; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5572 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5573 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5574 | if_addr = wpa_s->pending_interface_addr; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5575 | } else { | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 5576 | if (wpa_s->p2p_mgmt) | 
|  | 5577 | if_addr = wpa_s->parent->own_addr; | 
|  | 5578 | else | 
|  | 5579 | if_addr = wpa_s->own_addr; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5580 | os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN); | 
|  | 5581 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5582 |  | 
|  | 5583 | if (auth) { | 
|  | 5584 | if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method, | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5585 | go_intent, if_addr, | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5586 | force_freq, persistent_group, ssid, | 
|  | 5587 | pref_freq) < 0) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5588 | return -1; | 
|  | 5589 | return ret; | 
|  | 5590 | } | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5591 |  | 
|  | 5592 | if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, | 
|  | 5593 | go_intent, if_addr, force_freq, | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5594 | persistent_group, ssid, pref_freq) < 0) { | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5595 | if (wpa_s->create_p2p_iface) | 
|  | 5596 | wpas_p2p_remove_pending_group_interface(wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5597 | return -1; | 
|  | 5598 | } | 
|  | 5599 | return ret; | 
|  | 5600 | } | 
|  | 5601 |  | 
|  | 5602 |  | 
|  | 5603 | /** | 
|  | 5604 | * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start | 
|  | 5605 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | 
|  | 5606 | * @freq: Frequency of the channel in MHz | 
|  | 5607 | * @duration: Duration of the stay on the channel in milliseconds | 
|  | 5608 | * | 
|  | 5609 | * This callback is called when the driver indicates that it has started the | 
|  | 5610 | * requested remain-on-channel duration. | 
|  | 5611 | */ | 
|  | 5612 | void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s, | 
|  | 5613 | unsigned int freq, unsigned int duration) | 
|  | 5614 | { | 
|  | 5615 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 5616 | return; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5617 | wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)", | 
|  | 5618 | wpa_s->off_channel_freq, wpa_s->pending_listen_freq, | 
|  | 5619 | wpa_s->roc_waiting_drv_freq, freq, duration); | 
|  | 5620 | if (wpa_s->off_channel_freq && | 
|  | 5621 | wpa_s->off_channel_freq == wpa_s->pending_listen_freq) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5622 | p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq, | 
|  | 5623 | wpa_s->pending_listen_duration); | 
|  | 5624 | wpa_s->pending_listen_freq = 0; | 
| Dmitry Shmidt | 7f93d6f | 2014-02-21 11:22:49 -0800 | [diff] [blame] | 5625 | } else { | 
|  | 5626 | wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)", | 
|  | 5627 | wpa_s->off_channel_freq, wpa_s->pending_listen_freq, | 
|  | 5628 | freq, duration); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5629 | } | 
|  | 5630 | } | 
|  | 5631 |  | 
|  | 5632 |  | 
| Jithu Jance | 57cea1a | 2014-08-20 10:22:04 -0700 | [diff] [blame] | 5633 | int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5634 | { | 
|  | 5635 | /* Limit maximum Listen state time based on driver limitation. */ | 
|  | 5636 | if (timeout > wpa_s->max_remain_on_chan) | 
|  | 5637 | timeout = wpa_s->max_remain_on_chan; | 
|  | 5638 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5639 | return p2p_listen(wpa_s->global->p2p, timeout); | 
|  | 5640 | } | 
|  | 5641 |  | 
|  | 5642 |  | 
|  | 5643 | /** | 
|  | 5644 | * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout | 
|  | 5645 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | 
|  | 5646 | * @freq: Frequency of the channel in MHz | 
|  | 5647 | * | 
|  | 5648 | * This callback is called when the driver indicates that a remain-on-channel | 
|  | 5649 | * operation has been completed, i.e., the duration on the requested channel | 
|  | 5650 | * has timed out. | 
|  | 5651 | */ | 
|  | 5652 | void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s, | 
|  | 5653 | unsigned int freq) | 
|  | 5654 | { | 
|  | 5655 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback " | 
|  | 5656 | "(p2p_long_listen=%d ms pending_action_tx=%p)", | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5657 | wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s)); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5658 | wpas_p2p_listen_work_done(wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5659 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 5660 | return; | 
| Jithu Jance | 57cea1a | 2014-08-20 10:22:04 -0700 | [diff] [blame] | 5661 | if (wpa_s->p2p_long_listen > 0) | 
|  | 5662 | wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5663 | if (p2p_listen_end(wpa_s->global->p2p, freq) > 0) | 
|  | 5664 | return; /* P2P module started a new operation */ | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5665 | if (offchannel_pending_action_tx(wpa_s)) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5666 | return; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5667 | if (wpa_s->p2p_long_listen > 0) { | 
|  | 5668 | wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state"); | 
|  | 5669 | wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen); | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5670 | } else { | 
|  | 5671 | /* | 
|  | 5672 | * When listen duration is over, stop listen & update p2p_state | 
|  | 5673 | * to IDLE. | 
|  | 5674 | */ | 
|  | 5675 | p2p_stop_listen(wpa_s->global->p2p); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5676 | } | 
|  | 5677 | } | 
|  | 5678 |  | 
|  | 5679 |  | 
|  | 5680 | /** | 
|  | 5681 | * wpas_p2p_group_remove - Remove a P2P group | 
|  | 5682 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | 
|  | 5683 | * @ifname: Network interface name of the group interface or "*" to remove all | 
|  | 5684 | *	groups | 
|  | 5685 | * Returns: 0 on success, -1 on failure | 
|  | 5686 | * | 
|  | 5687 | * This function is used to remove a P2P group. This can be used to disconnect | 
|  | 5688 | * from a group in which the local end is a P2P Client or to end a P2P Group in | 
|  | 5689 | * case the local end is the Group Owner. If a virtual network interface was | 
|  | 5690 | * created for this group, that interface will be removed. Otherwise, only the | 
|  | 5691 | * configured P2P group network will be removed from the interface. | 
|  | 5692 | */ | 
|  | 5693 | int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname) | 
|  | 5694 | { | 
|  | 5695 | struct wpa_global *global = wpa_s->global; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5696 | struct wpa_supplicant *calling_wpa_s = wpa_s; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5697 |  | 
|  | 5698 | if (os_strcmp(ifname, "*") == 0) { | 
|  | 5699 | struct wpa_supplicant *prev; | 
|  | 5700 | wpa_s = global->ifaces; | 
|  | 5701 | while (wpa_s) { | 
|  | 5702 | prev = wpa_s; | 
|  | 5703 | wpa_s = wpa_s->next; | 
| Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 5704 | if (prev->p2p_group_interface != | 
|  | 5705 | NOT_P2P_GROUP_INTERFACE || | 
|  | 5706 | (prev->current_ssid && | 
|  | 5707 | prev->current_ssid->p2p_group)) | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5708 | wpas_p2p_disconnect_safely(prev, calling_wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5709 | } | 
|  | 5710 | return 0; | 
|  | 5711 | } | 
|  | 5712 |  | 
|  | 5713 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
|  | 5714 | if (os_strcmp(wpa_s->ifname, ifname) == 0) | 
|  | 5715 | break; | 
|  | 5716 | } | 
|  | 5717 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5718 | return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5719 | } | 
|  | 5720 |  | 
|  | 5721 |  | 
| Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5722 | static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq) | 
|  | 5723 | { | 
|  | 5724 | unsigned int r; | 
|  | 5725 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5726 | if (!wpa_s->conf->num_p2p_pref_chan && !freq) { | 
|  | 5727 | unsigned int i, size = P2P_MAX_PREF_CHANNELS; | 
|  | 5728 | unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS]; | 
|  | 5729 | int res; | 
|  | 5730 |  | 
|  | 5731 | res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO, | 
|  | 5732 | &size, pref_freq_list); | 
|  | 5733 | if (!res && size > 0) { | 
|  | 5734 | i = 0; | 
| Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 5735 | while (i < size && | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5736 | (!p2p_supported_freq(wpa_s->global->p2p, | 
|  | 5737 | pref_freq_list[i]) || | 
|  | 5738 | wpas_p2p_disallowed_freq(wpa_s->global, | 
|  | 5739 | pref_freq_list[i]))) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5740 | wpa_printf(MSG_DEBUG, | 
|  | 5741 | "P2P: preferred_freq_list[%d]=%d is disallowed", | 
|  | 5742 | i, pref_freq_list[i]); | 
|  | 5743 | i++; | 
|  | 5744 | } | 
|  | 5745 | if (i != size) { | 
|  | 5746 | freq = pref_freq_list[i]; | 
|  | 5747 | wpa_printf(MSG_DEBUG, | 
|  | 5748 | "P2P: Using preferred_freq_list[%d]=%d", | 
|  | 5749 | i, freq); | 
|  | 5750 | } else { | 
|  | 5751 | wpa_printf(MSG_DEBUG, | 
|  | 5752 | "P2P: All driver preferred frequencies are disallowed for P2P use"); | 
|  | 5753 | } | 
|  | 5754 | } else { | 
|  | 5755 | wpa_printf(MSG_DEBUG, | 
|  | 5756 | "P2P: No preferred frequency list available"); | 
|  | 5757 | } | 
|  | 5758 | } | 
|  | 5759 |  | 
| Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5760 | if (freq == 2) { | 
|  | 5761 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz " | 
|  | 5762 | "band"); | 
|  | 5763 | if (wpa_s->best_24_freq > 0 && | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5764 | p2p_supported_freq_go(wpa_s->global->p2p, | 
|  | 5765 | wpa_s->best_24_freq)) { | 
| Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5766 | freq = wpa_s->best_24_freq; | 
|  | 5767 | wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band " | 
|  | 5768 | "channel: %d MHz", freq); | 
|  | 5769 | } else { | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 5770 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) | 
|  | 5771 | return -1; | 
| Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5772 | freq = 2412 + (r % 3) * 25; | 
|  | 5773 | wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band " | 
|  | 5774 | "channel: %d MHz", freq); | 
|  | 5775 | } | 
|  | 5776 | } | 
|  | 5777 |  | 
|  | 5778 | if (freq == 5) { | 
|  | 5779 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz " | 
|  | 5780 | "band"); | 
|  | 5781 | if (wpa_s->best_5_freq > 0 && | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5782 | p2p_supported_freq_go(wpa_s->global->p2p, | 
| Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5783 | wpa_s->best_5_freq)) { | 
|  | 5784 | freq = wpa_s->best_5_freq; | 
|  | 5785 | wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band " | 
|  | 5786 | "channel: %d MHz", freq); | 
|  | 5787 | } else { | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 5788 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) | 
|  | 5789 | return -1; | 
| Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5790 | freq = 5180 + (r % 4) * 20; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5791 | if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) { | 
| Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5792 | wpa_printf(MSG_DEBUG, "P2P: Could not select " | 
|  | 5793 | "5 GHz channel for P2P group"); | 
|  | 5794 | return -1; | 
|  | 5795 | } | 
|  | 5796 | wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band " | 
|  | 5797 | "channel: %d MHz", freq); | 
|  | 5798 | } | 
|  | 5799 | } | 
|  | 5800 |  | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5801 | if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) { | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5802 | if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 5803 | ieee80211_is_dfs(freq, wpa_s->hw.modes, | 
|  | 5804 | wpa_s->hw.num_modes)) { | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5805 | /* | 
|  | 5806 | * If freq is a DFS channel and DFS is offloaded to the | 
|  | 5807 | * driver, allow P2P GO to use it. | 
|  | 5808 | */ | 
|  | 5809 | wpa_printf(MSG_DEBUG, "P2P: " | 
|  | 5810 | "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded", | 
|  | 5811 | __func__, freq); | 
|  | 5812 | return freq; | 
|  | 5813 | } | 
| Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5814 | wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO " | 
|  | 5815 | "(%u MHz) is not supported for P2P uses", | 
|  | 5816 | freq); | 
|  | 5817 | return -1; | 
|  | 5818 | } | 
|  | 5819 |  | 
|  | 5820 | return freq; | 
|  | 5821 | } | 
|  | 5822 |  | 
|  | 5823 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5824 | static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s, | 
|  | 5825 | const struct p2p_channels *channels, | 
|  | 5826 | int freq) | 
|  | 5827 | { | 
|  | 5828 | if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) && | 
|  | 5829 | p2p_supported_freq_go(wpa_s->global->p2p, freq) && | 
|  | 5830 | freq_included(wpa_s, channels, freq)) | 
|  | 5831 | return 1; | 
|  | 5832 | return 0; | 
|  | 5833 | } | 
|  | 5834 |  | 
|  | 5835 |  | 
|  | 5836 | static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s, | 
|  | 5837 | struct p2p_go_neg_results *params, | 
|  | 5838 | const struct p2p_channels *channels) | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5839 | { | 
|  | 5840 | unsigned int i, r; | 
|  | 5841 |  | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5842 | /* try all channels in operating class 115 */ | 
|  | 5843 | for (i = 0; i < 4; i++) { | 
|  | 5844 | params->freq = 5180 + i * 20; | 
|  | 5845 | if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5846 | freq_included(wpa_s, channels, params->freq) && | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5847 | p2p_supported_freq(wpa_s->global->p2p, params->freq)) | 
|  | 5848 | goto out; | 
|  | 5849 | } | 
|  | 5850 |  | 
|  | 5851 | /* try all channels in operating class 124 */ | 
|  | 5852 | for (i = 0; i < 4; i++) { | 
|  | 5853 | params->freq = 5745 + i * 20; | 
|  | 5854 | if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5855 | freq_included(wpa_s, channels, params->freq) && | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5856 | p2p_supported_freq(wpa_s->global->p2p, params->freq)) | 
|  | 5857 | goto out; | 
|  | 5858 | } | 
|  | 5859 |  | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 5860 | /* try social channel class 180 channel 2 */ | 
|  | 5861 | params->freq = 58320 + 1 * 2160; | 
|  | 5862 | if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5863 | freq_included(wpa_s, channels, params->freq) && | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 5864 | p2p_supported_freq(wpa_s->global->p2p, params->freq)) | 
|  | 5865 | goto out; | 
|  | 5866 |  | 
|  | 5867 | /* try all channels in reg. class 180 */ | 
|  | 5868 | for (i = 0; i < 4; i++) { | 
|  | 5869 | params->freq = 58320 + i * 2160; | 
|  | 5870 | if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5871 | freq_included(wpa_s, channels, params->freq) && | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 5872 | p2p_supported_freq(wpa_s->global->p2p, params->freq)) | 
|  | 5873 | goto out; | 
|  | 5874 | } | 
|  | 5875 |  | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5876 | /* try some random selection of the social channels */ | 
|  | 5877 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) | 
|  | 5878 | return; | 
|  | 5879 |  | 
|  | 5880 | for (i = 0; i < 3; i++) { | 
|  | 5881 | params->freq = 2412 + ((r + i) % 3) * 25; | 
|  | 5882 | if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq)) | 
|  | 5883 | goto out; | 
|  | 5884 | } | 
|  | 5885 |  | 
|  | 5886 | /* try all other channels in operating class 81 */ | 
|  | 5887 | for (i = 0; i < 11; i++) { | 
|  | 5888 | params->freq = 2412 + i * 5; | 
|  | 5889 |  | 
|  | 5890 | /* skip social channels; covered in the previous loop */ | 
|  | 5891 | if (params->freq == 2412 || | 
|  | 5892 | params->freq == 2437 || | 
|  | 5893 | params->freq == 2462) | 
|  | 5894 | continue; | 
|  | 5895 |  | 
|  | 5896 | if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq)) | 
|  | 5897 | goto out; | 
|  | 5898 | } | 
|  | 5899 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5900 | params->freq = 0; | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5901 | wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed"); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5902 | return; | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5903 | out: | 
|  | 5904 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)", | 
|  | 5905 | params->freq); | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5906 | } | 
|  | 5907 |  | 
|  | 5908 |  | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 5909 | static int wpas_same_band(int freq1, int freq2) | 
|  | 5910 | { | 
|  | 5911 | enum hostapd_hw_mode mode1, mode2; | 
|  | 5912 | u8 chan1, chan2; | 
|  | 5913 |  | 
|  | 5914 | mode1 = ieee80211_freq_to_chan(freq1, &chan1); | 
|  | 5915 | mode2 = ieee80211_freq_to_chan(freq2, &chan2); | 
|  | 5916 | if (mode1 == NUM_HOSTAPD_MODES) | 
|  | 5917 | return 0; | 
|  | 5918 | return mode1 == mode2; | 
|  | 5919 | } | 
|  | 5920 |  | 
|  | 5921 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5922 | static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s, | 
|  | 5923 | struct p2p_go_neg_results *params, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 5924 | int freq, int vht_center_freq2, int ht40, | 
|  | 5925 | int vht, int max_oper_chwidth, | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5926 | const struct p2p_channels *channels) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5927 | { | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5928 | struct wpa_used_freq_data *freqs; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5929 | unsigned int cand; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5930 | unsigned int num, i; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5931 | int ignore_no_freqs = 0; | 
| Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 5932 | int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5933 |  | 
|  | 5934 | os_memset(params, 0, sizeof(*params)); | 
|  | 5935 | params->role_go = 1; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5936 | params->ht40 = ht40; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5937 | params->vht = vht; | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 5938 | params->max_oper_chwidth = max_oper_chwidth; | 
|  | 5939 | params->vht_center_freq2 = vht_center_freq2; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5940 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5941 | freqs = os_calloc(wpa_s->num_multichan_concurrent, | 
|  | 5942 | sizeof(struct wpa_used_freq_data)); | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5943 | if (!freqs) | 
|  | 5944 | return -1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5945 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5946 | num = get_shared_radio_freqs_data(wpa_s, freqs, | 
|  | 5947 | wpa_s->num_multichan_concurrent); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5948 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5949 | if (wpa_s->current_ssid && | 
|  | 5950 | wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO && | 
|  | 5951 | wpa_s->wpa_state == WPA_COMPLETED) { | 
|  | 5952 | wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO", | 
|  | 5953 | __func__); | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5954 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5955 | /* | 
|  | 5956 | * If the frequency selection is done for an active P2P GO that | 
|  | 5957 | * is not sharing a frequency, allow to select a new frequency | 
|  | 5958 | * even if there are no unused frequencies as we are about to | 
|  | 5959 | * move the P2P GO so its frequency can be re-used. | 
|  | 5960 | */ | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5961 | for (i = 0; i < num; i++) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5962 | if (freqs[i].freq == wpa_s->current_ssid->frequency && | 
|  | 5963 | freqs[i].flags == 0) { | 
|  | 5964 | ignore_no_freqs = 1; | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5965 | break; | 
|  | 5966 | } | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5967 | } | 
| Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 5968 | } | 
|  | 5969 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5970 | /* try using the forced freq */ | 
|  | 5971 | if (freq) { | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5972 | if (wpas_p2p_disallowed_freq(wpa_s->global, freq) || | 
|  | 5973 | !freq_included(wpa_s, channels, freq)) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5974 | wpa_printf(MSG_DEBUG, | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5975 | "P2P: Forced GO freq %d MHz disallowed", | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5976 | freq); | 
|  | 5977 | goto fail; | 
|  | 5978 | } | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5979 | if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) { | 
|  | 5980 | if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 5981 | ieee80211_is_dfs(freq, wpa_s->hw.modes, | 
|  | 5982 | wpa_s->hw.num_modes)) { | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5983 | /* | 
|  | 5984 | * If freq is a DFS channel and DFS is offloaded | 
|  | 5985 | * to the driver, allow P2P GO to use it. | 
|  | 5986 | */ | 
|  | 5987 | wpa_printf(MSG_DEBUG, | 
|  | 5988 | "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded", | 
|  | 5989 | __func__, freq); | 
|  | 5990 | } else { | 
|  | 5991 | wpa_printf(MSG_DEBUG, | 
|  | 5992 | "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses", | 
|  | 5993 | freq); | 
|  | 5994 | goto fail; | 
|  | 5995 | } | 
|  | 5996 | } | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5997 |  | 
|  | 5998 | for (i = 0; i < num; i++) { | 
|  | 5999 | if (freqs[i].freq == freq) { | 
|  | 6000 | wpa_printf(MSG_DEBUG, | 
|  | 6001 | "P2P: forced freq (%d MHz) is also shared", | 
|  | 6002 | freq); | 
|  | 6003 | params->freq = freq; | 
|  | 6004 | goto success; | 
|  | 6005 | } | 
|  | 6006 | } | 
|  | 6007 |  | 
| Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 6008 | if (!ignore_no_freqs && !unused_channels) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6009 | wpa_printf(MSG_DEBUG, | 
|  | 6010 | "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use", | 
|  | 6011 | freq); | 
|  | 6012 | goto fail; | 
|  | 6013 | } | 
|  | 6014 |  | 
|  | 6015 | wpa_printf(MSG_DEBUG, | 
|  | 6016 | "P2P: force GO freq (%d MHz) on a free channel", | 
|  | 6017 | freq); | 
|  | 6018 | params->freq = freq; | 
|  | 6019 | goto success; | 
|  | 6020 | } | 
|  | 6021 |  | 
|  | 6022 | /* consider using one of the shared frequencies */ | 
| Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 6023 | if (num && | 
|  | 6024 | (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6025 | cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num); | 
|  | 6026 | if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) { | 
|  | 6027 | wpa_printf(MSG_DEBUG, | 
|  | 6028 | "P2P: Use shared freq (%d MHz) for GO", | 
| Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 6029 | cand); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6030 | params->freq = cand; | 
|  | 6031 | goto success; | 
|  | 6032 | } | 
|  | 6033 |  | 
|  | 6034 | /* try using one of the shared freqs */ | 
|  | 6035 | for (i = 0; i < num; i++) { | 
|  | 6036 | if (wpas_p2p_supported_freq_go(wpa_s, channels, | 
|  | 6037 | freqs[i].freq)) { | 
|  | 6038 | wpa_printf(MSG_DEBUG, | 
|  | 6039 | "P2P: Use shared freq (%d MHz) for GO", | 
| Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 6040 | freqs[i].freq); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6041 | params->freq = freqs[i].freq; | 
|  | 6042 | goto success; | 
|  | 6043 | } | 
|  | 6044 | } | 
|  | 6045 | } | 
|  | 6046 |  | 
| Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 6047 | if (!ignore_no_freqs && !unused_channels) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6048 | wpa_printf(MSG_DEBUG, | 
|  | 6049 | "P2P: Cannot force GO on any of the channels we are already using"); | 
|  | 6050 | goto fail; | 
|  | 6051 | } | 
|  | 6052 |  | 
|  | 6053 | /* try using the setting from the configuration file */ | 
|  | 6054 | if (wpa_s->conf->p2p_oper_reg_class == 81 && | 
|  | 6055 | wpa_s->conf->p2p_oper_channel >= 1 && | 
|  | 6056 | wpa_s->conf->p2p_oper_channel <= 11 && | 
|  | 6057 | wpas_p2p_supported_freq_go( | 
|  | 6058 | wpa_s, channels, | 
|  | 6059 | 2407 + 5 * wpa_s->conf->p2p_oper_channel)) { | 
|  | 6060 | params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel; | 
|  | 6061 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " | 
|  | 6062 | "frequency %d MHz", params->freq); | 
|  | 6063 | goto success; | 
|  | 6064 | } | 
|  | 6065 |  | 
|  | 6066 | if ((wpa_s->conf->p2p_oper_reg_class == 115 || | 
|  | 6067 | wpa_s->conf->p2p_oper_reg_class == 116 || | 
|  | 6068 | wpa_s->conf->p2p_oper_reg_class == 117 || | 
|  | 6069 | wpa_s->conf->p2p_oper_reg_class == 124 || | 
|  | 6070 | wpa_s->conf->p2p_oper_reg_class == 125 || | 
|  | 6071 | wpa_s->conf->p2p_oper_reg_class == 126 || | 
|  | 6072 | wpa_s->conf->p2p_oper_reg_class == 127) && | 
|  | 6073 | wpas_p2p_supported_freq_go(wpa_s, channels, | 
|  | 6074 | 5000 + | 
|  | 6075 | 5 * wpa_s->conf->p2p_oper_channel)) { | 
|  | 6076 | params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel; | 
|  | 6077 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " | 
|  | 6078 | "frequency %d MHz", params->freq); | 
|  | 6079 | goto success; | 
|  | 6080 | } | 
|  | 6081 |  | 
|  | 6082 | /* Try using best channels */ | 
|  | 6083 | if (wpa_s->conf->p2p_oper_channel == 0 && | 
|  | 6084 | wpa_s->best_overall_freq > 0 && | 
|  | 6085 | wpas_p2p_supported_freq_go(wpa_s, channels, | 
|  | 6086 | wpa_s->best_overall_freq)) { | 
|  | 6087 | params->freq = wpa_s->best_overall_freq; | 
|  | 6088 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall " | 
|  | 6089 | "channel %d MHz", params->freq); | 
|  | 6090 | goto success; | 
|  | 6091 | } | 
|  | 6092 |  | 
|  | 6093 | if (wpa_s->conf->p2p_oper_channel == 0 && | 
|  | 6094 | wpa_s->best_24_freq > 0 && | 
|  | 6095 | wpas_p2p_supported_freq_go(wpa_s, channels, | 
|  | 6096 | wpa_s->best_24_freq)) { | 
|  | 6097 | params->freq = wpa_s->best_24_freq; | 
|  | 6098 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz " | 
|  | 6099 | "channel %d MHz", params->freq); | 
|  | 6100 | goto success; | 
|  | 6101 | } | 
|  | 6102 |  | 
|  | 6103 | if (wpa_s->conf->p2p_oper_channel == 0 && | 
|  | 6104 | wpa_s->best_5_freq > 0 && | 
|  | 6105 | wpas_p2p_supported_freq_go(wpa_s, channels, | 
|  | 6106 | wpa_s->best_5_freq)) { | 
|  | 6107 | params->freq = wpa_s->best_5_freq; | 
|  | 6108 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz " | 
|  | 6109 | "channel %d MHz", params->freq); | 
|  | 6110 | goto success; | 
|  | 6111 | } | 
|  | 6112 |  | 
|  | 6113 | /* try using preferred channels */ | 
|  | 6114 | cand = p2p_get_pref_freq(wpa_s->global->p2p, channels); | 
|  | 6115 | if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) { | 
|  | 6116 | params->freq = cand; | 
|  | 6117 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred " | 
|  | 6118 | "channels", params->freq); | 
|  | 6119 | goto success; | 
|  | 6120 | } | 
|  | 6121 |  | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 6122 | /* Try using a channel that allows VHT to be used with 80 MHz */ | 
|  | 6123 | if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) { | 
|  | 6124 | for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) { | 
|  | 6125 | enum hostapd_hw_mode mode; | 
|  | 6126 | struct hostapd_hw_modes *hwmode; | 
|  | 6127 | u8 chan; | 
|  | 6128 |  | 
|  | 6129 | cand = wpa_s->p2p_group_common_freqs[i]; | 
|  | 6130 | mode = ieee80211_freq_to_chan(cand, &chan); | 
|  | 6131 | hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, | 
|  | 6132 | mode); | 
|  | 6133 | if (!hwmode || | 
|  | 6134 | wpas_p2p_verify_channel(wpa_s, hwmode, chan, | 
|  | 6135 | BW80) != ALLOWED) | 
|  | 6136 | continue; | 
|  | 6137 | if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) { | 
|  | 6138 | params->freq = cand; | 
|  | 6139 | wpa_printf(MSG_DEBUG, | 
|  | 6140 | "P2P: Use freq %d MHz common with the peer and allowing VHT80", | 
|  | 6141 | params->freq); | 
|  | 6142 | goto success; | 
|  | 6143 | } | 
|  | 6144 | } | 
|  | 6145 | } | 
|  | 6146 |  | 
|  | 6147 | /* Try using a channel that allows HT to be used with 40 MHz on the same | 
|  | 6148 | * band so that CSA can be used */ | 
|  | 6149 | if (wpa_s->current_ssid && wpa_s->hw.modes && | 
|  | 6150 | wpa_s->p2p_group_common_freqs) { | 
|  | 6151 | for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) { | 
|  | 6152 | enum hostapd_hw_mode mode; | 
|  | 6153 | struct hostapd_hw_modes *hwmode; | 
|  | 6154 | u8 chan; | 
|  | 6155 |  | 
|  | 6156 | cand = wpa_s->p2p_group_common_freqs[i]; | 
|  | 6157 | mode = ieee80211_freq_to_chan(cand, &chan); | 
|  | 6158 | hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, | 
|  | 6159 | mode); | 
|  | 6160 | if (!wpas_same_band(wpa_s->current_ssid->frequency, | 
|  | 6161 | cand) || | 
|  | 6162 | !hwmode || | 
|  | 6163 | (wpas_p2p_verify_channel(wpa_s, hwmode, chan, | 
|  | 6164 | BW40MINUS) != ALLOWED && | 
|  | 6165 | wpas_p2p_verify_channel(wpa_s, hwmode, chan, | 
|  | 6166 | BW40PLUS) != ALLOWED)) | 
|  | 6167 | continue; | 
|  | 6168 | if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) { | 
|  | 6169 | params->freq = cand; | 
|  | 6170 | wpa_printf(MSG_DEBUG, | 
|  | 6171 | "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band", | 
|  | 6172 | params->freq); | 
|  | 6173 | goto success; | 
|  | 6174 | } | 
|  | 6175 | } | 
|  | 6176 | } | 
|  | 6177 |  | 
|  | 6178 | /* Try using one of the group common freqs on the same band so that CSA | 
|  | 6179 | * can be used */ | 
|  | 6180 | if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) { | 
|  | 6181 | for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) { | 
|  | 6182 | cand = wpa_s->p2p_group_common_freqs[i]; | 
|  | 6183 | if (!wpas_same_band(wpa_s->current_ssid->frequency, | 
|  | 6184 | cand)) | 
|  | 6185 | continue; | 
|  | 6186 | if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) { | 
|  | 6187 | params->freq = cand; | 
|  | 6188 | wpa_printf(MSG_DEBUG, | 
|  | 6189 | "P2P: Use freq %d MHz common with the peer and maintaining same band", | 
|  | 6190 | params->freq); | 
|  | 6191 | goto success; | 
|  | 6192 | } | 
|  | 6193 | } | 
|  | 6194 | } | 
|  | 6195 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6196 | /* Try using one of the group common freqs */ | 
|  | 6197 | if (wpa_s->p2p_group_common_freqs) { | 
|  | 6198 | for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) { | 
|  | 6199 | cand = wpa_s->p2p_group_common_freqs[i]; | 
|  | 6200 | if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) { | 
|  | 6201 | params->freq = cand; | 
|  | 6202 | wpa_printf(MSG_DEBUG, | 
|  | 6203 | "P2P: Use freq %d MHz common with the peer", | 
|  | 6204 | params->freq); | 
|  | 6205 | goto success; | 
|  | 6206 | } | 
|  | 6207 | } | 
|  | 6208 | } | 
|  | 6209 |  | 
|  | 6210 | /* no preference, select some channel */ | 
|  | 6211 | wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels); | 
|  | 6212 |  | 
|  | 6213 | if (params->freq == 0) { | 
|  | 6214 | wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use"); | 
|  | 6215 | goto fail; | 
|  | 6216 | } | 
|  | 6217 |  | 
|  | 6218 | success: | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 6219 | os_free(freqs); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6220 | return 0; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6221 | fail: | 
|  | 6222 | os_free(freqs); | 
|  | 6223 | return -1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6224 | } | 
|  | 6225 |  | 
|  | 6226 |  | 
|  | 6227 | static struct wpa_supplicant * | 
|  | 6228 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, | 
|  | 6229 | int go) | 
|  | 6230 | { | 
|  | 6231 | struct wpa_supplicant *group_wpa_s; | 
|  | 6232 |  | 
| Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 6233 | if (!wpas_p2p_create_iface(wpa_s)) { | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 6234 | if (wpa_s->p2p_mgmt) { | 
|  | 6235 | /* | 
|  | 6236 | * We may be called on the p2p_dev interface which | 
|  | 6237 | * cannot be used for group operations, so always use | 
|  | 6238 | * the primary interface. | 
|  | 6239 | */ | 
|  | 6240 | wpa_s->parent->p2pdev = wpa_s; | 
|  | 6241 | wpa_s = wpa_s->parent; | 
|  | 6242 | } | 
|  | 6243 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 6244 | "P2P: Use primary interface for group operations"); | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 6245 | wpa_s->p2p_first_connection_timeout = 0; | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 6246 | if (wpa_s != wpa_s->p2pdev) | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 6247 | wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6248 | return wpa_s; | 
| Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 6249 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6250 |  | 
|  | 6251 | 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] | 6252 | WPA_IF_P2P_CLIENT) < 0) { | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6253 | wpa_msg_global(wpa_s, MSG_ERROR, | 
|  | 6254 | "P2P: Failed to add group interface"); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6255 | return NULL; | 
| Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 6256 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6257 | group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go); | 
|  | 6258 | if (group_wpa_s == NULL) { | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6259 | wpa_msg_global(wpa_s, MSG_ERROR, | 
|  | 6260 | "P2P: Failed to initialize group interface"); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6261 | wpas_p2p_remove_pending_group_interface(wpa_s); | 
|  | 6262 | return NULL; | 
|  | 6263 | } | 
|  | 6264 |  | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 6265 | if (go && wpa_s->p2p_go_do_acs) { | 
|  | 6266 | group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs; | 
|  | 6267 | group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band; | 
|  | 6268 | wpa_s->p2p_go_do_acs = 0; | 
|  | 6269 | } | 
|  | 6270 |  | 
| Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 6271 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s", | 
|  | 6272 | group_wpa_s->ifname); | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 6273 | group_wpa_s->p2p_first_connection_timeout = 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6274 | return group_wpa_s; | 
|  | 6275 | } | 
|  | 6276 |  | 
|  | 6277 |  | 
|  | 6278 | /** | 
|  | 6279 | * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner | 
|  | 6280 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() | 
|  | 6281 | * @persistent_group: Whether to create a persistent group | 
|  | 6282 | * @freq: Frequency for the group or 0 to indicate no hardcoding | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 6283 | * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80 | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6284 | * @ht40: Start GO with 40 MHz channel width | 
|  | 6285 | * @vht:  Start GO with VHT support | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 6286 | * @vht_chwidth: channel bandwidth for GO operating with VHT support | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6287 | * Returns: 0 on success, -1 on failure | 
|  | 6288 | * | 
|  | 6289 | * This function creates a new P2P group with the local end as the Group Owner, | 
|  | 6290 | * i.e., without using Group Owner Negotiation. | 
|  | 6291 | */ | 
|  | 6292 | int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 6293 | int freq, int vht_center_freq2, int ht40, int vht, | 
|  | 6294 | int max_oper_chwidth) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6295 | { | 
|  | 6296 | struct p2p_go_neg_results params; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6297 |  | 
|  | 6298 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 6299 | return -1; | 
|  | 6300 |  | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6301 | os_free(wpa_s->global->add_psk); | 
|  | 6302 | wpa_s->global->add_psk = NULL; | 
|  | 6303 |  | 
| Dmitry Shmidt | dca3979 | 2011-09-06 11:17:33 -0700 | [diff] [blame] | 6304 | /* Make sure we are not running find during connection establishment */ | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6305 | wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND"); | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 6306 | wpas_p2p_stop_find_oper(wpa_s); | 
| Dmitry Shmidt | dca3979 | 2011-09-06 11:17:33 -0700 | [diff] [blame] | 6307 |  | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 6308 | if (!wpa_s->p2p_go_do_acs) { | 
|  | 6309 | freq = wpas_p2p_select_go_freq(wpa_s, freq); | 
|  | 6310 | if (freq < 0) | 
|  | 6311 | return -1; | 
|  | 6312 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6313 |  | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 6314 | if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, vht_center_freq2, | 
|  | 6315 | ht40, vht, max_oper_chwidth, NULL)) | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6316 | return -1; | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 6317 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6318 | p2p_go_params(wpa_s->global->p2p, ¶ms); | 
|  | 6319 | params.persistent_group = persistent_group; | 
|  | 6320 |  | 
|  | 6321 | wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1); | 
|  | 6322 | if (wpa_s == NULL) | 
|  | 6323 | return -1; | 
|  | 6324 | wpas_start_wps_go(wpa_s, ¶ms, 0); | 
|  | 6325 |  | 
|  | 6326 | return 0; | 
|  | 6327 | } | 
|  | 6328 |  | 
|  | 6329 |  | 
|  | 6330 | static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s, | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 6331 | struct wpa_ssid *params, int addr_allocated, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6332 | int freq, int force_scan) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6333 | { | 
|  | 6334 | struct wpa_ssid *ssid; | 
|  | 6335 |  | 
|  | 6336 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0); | 
|  | 6337 | if (wpa_s == NULL) | 
|  | 6338 | return -1; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6339 | if (force_scan) | 
|  | 6340 | os_get_reltime(&wpa_s->scan_min_time); | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6341 | wpa_s->p2p_last_4way_hs_fail = NULL; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6342 |  | 
|  | 6343 | wpa_supplicant_ap_deinit(wpa_s); | 
|  | 6344 |  | 
|  | 6345 | ssid = wpa_config_add_network(wpa_s->conf); | 
|  | 6346 | if (ssid == NULL) | 
|  | 6347 | return -1; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6348 | os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6349 | wpa_config_set_network_defaults(ssid); | 
|  | 6350 | ssid->temporary = 1; | 
|  | 6351 | ssid->proto = WPA_PROTO_RSN; | 
| Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 6352 | ssid->pbss = params->pbss; | 
|  | 6353 | ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP : | 
|  | 6354 | WPA_CIPHER_CCMP; | 
|  | 6355 | ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6356 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; | 
|  | 6357 | ssid->ssid = os_malloc(params->ssid_len); | 
|  | 6358 | if (ssid->ssid == NULL) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6359 | wpa_config_remove_network(wpa_s->conf, ssid->id); | 
|  | 6360 | return -1; | 
|  | 6361 | } | 
|  | 6362 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); | 
|  | 6363 | ssid->ssid_len = params->ssid_len; | 
|  | 6364 | ssid->p2p_group = 1; | 
|  | 6365 | ssid->export_keys = 1; | 
|  | 6366 | if (params->psk_set) { | 
|  | 6367 | os_memcpy(ssid->psk, params->psk, 32); | 
|  | 6368 | ssid->psk_set = 1; | 
|  | 6369 | } | 
|  | 6370 | if (params->passphrase) | 
|  | 6371 | ssid->passphrase = os_strdup(params->passphrase); | 
|  | 6372 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6373 | wpa_s->show_group_started = 1; | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 6374 | wpa_s->p2p_in_invitation = 1; | 
|  | 6375 | wpa_s->p2p_invite_go_freq = freq; | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 6376 | wpa_s->p2p_go_group_formation_completed = 0; | 
|  | 6377 | wpa_s->global->p2p_group_formation = wpa_s; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6378 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 6379 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev, | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 6380 | NULL); | 
|  | 6381 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, | 
|  | 6382 | wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 6383 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 6384 | wpa_supplicant_select_network(wpa_s, ssid); | 
|  | 6385 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6386 | return 0; | 
|  | 6387 | } | 
|  | 6388 |  | 
|  | 6389 |  | 
|  | 6390 | int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s, | 
|  | 6391 | struct wpa_ssid *ssid, int addr_allocated, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 6392 | int force_freq, int neg_freq, | 
|  | 6393 | int vht_center_freq2, int ht40, | 
|  | 6394 | int vht, int max_oper_chwidth, | 
|  | 6395 | const struct p2p_channels *channels, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6396 | int connection_timeout, int force_scan) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6397 | { | 
|  | 6398 | struct p2p_go_neg_results params; | 
| Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 6399 | int go = 0, freq; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6400 |  | 
|  | 6401 | if (ssid->disabled != 2 || ssid->ssid == NULL) | 
|  | 6402 | return -1; | 
|  | 6403 |  | 
|  | 6404 | if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) && | 
|  | 6405 | go == (ssid->mode == WPAS_MODE_P2P_GO)) { | 
|  | 6406 | wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is " | 
|  | 6407 | "already running"); | 
| Dmitry Shmidt | b1e5210 | 2015-05-29 12:36:29 -0700 | [diff] [blame] | 6408 | if (go == 0 && | 
|  | 6409 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 6410 | wpa_s->p2pdev, NULL)) { | 
| Dmitry Shmidt | b1e5210 | 2015-05-29 12:36:29 -0700 | [diff] [blame] | 6411 | /* | 
|  | 6412 | * This can happen if Invitation Response frame was lost | 
|  | 6413 | * and the peer (GO of a persistent group) tries to | 
|  | 6414 | * invite us again. Reschedule the timeout to avoid | 
|  | 6415 | * terminating the wait for the connection too early | 
|  | 6416 | * since we now know that the peer is still trying to | 
|  | 6417 | * invite us instead of having already started the GO. | 
|  | 6418 | */ | 
|  | 6419 | wpa_printf(MSG_DEBUG, | 
|  | 6420 | "P2P: Reschedule group formation timeout since peer is still trying to invite us"); | 
|  | 6421 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, | 
|  | 6422 | wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 6423 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | b1e5210 | 2015-05-29 12:36:29 -0700 | [diff] [blame] | 6424 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6425 | return 0; | 
|  | 6426 | } | 
|  | 6427 |  | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6428 | os_free(wpa_s->global->add_psk); | 
|  | 6429 | wpa_s->global->add_psk = NULL; | 
|  | 6430 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6431 | /* Make sure we are not running find during connection establishment */ | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 6432 | wpas_p2p_stop_find_oper(wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6433 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6434 | wpa_s->p2p_fallback_to_go_neg = 0; | 
|  | 6435 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 6436 | if (ssid->mode == WPAS_MODE_P2P_GO) { | 
|  | 6437 | if (force_freq > 0) { | 
|  | 6438 | freq = wpas_p2p_select_go_freq(wpa_s, force_freq); | 
|  | 6439 | if (freq < 0) | 
|  | 6440 | return -1; | 
|  | 6441 | } else { | 
|  | 6442 | freq = wpas_p2p_select_go_freq(wpa_s, neg_freq); | 
|  | 6443 | if (freq < 0 || | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6444 | (freq > 0 && !freq_included(wpa_s, channels, freq))) | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 6445 | freq = 0; | 
|  | 6446 | } | 
| Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 6447 | } else if (ssid->mode == WPAS_MODE_INFRA) { | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 6448 | freq = neg_freq; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6449 | if (freq <= 0 || !freq_included(wpa_s, channels, freq)) { | 
| Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 6450 | struct os_reltime now; | 
|  | 6451 | struct wpa_bss *bss = | 
|  | 6452 | wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid); | 
| Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 6453 |  | 
| Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 6454 | os_get_reltime(&now); | 
|  | 6455 | if (bss && | 
|  | 6456 | !os_reltime_expired(&now, &bss->last_update, 5) && | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6457 | freq_included(wpa_s, channels, bss->freq)) | 
| Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 6458 | freq = bss->freq; | 
|  | 6459 | else | 
|  | 6460 | freq = 0; | 
|  | 6461 | } | 
|  | 6462 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6463 | return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq, | 
|  | 6464 | force_scan); | 
| Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 6465 | } else { | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 6466 | return -1; | 
| Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 6467 | } | 
| Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 6468 |  | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 6469 | if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, vht_center_freq2, | 
|  | 6470 | ht40, vht, max_oper_chwidth, channels)) | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6471 | return -1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6472 |  | 
|  | 6473 | params.role_go = 1; | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 6474 | params.psk_set = ssid->psk_set; | 
|  | 6475 | if (params.psk_set) | 
|  | 6476 | os_memcpy(params.psk, ssid->psk, sizeof(params.psk)); | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6477 | if (ssid->passphrase) { | 
|  | 6478 | if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) { | 
|  | 6479 | wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in " | 
|  | 6480 | "persistent group"); | 
|  | 6481 | return -1; | 
|  | 6482 | } | 
|  | 6483 | os_strlcpy(params.passphrase, ssid->passphrase, | 
|  | 6484 | sizeof(params.passphrase)); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6485 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6486 | os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len); | 
|  | 6487 | params.ssid_len = ssid->ssid_len; | 
|  | 6488 | params.persistent_group = 1; | 
|  | 6489 |  | 
|  | 6490 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1); | 
|  | 6491 | if (wpa_s == NULL) | 
|  | 6492 | return -1; | 
|  | 6493 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 6494 | p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS); | 
|  | 6495 |  | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 6496 | wpa_s->p2p_first_connection_timeout = connection_timeout; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6497 | wpas_start_wps_go(wpa_s, ¶ms, 0); | 
|  | 6498 |  | 
|  | 6499 | return 0; | 
|  | 6500 | } | 
|  | 6501 |  | 
|  | 6502 |  | 
|  | 6503 | static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies, | 
|  | 6504 | struct wpabuf *proberesp_ies) | 
|  | 6505 | { | 
|  | 6506 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 6507 | if (wpa_s->ap_iface) { | 
|  | 6508 | struct hostapd_data *hapd = wpa_s->ap_iface->bss[0]; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6509 | if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) { | 
|  | 6510 | wpabuf_free(beacon_ies); | 
|  | 6511 | wpabuf_free(proberesp_ies); | 
|  | 6512 | return; | 
|  | 6513 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6514 | if (beacon_ies) { | 
|  | 6515 | wpabuf_free(hapd->p2p_beacon_ie); | 
|  | 6516 | hapd->p2p_beacon_ie = beacon_ies; | 
|  | 6517 | } | 
|  | 6518 | wpabuf_free(hapd->p2p_probe_resp_ie); | 
|  | 6519 | hapd->p2p_probe_resp_ie = proberesp_ies; | 
|  | 6520 | } else { | 
|  | 6521 | wpabuf_free(beacon_ies); | 
|  | 6522 | wpabuf_free(proberesp_ies); | 
|  | 6523 | } | 
|  | 6524 | wpa_supplicant_ap_update_beacon(wpa_s); | 
|  | 6525 | } | 
|  | 6526 |  | 
|  | 6527 |  | 
|  | 6528 | static void wpas_p2p_idle_update(void *ctx, int idle) | 
|  | 6529 | { | 
|  | 6530 | struct wpa_supplicant *wpa_s = ctx; | 
|  | 6531 | if (!wpa_s->ap_iface) | 
|  | 6532 | return; | 
|  | 6533 | wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not "); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6534 | if (idle) { | 
|  | 6535 | if (wpa_s->global->p2p_fail_on_wps_complete && | 
|  | 6536 | wpa_s->p2p_in_provisioning) { | 
|  | 6537 | wpas_p2p_grpform_fail_after_wps(wpa_s); | 
|  | 6538 | return; | 
|  | 6539 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6540 | wpas_p2p_set_group_idle_timeout(wpa_s); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6541 | } else | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6542 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); | 
|  | 6543 | } | 
|  | 6544 |  | 
|  | 6545 |  | 
|  | 6546 | struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6547 | struct wpa_ssid *ssid) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6548 | { | 
|  | 6549 | struct p2p_group *group; | 
|  | 6550 | struct p2p_group_config *cfg; | 
|  | 6551 |  | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 6552 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL || | 
|  | 6553 | !ssid->p2p_group) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6554 | return NULL; | 
|  | 6555 |  | 
|  | 6556 | cfg = os_zalloc(sizeof(*cfg)); | 
|  | 6557 | if (cfg == NULL) | 
|  | 6558 | return NULL; | 
|  | 6559 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6560 | if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect) | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6561 | cfg->persistent_group = 2; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6562 | else if (ssid->p2p_persistent_group) | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6563 | cfg->persistent_group = 1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6564 | os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN); | 
|  | 6565 | if (wpa_s->max_stations && | 
|  | 6566 | wpa_s->max_stations < wpa_s->conf->max_num_sta) | 
|  | 6567 | cfg->max_clients = wpa_s->max_stations; | 
|  | 6568 | else | 
|  | 6569 | cfg->max_clients = wpa_s->conf->max_num_sta; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6570 | os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len); | 
|  | 6571 | cfg->ssid_len = ssid->ssid_len; | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 6572 | cfg->freq = ssid->frequency; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6573 | cfg->cb_ctx = wpa_s; | 
|  | 6574 | cfg->ie_update = wpas_p2p_ie_update; | 
|  | 6575 | cfg->idle_update = wpas_p2p_idle_update; | 
| Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 6576 | cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start) | 
|  | 6577 | != 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6578 |  | 
|  | 6579 | group = p2p_group_init(wpa_s->global->p2p, cfg); | 
|  | 6580 | if (group == NULL) | 
|  | 6581 | os_free(cfg); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6582 | if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6583 | p2p_group_notif_formation_done(group); | 
|  | 6584 | wpa_s->p2p_group = group; | 
|  | 6585 | return group; | 
|  | 6586 | } | 
|  | 6587 |  | 
|  | 6588 |  | 
|  | 6589 | void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr, | 
|  | 6590 | int registrar) | 
|  | 6591 | { | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6592 | struct wpa_ssid *ssid = wpa_s->current_ssid; | 
|  | 6593 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6594 | if (!wpa_s->p2p_in_provisioning) { | 
|  | 6595 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P " | 
|  | 6596 | "provisioning not in progress"); | 
|  | 6597 | return; | 
|  | 6598 | } | 
|  | 6599 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6600 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { | 
|  | 6601 | u8 go_dev_addr[ETH_ALEN]; | 
|  | 6602 | os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN); | 
|  | 6603 | wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, | 
|  | 6604 | ssid->ssid_len); | 
|  | 6605 | /* Clear any stored provisioning info */ | 
|  | 6606 | p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr); | 
|  | 6607 | } | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6608 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 6609 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev, | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6610 | NULL); | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 6611 | wpa_s->p2p_go_group_formation_completed = 1; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6612 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { | 
|  | 6613 | /* | 
|  | 6614 | * Use a separate timeout for initial data connection to | 
|  | 6615 | * complete to allow the group to be removed automatically if | 
|  | 6616 | * something goes wrong in this step before the P2P group idle | 
|  | 6617 | * timeout mechanism is taken into use. | 
|  | 6618 | */ | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 6619 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 6620 | "P2P: Re-start group formation timeout (%d seconds) as client for initial connection", | 
|  | 6621 | P2P_MAX_INITIAL_CONN_WAIT); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6622 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, | 
|  | 6623 | wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 6624 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 6625 | /* Complete group formation on successful data connection. */ | 
|  | 6626 | wpa_s->p2p_go_group_formation_completed = 0; | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 6627 | } else if (ssid) { | 
|  | 6628 | /* | 
|  | 6629 | * Use a separate timeout for initial data connection to | 
|  | 6630 | * complete to allow the group to be removed automatically if | 
|  | 6631 | * the client does not complete data connection successfully. | 
|  | 6632 | */ | 
|  | 6633 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 6634 | "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection", | 
|  | 6635 | P2P_MAX_INITIAL_CONN_WAIT_GO); | 
|  | 6636 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0, | 
|  | 6637 | wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 6638 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 6639 | /* | 
|  | 6640 | * Complete group formation on first successful data connection | 
|  | 6641 | */ | 
|  | 6642 | wpa_s->p2p_go_group_formation_completed = 0; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6643 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6644 | if (wpa_s->global->p2p) | 
|  | 6645 | p2p_wps_success_cb(wpa_s->global->p2p, peer_addr); | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 6646 | wpas_group_formation_completed(wpa_s, 1, 0); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6647 | } | 
|  | 6648 |  | 
|  | 6649 |  | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6650 | void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s, | 
|  | 6651 | struct wps_event_fail *fail) | 
|  | 6652 | { | 
|  | 6653 | if (!wpa_s->p2p_in_provisioning) { | 
|  | 6654 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P " | 
|  | 6655 | "provisioning not in progress"); | 
|  | 6656 | return; | 
|  | 6657 | } | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6658 |  | 
|  | 6659 | if (wpa_s->go_params) { | 
|  | 6660 | p2p_clear_provisioning_info( | 
|  | 6661 | wpa_s->global->p2p, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6662 | wpa_s->go_params->peer_device_addr); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6663 | } | 
|  | 6664 |  | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6665 | wpas_notify_p2p_wps_failed(wpa_s, fail); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6666 |  | 
|  | 6667 | if (wpa_s == wpa_s->global->p2p_group_formation) { | 
|  | 6668 | /* | 
|  | 6669 | * Allow some time for the failed WPS negotiation exchange to | 
|  | 6670 | * complete, but remove the group since group formation cannot | 
|  | 6671 | * succeed after provisioning failure. | 
|  | 6672 | */ | 
|  | 6673 | wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout"); | 
|  | 6674 | wpa_s->global->p2p_fail_on_wps_complete = 1; | 
|  | 6675 | eloop_deplete_timeout(0, 50000, | 
|  | 6676 | wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 6677 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6678 | } | 
|  | 6679 | } | 
|  | 6680 |  | 
|  | 6681 |  | 
|  | 6682 | int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s) | 
|  | 6683 | { | 
|  | 6684 | if (!wpa_s->global->p2p_fail_on_wps_complete || | 
|  | 6685 | !wpa_s->p2p_in_provisioning) | 
|  | 6686 | return 0; | 
|  | 6687 |  | 
|  | 6688 | wpas_p2p_grpform_fail_after_wps(wpa_s); | 
|  | 6689 |  | 
|  | 6690 | return 1; | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6691 | } | 
|  | 6692 |  | 
|  | 6693 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6694 | 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] | 6695 | const char *config_method, | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 6696 | enum wpas_p2p_prov_disc_use use, | 
|  | 6697 | struct p2ps_provision *p2ps_prov) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6698 | { | 
|  | 6699 | u16 config_methods; | 
|  | 6700 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 6701 | wpa_s->global->pending_p2ps_group = 0; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6702 | wpa_s->global->pending_p2ps_group_freq = 0; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6703 | wpa_s->p2p_fallback_to_go_neg = 0; | 
|  | 6704 | wpa_s->pending_pd_use = NORMAL_PD; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 6705 | if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) { | 
|  | 6706 | p2ps_prov->conncap = p2ps_group_capability( | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 6707 | wpa_s, P2PS_SETUP_NONE, p2ps_prov->role, | 
|  | 6708 | &p2ps_prov->force_freq, &p2ps_prov->pref_freq); | 
|  | 6709 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 6710 | wpa_printf(MSG_DEBUG, | 
|  | 6711 | "P2P: %s conncap: %d - ASP parsed: %x %x %d %s", | 
|  | 6712 | __func__, p2ps_prov->conncap, | 
|  | 6713 | p2ps_prov->adv_id, p2ps_prov->conncap, | 
|  | 6714 | p2ps_prov->status, p2ps_prov->info); | 
|  | 6715 |  | 
|  | 6716 | config_methods = 0; | 
|  | 6717 | } else if (os_strncmp(config_method, "display", 7) == 0) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6718 | config_methods = WPS_CONFIG_DISPLAY; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6719 | else if (os_strncmp(config_method, "keypad", 6) == 0) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6720 | config_methods = WPS_CONFIG_KEYPAD; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6721 | else if (os_strncmp(config_method, "pbc", 3) == 0 || | 
|  | 6722 | os_strncmp(config_method, "pushbutton", 10) == 0) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6723 | config_methods = WPS_CONFIG_PUSHBUTTON; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6724 | else { | 
|  | 6725 | wpa_printf(MSG_DEBUG, "P2P: Unknown config method"); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 6726 | os_free(p2ps_prov); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6727 | return -1; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6728 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6729 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6730 | if (use == WPAS_P2P_PD_AUTO) { | 
|  | 6731 | os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN); | 
|  | 6732 | wpa_s->pending_pd_config_methods = config_methods; | 
|  | 6733 | wpa_s->p2p_auto_pd = 1; | 
|  | 6734 | wpa_s->p2p_auto_join = 0; | 
|  | 6735 | wpa_s->pending_pd_before_join = 0; | 
|  | 6736 | wpa_s->auto_pd_scan_retry = 0; | 
|  | 6737 | wpas_p2p_stop_find(wpa_s); | 
|  | 6738 | wpa_s->p2p_join_scan_count = 0; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6739 | os_get_reltime(&wpa_s->p2p_auto_started); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6740 | wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld", | 
|  | 6741 | wpa_s->p2p_auto_started.sec, | 
|  | 6742 | wpa_s->p2p_auto_started.usec); | 
|  | 6743 | wpas_p2p_join_scan(wpa_s, NULL); | 
|  | 6744 | return 0; | 
|  | 6745 | } | 
|  | 6746 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 6747 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) { | 
|  | 6748 | os_free(p2ps_prov); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6749 | return -1; | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 6750 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6751 |  | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 6752 | return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6753 | config_methods, use == WPAS_P2P_PD_FOR_JOIN, | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 6754 | 0, 1); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6755 | } | 
|  | 6756 |  | 
|  | 6757 |  | 
|  | 6758 | int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, | 
|  | 6759 | char *end) | 
|  | 6760 | { | 
|  | 6761 | return p2p_scan_result_text(ies, ies_len, buf, end); | 
|  | 6762 | } | 
|  | 6763 |  | 
|  | 6764 |  | 
|  | 6765 | static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s) | 
|  | 6766 | { | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6767 | if (!offchannel_pending_action_tx(wpa_s)) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6768 | return; | 
|  | 6769 |  | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 6770 | if (wpa_s->p2p_send_action_work) { | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 6771 | wpas_p2p_free_send_action_work(wpa_s); | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 6772 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, | 
|  | 6773 | wpa_s, NULL); | 
|  | 6774 | offchannel_send_action_done(wpa_s); | 
|  | 6775 | } | 
| Dmitry Shmidt | 0365883 | 2014-08-13 11:03:49 -0700 | [diff] [blame] | 6776 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6777 | wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new " | 
|  | 6778 | "operation request"); | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6779 | offchannel_clear_pending_action_tx(wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6780 | } | 
|  | 6781 |  | 
|  | 6782 |  | 
|  | 6783 | int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout, | 
|  | 6784 | enum p2p_discovery_type type, | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6785 | unsigned int num_req_dev_types, const u8 *req_dev_types, | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 6786 | const u8 *dev_id, unsigned int search_delay, | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 6787 | u8 seek_cnt, const char **seek_string, int freq) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6788 | { | 
|  | 6789 | wpas_p2p_clear_pending_action_tx(wpa_s); | 
|  | 6790 | wpa_s->p2p_long_listen = 0; | 
|  | 6791 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6792 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL || | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 6793 | wpa_s->p2p_in_provisioning) { | 
|  | 6794 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s", | 
|  | 6795 | (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ? | 
|  | 6796 | " (P2P disabled)" : "", | 
|  | 6797 | wpa_s->p2p_in_provisioning ? | 
|  | 6798 | " (p2p_in_provisioning)" : ""); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6799 | return -1; | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 6800 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6801 |  | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6802 | wpa_supplicant_cancel_sched_scan(wpa_s); | 
|  | 6803 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6804 | return p2p_find(wpa_s->global->p2p, timeout, type, | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6805 | num_req_dev_types, req_dev_types, dev_id, | 
| Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 6806 | search_delay, seek_cnt, seek_string, freq); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6807 | } | 
|  | 6808 |  | 
|  | 6809 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 6810 | static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s, | 
|  | 6811 | struct wpa_scan_results *scan_res) | 
|  | 6812 | { | 
|  | 6813 | wpa_printf(MSG_DEBUG, "P2P: Ignore scan results"); | 
|  | 6814 |  | 
|  | 6815 | if (wpa_s->p2p_scan_work) { | 
|  | 6816 | struct wpa_radio_work *work = wpa_s->p2p_scan_work; | 
|  | 6817 | wpa_s->p2p_scan_work = NULL; | 
|  | 6818 | radio_work_done(work); | 
|  | 6819 | } | 
|  | 6820 |  | 
|  | 6821 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 6822 | return; | 
|  | 6823 |  | 
|  | 6824 | /* | 
|  | 6825 | * Indicate that results have been processed so that the P2P module can | 
|  | 6826 | * continue pending tasks. | 
|  | 6827 | */ | 
|  | 6828 | p2p_scan_res_handled(wpa_s->global->p2p); | 
|  | 6829 | } | 
|  | 6830 |  | 
|  | 6831 |  | 
|  | 6832 | static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6833 | { | 
|  | 6834 | wpas_p2p_clear_pending_action_tx(wpa_s); | 
|  | 6835 | wpa_s->p2p_long_listen = 0; | 
|  | 6836 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); | 
|  | 6837 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6838 |  | 
|  | 6839 | if (wpa_s->global->p2p) | 
|  | 6840 | p2p_stop_find(wpa_s->global->p2p); | 
|  | 6841 |  | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 6842 | if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) { | 
|  | 6843 | wpa_printf(MSG_DEBUG, | 
|  | 6844 | "P2P: Do not consider the scan results after stop_find"); | 
|  | 6845 | wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search; | 
|  | 6846 | } | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 6847 | } | 
|  | 6848 |  | 
|  | 6849 |  | 
|  | 6850 | void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s) | 
|  | 6851 | { | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 6852 | wpas_p2p_stop_find_oper(wpa_s); | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 6853 | if (!wpa_s->global->pending_group_iface_for_p2ps) | 
|  | 6854 | wpas_p2p_remove_pending_group_interface(wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6855 | } | 
|  | 6856 |  | 
|  | 6857 |  | 
|  | 6858 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx) | 
|  | 6859 | { | 
|  | 6860 | struct wpa_supplicant *wpa_s = eloop_ctx; | 
|  | 6861 | wpa_s->p2p_long_listen = 0; | 
|  | 6862 | } | 
|  | 6863 |  | 
|  | 6864 |  | 
|  | 6865 | int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout) | 
|  | 6866 | { | 
|  | 6867 | int res; | 
|  | 6868 |  | 
|  | 6869 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 6870 | return -1; | 
|  | 6871 |  | 
| Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 6872 | if (wpa_s->p2p_lo_started) { | 
|  | 6873 | wpa_printf(MSG_DEBUG, | 
|  | 6874 | "P2P: Cannot start P2P listen, it is offloaded"); | 
|  | 6875 | return -1; | 
|  | 6876 | } | 
|  | 6877 |  | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6878 | wpa_supplicant_cancel_sched_scan(wpa_s); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6879 | wpas_p2p_clear_pending_action_tx(wpa_s); | 
|  | 6880 |  | 
|  | 6881 | if (timeout == 0) { | 
|  | 6882 | /* | 
|  | 6883 | * This is a request for unlimited Listen state. However, at | 
|  | 6884 | * least for now, this is mapped to a Listen state for one | 
|  | 6885 | * hour. | 
|  | 6886 | */ | 
|  | 6887 | timeout = 3600; | 
|  | 6888 | } | 
|  | 6889 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); | 
|  | 6890 | wpa_s->p2p_long_listen = 0; | 
|  | 6891 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6892 | /* | 
|  | 6893 | * Stop previous find/listen operation to avoid trying to request a new | 
|  | 6894 | * remain-on-channel operation while the driver is still running the | 
|  | 6895 | * previous one. | 
|  | 6896 | */ | 
|  | 6897 | if (wpa_s->global->p2p) | 
|  | 6898 | p2p_stop_find(wpa_s->global->p2p); | 
|  | 6899 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6900 | res = wpas_p2p_listen_start(wpa_s, timeout * 1000); | 
|  | 6901 | if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) { | 
|  | 6902 | wpa_s->p2p_long_listen = timeout * 1000; | 
|  | 6903 | eloop_register_timeout(timeout, 0, | 
|  | 6904 | wpas_p2p_long_listen_timeout, | 
|  | 6905 | wpa_s, NULL); | 
|  | 6906 | } | 
|  | 6907 |  | 
|  | 6908 | return res; | 
|  | 6909 | } | 
|  | 6910 |  | 
|  | 6911 |  | 
|  | 6912 | int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, | 
|  | 6913 | u8 *buf, size_t len, int p2p_group) | 
|  | 6914 | { | 
|  | 6915 | struct wpabuf *p2p_ie; | 
|  | 6916 | int ret; | 
|  | 6917 |  | 
|  | 6918 | if (wpa_s->global->p2p_disabled) | 
|  | 6919 | return -1; | 
| Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 6920 | /* | 
|  | 6921 | * Advertize mandatory cross connection capability even on | 
|  | 6922 | * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP. | 
|  | 6923 | */ | 
|  | 6924 | if (wpa_s->conf->p2p_disabled && p2p_group) | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 6925 | return -1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6926 | if (wpa_s->global->p2p == NULL) | 
|  | 6927 | return -1; | 
|  | 6928 | if (bss == NULL) | 
|  | 6929 | return -1; | 
|  | 6930 |  | 
|  | 6931 | p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); | 
|  | 6932 | ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len, | 
|  | 6933 | p2p_group, p2p_ie); | 
|  | 6934 | wpabuf_free(p2p_ie); | 
|  | 6935 |  | 
|  | 6936 | return ret; | 
|  | 6937 | } | 
|  | 6938 |  | 
|  | 6939 |  | 
|  | 6940 | 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] | 6941 | const u8 *dst, const u8 *bssid, | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 6942 | const u8 *ie, size_t ie_len, | 
|  | 6943 | unsigned int rx_freq, int ssi_signal) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6944 | { | 
|  | 6945 | if (wpa_s->global->p2p_disabled) | 
|  | 6946 | return 0; | 
|  | 6947 | if (wpa_s->global->p2p == NULL) | 
|  | 6948 | return 0; | 
|  | 6949 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6950 | switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid, | 
| Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 6951 | ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) { | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6952 | case P2P_PREQ_NOT_P2P: | 
|  | 6953 | wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len, | 
|  | 6954 | ssi_signal); | 
|  | 6955 | /* fall through */ | 
|  | 6956 | case P2P_PREQ_MALFORMED: | 
|  | 6957 | case P2P_PREQ_NOT_LISTEN: | 
|  | 6958 | case P2P_PREQ_NOT_PROCESSED: | 
|  | 6959 | default: /* make gcc happy */ | 
|  | 6960 | return 0; | 
|  | 6961 | case P2P_PREQ_PROCESSED: | 
|  | 6962 | return 1; | 
|  | 6963 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6964 | } | 
|  | 6965 |  | 
|  | 6966 |  | 
|  | 6967 | void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da, | 
|  | 6968 | const u8 *sa, const u8 *bssid, | 
|  | 6969 | u8 category, const u8 *data, size_t len, int freq) | 
|  | 6970 | { | 
|  | 6971 | if (wpa_s->global->p2p_disabled) | 
|  | 6972 | return; | 
|  | 6973 | if (wpa_s->global->p2p == NULL) | 
|  | 6974 | return; | 
|  | 6975 |  | 
|  | 6976 | p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len, | 
|  | 6977 | freq); | 
|  | 6978 | } | 
|  | 6979 |  | 
|  | 6980 |  | 
|  | 6981 | void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies) | 
|  | 6982 | { | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 6983 | unsigned int bands; | 
|  | 6984 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6985 | if (wpa_s->global->p2p_disabled) | 
|  | 6986 | return; | 
|  | 6987 | if (wpa_s->global->p2p == NULL) | 
|  | 6988 | return; | 
|  | 6989 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 6990 | bands = wpas_get_bands(wpa_s, NULL); | 
|  | 6991 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6992 | } | 
|  | 6993 |  | 
|  | 6994 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 6995 | static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6996 | { | 
|  | 6997 | p2p_group_deinit(wpa_s->p2p_group); | 
|  | 6998 | wpa_s->p2p_group = NULL; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6999 |  | 
|  | 7000 | wpa_s->ap_configured_cb = NULL; | 
|  | 7001 | wpa_s->ap_configured_cb_ctx = NULL; | 
|  | 7002 | wpa_s->ap_configured_cb_data = NULL; | 
|  | 7003 | wpa_s->connect_without_scan = NULL; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7004 | } | 
|  | 7005 |  | 
|  | 7006 |  | 
|  | 7007 | int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr) | 
|  | 7008 | { | 
|  | 7009 | wpa_s->p2p_long_listen = 0; | 
|  | 7010 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7011 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 7012 | return -1; | 
|  | 7013 |  | 
|  | 7014 | return p2p_reject(wpa_s->global->p2p, addr); | 
|  | 7015 | } | 
|  | 7016 |  | 
|  | 7017 |  | 
|  | 7018 | /* Invite to reinvoke a persistent group */ | 
|  | 7019 | 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] | 7020 | struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 7021 | int vht_center_freq2, int ht40, int vht, int max_chwidth, | 
|  | 7022 | int pref_freq) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7023 | { | 
|  | 7024 | enum p2p_invite_role role; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 7025 | u8 *bssid = NULL; | 
|  | 7026 | int force_freq = 0; | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 7027 | int res; | 
| Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 7028 | int no_pref_freq_given = pref_freq == 0; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 7029 | unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size; | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 7030 |  | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 7031 | wpa_s->global->p2p_invite_group = NULL; | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 7032 | if (peer_addr) | 
|  | 7033 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); | 
|  | 7034 | else | 
|  | 7035 | os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7036 |  | 
| Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 7037 | wpa_s->p2p_persistent_go_freq = freq; | 
|  | 7038 | wpa_s->p2p_go_ht40 = !!ht40; | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 7039 | wpa_s->p2p_go_vht = !!vht; | 
|  | 7040 | wpa_s->p2p_go_max_oper_chwidth = max_chwidth; | 
|  | 7041 | wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7042 | if (ssid->mode == WPAS_MODE_P2P_GO) { | 
|  | 7043 | role = P2P_INVITE_ROLE_GO; | 
|  | 7044 | if (peer_addr == NULL) { | 
|  | 7045 | wpa_printf(MSG_DEBUG, "P2P: Missing peer " | 
|  | 7046 | "address in invitation command"); | 
|  | 7047 | return -1; | 
|  | 7048 | } | 
|  | 7049 | if (wpas_p2p_create_iface(wpa_s)) { | 
|  | 7050 | if (wpas_p2p_add_group_interface(wpa_s, | 
|  | 7051 | WPA_IF_P2P_GO) < 0) { | 
|  | 7052 | wpa_printf(MSG_ERROR, "P2P: Failed to " | 
|  | 7053 | "allocate a new interface for the " | 
|  | 7054 | "group"); | 
|  | 7055 | return -1; | 
|  | 7056 | } | 
|  | 7057 | bssid = wpa_s->pending_interface_addr; | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 7058 | } else if (wpa_s->p2p_mgmt) | 
|  | 7059 | bssid = wpa_s->parent->own_addr; | 
|  | 7060 | else | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7061 | bssid = wpa_s->own_addr; | 
|  | 7062 | } else { | 
|  | 7063 | role = P2P_INVITE_ROLE_CLIENT; | 
|  | 7064 | peer_addr = ssid->bssid; | 
|  | 7065 | } | 
|  | 7066 | wpa_s->pending_invite_ssid_id = ssid->id; | 
|  | 7067 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 7068 | size = P2P_MAX_PREF_CHANNELS; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 7069 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 7070 | role == P2P_INVITE_ROLE_GO, | 
|  | 7071 | pref_freq_list, &size); | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 7072 | if (res) | 
|  | 7073 | return res; | 
| Irfan Sheriff | af84a57 | 2012-09-22 16:59:30 -0700 | [diff] [blame] | 7074 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7075 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 7076 | return -1; | 
|  | 7077 |  | 
| Dmitry Shmidt | 7d56b75 | 2015-12-22 10:59:44 -0800 | [diff] [blame] | 7078 | p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size); | 
|  | 7079 |  | 
| Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 7080 | if (wpa_s->parent->conf->p2p_ignore_shared_freq && | 
|  | 7081 | no_pref_freq_given && pref_freq > 0 && | 
|  | 7082 | wpa_s->num_multichan_concurrent > 1 && | 
|  | 7083 | wpas_p2p_num_unused_channels(wpa_s) > 0) { | 
|  | 7084 | wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration", | 
|  | 7085 | pref_freq); | 
|  | 7086 | pref_freq = 0; | 
|  | 7087 | } | 
|  | 7088 |  | 
| Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 7089 | /* | 
|  | 7090 | * Stop any find/listen operations before invitation and possibly | 
|  | 7091 | * connection establishment. | 
|  | 7092 | */ | 
|  | 7093 | wpas_p2p_stop_find_oper(wpa_s); | 
|  | 7094 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7095 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 7096 | ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7097 | 1, pref_freq, -1); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7098 | } | 
|  | 7099 |  | 
|  | 7100 |  | 
|  | 7101 | /* Invite to join an active group */ | 
|  | 7102 | int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname, | 
|  | 7103 | const u8 *peer_addr, const u8 *go_dev_addr) | 
|  | 7104 | { | 
|  | 7105 | struct wpa_global *global = wpa_s->global; | 
|  | 7106 | enum p2p_invite_role role; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 7107 | u8 *bssid = NULL; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7108 | struct wpa_ssid *ssid; | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 7109 | int persistent; | 
| Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 7110 | int freq = 0, force_freq = 0, pref_freq = 0; | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 7111 | int res; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 7112 | unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7113 |  | 
| Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 7114 | wpa_s->p2p_persistent_go_freq = 0; | 
|  | 7115 | wpa_s->p2p_go_ht40 = 0; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 7116 | wpa_s->p2p_go_vht = 0; | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 7117 | wpa_s->p2p_go_vht_center_freq2 = 0; | 
|  | 7118 | wpa_s->p2p_go_max_oper_chwidth = 0; | 
| Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 7119 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7120 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
|  | 7121 | if (os_strcmp(wpa_s->ifname, ifname) == 0) | 
|  | 7122 | break; | 
|  | 7123 | } | 
|  | 7124 | if (wpa_s == NULL) { | 
|  | 7125 | wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname); | 
|  | 7126 | return -1; | 
|  | 7127 | } | 
|  | 7128 |  | 
|  | 7129 | ssid = wpa_s->current_ssid; | 
|  | 7130 | if (ssid == NULL) { | 
|  | 7131 | wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for " | 
|  | 7132 | "invitation"); | 
|  | 7133 | return -1; | 
|  | 7134 | } | 
|  | 7135 |  | 
| Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 7136 | wpa_s->global->p2p_invite_group = wpa_s; | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 7137 | persistent = ssid->p2p_persistent_group && | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 7138 | wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr, | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 7139 | ssid->ssid, ssid->ssid_len); | 
|  | 7140 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7141 | if (ssid->mode == WPAS_MODE_P2P_GO) { | 
|  | 7142 | role = P2P_INVITE_ROLE_ACTIVE_GO; | 
|  | 7143 | bssid = wpa_s->own_addr; | 
|  | 7144 | if (go_dev_addr == NULL) | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7145 | go_dev_addr = wpa_s->global->p2p_dev_addr; | 
| Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 7146 | freq = ssid->frequency; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7147 | } else { | 
|  | 7148 | role = P2P_INVITE_ROLE_CLIENT; | 
|  | 7149 | if (wpa_s->wpa_state < WPA_ASSOCIATED) { | 
|  | 7150 | wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot " | 
|  | 7151 | "invite to current group"); | 
|  | 7152 | return -1; | 
|  | 7153 | } | 
|  | 7154 | bssid = wpa_s->bssid; | 
|  | 7155 | if (go_dev_addr == NULL && | 
|  | 7156 | !is_zero_ether_addr(wpa_s->go_dev_addr)) | 
|  | 7157 | go_dev_addr = wpa_s->go_dev_addr; | 
| Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 7158 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : | 
|  | 7159 | (int) wpa_s->assoc_freq; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7160 | } | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 7161 | wpa_s->p2pdev->pending_invite_ssid_id = -1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7162 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7163 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 7164 | return -1; | 
|  | 7165 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 7166 | size = P2P_MAX_PREF_CHANNELS; | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 7167 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 7168 | role == P2P_INVITE_ROLE_ACTIVE_GO, | 
|  | 7169 | pref_freq_list, &size); | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 7170 | if (res) | 
|  | 7171 | return res; | 
| Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 7172 | wpas_p2p_set_own_freq_preference(wpa_s, force_freq); | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 7173 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7174 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, | 
| Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 7175 | ssid->ssid, ssid->ssid_len, force_freq, | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7176 | go_dev_addr, persistent, pref_freq, -1); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7177 | } | 
|  | 7178 |  | 
|  | 7179 |  | 
|  | 7180 | void wpas_p2p_completed(struct wpa_supplicant *wpa_s) | 
|  | 7181 | { | 
|  | 7182 | struct wpa_ssid *ssid = wpa_s->current_ssid; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7183 | u8 go_dev_addr[ETH_ALEN]; | 
|  | 7184 | int persistent; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7185 | int freq; | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7186 | u8 ip[3 * 4]; | 
|  | 7187 | char ip_addr[100]; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7188 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7189 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) { | 
|  | 7190 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 7191 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7192 | } | 
|  | 7193 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7194 | if (!wpa_s->show_group_started || !ssid) | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 7195 | return; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7196 |  | 
|  | 7197 | wpa_s->show_group_started = 0; | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 7198 | if (!wpa_s->p2p_go_group_formation_completed && | 
|  | 7199 | wpa_s->global->p2p_group_formation == wpa_s) { | 
|  | 7200 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 7201 | "P2P: Marking group formation completed on client on data connection"); | 
|  | 7202 | wpa_s->p2p_go_group_formation_completed = 1; | 
|  | 7203 | wpa_s->global->p2p_group_formation = NULL; | 
|  | 7204 | wpa_s->p2p_in_provisioning = 0; | 
|  | 7205 | wpa_s->p2p_in_invitation = 0; | 
|  | 7206 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7207 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7208 | os_memset(go_dev_addr, 0, ETH_ALEN); | 
|  | 7209 | if (ssid->bssid_set) | 
|  | 7210 | os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN); | 
|  | 7211 | persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, | 
|  | 7212 | ssid->ssid_len); | 
|  | 7213 | os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN); | 
|  | 7214 |  | 
|  | 7215 | if (wpa_s->global->p2p_group_formation == wpa_s) | 
|  | 7216 | wpa_s->global->p2p_group_formation = NULL; | 
|  | 7217 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7218 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : | 
|  | 7219 | (int) wpa_s->assoc_freq; | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7220 |  | 
|  | 7221 | ip_addr[0] = '\0'; | 
|  | 7222 | if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) { | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 7223 | int res; | 
|  | 7224 |  | 
|  | 7225 | res = os_snprintf(ip_addr, sizeof(ip_addr), | 
|  | 7226 | " ip_addr=%u.%u.%u.%u " | 
|  | 7227 | "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u", | 
|  | 7228 | ip[0], ip[1], ip[2], ip[3], | 
|  | 7229 | ip[4], ip[5], ip[6], ip[7], | 
|  | 7230 | ip[8], ip[9], ip[10], ip[11]); | 
|  | 7231 | if (os_snprintf_error(sizeof(ip_addr), res)) | 
|  | 7232 | ip_addr[0] = '\0'; | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7233 | } | 
|  | 7234 |  | 
| Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 7235 | wpas_p2p_group_started(wpa_s, 0, ssid, freq, | 
|  | 7236 | ssid->passphrase == NULL && ssid->psk_set ? | 
|  | 7237 | ssid->psk : NULL, | 
|  | 7238 | ssid->passphrase, go_dev_addr, persistent, | 
|  | 7239 | ip_addr); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7240 |  | 
|  | 7241 | if (persistent) | 
| Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 7242 | wpas_p2p_store_persistent_group(wpa_s->p2pdev, | 
|  | 7243 | ssid, go_dev_addr); | 
|  | 7244 |  | 
| Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 7245 | wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7246 | } | 
|  | 7247 |  | 
|  | 7248 |  | 
|  | 7249 | int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1, | 
|  | 7250 | u32 interval1, u32 duration2, u32 interval2) | 
|  | 7251 | { | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 7252 | int ret; | 
|  | 7253 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7254 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 7255 | return -1; | 
|  | 7256 |  | 
|  | 7257 | if (wpa_s->wpa_state < WPA_ASSOCIATED || | 
|  | 7258 | wpa_s->current_ssid == NULL || | 
|  | 7259 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA) | 
|  | 7260 | return -1; | 
|  | 7261 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 7262 | ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid, | 
|  | 7263 | wpa_s->own_addr, wpa_s->assoc_freq, | 
|  | 7264 | duration1, interval1, duration2, interval2); | 
|  | 7265 | if (ret == 0) | 
|  | 7266 | wpa_s->waiting_presence_resp = 1; | 
|  | 7267 |  | 
|  | 7268 | return ret; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7269 | } | 
|  | 7270 |  | 
|  | 7271 |  | 
|  | 7272 | int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period, | 
|  | 7273 | unsigned int interval) | 
|  | 7274 | { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7275 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 7276 | return -1; | 
|  | 7277 |  | 
|  | 7278 | return p2p_ext_listen(wpa_s->global->p2p, period, interval); | 
|  | 7279 | } | 
|  | 7280 |  | 
|  | 7281 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7282 | static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s) | 
|  | 7283 | { | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 7284 | if (wpa_s->current_ssid == NULL) { | 
|  | 7285 | /* | 
|  | 7286 | * current_ssid can be cleared when P2P client interface gets | 
|  | 7287 | * disconnected, so assume this interface was used as P2P | 
|  | 7288 | * client. | 
|  | 7289 | */ | 
|  | 7290 | return 1; | 
|  | 7291 | } | 
|  | 7292 | return wpa_s->current_ssid->p2p_group && | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7293 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA; | 
|  | 7294 | } | 
|  | 7295 |  | 
|  | 7296 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7297 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx) | 
|  | 7298 | { | 
|  | 7299 | struct wpa_supplicant *wpa_s = eloop_ctx; | 
|  | 7300 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7301 | 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] | 7302 | wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - " | 
|  | 7303 | "disabled"); | 
|  | 7304 | return; | 
|  | 7305 | } | 
|  | 7306 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7307 | wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate " | 
|  | 7308 | "group"); | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 7309 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7310 | } | 
|  | 7311 |  | 
|  | 7312 |  | 
|  | 7313 | static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s) | 
|  | 7314 | { | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7315 | int timeout; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7316 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7317 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) | 
|  | 7318 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); | 
|  | 7319 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7320 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) | 
|  | 7321 | return; | 
|  | 7322 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7323 | timeout = wpa_s->conf->p2p_group_idle; | 
|  | 7324 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA && | 
|  | 7325 | (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE)) | 
|  | 7326 | timeout = P2P_MAX_CLIENT_IDLE; | 
|  | 7327 |  | 
|  | 7328 | if (timeout == 0) | 
|  | 7329 | return; | 
|  | 7330 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7331 | if (timeout < 0) { | 
|  | 7332 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA) | 
|  | 7333 | timeout = 0; /* special client mode no-timeout */ | 
|  | 7334 | else | 
|  | 7335 | return; | 
|  | 7336 | } | 
|  | 7337 |  | 
|  | 7338 | if (wpa_s->p2p_in_provisioning) { | 
|  | 7339 | /* | 
|  | 7340 | * Use the normal group formation timeout during the | 
|  | 7341 | * provisioning phase to avoid terminating this process too | 
|  | 7342 | * early due to group idle timeout. | 
|  | 7343 | */ | 
|  | 7344 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " | 
|  | 7345 | "during provisioning"); | 
|  | 7346 | return; | 
|  | 7347 | } | 
| Deepthi Gowri | 9e4e8ac | 2013-04-16 11:57:05 +0530 | [diff] [blame] | 7348 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7349 | if (wpa_s->show_group_started) { | 
|  | 7350 | /* | 
|  | 7351 | * Use the normal group formation timeout between the end of | 
|  | 7352 | * the provisioning phase and completion of 4-way handshake to | 
|  | 7353 | * avoid terminating this process too early due to group idle | 
|  | 7354 | * timeout. | 
|  | 7355 | */ | 
|  | 7356 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " | 
|  | 7357 | "while waiting for initial 4-way handshake to " | 
|  | 7358 | "complete"); | 
|  | 7359 | return; | 
|  | 7360 | } | 
|  | 7361 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7362 | 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] | 7363 | timeout); | 
|  | 7364 | eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout, | 
|  | 7365 | wpa_s, NULL); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7366 | } | 
|  | 7367 |  | 
|  | 7368 |  | 
| Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 7369 | /* Returns 1 if the interface was removed */ | 
|  | 7370 | int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, | 
|  | 7371 | u16 reason_code, const u8 *ie, size_t ie_len, | 
|  | 7372 | int locally_generated) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7373 | { | 
|  | 7374 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
| Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 7375 | return 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7376 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7377 | if (!locally_generated) | 
|  | 7378 | p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, | 
|  | 7379 | ie_len); | 
|  | 7380 |  | 
|  | 7381 | if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated && | 
|  | 7382 | wpa_s->current_ssid && | 
|  | 7383 | wpa_s->current_ssid->p2p_group && | 
|  | 7384 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA) { | 
|  | 7385 | wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group " | 
|  | 7386 | "session is ending"); | 
| Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 7387 | if (wpas_p2p_group_delete(wpa_s, | 
|  | 7388 | P2P_GROUP_REMOVAL_GO_ENDING_SESSION) | 
|  | 7389 | > 0) | 
|  | 7390 | return 1; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7391 | } | 
| Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 7392 |  | 
|  | 7393 | return 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7394 | } | 
|  | 7395 |  | 
|  | 7396 |  | 
|  | 7397 | 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] | 7398 | u16 reason_code, const u8 *ie, size_t ie_len, | 
|  | 7399 | int locally_generated) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7400 | { | 
|  | 7401 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 7402 | return; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7403 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7404 | if (!locally_generated) | 
|  | 7405 | p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, | 
|  | 7406 | ie_len); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7407 | } | 
|  | 7408 |  | 
|  | 7409 |  | 
|  | 7410 | void wpas_p2p_update_config(struct wpa_supplicant *wpa_s) | 
|  | 7411 | { | 
|  | 7412 | struct p2p_data *p2p = wpa_s->global->p2p; | 
|  | 7413 |  | 
|  | 7414 | if (p2p == NULL) | 
|  | 7415 | return; | 
|  | 7416 |  | 
|  | 7417 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) | 
|  | 7418 | return; | 
|  | 7419 |  | 
|  | 7420 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME) | 
|  | 7421 | p2p_set_dev_name(p2p, wpa_s->conf->device_name); | 
|  | 7422 |  | 
|  | 7423 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) | 
|  | 7424 | p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type); | 
|  | 7425 |  | 
|  | 7426 | if (wpa_s->wps && | 
|  | 7427 | (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS)) | 
|  | 7428 | p2p_set_config_methods(p2p, wpa_s->wps->config_methods); | 
|  | 7429 |  | 
|  | 7430 | if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)) | 
|  | 7431 | p2p_set_uuid(p2p, wpa_s->wps->uuid); | 
|  | 7432 |  | 
|  | 7433 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) { | 
|  | 7434 | p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer); | 
|  | 7435 | p2p_set_model_name(p2p, wpa_s->conf->model_name); | 
|  | 7436 | p2p_set_model_number(p2p, wpa_s->conf->model_number); | 
|  | 7437 | p2p_set_serial_number(p2p, wpa_s->conf->serial_number); | 
|  | 7438 | } | 
|  | 7439 |  | 
|  | 7440 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) | 
|  | 7441 | p2p_set_sec_dev_types(p2p, | 
|  | 7442 | (void *) wpa_s->conf->sec_device_type, | 
|  | 7443 | wpa_s->conf->num_sec_device_types); | 
|  | 7444 |  | 
|  | 7445 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) { | 
|  | 7446 | int i; | 
|  | 7447 | p2p_remove_wps_vendor_extensions(p2p); | 
|  | 7448 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { | 
|  | 7449 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) | 
|  | 7450 | continue; | 
|  | 7451 | p2p_add_wps_vendor_extension( | 
|  | 7452 | p2p, wpa_s->conf->wps_vendor_ext[i]); | 
|  | 7453 | } | 
|  | 7454 | } | 
|  | 7455 |  | 
|  | 7456 | if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) && | 
|  | 7457 | wpa_s->conf->country[0] && wpa_s->conf->country[1]) { | 
|  | 7458 | char country[3]; | 
|  | 7459 | country[0] = wpa_s->conf->country[0]; | 
|  | 7460 | country[1] = wpa_s->conf->country[1]; | 
|  | 7461 | country[2] = 0x04; | 
|  | 7462 | p2p_set_country(p2p, country); | 
|  | 7463 | } | 
|  | 7464 |  | 
|  | 7465 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) { | 
|  | 7466 | p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix, | 
|  | 7467 | wpa_s->conf->p2p_ssid_postfix ? | 
|  | 7468 | os_strlen(wpa_s->conf->p2p_ssid_postfix) : | 
|  | 7469 | 0); | 
|  | 7470 | } | 
|  | 7471 |  | 
|  | 7472 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS) | 
|  | 7473 | p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss); | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 7474 |  | 
|  | 7475 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) { | 
|  | 7476 | u8 reg_class, channel; | 
|  | 7477 | int ret; | 
|  | 7478 | unsigned int r; | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 7479 | u8 channel_forced; | 
|  | 7480 |  | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 7481 | if (wpa_s->conf->p2p_listen_reg_class && | 
|  | 7482 | wpa_s->conf->p2p_listen_channel) { | 
|  | 7483 | reg_class = wpa_s->conf->p2p_listen_reg_class; | 
|  | 7484 | channel = wpa_s->conf->p2p_listen_channel; | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 7485 | channel_forced = 1; | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 7486 | } else { | 
|  | 7487 | reg_class = 81; | 
|  | 7488 | /* | 
|  | 7489 | * Pick one of the social channels randomly as the | 
|  | 7490 | * listen channel. | 
|  | 7491 | */ | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 7492 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) | 
|  | 7493 | channel = 1; | 
|  | 7494 | else | 
|  | 7495 | channel = 1 + (r % 3) * 5; | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 7496 | channel_forced = 0; | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 7497 | } | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 7498 | ret = p2p_set_listen_channel(p2p, reg_class, channel, | 
|  | 7499 | channel_forced); | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 7500 | if (ret) | 
|  | 7501 | wpa_printf(MSG_ERROR, "P2P: Own listen channel update " | 
|  | 7502 | "failed: %d", ret); | 
|  | 7503 | } | 
|  | 7504 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) { | 
|  | 7505 | u8 op_reg_class, op_channel, cfg_op_channel; | 
|  | 7506 | int ret = 0; | 
|  | 7507 | unsigned int r; | 
|  | 7508 | if (wpa_s->conf->p2p_oper_reg_class && | 
|  | 7509 | wpa_s->conf->p2p_oper_channel) { | 
|  | 7510 | op_reg_class = wpa_s->conf->p2p_oper_reg_class; | 
|  | 7511 | op_channel = wpa_s->conf->p2p_oper_channel; | 
|  | 7512 | cfg_op_channel = 1; | 
|  | 7513 | } else { | 
|  | 7514 | op_reg_class = 81; | 
|  | 7515 | /* | 
|  | 7516 | * Use random operation channel from (1, 6, 11) | 
|  | 7517 | *if no other preference is indicated. | 
|  | 7518 | */ | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 7519 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) | 
|  | 7520 | op_channel = 1; | 
|  | 7521 | else | 
|  | 7522 | op_channel = 1 + (r % 3) * 5; | 
| Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 7523 | cfg_op_channel = 0; | 
|  | 7524 | } | 
|  | 7525 | ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel, | 
|  | 7526 | cfg_op_channel); | 
|  | 7527 | if (ret) | 
|  | 7528 | wpa_printf(MSG_ERROR, "P2P: Own oper channel update " | 
|  | 7529 | "failed: %d", ret); | 
|  | 7530 | } | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7531 |  | 
|  | 7532 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) { | 
|  | 7533 | if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan, | 
|  | 7534 | wpa_s->conf->p2p_pref_chan) < 0) { | 
|  | 7535 | wpa_printf(MSG_ERROR, "P2P: Preferred channel list " | 
|  | 7536 | "update failed"); | 
|  | 7537 | } | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 7538 |  | 
|  | 7539 | if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) { | 
|  | 7540 | wpa_printf(MSG_ERROR, "P2P: No GO channel list " | 
|  | 7541 | "update failed"); | 
|  | 7542 | } | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7543 | } | 
| Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 7544 |  | 
|  | 7545 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN) | 
|  | 7546 | p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7547 | } | 
|  | 7548 |  | 
|  | 7549 |  | 
|  | 7550 | int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start, | 
|  | 7551 | int duration) | 
|  | 7552 | { | 
|  | 7553 | if (!wpa_s->ap_iface) | 
|  | 7554 | return -1; | 
|  | 7555 | return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start, | 
|  | 7556 | duration); | 
|  | 7557 | } | 
|  | 7558 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7559 |  | 
|  | 7560 | int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled) | 
|  | 7561 | { | 
|  | 7562 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 7563 | return -1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7564 |  | 
|  | 7565 | wpa_s->global->cross_connection = enabled; | 
|  | 7566 | p2p_set_cross_connect(wpa_s->global->p2p, enabled); | 
|  | 7567 |  | 
|  | 7568 | if (!enabled) { | 
|  | 7569 | struct wpa_supplicant *iface; | 
|  | 7570 |  | 
|  | 7571 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) | 
|  | 7572 | { | 
|  | 7573 | if (iface->cross_connect_enabled == 0) | 
|  | 7574 | continue; | 
|  | 7575 |  | 
|  | 7576 | iface->cross_connect_enabled = 0; | 
|  | 7577 | iface->cross_connect_in_use = 0; | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 7578 | wpa_msg_global(iface->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 7579 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", | 
|  | 7580 | iface->ifname, | 
|  | 7581 | iface->cross_connect_uplink); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7582 | } | 
|  | 7583 | } | 
|  | 7584 |  | 
|  | 7585 | return 0; | 
|  | 7586 | } | 
|  | 7587 |  | 
|  | 7588 |  | 
|  | 7589 | static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink) | 
|  | 7590 | { | 
|  | 7591 | struct wpa_supplicant *iface; | 
|  | 7592 |  | 
|  | 7593 | if (!uplink->global->cross_connection) | 
|  | 7594 | return; | 
|  | 7595 |  | 
|  | 7596 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { | 
|  | 7597 | if (!iface->cross_connect_enabled) | 
|  | 7598 | continue; | 
|  | 7599 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != | 
|  | 7600 | 0) | 
|  | 7601 | continue; | 
|  | 7602 | if (iface->ap_iface == NULL) | 
|  | 7603 | continue; | 
|  | 7604 | if (iface->cross_connect_in_use) | 
|  | 7605 | continue; | 
|  | 7606 |  | 
|  | 7607 | iface->cross_connect_in_use = 1; | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 7608 | wpa_msg_global(iface->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 7609 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", | 
|  | 7610 | iface->ifname, iface->cross_connect_uplink); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7611 | } | 
|  | 7612 | } | 
|  | 7613 |  | 
|  | 7614 |  | 
|  | 7615 | static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink) | 
|  | 7616 | { | 
|  | 7617 | struct wpa_supplicant *iface; | 
|  | 7618 |  | 
|  | 7619 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { | 
|  | 7620 | if (!iface->cross_connect_enabled) | 
|  | 7621 | continue; | 
|  | 7622 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != | 
|  | 7623 | 0) | 
|  | 7624 | continue; | 
|  | 7625 | if (!iface->cross_connect_in_use) | 
|  | 7626 | continue; | 
|  | 7627 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 7628 | wpa_msg_global(iface->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 7629 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", | 
|  | 7630 | iface->ifname, iface->cross_connect_uplink); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7631 | iface->cross_connect_in_use = 0; | 
|  | 7632 | } | 
|  | 7633 | } | 
|  | 7634 |  | 
|  | 7635 |  | 
|  | 7636 | void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s) | 
|  | 7637 | { | 
|  | 7638 | if (wpa_s->ap_iface || wpa_s->current_ssid == NULL || | 
|  | 7639 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA || | 
|  | 7640 | wpa_s->cross_connect_disallowed) | 
|  | 7641 | wpas_p2p_disable_cross_connect(wpa_s); | 
|  | 7642 | else | 
|  | 7643 | wpas_p2p_enable_cross_connect(wpa_s); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7644 | if (!wpa_s->ap_iface && | 
|  | 7645 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) | 
|  | 7646 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7647 | } | 
|  | 7648 |  | 
|  | 7649 |  | 
|  | 7650 | void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s) | 
|  | 7651 | { | 
|  | 7652 | wpas_p2p_disable_cross_connect(wpa_s); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7653 | if (!wpa_s->ap_iface && | 
|  | 7654 | !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout, | 
|  | 7655 | wpa_s, NULL)) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7656 | wpas_p2p_set_group_idle_timeout(wpa_s); | 
|  | 7657 | } | 
|  | 7658 |  | 
|  | 7659 |  | 
|  | 7660 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s) | 
|  | 7661 | { | 
|  | 7662 | struct wpa_supplicant *iface; | 
|  | 7663 |  | 
|  | 7664 | if (!wpa_s->global->cross_connection) | 
|  | 7665 | return; | 
|  | 7666 |  | 
|  | 7667 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { | 
|  | 7668 | if (iface == wpa_s) | 
|  | 7669 | continue; | 
|  | 7670 | if (iface->drv_flags & | 
|  | 7671 | WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE) | 
|  | 7672 | continue; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 7673 | if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) && | 
|  | 7674 | iface != wpa_s->parent) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7675 | continue; | 
|  | 7676 |  | 
|  | 7677 | wpa_s->cross_connect_enabled = 1; | 
|  | 7678 | os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname, | 
|  | 7679 | sizeof(wpa_s->cross_connect_uplink)); | 
|  | 7680 | wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from " | 
|  | 7681 | "%s to %s whenever uplink is available", | 
|  | 7682 | wpa_s->ifname, wpa_s->cross_connect_uplink); | 
|  | 7683 |  | 
|  | 7684 | if (iface->ap_iface || iface->current_ssid == NULL || | 
|  | 7685 | iface->current_ssid->mode != WPAS_MODE_INFRA || | 
|  | 7686 | iface->cross_connect_disallowed || | 
|  | 7687 | iface->wpa_state != WPA_COMPLETED) | 
|  | 7688 | break; | 
|  | 7689 |  | 
|  | 7690 | wpa_s->cross_connect_in_use = 1; | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 7691 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 7692 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", | 
|  | 7693 | wpa_s->ifname, wpa_s->cross_connect_uplink); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7694 | break; | 
|  | 7695 | } | 
|  | 7696 | } | 
|  | 7697 |  | 
|  | 7698 |  | 
|  | 7699 | int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s) | 
|  | 7700 | { | 
|  | 7701 | if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT && | 
|  | 7702 | !wpa_s->p2p_in_provisioning) | 
|  | 7703 | return 0; /* not P2P client operation */ | 
|  | 7704 |  | 
|  | 7705 | wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC " | 
|  | 7706 | "session overlap"); | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 7707 | if (wpa_s != wpa_s->p2pdev) | 
|  | 7708 | wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP); | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 7709 | wpas_p2p_group_formation_failed(wpa_s, 0); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7710 | return 1; | 
|  | 7711 | } | 
|  | 7712 |  | 
|  | 7713 |  | 
| Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 7714 | void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx) | 
|  | 7715 | { | 
|  | 7716 | struct wpa_supplicant *wpa_s = eloop_ctx; | 
|  | 7717 | wpas_p2p_notif_pbc_overlap(wpa_s); | 
|  | 7718 | } | 
|  | 7719 |  | 
|  | 7720 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 7721 | void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s, | 
|  | 7722 | enum wpas_p2p_channel_update_trig trig) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7723 | { | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 7724 | struct p2p_channels chan, cli_chan; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 7725 | struct wpa_used_freq_data *freqs = NULL; | 
|  | 7726 | unsigned int num = wpa_s->num_multichan_concurrent; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7727 |  | 
|  | 7728 | if (wpa_s->global == NULL || wpa_s->global->p2p == NULL) | 
|  | 7729 | return; | 
|  | 7730 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 7731 | freqs = os_calloc(num, sizeof(struct wpa_used_freq_data)); | 
|  | 7732 | if (!freqs) | 
|  | 7733 | return; | 
|  | 7734 |  | 
|  | 7735 | num = get_shared_radio_freqs_data(wpa_s, freqs, num); | 
|  | 7736 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7737 | os_memset(&chan, 0, sizeof(chan)); | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 7738 | os_memset(&cli_chan, 0, sizeof(cli_chan)); | 
|  | 7739 | if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7740 | wpa_printf(MSG_ERROR, "P2P: Failed to update supported " | 
|  | 7741 | "channel list"); | 
|  | 7742 | return; | 
|  | 7743 | } | 
|  | 7744 |  | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 7745 | p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan); | 
| Dmitry Shmidt | 684785c | 2014-05-12 13:34:29 -0700 | [diff] [blame] | 7746 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 7747 | wpas_p2p_optimize_listen_channel(wpa_s, freqs, num); | 
| Dmitry Shmidt | 684785c | 2014-05-12 13:34:29 -0700 | [diff] [blame] | 7748 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 7749 | /* | 
|  | 7750 | * The used frequencies map changed, so it is possible that a GO is | 
|  | 7751 | * using a channel that is no longer valid for P2P use. It is also | 
|  | 7752 | * possible that due to policy consideration, it would be preferable to | 
|  | 7753 | * move it to a frequency already used by other station interfaces. | 
|  | 7754 | */ | 
|  | 7755 | wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig); | 
|  | 7756 |  | 
|  | 7757 | os_free(freqs); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7758 | } | 
|  | 7759 |  | 
|  | 7760 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 7761 | static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s, | 
|  | 7762 | struct wpa_scan_results *scan_res) | 
|  | 7763 | { | 
|  | 7764 | wpa_printf(MSG_DEBUG, "P2P: Ignore scan results"); | 
|  | 7765 | } | 
|  | 7766 |  | 
|  | 7767 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7768 | int wpas_p2p_cancel(struct wpa_supplicant *wpa_s) | 
|  | 7769 | { | 
|  | 7770 | struct wpa_global *global = wpa_s->global; | 
|  | 7771 | int found = 0; | 
|  | 7772 | const u8 *peer; | 
|  | 7773 |  | 
|  | 7774 | if (global->p2p == NULL) | 
|  | 7775 | return -1; | 
|  | 7776 |  | 
|  | 7777 | wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation"); | 
|  | 7778 |  | 
|  | 7779 | if (wpa_s->pending_interface_name[0] && | 
|  | 7780 | !is_zero_ether_addr(wpa_s->pending_interface_addr)) | 
|  | 7781 | found = 1; | 
|  | 7782 |  | 
|  | 7783 | peer = p2p_get_go_neg_peer(global->p2p); | 
|  | 7784 | if (peer) { | 
|  | 7785 | wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer " | 
|  | 7786 | MACSTR, MAC2STR(peer)); | 
|  | 7787 | p2p_unauthorize(global->p2p, peer); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7788 | found = 1; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7789 | } | 
|  | 7790 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 7791 | if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) { | 
|  | 7792 | wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join"); | 
|  | 7793 | wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore; | 
|  | 7794 | found = 1; | 
|  | 7795 | } | 
|  | 7796 |  | 
|  | 7797 | if (wpa_s->pending_pd_before_join) { | 
|  | 7798 | wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join"); | 
|  | 7799 | wpa_s->pending_pd_before_join = 0; | 
|  | 7800 | found = 1; | 
|  | 7801 | } | 
|  | 7802 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7803 | wpas_p2p_stop_find(wpa_s); | 
|  | 7804 |  | 
|  | 7805 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
|  | 7806 | if (wpa_s == global->p2p_group_formation && | 
|  | 7807 | (wpa_s->p2p_in_provisioning || | 
|  | 7808 | wpa_s->parent->pending_interface_type == | 
|  | 7809 | WPA_IF_P2P_CLIENT)) { | 
|  | 7810 | wpa_printf(MSG_DEBUG, "P2P: Interface %s in group " | 
|  | 7811 | "formation found - cancelling", | 
|  | 7812 | wpa_s->ifname); | 
|  | 7813 | found = 1; | 
|  | 7814 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 7815 | wpa_s->p2pdev, NULL); | 
| Jouni Malinen | adddfc4 | 2012-10-03 14:31:41 -0700 | [diff] [blame] | 7816 | if (wpa_s->p2p_in_provisioning) { | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 7817 | wpas_group_formation_completed(wpa_s, 0, 0); | 
| Jouni Malinen | adddfc4 | 2012-10-03 14:31:41 -0700 | [diff] [blame] | 7818 | break; | 
|  | 7819 | } | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 7820 | wpas_p2p_group_delete(wpa_s, | 
|  | 7821 | P2P_GROUP_REMOVAL_REQUESTED); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7822 | break; | 
| Dmitry Shmidt | 21de214 | 2014-04-08 10:50:52 -0700 | [diff] [blame] | 7823 | } else if (wpa_s->p2p_in_invitation) { | 
|  | 7824 | wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling", | 
|  | 7825 | wpa_s->ifname); | 
|  | 7826 | found = 1; | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 7827 | wpas_p2p_group_formation_failed(wpa_s, 0); | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 7828 | break; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7829 | } | 
|  | 7830 | } | 
|  | 7831 |  | 
|  | 7832 | if (!found) { | 
|  | 7833 | wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found"); | 
|  | 7834 | return -1; | 
|  | 7835 | } | 
|  | 7836 |  | 
|  | 7837 | return 0; | 
|  | 7838 | } | 
|  | 7839 |  | 
|  | 7840 |  | 
|  | 7841 | void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s) | 
|  | 7842 | { | 
|  | 7843 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) | 
|  | 7844 | return; | 
|  | 7845 |  | 
|  | 7846 | wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not " | 
|  | 7847 | "being available anymore"); | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 7848 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7849 | } | 
|  | 7850 |  | 
|  | 7851 |  | 
|  | 7852 | void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s, | 
|  | 7853 | int freq_24, int freq_5, int freq_overall) | 
|  | 7854 | { | 
|  | 7855 | struct p2p_data *p2p = wpa_s->global->p2p; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 7856 | if (p2p == NULL) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7857 | return; | 
|  | 7858 | p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall); | 
|  | 7859 | } | 
|  | 7860 |  | 
|  | 7861 |  | 
|  | 7862 | int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr) | 
|  | 7863 | { | 
|  | 7864 | u8 peer[ETH_ALEN]; | 
|  | 7865 | struct p2p_data *p2p = wpa_s->global->p2p; | 
|  | 7866 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 7867 | if (p2p == NULL) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7868 | return -1; | 
|  | 7869 |  | 
|  | 7870 | if (hwaddr_aton(addr, peer)) | 
|  | 7871 | return -1; | 
|  | 7872 |  | 
|  | 7873 | return p2p_unauthorize(p2p, peer); | 
|  | 7874 | } | 
|  | 7875 |  | 
|  | 7876 |  | 
|  | 7877 | /** | 
|  | 7878 | * wpas_p2p_disconnect - Disconnect from a P2P Group | 
|  | 7879 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 7880 | * Returns: 0 on success, -1 on failure | 
|  | 7881 | * | 
|  | 7882 | * This can be used to disconnect from a group in which the local end is a P2P | 
|  | 7883 | * Client or to end a P2P Group in case the local end is the Group Owner. If a | 
|  | 7884 | * virtual network interface was created for this group, that interface will be | 
|  | 7885 | * removed. Otherwise, only the configured P2P group network will be removed | 
|  | 7886 | * from the interface. | 
|  | 7887 | */ | 
|  | 7888 | int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s) | 
|  | 7889 | { | 
|  | 7890 |  | 
|  | 7891 | if (wpa_s == NULL) | 
|  | 7892 | return -1; | 
|  | 7893 |  | 
| Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 7894 | return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ? | 
|  | 7895 | -1 : 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7896 | } | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7897 |  | 
|  | 7898 |  | 
|  | 7899 | int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s) | 
|  | 7900 | { | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 7901 | int ret; | 
|  | 7902 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7903 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 7904 | return 0; | 
|  | 7905 |  | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 7906 | ret = p2p_in_progress(wpa_s->global->p2p); | 
|  | 7907 | if (ret == 0) { | 
|  | 7908 | /* | 
|  | 7909 | * Check whether there is an ongoing WPS provisioning step (or | 
|  | 7910 | * other parts of group formation) on another interface since | 
|  | 7911 | * p2p_in_progress() does not report this to avoid issues for | 
|  | 7912 | * scans during such provisioning step. | 
|  | 7913 | */ | 
|  | 7914 | if (wpa_s->global->p2p_group_formation && | 
|  | 7915 | wpa_s->global->p2p_group_formation != wpa_s) { | 
|  | 7916 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) " | 
|  | 7917 | "in group formation", | 
|  | 7918 | wpa_s->global->p2p_group_formation->ifname); | 
|  | 7919 | ret = 1; | 
|  | 7920 | } | 
|  | 7921 | } | 
|  | 7922 |  | 
| Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 7923 | if (!ret && wpa_s->global->p2p_go_wait_client.sec) { | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 7924 | struct os_reltime now; | 
|  | 7925 | os_get_reltime(&now); | 
|  | 7926 | if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client, | 
|  | 7927 | P2P_MAX_INITIAL_CONN_WAIT_GO)) { | 
| Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 7928 | /* Wait for the first client has expired */ | 
|  | 7929 | wpa_s->global->p2p_go_wait_client.sec = 0; | 
|  | 7930 | } else { | 
|  | 7931 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation"); | 
|  | 7932 | ret = 1; | 
|  | 7933 | } | 
|  | 7934 | } | 
|  | 7935 |  | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 7936 | return ret; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7937 | } | 
|  | 7938 |  | 
|  | 7939 |  | 
|  | 7940 | void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s, | 
|  | 7941 | struct wpa_ssid *ssid) | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7942 | { | 
|  | 7943 | if (wpa_s->p2p_in_provisioning && ssid->p2p_group && | 
|  | 7944 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 7945 | wpa_s->p2pdev, NULL) > 0) { | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7946 | /** | 
|  | 7947 | * Remove the network by scheduling the group formation | 
|  | 7948 | * timeout to happen immediately. The teardown code | 
|  | 7949 | * needs to be scheduled to run asynch later so that we | 
|  | 7950 | * don't delete data from under ourselves unexpectedly. | 
|  | 7951 | * Calling wpas_p2p_group_formation_timeout directly | 
|  | 7952 | * causes a series of crashes in WPS failure scenarios. | 
|  | 7953 | */ | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7954 | wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to " | 
|  | 7955 | "P2P group network getting removed"); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7956 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 7957 | wpa_s->p2pdev, NULL); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7958 | } | 
|  | 7959 | } | 
|  | 7960 |  | 
|  | 7961 |  | 
|  | 7962 | struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s, | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 7963 | const u8 *addr, const u8 *ssid, | 
|  | 7964 | size_t ssid_len) | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7965 | { | 
|  | 7966 | struct wpa_ssid *s; | 
|  | 7967 | size_t i; | 
|  | 7968 |  | 
|  | 7969 | for (s = wpa_s->conf->ssid; s; s = s->next) { | 
|  | 7970 | if (s->disabled != 2) | 
|  | 7971 | continue; | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 7972 | if (ssid && | 
|  | 7973 | (ssid_len != s->ssid_len || | 
|  | 7974 | os_memcmp(ssid, s->ssid, ssid_len) != 0)) | 
|  | 7975 | continue; | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7976 | if (addr == NULL) { | 
|  | 7977 | if (s->mode == WPAS_MODE_P2P_GO) | 
|  | 7978 | return s; | 
|  | 7979 | continue; | 
|  | 7980 | } | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7981 | if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0) | 
|  | 7982 | return s; /* peer is GO in the persistent group */ | 
|  | 7983 | if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL) | 
|  | 7984 | continue; | 
|  | 7985 | for (i = 0; i < s->num_p2p_clients; i++) { | 
| Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 7986 | if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7987 | addr, ETH_ALEN) == 0) | 
|  | 7988 | return s; /* peer is P2P client in persistent | 
|  | 7989 | * group */ | 
|  | 7990 | } | 
|  | 7991 | } | 
|  | 7992 |  | 
|  | 7993 | return NULL; | 
|  | 7994 | } | 
|  | 7995 |  | 
|  | 7996 |  | 
|  | 7997 | void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s, | 
|  | 7998 | const u8 *addr) | 
|  | 7999 | { | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 8000 | if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 8001 | wpa_s->p2pdev, NULL) > 0) { | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 8002 | /* | 
|  | 8003 | * This can happen if WPS provisioning step is not terminated | 
|  | 8004 | * cleanly (e.g., P2P Client does not send WSC_Done). Since the | 
|  | 8005 | * peer was able to connect, there is no need to time out group | 
|  | 8006 | * formation after this, though. In addition, this is used with | 
|  | 8007 | * the initial connection wait on the GO as a separate formation | 
|  | 8008 | * timeout and as such, expected to be hit after the initial WPS | 
|  | 8009 | * provisioning step. | 
|  | 8010 | */ | 
|  | 8011 | wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection"); | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 8012 |  | 
|  | 8013 | if (!wpa_s->p2p_go_group_formation_completed && | 
|  | 8014 | !wpa_s->group_formation_reported) { | 
|  | 8015 | /* | 
|  | 8016 | * GO has not yet notified group formation success since | 
|  | 8017 | * the WPS step was not completed cleanly. Do that | 
|  | 8018 | * notification now since the P2P Client was able to | 
|  | 8019 | * connect and as such, must have received the | 
|  | 8020 | * credential from the WPS step. | 
|  | 8021 | */ | 
|  | 8022 | if (wpa_s->global->p2p) | 
|  | 8023 | p2p_wps_success_cb(wpa_s->global->p2p, addr); | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 8024 | wpas_group_formation_completed(wpa_s, 1, 0); | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 8025 | } | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 8026 | } | 
|  | 8027 | if (!wpa_s->p2p_go_group_formation_completed) { | 
|  | 8028 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection"); | 
|  | 8029 | wpa_s->p2p_go_group_formation_completed = 1; | 
|  | 8030 | wpa_s->global->p2p_group_formation = NULL; | 
|  | 8031 | wpa_s->p2p_in_provisioning = 0; | 
| Dmitry Shmidt | 21de214 | 2014-04-08 10:50:52 -0700 | [diff] [blame] | 8032 | wpa_s->p2p_in_invitation = 0; | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 8033 | } | 
| Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 8034 | wpa_s->global->p2p_go_wait_client.sec = 0; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 8035 | if (addr == NULL) | 
|  | 8036 | return; | 
|  | 8037 | wpas_p2p_add_persistent_group_client(wpa_s, addr); | 
|  | 8038 | } | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 8039 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 8040 |  | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 8041 | static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, | 
|  | 8042 | int group_added) | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 8043 | { | 
|  | 8044 | struct wpa_supplicant *group = wpa_s; | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 8045 | int ret = 0; | 
|  | 8046 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 8047 | if (wpa_s->global->p2p_group_formation) | 
|  | 8048 | group = wpa_s->global->p2p_group_formation; | 
| Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 8049 | wpa_s = wpa_s->global->p2p_init_wpa_s; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 8050 | offchannel_send_action_done(wpa_s); | 
|  | 8051 | if (group_added) | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 8052 | ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 8053 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation"); | 
|  | 8054 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin, | 
|  | 8055 | wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0, | 
|  | 8056 | 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 8057 | wpa_s->p2p_go_vht_center_freq2, | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 8058 | wpa_s->p2p_persistent_id, | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 8059 | wpa_s->p2p_pd_before_go_neg, | 
| Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 8060 | wpa_s->p2p_go_ht40, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 8061 | wpa_s->p2p_go_vht, | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 8062 | wpa_s->p2p_go_max_oper_chwidth, NULL, 0); | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 8063 | return ret; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 8064 | } | 
|  | 8065 |  | 
|  | 8066 |  | 
|  | 8067 | int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s) | 
|  | 8068 | { | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 8069 | int res; | 
|  | 8070 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 8071 | if (!wpa_s->p2p_fallback_to_go_neg || | 
|  | 8072 | wpa_s->p2p_in_provisioning <= 5) | 
|  | 8073 | return 0; | 
|  | 8074 |  | 
|  | 8075 | if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0) | 
|  | 8076 | return 0; /* peer operating as a GO */ | 
|  | 8077 |  | 
|  | 8078 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - " | 
|  | 8079 | "fallback to GO Negotiation"); | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 8080 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 8081 | "reason=GO-not-found"); | 
|  | 8082 | res = wpas_p2p_fallback_to_go_neg(wpa_s, 1); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 8083 |  | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 8084 | return res == 1 ? 2 : 1; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 8085 | } | 
|  | 8086 |  | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 8087 |  | 
|  | 8088 | unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s) | 
|  | 8089 | { | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 8090 | struct wpa_supplicant *ifs; | 
|  | 8091 |  | 
|  | 8092 | if (wpa_s->wpa_state > WPA_SCANNING) { | 
|  | 8093 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to " | 
|  | 8094 | "concurrent operation", | 
| Dmitry Shmidt | 09f57ba | 2014-06-10 16:07:13 -0700 | [diff] [blame] | 8095 | wpa_s->conf->p2p_search_delay); | 
|  | 8096 | return wpa_s->conf->p2p_search_delay; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 8097 | } | 
|  | 8098 |  | 
| Dmitry Shmidt | 01904cf | 2013-12-05 11:08:35 -0800 | [diff] [blame] | 8099 | dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant, | 
|  | 8100 | radio_list) { | 
|  | 8101 | if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) { | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 8102 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search " | 
|  | 8103 | "delay due to concurrent operation on " | 
|  | 8104 | "interface %s", | 
| Dmitry Shmidt | 09f57ba | 2014-06-10 16:07:13 -0700 | [diff] [blame] | 8105 | wpa_s->conf->p2p_search_delay, | 
|  | 8106 | ifs->ifname); | 
|  | 8107 | return wpa_s->conf->p2p_search_delay; | 
| Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 8108 | } | 
|  | 8109 | } | 
|  | 8110 |  | 
|  | 8111 | return 0; | 
|  | 8112 | } | 
|  | 8113 |  | 
| Dmitry Shmidt | 37d4d6a | 2013-03-18 13:09:42 -0700 | [diff] [blame] | 8114 |  | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8115 | static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s, | 
|  | 8116 | struct wpa_ssid *s, const u8 *addr, | 
|  | 8117 | int iface_addr) | 
|  | 8118 | { | 
|  | 8119 | struct psk_list_entry *psk, *tmp; | 
|  | 8120 | int changed = 0; | 
|  | 8121 |  | 
|  | 8122 | dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry, | 
|  | 8123 | list) { | 
|  | 8124 | if ((iface_addr && !psk->p2p && | 
|  | 8125 | os_memcmp(addr, psk->addr, ETH_ALEN) == 0) || | 
|  | 8126 | (!iface_addr && psk->p2p && | 
|  | 8127 | os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) { | 
|  | 8128 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 8129 | "P2P: Remove persistent group PSK list entry for " | 
|  | 8130 | MACSTR " p2p=%u", | 
|  | 8131 | MAC2STR(psk->addr), psk->p2p); | 
|  | 8132 | dl_list_del(&psk->list); | 
|  | 8133 | os_free(psk); | 
|  | 8134 | changed++; | 
|  | 8135 | } | 
|  | 8136 | } | 
|  | 8137 |  | 
|  | 8138 | return changed; | 
|  | 8139 | } | 
|  | 8140 |  | 
|  | 8141 |  | 
|  | 8142 | void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr, | 
|  | 8143 | const u8 *p2p_dev_addr, | 
|  | 8144 | const u8 *psk, size_t psk_len) | 
|  | 8145 | { | 
|  | 8146 | struct wpa_ssid *ssid = wpa_s->current_ssid; | 
|  | 8147 | struct wpa_ssid *persistent; | 
| Dmitry Shmidt | 7832adb | 2014-04-29 10:53:02 -0700 | [diff] [blame] | 8148 | struct psk_list_entry *p, *last; | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8149 |  | 
|  | 8150 | if (psk_len != sizeof(p->psk)) | 
|  | 8151 | return; | 
|  | 8152 |  | 
|  | 8153 | if (p2p_dev_addr) { | 
|  | 8154 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR | 
|  | 8155 | " p2p_dev_addr=" MACSTR, | 
|  | 8156 | MAC2STR(mac_addr), MAC2STR(p2p_dev_addr)); | 
|  | 8157 | if (is_zero_ether_addr(p2p_dev_addr)) | 
|  | 8158 | p2p_dev_addr = NULL; | 
|  | 8159 | } else { | 
|  | 8160 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR, | 
|  | 8161 | MAC2STR(mac_addr)); | 
|  | 8162 | } | 
|  | 8163 |  | 
|  | 8164 | if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { | 
|  | 8165 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation"); | 
|  | 8166 | /* To be added to persistent group once created */ | 
|  | 8167 | if (wpa_s->global->add_psk == NULL) { | 
|  | 8168 | wpa_s->global->add_psk = os_zalloc(sizeof(*p)); | 
|  | 8169 | if (wpa_s->global->add_psk == NULL) | 
|  | 8170 | return; | 
|  | 8171 | } | 
|  | 8172 | p = wpa_s->global->add_psk; | 
|  | 8173 | if (p2p_dev_addr) { | 
|  | 8174 | p->p2p = 1; | 
|  | 8175 | os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); | 
|  | 8176 | } else { | 
|  | 8177 | p->p2p = 0; | 
|  | 8178 | os_memcpy(p->addr, mac_addr, ETH_ALEN); | 
|  | 8179 | } | 
|  | 8180 | os_memcpy(p->psk, psk, psk_len); | 
|  | 8181 | return; | 
|  | 8182 | } | 
|  | 8183 |  | 
|  | 8184 | if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) { | 
|  | 8185 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO"); | 
|  | 8186 | return; | 
|  | 8187 | } | 
|  | 8188 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 8189 | persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid, | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8190 | ssid->ssid_len); | 
|  | 8191 | if (!persistent) { | 
|  | 8192 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK"); | 
|  | 8193 | return; | 
|  | 8194 | } | 
|  | 8195 |  | 
|  | 8196 | p = os_zalloc(sizeof(*p)); | 
|  | 8197 | if (p == NULL) | 
|  | 8198 | return; | 
|  | 8199 | if (p2p_dev_addr) { | 
|  | 8200 | p->p2p = 1; | 
|  | 8201 | os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); | 
|  | 8202 | } else { | 
|  | 8203 | p->p2p = 0; | 
|  | 8204 | os_memcpy(p->addr, mac_addr, ETH_ALEN); | 
|  | 8205 | } | 
|  | 8206 | os_memcpy(p->psk, psk, psk_len); | 
|  | 8207 |  | 
| Dmitry Shmidt | 7832adb | 2014-04-29 10:53:02 -0700 | [diff] [blame] | 8208 | if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS && | 
|  | 8209 | (last = dl_list_last(&persistent->psk_list, | 
|  | 8210 | struct psk_list_entry, list))) { | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8211 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for " | 
|  | 8212 | MACSTR " (p2p=%u) to make room for a new one", | 
|  | 8213 | MAC2STR(last->addr), last->p2p); | 
|  | 8214 | dl_list_del(&last->list); | 
|  | 8215 | os_free(last); | 
|  | 8216 | } | 
|  | 8217 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 8218 | wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent, | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8219 | p2p_dev_addr ? p2p_dev_addr : mac_addr, | 
|  | 8220 | p2p_dev_addr == NULL); | 
|  | 8221 | if (p2p_dev_addr) { | 
|  | 8222 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr=" | 
|  | 8223 | MACSTR, MAC2STR(p2p_dev_addr)); | 
|  | 8224 | } else { | 
|  | 8225 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR, | 
|  | 8226 | MAC2STR(mac_addr)); | 
|  | 8227 | } | 
|  | 8228 | dl_list_add(&persistent->psk_list, &p->list); | 
|  | 8229 |  | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 8230 | if (wpa_s->p2pdev->conf->update_config && | 
|  | 8231 | wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf)) | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8232 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8233 | } | 
|  | 8234 |  | 
|  | 8235 |  | 
|  | 8236 | static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s, | 
|  | 8237 | struct wpa_ssid *s, const u8 *addr, | 
|  | 8238 | int iface_addr) | 
|  | 8239 | { | 
|  | 8240 | int res; | 
|  | 8241 |  | 
|  | 8242 | res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr); | 
| Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 8243 | if (res > 0 && wpa_s->conf->update_config && | 
|  | 8244 | wpa_config_write(wpa_s->confname, wpa_s->conf)) | 
|  | 8245 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 8246 | "P2P: Failed to update configuration"); | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8247 | } | 
|  | 8248 |  | 
|  | 8249 |  | 
|  | 8250 | static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s, | 
|  | 8251 | const u8 *peer, int iface_addr) | 
|  | 8252 | { | 
|  | 8253 | struct hostapd_data *hapd; | 
|  | 8254 | struct hostapd_wpa_psk *psk, *prev, *rem; | 
|  | 8255 | struct sta_info *sta; | 
|  | 8256 |  | 
|  | 8257 | if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL || | 
|  | 8258 | wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO) | 
|  | 8259 | return; | 
|  | 8260 |  | 
|  | 8261 | /* Remove per-station PSK entry */ | 
|  | 8262 | hapd = wpa_s->ap_iface->bss[0]; | 
|  | 8263 | prev = NULL; | 
|  | 8264 | psk = hapd->conf->ssid.wpa_psk; | 
|  | 8265 | while (psk) { | 
|  | 8266 | if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) || | 
|  | 8267 | (!iface_addr && | 
|  | 8268 | os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) { | 
|  | 8269 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for " | 
|  | 8270 | MACSTR " iface_addr=%d", | 
|  | 8271 | MAC2STR(peer), iface_addr); | 
|  | 8272 | if (prev) | 
|  | 8273 | prev->next = psk->next; | 
|  | 8274 | else | 
|  | 8275 | hapd->conf->ssid.wpa_psk = psk->next; | 
|  | 8276 | rem = psk; | 
|  | 8277 | psk = psk->next; | 
|  | 8278 | os_free(rem); | 
|  | 8279 | } else { | 
|  | 8280 | prev = psk; | 
|  | 8281 | psk = psk->next; | 
|  | 8282 | } | 
|  | 8283 | } | 
|  | 8284 |  | 
|  | 8285 | /* Disconnect from group */ | 
|  | 8286 | if (iface_addr) | 
|  | 8287 | sta = ap_get_sta(hapd, peer); | 
|  | 8288 | else | 
|  | 8289 | sta = ap_get_sta_p2p(hapd, peer); | 
|  | 8290 | if (sta) { | 
|  | 8291 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR | 
|  | 8292 | " (iface_addr=%d) from group", | 
|  | 8293 | MAC2STR(peer), iface_addr); | 
|  | 8294 | hostapd_drv_sta_deauth(hapd, sta->addr, | 
|  | 8295 | WLAN_REASON_DEAUTH_LEAVING); | 
|  | 8296 | ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING); | 
|  | 8297 | } | 
|  | 8298 | } | 
|  | 8299 |  | 
|  | 8300 |  | 
|  | 8301 | void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer, | 
|  | 8302 | int iface_addr) | 
|  | 8303 | { | 
|  | 8304 | struct wpa_ssid *s; | 
|  | 8305 | struct wpa_supplicant *w; | 
| Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 8306 | struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s; | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8307 |  | 
|  | 8308 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer)); | 
|  | 8309 |  | 
|  | 8310 | /* Remove from any persistent group */ | 
| Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 8311 | for (s = p2p_wpa_s->conf->ssid; s; s = s->next) { | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8312 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) | 
|  | 8313 | continue; | 
|  | 8314 | if (!iface_addr) | 
| Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 8315 | wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0); | 
|  | 8316 | wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr); | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8317 | } | 
|  | 8318 |  | 
|  | 8319 | /* Remove from any operating group */ | 
|  | 8320 | for (w = wpa_s->global->ifaces; w; w = w->next) | 
|  | 8321 | wpas_p2p_remove_client_go(w, peer, iface_addr); | 
|  | 8322 | } | 
|  | 8323 |  | 
|  | 8324 |  | 
|  | 8325 | static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx) | 
|  | 8326 | { | 
|  | 8327 | struct wpa_supplicant *wpa_s = eloop_ctx; | 
|  | 8328 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE); | 
|  | 8329 | } | 
|  | 8330 |  | 
|  | 8331 |  | 
| Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 8332 | static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx) | 
|  | 8333 | { | 
|  | 8334 | struct wpa_supplicant *wpa_s = eloop_ctx; | 
|  | 8335 |  | 
|  | 8336 | wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group"); | 
|  | 8337 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT); | 
|  | 8338 | } | 
|  | 8339 |  | 
|  | 8340 |  | 
|  | 8341 | int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq, | 
|  | 8342 | struct wpa_ssid *ssid) | 
|  | 8343 | { | 
|  | 8344 | struct wpa_supplicant *iface; | 
|  | 8345 |  | 
|  | 8346 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { | 
|  | 8347 | if (!iface->current_ssid || | 
|  | 8348 | iface->current_ssid->frequency == freq || | 
|  | 8349 | (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE && | 
|  | 8350 | !iface->current_ssid->p2p_group)) | 
|  | 8351 | continue; | 
|  | 8352 |  | 
|  | 8353 | /* Remove the connection with least priority */ | 
|  | 8354 | if (!wpas_is_p2p_prioritized(iface)) { | 
|  | 8355 | /* STA connection has priority over existing | 
|  | 8356 | * P2P connection, so remove the interface. */ | 
|  | 8357 | wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict"); | 
|  | 8358 | eloop_register_timeout(0, 0, | 
|  | 8359 | wpas_p2p_group_freq_conflict, | 
|  | 8360 | iface, NULL); | 
|  | 8361 | /* If connection in progress is P2P connection, do not | 
|  | 8362 | * proceed for the connection. */ | 
|  | 8363 | if (wpa_s == iface) | 
|  | 8364 | return -1; | 
|  | 8365 | else | 
|  | 8366 | return 0; | 
|  | 8367 | } else { | 
|  | 8368 | /* P2P connection has priority, disable the STA network | 
|  | 8369 | */ | 
|  | 8370 | wpa_supplicant_disable_network(wpa_s->global->ifaces, | 
|  | 8371 | ssid); | 
|  | 8372 | wpa_msg(wpa_s->global->ifaces, MSG_INFO, | 
|  | 8373 | WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id); | 
|  | 8374 | os_memset(wpa_s->global->ifaces->pending_bssid, 0, | 
|  | 8375 | ETH_ALEN); | 
|  | 8376 | /* If P2P connection is in progress, continue | 
|  | 8377 | * connecting...*/ | 
|  | 8378 | if (wpa_s == iface) | 
|  | 8379 | return 0; | 
|  | 8380 | else | 
|  | 8381 | return -1; | 
|  | 8382 | } | 
|  | 8383 | } | 
|  | 8384 |  | 
|  | 8385 | return 0; | 
|  | 8386 | } | 
|  | 8387 |  | 
|  | 8388 |  | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8389 | int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s) | 
|  | 8390 | { | 
|  | 8391 | struct wpa_ssid *ssid = wpa_s->current_ssid; | 
|  | 8392 |  | 
|  | 8393 | if (ssid == NULL || !ssid->p2p_group) | 
|  | 8394 | return 0; | 
|  | 8395 |  | 
|  | 8396 | if (wpa_s->p2p_last_4way_hs_fail && | 
|  | 8397 | wpa_s->p2p_last_4way_hs_fail == ssid) { | 
|  | 8398 | u8 go_dev_addr[ETH_ALEN]; | 
|  | 8399 | struct wpa_ssid *persistent; | 
|  | 8400 |  | 
|  | 8401 | if (wpas_p2p_persistent_group(wpa_s, go_dev_addr, | 
|  | 8402 | ssid->ssid, | 
|  | 8403 | ssid->ssid_len) <= 0) { | 
|  | 8404 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group"); | 
|  | 8405 | goto disconnect; | 
|  | 8406 | } | 
|  | 8407 |  | 
|  | 8408 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr=" | 
|  | 8409 | MACSTR, MAC2STR(go_dev_addr)); | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 8410 | persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr, | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8411 | ssid->ssid, | 
|  | 8412 | ssid->ssid_len); | 
|  | 8413 | if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) { | 
|  | 8414 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored"); | 
|  | 8415 | goto disconnect; | 
|  | 8416 | } | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 8417 | wpa_msg_global(wpa_s->p2pdev, MSG_INFO, | 
| Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 8418 | P2P_EVENT_PERSISTENT_PSK_FAIL "%d", | 
|  | 8419 | persistent->id); | 
|  | 8420 | disconnect: | 
|  | 8421 | wpa_s->p2p_last_4way_hs_fail = NULL; | 
|  | 8422 | /* | 
|  | 8423 | * Remove the group from a timeout to avoid issues with caller | 
|  | 8424 | * continuing to use the interface if this is on a P2P group | 
|  | 8425 | * interface. | 
|  | 8426 | */ | 
|  | 8427 | eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal, | 
|  | 8428 | wpa_s, NULL); | 
|  | 8429 | return 1; | 
|  | 8430 | } | 
|  | 8431 |  | 
|  | 8432 | wpa_s->p2p_last_4way_hs_fail = ssid; | 
|  | 8433 | return 0; | 
|  | 8434 | } | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8435 |  | 
|  | 8436 |  | 
|  | 8437 | #ifdef CONFIG_WPS_NFC | 
|  | 8438 |  | 
|  | 8439 | static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc, | 
|  | 8440 | struct wpabuf *p2p) | 
|  | 8441 | { | 
|  | 8442 | struct wpabuf *ret; | 
|  | 8443 | size_t wsc_len; | 
|  | 8444 |  | 
|  | 8445 | if (p2p == NULL) { | 
|  | 8446 | wpabuf_free(wsc); | 
|  | 8447 | wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover"); | 
|  | 8448 | return NULL; | 
|  | 8449 | } | 
|  | 8450 |  | 
|  | 8451 | wsc_len = wsc ? wpabuf_len(wsc) : 0; | 
|  | 8452 | ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p)); | 
|  | 8453 | if (ret == NULL) { | 
|  | 8454 | wpabuf_free(wsc); | 
|  | 8455 | wpabuf_free(p2p); | 
|  | 8456 | return NULL; | 
|  | 8457 | } | 
|  | 8458 |  | 
|  | 8459 | wpabuf_put_be16(ret, wsc_len); | 
|  | 8460 | if (wsc) | 
|  | 8461 | wpabuf_put_buf(ret, wsc); | 
|  | 8462 | wpabuf_put_be16(ret, wpabuf_len(p2p)); | 
|  | 8463 | wpabuf_put_buf(ret, p2p); | 
|  | 8464 |  | 
|  | 8465 | wpabuf_free(wsc); | 
|  | 8466 | wpabuf_free(p2p); | 
|  | 8467 | wpa_hexdump_buf(MSG_DEBUG, | 
|  | 8468 | "P2P: Generated NFC connection handover message", ret); | 
|  | 8469 |  | 
|  | 8470 | if (ndef && ret) { | 
|  | 8471 | struct wpabuf *tmp; | 
|  | 8472 | tmp = ndef_build_p2p(ret); | 
|  | 8473 | wpabuf_free(ret); | 
|  | 8474 | if (tmp == NULL) { | 
|  | 8475 | wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request"); | 
|  | 8476 | return NULL; | 
|  | 8477 | } | 
|  | 8478 | ret = tmp; | 
|  | 8479 | } | 
|  | 8480 |  | 
|  | 8481 | return ret; | 
|  | 8482 | } | 
|  | 8483 |  | 
|  | 8484 |  | 
|  | 8485 | static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s, | 
|  | 8486 | struct wpa_ssid **ssid, u8 *go_dev_addr) | 
|  | 8487 | { | 
|  | 8488 | struct wpa_supplicant *iface; | 
|  | 8489 |  | 
|  | 8490 | if (go_dev_addr) | 
|  | 8491 | os_memset(go_dev_addr, 0, ETH_ALEN); | 
|  | 8492 | if (ssid) | 
|  | 8493 | *ssid = NULL; | 
|  | 8494 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { | 
|  | 8495 | if (iface->wpa_state < WPA_ASSOCIATING || | 
|  | 8496 | iface->current_ssid == NULL || iface->assoc_freq == 0 || | 
|  | 8497 | !iface->current_ssid->p2p_group || | 
|  | 8498 | iface->current_ssid->mode != WPAS_MODE_INFRA) | 
|  | 8499 | continue; | 
|  | 8500 | if (ssid) | 
|  | 8501 | *ssid = iface->current_ssid; | 
|  | 8502 | if (go_dev_addr) | 
|  | 8503 | os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN); | 
|  | 8504 | return iface->assoc_freq; | 
|  | 8505 | } | 
|  | 8506 | return 0; | 
|  | 8507 | } | 
|  | 8508 |  | 
|  | 8509 |  | 
|  | 8510 | struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s, | 
|  | 8511 | int ndef) | 
|  | 8512 | { | 
|  | 8513 | struct wpabuf *wsc, *p2p; | 
|  | 8514 | struct wpa_ssid *ssid; | 
|  | 8515 | u8 go_dev_addr[ETH_ALEN]; | 
|  | 8516 | int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr); | 
|  | 8517 |  | 
|  | 8518 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) { | 
|  | 8519 | wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request"); | 
|  | 8520 | return NULL; | 
|  | 8521 | } | 
|  | 8522 |  | 
|  | 8523 | if (wpa_s->conf->wps_nfc_dh_pubkey == NULL && | 
|  | 8524 | wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, | 
|  | 8525 | &wpa_s->conf->wps_nfc_dh_privkey) < 0) { | 
|  | 8526 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request"); | 
|  | 8527 | return NULL; | 
|  | 8528 | } | 
|  | 8529 |  | 
|  | 8530 | if (cli_freq == 0) { | 
|  | 8531 | wsc = wps_build_nfc_handover_req_p2p( | 
|  | 8532 | wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey); | 
|  | 8533 | } else | 
|  | 8534 | wsc = NULL; | 
|  | 8535 | p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq, | 
|  | 8536 | go_dev_addr, ssid ? ssid->ssid : NULL, | 
|  | 8537 | ssid ? ssid->ssid_len : 0); | 
|  | 8538 |  | 
|  | 8539 | return wpas_p2p_nfc_handover(ndef, wsc, p2p); | 
|  | 8540 | } | 
|  | 8541 |  | 
|  | 8542 |  | 
|  | 8543 | struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s, | 
|  | 8544 | int ndef, int tag) | 
|  | 8545 | { | 
|  | 8546 | struct wpabuf *wsc, *p2p; | 
|  | 8547 | struct wpa_ssid *ssid; | 
|  | 8548 | u8 go_dev_addr[ETH_ALEN]; | 
|  | 8549 | int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr); | 
|  | 8550 |  | 
|  | 8551 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 8552 | return NULL; | 
|  | 8553 |  | 
|  | 8554 | if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL && | 
|  | 8555 | wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, | 
|  | 8556 | &wpa_s->conf->wps_nfc_dh_privkey) < 0) | 
|  | 8557 | return NULL; | 
|  | 8558 |  | 
|  | 8559 | if (cli_freq == 0) { | 
|  | 8560 | wsc = wps_build_nfc_handover_sel_p2p( | 
|  | 8561 | wpa_s->parent->wps, | 
|  | 8562 | tag ? wpa_s->conf->wps_nfc_dev_pw_id : | 
|  | 8563 | DEV_PW_NFC_CONNECTION_HANDOVER, | 
|  | 8564 | wpa_s->conf->wps_nfc_dh_pubkey, | 
|  | 8565 | tag ? wpa_s->conf->wps_nfc_dev_pw : NULL); | 
|  | 8566 | } else | 
|  | 8567 | wsc = NULL; | 
|  | 8568 | p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq, | 
|  | 8569 | go_dev_addr, ssid ? ssid->ssid : NULL, | 
|  | 8570 | ssid ? ssid->ssid_len : 0); | 
|  | 8571 |  | 
|  | 8572 | return wpas_p2p_nfc_handover(ndef, wsc, p2p); | 
|  | 8573 | } | 
|  | 8574 |  | 
|  | 8575 |  | 
|  | 8576 | static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s, | 
|  | 8577 | struct p2p_nfc_params *params) | 
|  | 8578 | { | 
|  | 8579 | wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC " | 
|  | 8580 | "connection handover (freq=%d)", | 
|  | 8581 | params->go_freq); | 
|  | 8582 |  | 
|  | 8583 | if (params->go_freq && params->go_ssid_len) { | 
|  | 8584 | wpa_s->p2p_wps_method = WPS_NFC; | 
|  | 8585 | wpa_s->pending_join_wps_method = WPS_NFC; | 
|  | 8586 | os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN); | 
|  | 8587 | os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr, | 
|  | 8588 | ETH_ALEN); | 
|  | 8589 | return wpas_p2p_join_start(wpa_s, params->go_freq, | 
|  | 8590 | params->go_ssid, | 
|  | 8591 | params->go_ssid_len); | 
|  | 8592 | } | 
|  | 8593 |  | 
|  | 8594 | return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, | 
|  | 8595 | WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 8596 | params->go_freq, wpa_s->p2p_go_vht_center_freq2, | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 8597 | -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth, | 
|  | 8598 | params->go_ssid_len ? params->go_ssid : NULL, | 
|  | 8599 | params->go_ssid_len); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8600 | } | 
|  | 8601 |  | 
|  | 8602 |  | 
|  | 8603 | static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s, | 
|  | 8604 | struct p2p_nfc_params *params, int tag) | 
|  | 8605 | { | 
|  | 8606 | int res, persistent; | 
|  | 8607 | struct wpa_ssid *ssid; | 
|  | 8608 |  | 
|  | 8609 | wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC " | 
|  | 8610 | "connection handover"); | 
|  | 8611 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { | 
|  | 8612 | ssid = wpa_s->current_ssid; | 
|  | 8613 | if (ssid == NULL) | 
|  | 8614 | continue; | 
|  | 8615 | if (ssid->mode != WPAS_MODE_P2P_GO) | 
|  | 8616 | continue; | 
|  | 8617 | if (wpa_s->ap_iface == NULL) | 
|  | 8618 | continue; | 
|  | 8619 | break; | 
|  | 8620 | } | 
|  | 8621 | if (wpa_s == NULL) { | 
|  | 8622 | wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface"); | 
|  | 8623 | return -1; | 
|  | 8624 | } | 
|  | 8625 |  | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 8626 | if (wpa_s->p2pdev->p2p_oob_dev_pw_id != | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8627 | DEV_PW_NFC_CONNECTION_HANDOVER && | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 8628 | !wpa_s->p2pdev->p2p_oob_dev_pw) { | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8629 | wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known"); | 
|  | 8630 | return -1; | 
|  | 8631 | } | 
|  | 8632 | res = wpas_ap_wps_add_nfc_pw( | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 8633 | wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id, | 
|  | 8634 | wpa_s->p2pdev->p2p_oob_dev_pw, | 
|  | 8635 | wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ? | 
|  | 8636 | wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8637 | if (res) | 
|  | 8638 | return res; | 
|  | 8639 |  | 
|  | 8640 | if (!tag) { | 
|  | 8641 | wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation"); | 
|  | 8642 | return 0; | 
|  | 8643 | } | 
|  | 8644 |  | 
|  | 8645 | if (!params->peer || | 
|  | 8646 | !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE)) | 
|  | 8647 | return 0; | 
|  | 8648 |  | 
|  | 8649 | wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR | 
|  | 8650 | " to join", MAC2STR(params->peer->p2p_device_addr)); | 
|  | 8651 |  | 
|  | 8652 | wpa_s->global->p2p_invite_group = wpa_s; | 
|  | 8653 | persistent = ssid->p2p_persistent_group && | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 8654 | wpas_p2p_get_persistent(wpa_s->p2pdev, | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8655 | params->peer->p2p_device_addr, | 
|  | 8656 | ssid->ssid, ssid->ssid_len); | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 8657 | wpa_s->p2pdev->pending_invite_ssid_id = -1; | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8658 |  | 
|  | 8659 | return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr, | 
|  | 8660 | P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr, | 
|  | 8661 | ssid->ssid, ssid->ssid_len, ssid->frequency, | 
|  | 8662 | wpa_s->global->p2p_dev_addr, persistent, 0, | 
| Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 8663 | wpa_s->p2pdev->p2p_oob_dev_pw_id); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8664 | } | 
|  | 8665 |  | 
|  | 8666 |  | 
|  | 8667 | static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s, | 
|  | 8668 | struct p2p_nfc_params *params, | 
|  | 8669 | int forced_freq) | 
|  | 8670 | { | 
|  | 8671 | wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC " | 
|  | 8672 | "connection handover"); | 
|  | 8673 | return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, | 
|  | 8674 | WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 8675 | forced_freq, wpa_s->p2p_go_vht_center_freq2, | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 8676 | -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth, | 
|  | 8677 | NULL, 0); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8678 | } | 
|  | 8679 |  | 
|  | 8680 |  | 
|  | 8681 | static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s, | 
|  | 8682 | struct p2p_nfc_params *params, | 
|  | 8683 | int forced_freq) | 
|  | 8684 | { | 
|  | 8685 | int res; | 
|  | 8686 |  | 
|  | 8687 | wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC " | 
|  | 8688 | "connection handover"); | 
|  | 8689 | res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, | 
|  | 8690 | WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent, | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 8691 | forced_freq, wpa_s->p2p_go_vht_center_freq2, | 
| Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 8692 | -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth, | 
|  | 8693 | NULL, 0); | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8694 | if (res) | 
|  | 8695 | return res; | 
|  | 8696 |  | 
|  | 8697 | res = wpas_p2p_listen(wpa_s, 60); | 
|  | 8698 | if (res) { | 
|  | 8699 | p2p_unauthorize(wpa_s->global->p2p, | 
|  | 8700 | params->peer->p2p_device_addr); | 
|  | 8701 | } | 
|  | 8702 |  | 
|  | 8703 | return res; | 
|  | 8704 | } | 
|  | 8705 |  | 
|  | 8706 |  | 
|  | 8707 | static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s, | 
|  | 8708 | const struct wpabuf *data, | 
|  | 8709 | int sel, int tag, int forced_freq) | 
|  | 8710 | { | 
|  | 8711 | const u8 *pos, *end; | 
|  | 8712 | u16 len, id; | 
|  | 8713 | struct p2p_nfc_params params; | 
|  | 8714 | int res; | 
|  | 8715 |  | 
|  | 8716 | os_memset(¶ms, 0, sizeof(params)); | 
|  | 8717 | params.sel = sel; | 
|  | 8718 |  | 
|  | 8719 | wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data); | 
|  | 8720 |  | 
|  | 8721 | pos = wpabuf_head(data); | 
|  | 8722 | end = pos + wpabuf_len(data); | 
|  | 8723 |  | 
|  | 8724 | if (end - pos < 2) { | 
|  | 8725 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC " | 
|  | 8726 | "attributes"); | 
|  | 8727 | return -1; | 
|  | 8728 | } | 
|  | 8729 | len = WPA_GET_BE16(pos); | 
|  | 8730 | pos += 2; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 8731 | if (len > end - pos) { | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8732 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC " | 
|  | 8733 | "attributes"); | 
|  | 8734 | return -1; | 
|  | 8735 | } | 
|  | 8736 | params.wsc_attr = pos; | 
|  | 8737 | params.wsc_len = len; | 
|  | 8738 | pos += len; | 
|  | 8739 |  | 
|  | 8740 | if (end - pos < 2) { | 
|  | 8741 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P " | 
|  | 8742 | "attributes"); | 
|  | 8743 | return -1; | 
|  | 8744 | } | 
|  | 8745 | len = WPA_GET_BE16(pos); | 
|  | 8746 | pos += 2; | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 8747 | if (len > end - pos) { | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8748 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P " | 
|  | 8749 | "attributes"); | 
|  | 8750 | return -1; | 
|  | 8751 | } | 
|  | 8752 | params.p2p_attr = pos; | 
|  | 8753 | params.p2p_len = len; | 
|  | 8754 | pos += len; | 
|  | 8755 |  | 
|  | 8756 | wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes", | 
|  | 8757 | params.wsc_attr, params.wsc_len); | 
|  | 8758 | wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes", | 
|  | 8759 | params.p2p_attr, params.p2p_len); | 
|  | 8760 | if (pos < end) { | 
|  | 8761 | wpa_hexdump(MSG_DEBUG, | 
|  | 8762 | "P2P: Ignored extra data after P2P attributes", | 
|  | 8763 | pos, end - pos); | 
|  | 8764 | } | 
|  | 8765 |  | 
|  | 8766 | res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms); | 
|  | 8767 | if (res) | 
|  | 8768 | return res; | 
|  | 8769 |  | 
|  | 8770 | if (params.next_step == NO_ACTION) | 
|  | 8771 | return 0; | 
|  | 8772 |  | 
|  | 8773 | if (params.next_step == BOTH_GO) { | 
|  | 8774 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR, | 
|  | 8775 | MAC2STR(params.peer->p2p_device_addr)); | 
|  | 8776 | return 0; | 
|  | 8777 | } | 
|  | 8778 |  | 
|  | 8779 | if (params.next_step == PEER_CLIENT) { | 
|  | 8780 | if (!is_zero_ether_addr(params.go_dev_addr)) { | 
|  | 8781 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT | 
|  | 8782 | "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR | 
|  | 8783 | " ssid=\"%s\"", | 
|  | 8784 | MAC2STR(params.peer->p2p_device_addr), | 
|  | 8785 | params.go_freq, | 
|  | 8786 | MAC2STR(params.go_dev_addr), | 
|  | 8787 | wpa_ssid_txt(params.go_ssid, | 
|  | 8788 | params.go_ssid_len)); | 
|  | 8789 | } else { | 
|  | 8790 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT | 
|  | 8791 | "peer=" MACSTR " freq=%d", | 
|  | 8792 | MAC2STR(params.peer->p2p_device_addr), | 
|  | 8793 | params.go_freq); | 
|  | 8794 | } | 
|  | 8795 | return 0; | 
|  | 8796 | } | 
|  | 8797 |  | 
|  | 8798 | if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) { | 
|  | 8799 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer=" | 
|  | 8800 | MACSTR, MAC2STR(params.peer->p2p_device_addr)); | 
|  | 8801 | return 0; | 
|  | 8802 | } | 
|  | 8803 |  | 
|  | 8804 | wpabuf_free(wpa_s->p2p_oob_dev_pw); | 
|  | 8805 | wpa_s->p2p_oob_dev_pw = NULL; | 
|  | 8806 |  | 
|  | 8807 | if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) { | 
|  | 8808 | wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw " | 
|  | 8809 | "received"); | 
|  | 8810 | return -1; | 
|  | 8811 | } | 
|  | 8812 |  | 
|  | 8813 | id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN); | 
|  | 8814 | wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id); | 
|  | 8815 | wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash", | 
|  | 8816 | params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN); | 
|  | 8817 | os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash, | 
|  | 8818 | params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN); | 
|  | 8819 | wpa_s->p2p_peer_oob_pk_hash_known = 1; | 
|  | 8820 |  | 
|  | 8821 | if (tag) { | 
|  | 8822 | if (id < 0x10) { | 
|  | 8823 | wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid " | 
|  | 8824 | "peer OOB Device Password Id %u", id); | 
|  | 8825 | return -1; | 
|  | 8826 | } | 
|  | 8827 | wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB " | 
|  | 8828 | "Device Password Id %u", id); | 
|  | 8829 | wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password", | 
|  | 8830 | params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2, | 
|  | 8831 | params.oob_dev_pw_len - | 
|  | 8832 | WPS_OOB_PUBKEY_HASH_LEN - 2); | 
|  | 8833 | wpa_s->p2p_oob_dev_pw_id = id; | 
|  | 8834 | wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy( | 
|  | 8835 | params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2, | 
|  | 8836 | params.oob_dev_pw_len - | 
|  | 8837 | WPS_OOB_PUBKEY_HASH_LEN - 2); | 
|  | 8838 | if (wpa_s->p2p_oob_dev_pw == NULL) | 
|  | 8839 | return -1; | 
|  | 8840 |  | 
|  | 8841 | if (wpa_s->conf->wps_nfc_dh_pubkey == NULL && | 
|  | 8842 | wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, | 
|  | 8843 | &wpa_s->conf->wps_nfc_dh_privkey) < 0) | 
|  | 8844 | return -1; | 
|  | 8845 | } else { | 
|  | 8846 | wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake " | 
|  | 8847 | "without Device Password"); | 
|  | 8848 | wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER; | 
|  | 8849 | } | 
|  | 8850 |  | 
|  | 8851 | switch (params.next_step) { | 
|  | 8852 | case NO_ACTION: | 
|  | 8853 | case BOTH_GO: | 
|  | 8854 | case PEER_CLIENT: | 
|  | 8855 | /* already covered above */ | 
|  | 8856 | return 0; | 
|  | 8857 | case JOIN_GROUP: | 
|  | 8858 | return wpas_p2p_nfc_join_group(wpa_s, ¶ms); | 
|  | 8859 | case AUTH_JOIN: | 
|  | 8860 | return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag); | 
|  | 8861 | case INIT_GO_NEG: | 
|  | 8862 | return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq); | 
|  | 8863 | case RESP_GO_NEG: | 
|  | 8864 | /* TODO: use own OOB Dev Pw */ | 
|  | 8865 | return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq); | 
|  | 8866 | } | 
|  | 8867 |  | 
|  | 8868 | return -1; | 
|  | 8869 | } | 
|  | 8870 |  | 
|  | 8871 |  | 
|  | 8872 | int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s, | 
|  | 8873 | const struct wpabuf *data, int forced_freq) | 
|  | 8874 | { | 
|  | 8875 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 8876 | return -1; | 
|  | 8877 |  | 
|  | 8878 | return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq); | 
|  | 8879 | } | 
|  | 8880 |  | 
|  | 8881 |  | 
|  | 8882 | int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init, | 
|  | 8883 | const struct wpabuf *req, | 
|  | 8884 | const struct wpabuf *sel, int forced_freq) | 
|  | 8885 | { | 
|  | 8886 | struct wpabuf *tmp; | 
|  | 8887 | int ret; | 
|  | 8888 |  | 
|  | 8889 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 8890 | return -1; | 
|  | 8891 |  | 
|  | 8892 | wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported"); | 
|  | 8893 |  | 
|  | 8894 | wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req", | 
|  | 8895 | wpabuf_head(req), wpabuf_len(req)); | 
|  | 8896 | wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel", | 
|  | 8897 | wpabuf_head(sel), wpabuf_len(sel)); | 
|  | 8898 | if (forced_freq) | 
|  | 8899 | wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq); | 
|  | 8900 | tmp = ndef_parse_p2p(init ? sel : req); | 
|  | 8901 | if (tmp == NULL) { | 
|  | 8902 | wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF"); | 
|  | 8903 | return -1; | 
|  | 8904 | } | 
|  | 8905 |  | 
|  | 8906 | ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0, | 
|  | 8907 | forced_freq); | 
|  | 8908 | wpabuf_free(tmp); | 
|  | 8909 |  | 
|  | 8910 | return ret; | 
|  | 8911 | } | 
|  | 8912 |  | 
|  | 8913 |  | 
|  | 8914 | int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled) | 
|  | 8915 | { | 
|  | 8916 | const u8 *if_addr; | 
|  | 8917 | int go_intent = wpa_s->conf->p2p_go_intent; | 
|  | 8918 | struct wpa_supplicant *iface; | 
|  | 8919 |  | 
|  | 8920 | if (wpa_s->global->p2p == NULL) | 
|  | 8921 | return -1; | 
|  | 8922 |  | 
|  | 8923 | if (!enabled) { | 
|  | 8924 | wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag"); | 
|  | 8925 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) | 
|  | 8926 | { | 
|  | 8927 | if (!iface->ap_iface) | 
|  | 8928 | continue; | 
|  | 8929 | hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]); | 
|  | 8930 | } | 
|  | 8931 | p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0, | 
|  | 8932 | 0, NULL); | 
|  | 8933 | if (wpa_s->p2p_nfc_tag_enabled) | 
|  | 8934 | wpas_p2p_remove_pending_group_interface(wpa_s); | 
|  | 8935 | wpa_s->p2p_nfc_tag_enabled = 0; | 
|  | 8936 | return 0; | 
|  | 8937 | } | 
|  | 8938 |  | 
|  | 8939 | if (wpa_s->global->p2p_disabled) | 
|  | 8940 | return -1; | 
|  | 8941 |  | 
|  | 8942 | if (wpa_s->conf->wps_nfc_dh_pubkey == NULL || | 
|  | 8943 | wpa_s->conf->wps_nfc_dh_privkey == NULL || | 
|  | 8944 | wpa_s->conf->wps_nfc_dev_pw == NULL || | 
|  | 8945 | wpa_s->conf->wps_nfc_dev_pw_id < 0x10) { | 
|  | 8946 | wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured " | 
|  | 8947 | "to allow static handover cases"); | 
|  | 8948 | return -1; | 
|  | 8949 | } | 
|  | 8950 |  | 
|  | 8951 | wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag"); | 
|  | 8952 |  | 
|  | 8953 | wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id; | 
|  | 8954 | wpabuf_free(wpa_s->p2p_oob_dev_pw); | 
|  | 8955 | wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw); | 
|  | 8956 | if (wpa_s->p2p_oob_dev_pw == NULL) | 
|  | 8957 | return -1; | 
|  | 8958 | wpa_s->p2p_peer_oob_pk_hash_known = 0; | 
|  | 8959 |  | 
| Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 8960 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO || | 
|  | 8961 | wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) { | 
|  | 8962 | /* | 
|  | 8963 | * P2P Group Interface present and the command came on group | 
|  | 8964 | * interface, so enable the token for the current interface. | 
|  | 8965 | */ | 
|  | 8966 | wpa_s->create_p2p_iface = 0; | 
|  | 8967 | } else { | 
|  | 8968 | wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); | 
|  | 8969 | } | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8970 |  | 
|  | 8971 | if (wpa_s->create_p2p_iface) { | 
|  | 8972 | enum wpa_driver_if_type iftype; | 
|  | 8973 | /* Prepare to add a new interface for the group */ | 
|  | 8974 | iftype = WPA_IF_P2P_GROUP; | 
|  | 8975 | if (go_intent == 15) | 
|  | 8976 | iftype = WPA_IF_P2P_GO; | 
|  | 8977 | if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { | 
|  | 8978 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " | 
|  | 8979 | "interface for the group"); | 
|  | 8980 | return -1; | 
|  | 8981 | } | 
|  | 8982 |  | 
|  | 8983 | if_addr = wpa_s->pending_interface_addr; | 
| Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 8984 | } else if (wpa_s->p2p_mgmt) | 
|  | 8985 | if_addr = wpa_s->parent->own_addr; | 
|  | 8986 | else | 
| Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 8987 | if_addr = wpa_s->own_addr; | 
|  | 8988 |  | 
|  | 8989 | wpa_s->p2p_nfc_tag_enabled = enabled; | 
|  | 8990 |  | 
|  | 8991 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { | 
|  | 8992 | struct hostapd_data *hapd; | 
|  | 8993 | if (iface->ap_iface == NULL) | 
|  | 8994 | continue; | 
|  | 8995 | hapd = iface->ap_iface->bss[0]; | 
|  | 8996 | wpabuf_free(hapd->conf->wps_nfc_dh_pubkey); | 
|  | 8997 | hapd->conf->wps_nfc_dh_pubkey = | 
|  | 8998 | wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey); | 
|  | 8999 | wpabuf_free(hapd->conf->wps_nfc_dh_privkey); | 
|  | 9000 | hapd->conf->wps_nfc_dh_privkey = | 
|  | 9001 | wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey); | 
|  | 9002 | wpabuf_free(hapd->conf->wps_nfc_dev_pw); | 
|  | 9003 | hapd->conf->wps_nfc_dev_pw = | 
|  | 9004 | wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw); | 
|  | 9005 | hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id; | 
|  | 9006 |  | 
|  | 9007 | if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) { | 
|  | 9008 | wpa_dbg(iface, MSG_DEBUG, | 
|  | 9009 | "P2P: Failed to enable NFC Tag for GO"); | 
|  | 9010 | } | 
|  | 9011 | } | 
|  | 9012 | p2p_set_authorized_oob_dev_pw_id( | 
|  | 9013 | wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent, | 
|  | 9014 | if_addr); | 
|  | 9015 |  | 
|  | 9016 | return 0; | 
|  | 9017 | } | 
|  | 9018 |  | 
|  | 9019 | #endif /* CONFIG_WPS_NFC */ | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9020 |  | 
|  | 9021 |  | 
|  | 9022 | static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s, | 
|  | 9023 | struct wpa_used_freq_data *freqs, | 
|  | 9024 | unsigned int num) | 
|  | 9025 | { | 
|  | 9026 | u8 curr_chan, cand, chan; | 
|  | 9027 | unsigned int i; | 
|  | 9028 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9029 | /* | 
|  | 9030 | * If possible, optimize the Listen channel to be a channel that is | 
|  | 9031 | * already used by one of the other interfaces. | 
|  | 9032 | */ | 
|  | 9033 | if (!wpa_s->conf->p2p_optimize_listen_chan) | 
|  | 9034 | return; | 
|  | 9035 |  | 
|  | 9036 | if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED) | 
|  | 9037 | return; | 
|  | 9038 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9039 | curr_chan = p2p_get_listen_channel(wpa_s->global->p2p); | 
|  | 9040 | for (i = 0, cand = 0; i < num; i++) { | 
|  | 9041 | ieee80211_freq_to_chan(freqs[i].freq, &chan); | 
|  | 9042 | if (curr_chan == chan) { | 
|  | 9043 | cand = 0; | 
|  | 9044 | break; | 
|  | 9045 | } | 
|  | 9046 |  | 
|  | 9047 | if (chan == 1 || chan == 6 || chan == 11) | 
|  | 9048 | cand = chan; | 
|  | 9049 | } | 
|  | 9050 |  | 
|  | 9051 | if (cand) { | 
|  | 9052 | wpa_dbg(wpa_s, MSG_DEBUG, | 
| Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 9053 | "P2P: Update Listen channel to %u based on operating channel", | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9054 | cand); | 
|  | 9055 | p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0); | 
|  | 9056 | } | 
|  | 9057 | } | 
|  | 9058 |  | 
|  | 9059 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9060 | static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s) | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9061 | { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9062 | struct hostapd_config *conf; | 
|  | 9063 | struct p2p_go_neg_results params; | 
|  | 9064 | struct csa_settings csa_settings; | 
|  | 9065 | struct wpa_ssid *current_ssid = wpa_s->current_ssid; | 
|  | 9066 | int old_freq = current_ssid->frequency; | 
|  | 9067 | int ret; | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9068 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9069 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) { | 
|  | 9070 | wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled"); | 
|  | 9071 | return -1; | 
|  | 9072 | } | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9073 |  | 
|  | 9074 | /* | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9075 | * TODO: This function may not always work correctly. For example, | 
|  | 9076 | * when we have a running GO and a BSS on a DFS channel. | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9077 | */ | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 9078 | if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, 0, 0, NULL)) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9079 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9080 | "P2P CSA: Failed to select new frequency for GO"); | 
|  | 9081 | return -1; | 
|  | 9082 | } | 
|  | 9083 |  | 
|  | 9084 | if (current_ssid->frequency == params.freq) { | 
|  | 9085 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9086 | "P2P CSA: Selected same frequency - not moving GO"); | 
|  | 9087 | return 0; | 
|  | 9088 | } | 
|  | 9089 |  | 
|  | 9090 | conf = hostapd_config_defaults(); | 
|  | 9091 | if (!conf) { | 
|  | 9092 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9093 | "P2P CSA: Failed to allocate default config"); | 
|  | 9094 | return -1; | 
|  | 9095 | } | 
|  | 9096 |  | 
|  | 9097 | current_ssid->frequency = params.freq; | 
|  | 9098 | if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) { | 
|  | 9099 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9100 | "P2P CSA: Failed to create new GO config"); | 
|  | 9101 | ret = -1; | 
|  | 9102 | goto out; | 
|  | 9103 | } | 
|  | 9104 |  | 
|  | 9105 | if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) { | 
|  | 9106 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9107 | "P2P CSA: CSA to a different band is not supported"); | 
|  | 9108 | ret = -1; | 
|  | 9109 | goto out; | 
|  | 9110 | } | 
|  | 9111 |  | 
|  | 9112 | os_memset(&csa_settings, 0, sizeof(csa_settings)); | 
|  | 9113 | csa_settings.cs_count = P2P_GO_CSA_COUNT; | 
|  | 9114 | csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX; | 
|  | 9115 | csa_settings.freq_params.freq = params.freq; | 
|  | 9116 | csa_settings.freq_params.sec_channel_offset = conf->secondary_channel; | 
|  | 9117 | csa_settings.freq_params.ht_enabled = conf->ieee80211n; | 
|  | 9118 | csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20; | 
|  | 9119 |  | 
|  | 9120 | if (conf->ieee80211ac) { | 
|  | 9121 | int freq1 = 0, freq2 = 0; | 
|  | 9122 | u8 chan, opclass; | 
|  | 9123 |  | 
|  | 9124 | if (ieee80211_freq_to_channel_ext(params.freq, | 
|  | 9125 | conf->secondary_channel, | 
|  | 9126 | conf->vht_oper_chwidth, | 
|  | 9127 | &opclass, &chan) == | 
|  | 9128 | NUM_HOSTAPD_MODES) { | 
|  | 9129 | wpa_printf(MSG_ERROR, "P2P CSA: Bad freq"); | 
|  | 9130 | ret = -1; | 
|  | 9131 | goto out; | 
|  | 9132 | } | 
|  | 9133 |  | 
|  | 9134 | if (conf->vht_oper_centr_freq_seg0_idx) | 
|  | 9135 | freq1 = ieee80211_chan_to_freq( | 
|  | 9136 | NULL, opclass, | 
|  | 9137 | conf->vht_oper_centr_freq_seg0_idx); | 
|  | 9138 |  | 
|  | 9139 | if (conf->vht_oper_centr_freq_seg1_idx) | 
|  | 9140 | freq2 = ieee80211_chan_to_freq( | 
|  | 9141 | NULL, opclass, | 
|  | 9142 | conf->vht_oper_centr_freq_seg1_idx); | 
|  | 9143 |  | 
|  | 9144 | if (freq1 < 0 || freq2 < 0) { | 
|  | 9145 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9146 | "P2P CSA: Selected invalid VHT center freqs"); | 
|  | 9147 | ret = -1; | 
|  | 9148 | goto out; | 
|  | 9149 | } | 
|  | 9150 |  | 
|  | 9151 | csa_settings.freq_params.vht_enabled = conf->ieee80211ac; | 
|  | 9152 | csa_settings.freq_params.center_freq1 = freq1; | 
|  | 9153 | csa_settings.freq_params.center_freq2 = freq2; | 
|  | 9154 |  | 
|  | 9155 | switch (conf->vht_oper_chwidth) { | 
|  | 9156 | case VHT_CHANWIDTH_80MHZ: | 
|  | 9157 | case VHT_CHANWIDTH_80P80MHZ: | 
|  | 9158 | csa_settings.freq_params.bandwidth = 80; | 
|  | 9159 | break; | 
|  | 9160 | case VHT_CHANWIDTH_160MHZ: | 
|  | 9161 | csa_settings.freq_params.bandwidth = 160; | 
|  | 9162 | break; | 
|  | 9163 | } | 
|  | 9164 | } | 
|  | 9165 |  | 
|  | 9166 | ret = ap_switch_channel(wpa_s, &csa_settings); | 
|  | 9167 | out: | 
|  | 9168 | current_ssid->frequency = old_freq; | 
|  | 9169 | hostapd_config_free(conf); | 
|  | 9170 | return ret; | 
|  | 9171 | } | 
|  | 9172 |  | 
|  | 9173 |  | 
|  | 9174 | static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s) | 
|  | 9175 | { | 
|  | 9176 | struct p2p_go_neg_results params; | 
|  | 9177 | struct wpa_ssid *current_ssid = wpa_s->current_ssid; | 
|  | 9178 |  | 
|  | 9179 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP); | 
|  | 9180 |  | 
|  | 9181 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz", | 
|  | 9182 | current_ssid->frequency); | 
|  | 9183 |  | 
|  | 9184 | /* Stop the AP functionality */ | 
|  | 9185 | /* TODO: Should do this in a way that does not indicated to possible | 
|  | 9186 | * P2P Clients in the group that the group is terminated. */ | 
|  | 9187 | wpa_supplicant_ap_deinit(wpa_s); | 
|  | 9188 |  | 
|  | 9189 | /* Reselect the GO frequency */ | 
| Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 9190 | if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, 0, 0, NULL)) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9191 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq"); | 
|  | 9192 | wpas_p2p_group_delete(wpa_s, | 
|  | 9193 | P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL); | 
|  | 9194 | return; | 
|  | 9195 | } | 
|  | 9196 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)", | 
|  | 9197 | params.freq); | 
|  | 9198 |  | 
|  | 9199 | if (params.freq && | 
|  | 9200 | !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) { | 
|  | 9201 | wpa_printf(MSG_DEBUG, | 
|  | 9202 | "P2P: Selected freq (%u MHz) is not valid for P2P", | 
|  | 9203 | params.freq); | 
|  | 9204 | wpas_p2p_group_delete(wpa_s, | 
|  | 9205 | P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL); | 
|  | 9206 | return; | 
|  | 9207 | } | 
|  | 9208 |  | 
|  | 9209 | /* Update the frequency */ | 
|  | 9210 | current_ssid->frequency = params.freq; | 
|  | 9211 | wpa_s->connect_without_scan = current_ssid; | 
|  | 9212 | wpa_s->reassociate = 1; | 
|  | 9213 | wpa_s->disconnected = 0; | 
|  | 9214 | wpa_supplicant_req_scan(wpa_s, 0, 0); | 
|  | 9215 | } | 
|  | 9216 |  | 
|  | 9217 |  | 
|  | 9218 | static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx) | 
|  | 9219 | { | 
|  | 9220 | struct wpa_supplicant *wpa_s = eloop_ctx; | 
|  | 9221 |  | 
|  | 9222 | if (!wpa_s->ap_iface || !wpa_s->current_ssid) | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9223 | return; | 
|  | 9224 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9225 | wpas_p2p_go_update_common_freqs(wpa_s); | 
|  | 9226 |  | 
|  | 9227 | /* Do not move GO in the middle of a CSA */ | 
|  | 9228 | if (hostapd_csa_in_progress(wpa_s->ap_iface)) { | 
|  | 9229 | wpa_printf(MSG_DEBUG, | 
|  | 9230 | "P2P: CSA is in progress - not moving GO"); | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9231 | return; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9232 | } | 
|  | 9233 |  | 
|  | 9234 | /* | 
|  | 9235 | * First, try a channel switch flow. If it is not supported or fails, | 
|  | 9236 | * take down the GO and bring it up again. | 
|  | 9237 | */ | 
|  | 9238 | if (wpas_p2p_move_go_csa(wpa_s) < 0) | 
|  | 9239 | wpas_p2p_move_go_no_csa(wpa_s); | 
|  | 9240 | } | 
|  | 9241 |  | 
|  | 9242 |  | 
|  | 9243 | static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx) | 
|  | 9244 | { | 
|  | 9245 | struct wpa_supplicant *wpa_s = eloop_ctx; | 
|  | 9246 | struct wpa_used_freq_data *freqs = NULL; | 
|  | 9247 | unsigned int num = wpa_s->num_multichan_concurrent; | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9248 |  | 
|  | 9249 | freqs = os_calloc(num, sizeof(struct wpa_used_freq_data)); | 
|  | 9250 | if (!freqs) | 
|  | 9251 | return; | 
|  | 9252 |  | 
|  | 9253 | num = get_shared_radio_freqs_data(wpa_s, freqs, num); | 
|  | 9254 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9255 | /* Previous attempt to move a GO was not possible -- try again. */ | 
|  | 9256 | wpas_p2p_consider_moving_gos(wpa_s, freqs, num, | 
|  | 9257 | WPAS_P2P_CHANNEL_UPDATE_ANY); | 
|  | 9258 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9259 | os_free(freqs); | 
|  | 9260 | } | 
|  | 9261 |  | 
|  | 9262 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9263 | /* | 
|  | 9264 | * Consider moving a GO from its currently used frequency: | 
|  | 9265 | * 1. It is possible that due to regulatory consideration the frequency | 
|  | 9266 | *    can no longer be used and there is a need to evacuate the GO. | 
|  | 9267 | * 2. It is possible that due to MCC considerations, it would be preferable | 
|  | 9268 | *    to move the GO to a channel that is currently used by some other | 
|  | 9269 | *    station interface. | 
|  | 9270 | * | 
|  | 9271 | * In case a frequency that became invalid is once again valid, cancel a | 
|  | 9272 | * previously initiated GO frequency change. | 
|  | 9273 | */ | 
|  | 9274 | static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s, | 
|  | 9275 | struct wpa_used_freq_data *freqs, | 
|  | 9276 | unsigned int num) | 
|  | 9277 | { | 
|  | 9278 | unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0; | 
|  | 9279 | unsigned int timeout; | 
|  | 9280 | int freq; | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 9281 | int dfs_offload; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9282 |  | 
|  | 9283 | wpas_p2p_go_update_common_freqs(wpa_s); | 
|  | 9284 |  | 
|  | 9285 | freq = wpa_s->current_ssid->frequency; | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 9286 | dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 9287 | ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9288 | for (i = 0, invalid_freq = 0; i < num; i++) { | 
|  | 9289 | if (freqs[i].freq == freq) { | 
|  | 9290 | flags = freqs[i].flags; | 
|  | 9291 |  | 
|  | 9292 | /* The channel is invalid, must change it */ | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 9293 | if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) && | 
|  | 9294 | !dfs_offload) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9295 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9296 | "P2P: Freq=%d MHz no longer valid for GO", | 
|  | 9297 | freq); | 
|  | 9298 | invalid_freq = 1; | 
|  | 9299 | } | 
|  | 9300 | } else if (freqs[i].flags == 0) { | 
|  | 9301 | /* Freq is not used by any other station interface */ | 
|  | 9302 | continue; | 
|  | 9303 | } else if (!p2p_supported_freq(wpa_s->global->p2p, | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 9304 | freqs[i].freq) && !dfs_offload) { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 9305 | /* Freq is not valid for P2P use cases */ | 
|  | 9306 | continue; | 
|  | 9307 | } else if (wpa_s->conf->p2p_go_freq_change_policy == | 
|  | 9308 | P2P_GO_FREQ_MOVE_SCM) { | 
|  | 9309 | policy_move = 1; | 
|  | 9310 | } else if (wpa_s->conf->p2p_go_freq_change_policy == | 
|  | 9311 | P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS && | 
|  | 9312 | wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) { | 
|  | 9313 | policy_move = 1; | 
|  | 9314 | } else if ((wpa_s->conf->p2p_go_freq_change_policy == | 
|  | 9315 | P2P_GO_FREQ_MOVE_SCM_ECSA) && | 
|  | 9316 | wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) { | 
|  | 9317 | if (!p2p_get_group_num_members(wpa_s->p2p_group)) { | 
|  | 9318 | policy_move = 1; | 
|  | 9319 | } else if ((wpa_s->drv_flags & | 
|  | 9320 | WPA_DRIVER_FLAGS_AP_CSA) && | 
|  | 9321 | wpas_p2p_go_clients_support_ecsa(wpa_s)) { | 
|  | 9322 | u8 chan; | 
|  | 9323 |  | 
|  | 9324 | /* | 
|  | 9325 | * We do not support CSA between bands, so move | 
|  | 9326 | * GO only within the same band. | 
|  | 9327 | */ | 
|  | 9328 | if (wpa_s->ap_iface->current_mode->mode == | 
|  | 9329 | ieee80211_freq_to_chan(freqs[i].freq, | 
|  | 9330 | &chan)) | 
|  | 9331 | policy_move = 1; | 
|  | 9332 | } | 
|  | 9333 | } | 
|  | 9334 | } | 
|  | 9335 |  | 
|  | 9336 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9337 | "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X", | 
|  | 9338 | invalid_freq, policy_move, flags); | 
|  | 9339 |  | 
|  | 9340 | /* | 
|  | 9341 | * The channel is valid, or we are going to have a policy move, so | 
|  | 9342 | * cancel timeout. | 
|  | 9343 | */ | 
|  | 9344 | if (!invalid_freq || policy_move) { | 
|  | 9345 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9346 | "P2P: Cancel a GO move from freq=%d MHz", freq); | 
|  | 9347 | eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL); | 
|  | 9348 |  | 
|  | 9349 | if (wpas_p2p_in_progress(wpa_s)) { | 
|  | 9350 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9351 | "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move"); | 
|  | 9352 | eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, | 
|  | 9353 | wpa_s, NULL); | 
|  | 9354 | eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0, | 
|  | 9355 | wpas_p2p_reconsider_moving_go, | 
|  | 9356 | wpa_s, NULL); | 
|  | 9357 | return; | 
|  | 9358 | } | 
|  | 9359 | } | 
|  | 9360 |  | 
|  | 9361 | if (!invalid_freq && (!policy_move || flags != 0)) { | 
|  | 9362 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9363 | "P2P: Not initiating a GO frequency change"); | 
|  | 9364 | return; | 
|  | 9365 | } | 
|  | 9366 |  | 
|  | 9367 | /* | 
|  | 9368 | * Do not consider moving GO if it is in the middle of a CSA. When the | 
|  | 9369 | * CSA is finished this flow should be retriggered. | 
|  | 9370 | */ | 
|  | 9371 | if (hostapd_csa_in_progress(wpa_s->ap_iface)) { | 
|  | 9372 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9373 | "P2P: Not initiating a GO frequency change - CSA is in progress"); | 
|  | 9374 | return; | 
|  | 9375 | } | 
|  | 9376 |  | 
|  | 9377 | if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq)) | 
|  | 9378 | timeout = P2P_GO_FREQ_CHANGE_TIME; | 
|  | 9379 | else | 
|  | 9380 | timeout = 0; | 
|  | 9381 |  | 
|  | 9382 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs", | 
|  | 9383 | freq, timeout); | 
|  | 9384 | eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL); | 
|  | 9385 | eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL); | 
|  | 9386 | } | 
|  | 9387 |  | 
|  | 9388 |  | 
|  | 9389 | static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s, | 
|  | 9390 | struct wpa_used_freq_data *freqs, | 
|  | 9391 | unsigned int num, | 
|  | 9392 | enum wpas_p2p_channel_update_trig trig) | 
|  | 9393 | { | 
|  | 9394 | struct wpa_supplicant *ifs; | 
|  | 9395 |  | 
|  | 9396 | eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX, | 
|  | 9397 | NULL); | 
|  | 9398 |  | 
|  | 9399 | /* | 
|  | 9400 | * Travers all the radio interfaces, and for each GO interface, check | 
|  | 9401 | * if there is a need to move the GO from the frequency it is using, | 
|  | 9402 | * or in case the frequency is valid again, cancel the evacuation flow. | 
|  | 9403 | */ | 
|  | 9404 | dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant, | 
|  | 9405 | radio_list) { | 
|  | 9406 | if (ifs->current_ssid == NULL || | 
|  | 9407 | ifs->current_ssid->mode != WPAS_MODE_P2P_GO) | 
|  | 9408 | continue; | 
|  | 9409 |  | 
|  | 9410 | /* | 
|  | 9411 | * The GO was just started or completed channel switch, no need | 
|  | 9412 | * to move it. | 
|  | 9413 | */ | 
|  | 9414 | if (wpa_s == ifs && | 
|  | 9415 | (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE || | 
|  | 9416 | trig == WPAS_P2P_CHANNEL_UPDATE_CS)) { | 
|  | 9417 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9418 | "P2P: GO move - schedule re-consideration"); | 
|  | 9419 | eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0, | 
|  | 9420 | wpas_p2p_reconsider_moving_go, | 
|  | 9421 | wpa_s, NULL); | 
|  | 9422 | continue; | 
|  | 9423 | } | 
|  | 9424 |  | 
|  | 9425 | wpas_p2p_consider_moving_one_go(ifs, freqs, num); | 
|  | 9426 | } | 
|  | 9427 | } | 
|  | 9428 |  | 
|  | 9429 |  | 
|  | 9430 | void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s) | 
|  | 9431 | { | 
|  | 9432 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) | 
|  | 9433 | return; | 
|  | 9434 |  | 
|  | 9435 | wpas_p2p_update_channel_list(wpa_s, | 
|  | 9436 | WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE); | 
|  | 9437 | } | 
|  | 9438 |  | 
|  | 9439 |  | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9440 | void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s) | 
|  | 9441 | { | 
| Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 9442 | if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) { | 
|  | 9443 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing " | 
|  | 9444 | "the management interface is being removed"); | 
|  | 9445 | wpas_p2p_deinit_global(wpa_s->global); | 
|  | 9446 | } | 
|  | 9447 | } | 
|  | 9448 |  | 
|  | 9449 |  | 
|  | 9450 | void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s) | 
|  | 9451 | { | 
|  | 9452 | if (wpa_s->ap_iface->bss) | 
|  | 9453 | wpa_s->ap_iface->bss[0]->p2p_group = NULL; | 
|  | 9454 | wpas_p2p_group_deinit(wpa_s); | 
|  | 9455 | } | 
| Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 9456 |  | 
|  | 9457 |  | 
|  | 9458 | int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq, | 
|  | 9459 | unsigned int period, unsigned int interval, | 
|  | 9460 | unsigned int count) | 
|  | 9461 | { | 
|  | 9462 | struct p2p_data *p2p = wpa_s->global->p2p; | 
|  | 9463 | u8 *device_types; | 
|  | 9464 | size_t dev_types_len; | 
|  | 9465 | struct wpabuf *buf; | 
|  | 9466 | int ret; | 
|  | 9467 |  | 
|  | 9468 | if (wpa_s->p2p_lo_started) { | 
|  | 9469 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9470 | "P2P Listen offload is already started"); | 
|  | 9471 | return 0; | 
|  | 9472 | } | 
|  | 9473 |  | 
|  | 9474 | if (wpa_s->global->p2p == NULL || | 
|  | 9475 | !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) { | 
|  | 9476 | wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported"); | 
|  | 9477 | return -1; | 
|  | 9478 | } | 
|  | 9479 |  | 
|  | 9480 | if (!p2p_supported_freq(wpa_s->global->p2p, freq)) { | 
|  | 9481 | wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u", | 
|  | 9482 | freq); | 
|  | 9483 | return -1; | 
|  | 9484 | } | 
|  | 9485 |  | 
|  | 9486 | /* Get device type */ | 
|  | 9487 | dev_types_len = (wpa_s->conf->num_sec_device_types + 1) * | 
|  | 9488 | WPS_DEV_TYPE_LEN; | 
|  | 9489 | device_types = os_malloc(dev_types_len); | 
|  | 9490 | if (!device_types) | 
|  | 9491 | return -1; | 
|  | 9492 | os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN); | 
|  | 9493 | os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type, | 
|  | 9494 | wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN); | 
|  | 9495 |  | 
|  | 9496 | /* Get Probe Response IE(s) */ | 
|  | 9497 | buf = p2p_build_probe_resp_template(p2p, freq); | 
|  | 9498 | if (!buf) { | 
|  | 9499 | os_free(device_types); | 
|  | 9500 | return -1; | 
|  | 9501 | } | 
|  | 9502 |  | 
|  | 9503 | ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count, | 
|  | 9504 | device_types, dev_types_len, | 
|  | 9505 | wpabuf_mhead_u8(buf), wpabuf_len(buf)); | 
|  | 9506 | if (ret < 0) | 
|  | 9507 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9508 | "P2P: Failed to start P2P listen offload"); | 
|  | 9509 |  | 
|  | 9510 | os_free(device_types); | 
|  | 9511 | wpabuf_free(buf); | 
|  | 9512 |  | 
|  | 9513 | if (ret == 0) { | 
|  | 9514 | wpa_s->p2p_lo_started = 1; | 
|  | 9515 |  | 
|  | 9516 | /* Stop current P2P listen if any */ | 
|  | 9517 | wpas_stop_listen(wpa_s); | 
|  | 9518 | } | 
|  | 9519 |  | 
|  | 9520 | return ret; | 
|  | 9521 | } | 
|  | 9522 |  | 
|  | 9523 |  | 
|  | 9524 | int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s) | 
|  | 9525 | { | 
|  | 9526 | int ret; | 
|  | 9527 |  | 
|  | 9528 | if (!wpa_s->p2p_lo_started) | 
|  | 9529 | return 0; | 
|  | 9530 |  | 
|  | 9531 | ret = wpa_drv_p2p_lo_stop(wpa_s); | 
|  | 9532 | if (ret < 0) | 
|  | 9533 | wpa_dbg(wpa_s, MSG_DEBUG, | 
|  | 9534 | "P2P: Failed to stop P2P listen offload"); | 
|  | 9535 |  | 
|  | 9536 | wpa_s->p2p_lo_started = 0; | 
|  | 9537 | return ret; | 
|  | 9538 | } |