Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Wi-Fi Direct - P2P module |
| 3 | * Copyright (c) 2009-2010, Atheros Communications |
| 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
| 10 | |
| 11 | #include "common.h" |
| 12 | #include "eloop.h" |
| 13 | #include "common/ieee802_11_defs.h" |
| 14 | #include "common/ieee802_11_common.h" |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 15 | #include "common/wpa_ctrl.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 16 | #include "wps/wps_i.h" |
| 17 | #include "p2p_i.h" |
| 18 | #include "p2p.h" |
| 19 | |
| 20 | |
| 21 | static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx); |
| 22 | static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev); |
| 23 | static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, |
| 24 | const u8 *sa, const u8 *data, size_t len, |
| 25 | int rx_freq); |
| 26 | static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, |
| 27 | const u8 *sa, const u8 *data, |
| 28 | size_t len); |
| 29 | static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx); |
| 30 | static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx); |
| 31 | |
| 32 | |
| 33 | /* |
| 34 | * p2p_scan recovery timeout |
| 35 | * |
| 36 | * Many drivers are using 30 second timeout on scan results. Allow a bit larger |
| 37 | * timeout for this to avoid hitting P2P timeout unnecessarily. |
| 38 | */ |
| 39 | #define P2P_SCAN_TIMEOUT 35 |
| 40 | |
| 41 | /** |
| 42 | * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer |
| 43 | * entries will be removed |
| 44 | */ |
Dmitry Shmidt | 2fb777c | 2012-05-02 12:29:53 -0700 | [diff] [blame] | 45 | #ifdef ANDROID_P2P |
| 46 | #define P2P_PEER_EXPIRATION_AGE 30 |
| 47 | #else |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 48 | #define P2P_PEER_EXPIRATION_AGE 300 |
Dmitry Shmidt | 2fb777c | 2012-05-02 12:29:53 -0700 | [diff] [blame] | 49 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 50 | |
| 51 | #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2) |
| 52 | |
Dmitry Shmidt | 2fb777c | 2012-05-02 12:29:53 -0700 | [diff] [blame] | 53 | #ifdef ANDROID_P2P |
| 54 | int p2p_connection_in_progress(struct p2p_data *p2p) |
| 55 | { |
| 56 | int ret = 0; |
| 57 | |
| 58 | switch (p2p->state) { |
| 59 | case P2P_CONNECT: |
| 60 | case P2P_CONNECT_LISTEN: |
| 61 | case P2P_GO_NEG: |
| 62 | case P2P_WAIT_PEER_CONNECT: |
Dmitry Shmidt | 98f9e76 | 2012-05-30 11:18:46 -0700 | [diff] [blame] | 63 | case P2P_WAIT_PEER_IDLE: |
Dmitry Shmidt | 2fb777c | 2012-05-02 12:29:53 -0700 | [diff] [blame] | 64 | case P2P_PROVISIONING: |
| 65 | case P2P_INVITE: |
| 66 | case P2P_INVITE_LISTEN: |
| 67 | ret = 1; |
| 68 | break; |
| 69 | |
| 70 | default: |
Dmitry Shmidt | 98f9e76 | 2012-05-30 11:18:46 -0700 | [diff] [blame] | 71 | wpa_printf(MSG_DEBUG, "p2p_connection_in_progress state %d", p2p->state); |
Dmitry Shmidt | 2fb777c | 2012-05-02 12:29:53 -0700 | [diff] [blame] | 72 | ret = 0; |
| 73 | } |
| 74 | |
| 75 | return ret; |
| 76 | } |
| 77 | #endif |
| 78 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 79 | static void p2p_expire_peers(struct p2p_data *p2p) |
| 80 | { |
| 81 | struct p2p_device *dev, *n; |
| 82 | struct os_time now; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 83 | size_t i; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 84 | |
| 85 | os_get_time(&now); |
| 86 | dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) { |
| 87 | if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec) |
| 88 | continue; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 89 | |
| 90 | if (p2p->cfg->go_connected && |
| 91 | p2p->cfg->go_connected(p2p->cfg->cb_ctx, |
| 92 | dev->info.p2p_device_addr)) { |
| 93 | /* |
| 94 | * We are connected as a client to a group in which the |
| 95 | * peer is the GO, so do not expire the peer entry. |
| 96 | */ |
| 97 | os_get_time(&dev->last_seen); |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | for (i = 0; i < p2p->num_groups; i++) { |
| 102 | if (p2p_group_is_client_connected( |
| 103 | p2p->groups[i], dev->info.p2p_device_addr)) |
| 104 | break; |
| 105 | } |
| 106 | if (i < p2p->num_groups) { |
| 107 | /* |
| 108 | * The peer is connected as a client in a group where |
| 109 | * we are the GO, so do not expire the peer entry. |
| 110 | */ |
| 111 | os_get_time(&dev->last_seen); |
| 112 | continue; |
| 113 | } |
| 114 | |
Dmitry Shmidt | 2fb777c | 2012-05-02 12:29:53 -0700 | [diff] [blame] | 115 | #ifdef ANDROID_P2P |
| 116 | /* If Connection is in progress, don't expire the peer |
| 117 | */ |
| 118 | if (p2p_connection_in_progress(p2p)) |
| 119 | continue; |
| 120 | #endif |
| 121 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 122 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 123 | "entry " MACSTR, MAC2STR(dev->info.p2p_device_addr)); |
Irfan Sheriff | f44b9c4 | 2012-06-13 11:09:23 -0700 | [diff] [blame] | 124 | #ifdef ANDROID_P2P |
| 125 | /* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */ |
| 126 | if(&dev->list == p2p->sd_dev_list) |
| 127 | p2p->sd_dev_list = dev->list.next; |
| 128 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 129 | dl_list_del(&dev->list); |
| 130 | p2p_device_free(p2p, dev); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx) |
| 136 | { |
| 137 | struct p2p_data *p2p = eloop_ctx; |
| 138 | p2p_expire_peers(p2p); |
| 139 | eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0, |
| 140 | p2p_expiration_timeout, p2p, NULL); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | static const char * p2p_state_txt(int state) |
| 145 | { |
| 146 | switch (state) { |
| 147 | case P2P_IDLE: |
| 148 | return "IDLE"; |
| 149 | case P2P_SEARCH: |
| 150 | return "SEARCH"; |
| 151 | case P2P_CONNECT: |
| 152 | return "CONNECT"; |
| 153 | case P2P_CONNECT_LISTEN: |
| 154 | return "CONNECT_LISTEN"; |
| 155 | case P2P_GO_NEG: |
| 156 | return "GO_NEG"; |
| 157 | case P2P_LISTEN_ONLY: |
| 158 | return "LISTEN_ONLY"; |
| 159 | case P2P_WAIT_PEER_CONNECT: |
| 160 | return "WAIT_PEER_CONNECT"; |
| 161 | case P2P_WAIT_PEER_IDLE: |
| 162 | return "WAIT_PEER_IDLE"; |
| 163 | case P2P_SD_DURING_FIND: |
| 164 | return "SD_DURING_FIND"; |
| 165 | case P2P_PROVISIONING: |
| 166 | return "PROVISIONING"; |
| 167 | case P2P_PD_DURING_FIND: |
| 168 | return "PD_DURING_FIND"; |
| 169 | case P2P_INVITE: |
| 170 | return "INVITE"; |
| 171 | case P2P_INVITE_LISTEN: |
| 172 | return "INVITE_LISTEN"; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 173 | case P2P_SEARCH_WHEN_READY: |
| 174 | return "SEARCH_WHEN_READY"; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 175 | default: |
| 176 | return "?"; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 181 | u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr) |
| 182 | { |
| 183 | struct p2p_device *dev = NULL; |
| 184 | |
| 185 | if (!addr || !p2p) |
| 186 | return 0; |
| 187 | |
| 188 | dev = p2p_get_device(p2p, addr); |
| 189 | if (dev) |
| 190 | return dev->wps_prov_info; |
| 191 | else |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 196 | void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 197 | { |
| 198 | struct p2p_device *dev = NULL; |
| 199 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 200 | if (!addr || !p2p) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 201 | return; |
| 202 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 203 | dev = p2p_get_device(p2p, addr); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 204 | if (dev) |
| 205 | dev->wps_prov_info = 0; |
| 206 | } |
| 207 | |
| 208 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 209 | void p2p_set_state(struct p2p_data *p2p, int new_state) |
| 210 | { |
| 211 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s", |
| 212 | p2p_state_txt(p2p->state), p2p_state_txt(new_state)); |
| 213 | p2p->state = new_state; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec) |
| 218 | { |
| 219 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 220 | "P2P: Set timeout (state=%s): %u.%06u sec", |
| 221 | p2p_state_txt(p2p->state), sec, usec); |
| 222 | eloop_cancel_timeout(p2p_state_timeout, p2p, NULL); |
| 223 | eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | void p2p_clear_timeout(struct p2p_data *p2p) |
| 228 | { |
| 229 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)", |
| 230 | p2p_state_txt(p2p->state)); |
| 231 | eloop_cancel_timeout(p2p_state_timeout, p2p, NULL); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer, |
| 236 | int status) |
| 237 | { |
| 238 | struct p2p_go_neg_results res; |
| 239 | p2p_clear_timeout(p2p); |
| 240 | p2p_set_state(p2p, P2P_IDLE); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 241 | if (p2p->go_neg_peer) |
| 242 | p2p->go_neg_peer->wps_method = WPS_NOT_READY; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 243 | p2p->go_neg_peer = NULL; |
| 244 | |
| 245 | os_memset(&res, 0, sizeof(res)); |
| 246 | res.status = status; |
| 247 | if (peer) { |
| 248 | os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, |
| 249 | ETH_ALEN); |
| 250 | os_memcpy(res.peer_interface_addr, peer->intended_addr, |
| 251 | ETH_ALEN); |
| 252 | } |
| 253 | p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | static void p2p_listen_in_find(struct p2p_data *p2p) |
| 258 | { |
| 259 | unsigned int r, tu; |
| 260 | int freq; |
| 261 | struct wpabuf *ies; |
| 262 | |
| 263 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 264 | "P2P: Starting short listen state (state=%s)", |
| 265 | p2p_state_txt(p2p->state)); |
| 266 | |
| 267 | freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class, |
| 268 | p2p->cfg->channel); |
| 269 | if (freq < 0) { |
| 270 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 271 | "P2P: Unknown regulatory class/channel"); |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | os_get_random((u8 *) &r, sizeof(r)); |
| 276 | tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) + |
| 277 | p2p->min_disc_int) * 100; |
| 278 | |
| 279 | p2p->pending_listen_freq = freq; |
| 280 | p2p->pending_listen_sec = 0; |
| 281 | p2p->pending_listen_usec = 1024 * tu; |
| 282 | |
| 283 | ies = p2p_build_probe_resp_ies(p2p); |
| 284 | if (ies == NULL) |
| 285 | return; |
| 286 | |
| 287 | if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000, |
| 288 | ies) < 0) { |
| 289 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 290 | "P2P: Failed to start listen mode"); |
| 291 | p2p->pending_listen_freq = 0; |
| 292 | } |
| 293 | wpabuf_free(ies); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | int p2p_listen(struct p2p_data *p2p, unsigned int timeout) |
| 298 | { |
| 299 | int freq; |
| 300 | struct wpabuf *ies; |
| 301 | |
| 302 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 303 | "P2P: Going to listen(only) state"); |
| 304 | |
| 305 | freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class, |
| 306 | p2p->cfg->channel); |
| 307 | if (freq < 0) { |
| 308 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 309 | "P2P: Unknown regulatory class/channel"); |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | p2p->pending_listen_freq = freq; |
| 314 | p2p->pending_listen_sec = timeout / 1000; |
| 315 | p2p->pending_listen_usec = (timeout % 1000) * 1000; |
| 316 | |
| 317 | if (p2p->p2p_scan_running) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 318 | if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 319 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 320 | "P2P: p2p_scan running - connect is already " |
| 321 | "pending - skip listen"); |
| 322 | return 0; |
| 323 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 324 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 325 | "P2P: p2p_scan running - delay start of listen state"); |
| 326 | p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN; |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | ies = p2p_build_probe_resp_ies(p2p); |
| 331 | if (ies == NULL) |
| 332 | return -1; |
| 333 | |
| 334 | if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) { |
| 335 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 336 | "P2P: Failed to start listen mode"); |
| 337 | p2p->pending_listen_freq = 0; |
| 338 | wpabuf_free(ies); |
| 339 | return -1; |
| 340 | } |
| 341 | wpabuf_free(ies); |
| 342 | |
| 343 | p2p_set_state(p2p, P2P_LISTEN_ONLY); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | |
| 349 | static void p2p_device_clear_reported(struct p2p_data *p2p) |
| 350 | { |
| 351 | struct p2p_device *dev; |
| 352 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) |
| 353 | dev->flags &= ~P2P_DEV_REPORTED; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /** |
| 358 | * p2p_get_device - Fetch a peer entry |
| 359 | * @p2p: P2P module context from p2p_init() |
| 360 | * @addr: P2P Device Address of the peer |
| 361 | * Returns: Pointer to the device entry or %NULL if not found |
| 362 | */ |
| 363 | struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr) |
| 364 | { |
| 365 | struct p2p_device *dev; |
| 366 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
| 367 | if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0) |
| 368 | return dev; |
| 369 | } |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | /** |
| 375 | * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address |
| 376 | * @p2p: P2P module context from p2p_init() |
| 377 | * @addr: P2P Interface Address of the peer |
| 378 | * Returns: Pointer to the device entry or %NULL if not found |
| 379 | */ |
| 380 | struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p, |
| 381 | const u8 *addr) |
| 382 | { |
| 383 | struct p2p_device *dev; |
| 384 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
| 385 | if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0) |
| 386 | return dev; |
| 387 | } |
| 388 | return NULL; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | /** |
| 393 | * p2p_create_device - Create a peer entry |
| 394 | * @p2p: P2P module context from p2p_init() |
| 395 | * @addr: P2P Device Address of the peer |
| 396 | * Returns: Pointer to the device entry or %NULL on failure |
| 397 | * |
| 398 | * If there is already an entry for the peer, it will be returned instead of |
| 399 | * creating a new one. |
| 400 | */ |
| 401 | static struct p2p_device * p2p_create_device(struct p2p_data *p2p, |
| 402 | const u8 *addr) |
| 403 | { |
| 404 | struct p2p_device *dev, *oldest = NULL; |
| 405 | size_t count = 0; |
| 406 | |
| 407 | dev = p2p_get_device(p2p, addr); |
| 408 | if (dev) |
| 409 | return dev; |
| 410 | |
| 411 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
| 412 | count++; |
| 413 | if (oldest == NULL || |
| 414 | os_time_before(&dev->last_seen, &oldest->last_seen)) |
| 415 | oldest = dev; |
| 416 | } |
| 417 | if (count + 1 > p2p->cfg->max_peers && oldest) { |
| 418 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 419 | "P2P: Remove oldest peer entry to make room for a new " |
| 420 | "peer"); |
Irfan Sheriff | f44b9c4 | 2012-06-13 11:09:23 -0700 | [diff] [blame] | 421 | #ifdef ANDROID_P2P |
| 422 | /* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */ |
| 423 | if(&oldest->list == p2p->sd_dev_list) |
| 424 | p2p->sd_dev_list = oldest->list.next; |
| 425 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 426 | dl_list_del(&oldest->list); |
| 427 | p2p_device_free(p2p, oldest); |
| 428 | } |
| 429 | |
| 430 | dev = os_zalloc(sizeof(*dev)); |
| 431 | if (dev == NULL) |
| 432 | return NULL; |
| 433 | dl_list_add(&p2p->devices, &dev->list); |
| 434 | os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN); |
| 435 | |
| 436 | return dev; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | static void p2p_copy_client_info(struct p2p_device *dev, |
| 441 | struct p2p_client_info *cli) |
| 442 | { |
| 443 | os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len); |
| 444 | dev->info.device_name[cli->dev_name_len] = '\0'; |
| 445 | dev->info.dev_capab = cli->dev_capab; |
| 446 | dev->info.config_methods = cli->config_methods; |
| 447 | os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8); |
| 448 | dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types; |
| 449 | os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types, |
| 450 | dev->info.wps_sec_dev_type_list_len); |
| 451 | } |
| 452 | |
| 453 | |
| 454 | static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr, |
| 455 | const u8 *go_interface_addr, int freq, |
| 456 | const u8 *gi, size_t gi_len) |
| 457 | { |
| 458 | struct p2p_group_info info; |
| 459 | size_t c; |
| 460 | struct p2p_device *dev; |
| 461 | |
| 462 | if (gi == NULL) |
| 463 | return 0; |
| 464 | |
| 465 | if (p2p_group_info_parse(gi, gi_len, &info) < 0) |
| 466 | return -1; |
| 467 | |
| 468 | /* |
| 469 | * Clear old data for this group; if the devices are still in the |
| 470 | * group, the information will be restored in the loop following this. |
| 471 | */ |
| 472 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 473 | if (os_memcmp(dev->member_in_go_iface, go_interface_addr, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 474 | ETH_ALEN) == 0) { |
| 475 | os_memset(dev->member_in_go_iface, 0, ETH_ALEN); |
| 476 | os_memset(dev->member_in_go_dev, 0, ETH_ALEN); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | for (c = 0; c < info.num_clients; c++) { |
| 481 | struct p2p_client_info *cli = &info.client[c]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 482 | if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr, |
| 483 | ETH_ALEN) == 0) |
| 484 | continue; /* ignore our own entry */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 485 | dev = p2p_get_device(p2p, cli->p2p_device_addr); |
| 486 | if (dev) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 487 | if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 488 | P2P_DEV_PROBE_REQ_ONLY)) { |
| 489 | /* |
| 490 | * Update information since we have not |
| 491 | * received this directly from the client. |
| 492 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 493 | p2p_copy_client_info(dev, cli); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 494 | } else { |
| 495 | /* |
| 496 | * Need to update P2P Client Discoverability |
| 497 | * flag since it is valid only in P2P Group |
| 498 | * Info attribute. |
| 499 | */ |
| 500 | dev->info.dev_capab &= |
| 501 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
| 502 | dev->info.dev_capab |= |
| 503 | cli->dev_capab & |
| 504 | P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
| 505 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 506 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { |
| 507 | dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY; |
| 508 | } |
| 509 | } else { |
| 510 | dev = p2p_create_device(p2p, cli->p2p_device_addr); |
| 511 | if (dev == NULL) |
| 512 | continue; |
| 513 | dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY; |
| 514 | p2p_copy_client_info(dev, cli); |
| 515 | dev->oper_freq = freq; |
| 516 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, |
| 517 | dev->info.p2p_device_addr, |
| 518 | &dev->info, 1); |
| 519 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; |
| 520 | } |
| 521 | |
| 522 | os_memcpy(dev->interface_addr, cli->p2p_interface_addr, |
| 523 | ETH_ALEN); |
| 524 | os_get_time(&dev->last_seen); |
| 525 | os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN); |
| 526 | os_memcpy(dev->member_in_go_iface, go_interface_addr, |
| 527 | ETH_ALEN); |
| 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | |
| 534 | static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req, |
| 535 | const struct p2p_message *msg) |
| 536 | { |
| 537 | os_memcpy(dev->info.device_name, msg->device_name, |
| 538 | sizeof(dev->info.device_name)); |
| 539 | |
| 540 | if (msg->manufacturer && |
| 541 | msg->manufacturer_len < sizeof(dev->info.manufacturer)) { |
| 542 | os_memset(dev->info.manufacturer, 0, |
| 543 | sizeof(dev->info.manufacturer)); |
| 544 | os_memcpy(dev->info.manufacturer, msg->manufacturer, |
| 545 | msg->manufacturer_len); |
| 546 | } |
| 547 | |
| 548 | if (msg->model_name && |
| 549 | msg->model_name_len < sizeof(dev->info.model_name)) { |
| 550 | os_memset(dev->info.model_name, 0, |
| 551 | sizeof(dev->info.model_name)); |
| 552 | os_memcpy(dev->info.model_name, msg->model_name, |
| 553 | msg->model_name_len); |
| 554 | } |
| 555 | |
| 556 | if (msg->model_number && |
| 557 | msg->model_number_len < sizeof(dev->info.model_number)) { |
| 558 | os_memset(dev->info.model_number, 0, |
| 559 | sizeof(dev->info.model_number)); |
| 560 | os_memcpy(dev->info.model_number, msg->model_number, |
| 561 | msg->model_number_len); |
| 562 | } |
| 563 | |
| 564 | if (msg->serial_number && |
| 565 | msg->serial_number_len < sizeof(dev->info.serial_number)) { |
| 566 | os_memset(dev->info.serial_number, 0, |
| 567 | sizeof(dev->info.serial_number)); |
| 568 | os_memcpy(dev->info.serial_number, msg->serial_number, |
| 569 | msg->serial_number_len); |
| 570 | } |
| 571 | |
| 572 | if (msg->pri_dev_type) |
| 573 | os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type, |
| 574 | sizeof(dev->info.pri_dev_type)); |
| 575 | else if (msg->wps_pri_dev_type) |
| 576 | os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type, |
| 577 | sizeof(dev->info.pri_dev_type)); |
| 578 | |
| 579 | if (msg->wps_sec_dev_type_list) { |
| 580 | os_memcpy(dev->info.wps_sec_dev_type_list, |
| 581 | msg->wps_sec_dev_type_list, |
| 582 | msg->wps_sec_dev_type_list_len); |
| 583 | dev->info.wps_sec_dev_type_list_len = |
| 584 | msg->wps_sec_dev_type_list_len; |
| 585 | } |
| 586 | |
| 587 | if (msg->capability) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 588 | /* |
| 589 | * P2P Client Discoverability bit is reserved in all frames |
| 590 | * that use this function, so do not change its value here. |
| 591 | */ |
| 592 | dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
| 593 | dev->info.dev_capab |= msg->capability[0] & |
| 594 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 595 | dev->info.group_capab = msg->capability[1]; |
| 596 | } |
| 597 | |
| 598 | if (msg->ext_listen_timing) { |
| 599 | dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing); |
| 600 | dev->ext_listen_interval = |
| 601 | WPA_GET_LE16(msg->ext_listen_timing + 2); |
| 602 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 603 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 604 | if (!probe_req) { |
| 605 | dev->info.config_methods = msg->config_methods ? |
| 606 | msg->config_methods : msg->wps_config_methods; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | |
| 611 | /** |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 612 | * p2p_add_device - Add peer entries based on scan results or P2P frames |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 613 | * @p2p: P2P module context from p2p_init() |
| 614 | * @addr: Source address of Beacon or Probe Response frame (may be either |
| 615 | * P2P Device Address or P2P Interface Address) |
| 616 | * @level: Signal level (signal strength of the received frame from the peer) |
| 617 | * @freq: Frequency on which the Beacon or Probe Response frame was received |
| 618 | * @ies: IEs from the Beacon or Probe Response frame |
| 619 | * @ies_len: Length of ies buffer in octets |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 620 | * @scan_res: Whether this was based on scan results |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 621 | * Returns: 0 on success, -1 on failure |
| 622 | * |
| 623 | * If the scan result is for a GO, the clients in the group will also be added |
| 624 | * to the peer table. This function can also be used with some other frames |
| 625 | * like Provision Discovery Request that contains P2P Capability and P2P Device |
| 626 | * Info attributes. |
| 627 | */ |
| 628 | int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 629 | const u8 *ies, size_t ies_len, int scan_res) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 630 | { |
| 631 | struct p2p_device *dev; |
| 632 | struct p2p_message msg; |
| 633 | const u8 *p2p_dev_addr; |
| 634 | int i; |
| 635 | |
| 636 | os_memset(&msg, 0, sizeof(msg)); |
| 637 | if (p2p_parse_ies(ies, ies_len, &msg)) { |
| 638 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 639 | "P2P: Failed to parse P2P IE for a device entry"); |
| 640 | p2p_parse_free(&msg); |
| 641 | return -1; |
| 642 | } |
| 643 | |
| 644 | if (msg.p2p_device_addr) |
| 645 | p2p_dev_addr = msg.p2p_device_addr; |
| 646 | else if (msg.device_id) |
| 647 | p2p_dev_addr = msg.device_id; |
| 648 | else { |
| 649 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 650 | "P2P: Ignore scan data without P2P Device Info or " |
| 651 | "P2P Device Id"); |
| 652 | p2p_parse_free(&msg); |
| 653 | return -1; |
| 654 | } |
| 655 | |
| 656 | if (!is_zero_ether_addr(p2p->peer_filter) && |
| 657 | os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) { |
| 658 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer " |
| 659 | "filter for " MACSTR " due to peer filter", |
| 660 | MAC2STR(p2p_dev_addr)); |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | dev = p2p_create_device(p2p, p2p_dev_addr); |
| 665 | if (dev == NULL) { |
| 666 | p2p_parse_free(&msg); |
| 667 | return -1; |
| 668 | } |
| 669 | os_get_time(&dev->last_seen); |
| 670 | dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY); |
| 671 | |
| 672 | if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0) |
| 673 | os_memcpy(dev->interface_addr, addr, ETH_ALEN); |
| 674 | if (msg.ssid && |
| 675 | (msg.ssid[1] != P2P_WILDCARD_SSID_LEN || |
| 676 | os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) |
| 677 | != 0)) { |
| 678 | os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]); |
| 679 | dev->oper_ssid_len = msg.ssid[1]; |
| 680 | } |
| 681 | |
| 682 | if (freq >= 2412 && freq <= 2484 && msg.ds_params && |
| 683 | *msg.ds_params >= 1 && *msg.ds_params <= 14) { |
| 684 | int ds_freq; |
| 685 | if (*msg.ds_params == 14) |
| 686 | ds_freq = 2484; |
| 687 | else |
| 688 | ds_freq = 2407 + *msg.ds_params * 5; |
| 689 | if (freq != ds_freq) { |
| 690 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 691 | "P2P: Update Listen frequency based on DS " |
| 692 | "Parameter Set IE: %d -> %d MHz", |
| 693 | freq, ds_freq); |
| 694 | freq = ds_freq; |
| 695 | } |
| 696 | } |
| 697 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 698 | if (dev->listen_freq && dev->listen_freq != freq && scan_res) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 699 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 700 | "P2P: Update Listen frequency based on scan " |
| 701 | "results (" MACSTR " %d -> %d MHz (DS param %d)", |
| 702 | MAC2STR(dev->info.p2p_device_addr), dev->listen_freq, |
| 703 | freq, msg.ds_params ? *msg.ds_params : -1); |
| 704 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 705 | if (scan_res) { |
| 706 | dev->listen_freq = freq; |
| 707 | if (msg.group_info) |
| 708 | dev->oper_freq = freq; |
| 709 | } |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 710 | dev->info.level = level; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 711 | |
| 712 | p2p_copy_wps_info(dev, 0, &msg); |
| 713 | |
| 714 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
| 715 | wpabuf_free(dev->info.wps_vendor_ext[i]); |
| 716 | dev->info.wps_vendor_ext[i] = NULL; |
| 717 | } |
| 718 | |
| 719 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
| 720 | if (msg.wps_vendor_ext[i] == NULL) |
| 721 | break; |
| 722 | dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy( |
| 723 | msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]); |
| 724 | if (dev->info.wps_vendor_ext[i] == NULL) |
| 725 | break; |
| 726 | } |
| 727 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 728 | if (scan_res) { |
| 729 | p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, |
| 730 | msg.group_info, msg.group_info_len); |
| 731 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 732 | |
| 733 | p2p_parse_free(&msg); |
| 734 | |
| 735 | if (p2p_pending_sd_req(p2p, dev)) |
| 736 | dev->flags |= P2P_DEV_SD_SCHEDULE; |
| 737 | |
| 738 | if (dev->flags & P2P_DEV_REPORTED) |
| 739 | return 0; |
| 740 | |
| 741 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 742 | "P2P: Peer found with Listen frequency %d MHz", freq); |
| 743 | if (dev->flags & P2P_DEV_USER_REJECTED) { |
| 744 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 745 | "P2P: Do not report rejected device"); |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info, |
| 750 | !(dev->flags & P2P_DEV_REPORTED_ONCE)); |
| 751 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; |
| 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | |
| 757 | static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev) |
| 758 | { |
| 759 | int i; |
| 760 | |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 761 | if (p2p->go_neg_peer == dev) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 762 | /* |
| 763 | * If GO Negotiation is in progress, report that it has failed. |
| 764 | */ |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 765 | p2p_go_neg_failed(p2p, dev, -1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 766 | p2p->go_neg_peer = NULL; |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 767 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 768 | if (p2p->invite_peer == dev) |
| 769 | p2p->invite_peer = NULL; |
| 770 | if (p2p->sd_peer == dev) |
| 771 | p2p->sd_peer = NULL; |
| 772 | if (p2p->pending_client_disc_go == dev) |
| 773 | p2p->pending_client_disc_go = NULL; |
| 774 | |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 775 | /* dev_lost() device, but only if it was previously dev_found() */ |
| 776 | if (dev->flags & P2P_DEV_REPORTED_ONCE) |
| 777 | p2p->cfg->dev_lost(p2p->cfg->cb_ctx, |
| 778 | dev->info.p2p_device_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 779 | |
| 780 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
| 781 | wpabuf_free(dev->info.wps_vendor_ext[i]); |
| 782 | dev->info.wps_vendor_ext[i] = NULL; |
| 783 | } |
| 784 | |
| 785 | os_free(dev); |
| 786 | } |
| 787 | |
| 788 | |
| 789 | static int p2p_get_next_prog_freq(struct p2p_data *p2p) |
| 790 | { |
| 791 | struct p2p_channels *c; |
| 792 | struct p2p_reg_class *cla; |
| 793 | size_t cl, ch; |
| 794 | int found = 0; |
| 795 | u8 reg_class; |
| 796 | u8 channel; |
| 797 | int freq; |
| 798 | |
| 799 | c = &p2p->cfg->channels; |
| 800 | for (cl = 0; cl < c->reg_classes; cl++) { |
| 801 | cla = &c->reg_class[cl]; |
| 802 | if (cla->reg_class != p2p->last_prog_scan_class) |
| 803 | continue; |
| 804 | for (ch = 0; ch < cla->channels; ch++) { |
| 805 | if (cla->channel[ch] == p2p->last_prog_scan_chan) { |
| 806 | found = 1; |
| 807 | break; |
| 808 | } |
| 809 | } |
| 810 | if (found) |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | if (!found) { |
| 815 | /* Start from beginning */ |
| 816 | reg_class = c->reg_class[0].reg_class; |
| 817 | channel = c->reg_class[0].channel[0]; |
| 818 | } else { |
| 819 | /* Pick the next channel */ |
| 820 | ch++; |
| 821 | if (ch == cla->channels) { |
| 822 | cl++; |
| 823 | if (cl == c->reg_classes) |
| 824 | cl = 0; |
| 825 | ch = 0; |
| 826 | } |
| 827 | reg_class = c->reg_class[cl].reg_class; |
| 828 | channel = c->reg_class[cl].channel[ch]; |
| 829 | } |
| 830 | |
| 831 | freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel); |
| 832 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search " |
| 833 | "channel: reg_class %u channel %u -> %d MHz", |
| 834 | reg_class, channel, freq); |
| 835 | p2p->last_prog_scan_class = reg_class; |
| 836 | p2p->last_prog_scan_chan = channel; |
| 837 | |
| 838 | if (freq == 2412 || freq == 2437 || freq == 2462) |
| 839 | return 0; /* No need to add social channels */ |
| 840 | return freq; |
| 841 | } |
| 842 | |
| 843 | |
| 844 | static void p2p_search(struct p2p_data *p2p) |
| 845 | { |
| 846 | int freq = 0; |
| 847 | enum p2p_scan_type type; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 848 | u16 pw_id = DEV_PW_DEFAULT; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 849 | |
| 850 | if (p2p->drv_in_listen) { |
| 851 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still " |
| 852 | "in Listen state - wait for it to end before " |
| 853 | "continuing"); |
| 854 | return; |
| 855 | } |
| 856 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
| 857 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 858 | if (p2p->find_type == P2P_FIND_PROGRESSIVE && |
| 859 | (freq = p2p_get_next_prog_freq(p2p)) > 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 860 | type = P2P_SCAN_SOCIAL_PLUS_ONE; |
| 861 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search " |
| 862 | "(+ freq %u)", freq); |
| 863 | } else { |
| 864 | type = P2P_SCAN_SOCIAL; |
| 865 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search"); |
| 866 | } |
| 867 | |
| 868 | if (p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 869 | p2p->num_req_dev_types, p2p->req_dev_types, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 870 | p2p->find_dev_id, pw_id)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 871 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 872 | "P2P: Scan request failed"); |
| 873 | p2p_continue_find(p2p); |
| 874 | } else { |
| 875 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan"); |
| 876 | p2p->p2p_scan_running = 1; |
| 877 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); |
| 878 | eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout, |
| 879 | p2p, NULL); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | |
| 884 | static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx) |
| 885 | { |
| 886 | struct p2p_data *p2p = eloop_ctx; |
| 887 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop"); |
| 888 | p2p_stop_find(p2p); |
| 889 | } |
| 890 | |
| 891 | |
| 892 | static int p2p_run_after_scan(struct p2p_data *p2p) |
| 893 | { |
| 894 | struct p2p_device *dev; |
| 895 | enum p2p_after_scan op; |
| 896 | |
| 897 | if (p2p->after_scan_tx) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 898 | /* TODO: schedule p2p_run_after_scan to be called from TX |
| 899 | * status callback(?) */ |
| 900 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending " |
| 901 | "Action frame at p2p_scan completion"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 902 | p2p->cfg->send_action(p2p->cfg->cb_ctx, |
| 903 | p2p->after_scan_tx->freq, |
| 904 | p2p->after_scan_tx->dst, |
| 905 | p2p->after_scan_tx->src, |
| 906 | p2p->after_scan_tx->bssid, |
| 907 | (u8 *) (p2p->after_scan_tx + 1), |
| 908 | p2p->after_scan_tx->len, |
| 909 | p2p->after_scan_tx->wait_time); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 910 | os_free(p2p->after_scan_tx); |
| 911 | p2p->after_scan_tx = NULL; |
Irfan Sheriff | f44b9c4 | 2012-06-13 11:09:23 -0700 | [diff] [blame] | 912 | #ifdef ANDROID_P2P |
| 913 | /* For SD frames, there is a scenario, where we can receive a SD request frame during p2p_scan. |
| 914 | * At that moment, we will send the SD response from this context. After sending the SD response, |
| 915 | * we need to continue p2p_find. But if we return 1 from here, p2p_find is going to be stopped. |
| 916 | */ |
| 917 | return 0; |
| 918 | #else |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 919 | return 1; |
Irfan Sheriff | f44b9c4 | 2012-06-13 11:09:23 -0700 | [diff] [blame] | 920 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | op = p2p->start_after_scan; |
| 924 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; |
| 925 | switch (op) { |
| 926 | case P2P_AFTER_SCAN_NOTHING: |
| 927 | break; |
| 928 | case P2P_AFTER_SCAN_LISTEN: |
| 929 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously " |
| 930 | "requested Listen state"); |
| 931 | p2p_listen(p2p, p2p->pending_listen_sec * 1000 + |
| 932 | p2p->pending_listen_usec / 1000); |
| 933 | return 1; |
| 934 | case P2P_AFTER_SCAN_CONNECT: |
| 935 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously " |
| 936 | "requested connect with " MACSTR, |
| 937 | MAC2STR(p2p->after_scan_peer)); |
| 938 | dev = p2p_get_device(p2p, p2p->after_scan_peer); |
| 939 | if (dev == NULL) { |
| 940 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not " |
| 941 | "known anymore"); |
| 942 | break; |
| 943 | } |
| 944 | p2p_connect_send(p2p, dev); |
| 945 | return 1; |
| 946 | } |
| 947 | |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx) |
| 953 | { |
| 954 | struct p2p_data *p2p = eloop_ctx; |
| 955 | int running; |
| 956 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout " |
| 957 | "(running=%d)", p2p->p2p_scan_running); |
| 958 | running = p2p->p2p_scan_running; |
| 959 | /* Make sure we recover from missed scan results callback */ |
| 960 | p2p->p2p_scan_running = 0; |
| 961 | |
| 962 | if (running) |
| 963 | p2p_run_after_scan(p2p); |
| 964 | } |
| 965 | |
| 966 | |
| 967 | static void p2p_free_req_dev_types(struct p2p_data *p2p) |
| 968 | { |
| 969 | p2p->num_req_dev_types = 0; |
| 970 | os_free(p2p->req_dev_types); |
| 971 | p2p->req_dev_types = NULL; |
| 972 | } |
| 973 | |
| 974 | |
| 975 | int p2p_find(struct p2p_data *p2p, unsigned int timeout, |
| 976 | enum p2p_discovery_type type, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 977 | unsigned int num_req_dev_types, const u8 *req_dev_types, |
| 978 | const u8 *dev_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 979 | { |
| 980 | int res; |
| 981 | |
| 982 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)", |
| 983 | type); |
| 984 | if (p2p->p2p_scan_running) { |
| 985 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is " |
| 986 | "already running"); |
| 987 | } |
| 988 | |
| 989 | p2p_free_req_dev_types(p2p); |
| 990 | if (req_dev_types && num_req_dev_types) { |
| 991 | p2p->req_dev_types = os_malloc(num_req_dev_types * |
| 992 | WPS_DEV_TYPE_LEN); |
| 993 | if (p2p->req_dev_types == NULL) |
| 994 | return -1; |
| 995 | os_memcpy(p2p->req_dev_types, req_dev_types, |
| 996 | num_req_dev_types * WPS_DEV_TYPE_LEN); |
| 997 | p2p->num_req_dev_types = num_req_dev_types; |
| 998 | } |
| 999 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1000 | if (dev_id) { |
| 1001 | os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN); |
| 1002 | p2p->find_dev_id = p2p->find_dev_id_buf; |
| 1003 | } else |
| 1004 | p2p->find_dev_id = NULL; |
| 1005 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1006 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; |
| 1007 | p2p_clear_timeout(p2p); |
| 1008 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
| 1009 | p2p->find_type = type; |
| 1010 | p2p_device_clear_reported(p2p); |
| 1011 | p2p_set_state(p2p, P2P_SEARCH); |
| 1012 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1013 | p2p->last_p2p_find_timeout = timeout; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1014 | if (timeout) |
| 1015 | eloop_register_timeout(timeout, 0, p2p_find_timeout, |
| 1016 | p2p, NULL); |
| 1017 | switch (type) { |
| 1018 | case P2P_FIND_START_WITH_FULL: |
| 1019 | case P2P_FIND_PROGRESSIVE: |
| 1020 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0, |
| 1021 | p2p->num_req_dev_types, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1022 | p2p->req_dev_types, dev_id, |
| 1023 | DEV_PW_DEFAULT); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1024 | break; |
| 1025 | case P2P_FIND_ONLY_SOCIAL: |
| 1026 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0, |
| 1027 | p2p->num_req_dev_types, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1028 | p2p->req_dev_types, dev_id, |
| 1029 | DEV_PW_DEFAULT); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1030 | break; |
| 1031 | default: |
| 1032 | return -1; |
| 1033 | } |
| 1034 | |
| 1035 | if (res == 0) { |
| 1036 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan"); |
| 1037 | p2p->p2p_scan_running = 1; |
| 1038 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); |
| 1039 | eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout, |
| 1040 | p2p, NULL); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1041 | } else if (res == 1) { |
| 1042 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start " |
| 1043 | "p2p_scan at this point - will try again after " |
| 1044 | "previous scan completes"); |
| 1045 | res = 0; |
| 1046 | p2p_set_state(p2p, P2P_SEARCH_WHEN_READY); |
| 1047 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1048 | } else { |
| 1049 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start " |
| 1050 | "p2p_scan"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1051 | p2p_set_state(p2p, P2P_IDLE); |
| 1052 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | return res; |
| 1056 | } |
| 1057 | |
Dmitry Shmidt | 2fb777c | 2012-05-02 12:29:53 -0700 | [diff] [blame] | 1058 | #ifdef ANDROID_P2P |
| 1059 | int p2p_search_pending(struct p2p_data *p2p) |
| 1060 | { |
| 1061 | if(p2p == NULL) |
| 1062 | return 0; |
| 1063 | |
| 1064 | if(p2p->state == P2P_SEARCH_WHEN_READY) |
| 1065 | return 1; |
| 1066 | |
| 1067 | return 0; |
| 1068 | } |
| 1069 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1070 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1071 | int p2p_other_scan_completed(struct p2p_data *p2p) |
| 1072 | { |
| 1073 | if (p2p->state != P2P_SEARCH_WHEN_READY) |
| 1074 | return 0; |
| 1075 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find " |
| 1076 | "now that previous scan was completed"); |
| 1077 | if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1078 | p2p->num_req_dev_types, p2p->req_dev_types, |
| 1079 | p2p->find_dev_id) < 0) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1080 | return 0; |
| 1081 | return 1; |
| 1082 | } |
| 1083 | |
| 1084 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1085 | void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq) |
| 1086 | { |
| 1087 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find"); |
| 1088 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); |
| 1089 | p2p_clear_timeout(p2p); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1090 | if (p2p->state == P2P_SEARCH) |
| 1091 | wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1092 | p2p_set_state(p2p, P2P_IDLE); |
| 1093 | p2p_free_req_dev_types(p2p); |
| 1094 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; |
| 1095 | p2p->go_neg_peer = NULL; |
| 1096 | p2p->sd_peer = NULL; |
| 1097 | p2p->invite_peer = NULL; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1098 | p2p_stop_listen_for_freq(p2p, freq); |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq) |
| 1103 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1104 | if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) { |
| 1105 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen " |
| 1106 | "since we are on correct channel for response"); |
| 1107 | return; |
| 1108 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1109 | if (p2p->in_listen) { |
| 1110 | p2p->in_listen = 0; |
| 1111 | p2p_clear_timeout(p2p); |
| 1112 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1113 | if (p2p->drv_in_listen) { |
| 1114 | /* |
| 1115 | * The driver may not deliver callback to p2p_listen_end() |
| 1116 | * when the operation gets canceled, so clear the internal |
| 1117 | * variable that is tracking driver state. |
| 1118 | */ |
| 1119 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear " |
| 1120 | "drv_in_listen (%d)", p2p->drv_in_listen); |
| 1121 | p2p->drv_in_listen = 0; |
| 1122 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1123 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
| 1124 | } |
| 1125 | |
| 1126 | |
| 1127 | void p2p_stop_find(struct p2p_data *p2p) |
| 1128 | { |
| 1129 | p2p_stop_find_for_freq(p2p, 0); |
| 1130 | } |
| 1131 | |
| 1132 | |
| 1133 | static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq) |
| 1134 | { |
| 1135 | if (force_freq) { |
| 1136 | u8 op_reg_class, op_channel; |
| 1137 | if (p2p_freq_to_channel(p2p->cfg->country, force_freq, |
| 1138 | &op_reg_class, &op_channel) < 0) { |
| 1139 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1140 | "P2P: Unsupported frequency %u MHz", |
| 1141 | force_freq); |
| 1142 | return -1; |
| 1143 | } |
| 1144 | if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class, |
| 1145 | op_channel)) { |
| 1146 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1147 | "P2P: Frequency %u MHz (oper_class %u " |
| 1148 | "channel %u) not allowed for P2P", |
| 1149 | force_freq, op_reg_class, op_channel); |
| 1150 | return -1; |
| 1151 | } |
| 1152 | p2p->op_reg_class = op_reg_class; |
| 1153 | p2p->op_channel = op_channel; |
Dmitry Shmidt | 04abaa9 | 2012-08-17 16:22:31 -0700 | [diff] [blame] | 1154 | #ifndef ANDROID_P2P |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1155 | p2p->channels.reg_classes = 1; |
| 1156 | p2p->channels.reg_class[0].channels = 1; |
| 1157 | p2p->channels.reg_class[0].reg_class = p2p->op_reg_class; |
| 1158 | p2p->channels.reg_class[0].channel[0] = p2p->op_channel; |
Dmitry Shmidt | 04abaa9 | 2012-08-17 16:22:31 -0700 | [diff] [blame] | 1159 | #else |
| 1160 | if(p2p->cfg->p2p_concurrency == P2P_MULTI_CHANNEL_CONCURRENT) { |
| 1161 | /* We we are requesting for a preferred channel. But since |
| 1162 | * are multichannel concurrent, we have to poplulate the |
| 1163 | * p2p_channels with list of channels that we support. |
| 1164 | */ |
| 1165 | os_memcpy(&p2p->channels, &p2p->cfg->channels, |
| 1166 | sizeof(struct p2p_channels)); |
| 1167 | } else { |
| 1168 | p2p->channels.reg_classes = 1; |
| 1169 | p2p->channels.reg_class[0].channels = 1; |
| 1170 | p2p->channels.reg_class[0].reg_class = p2p->op_reg_class; |
| 1171 | p2p->channels.reg_class[0].channel[0] = p2p->op_channel; |
| 1172 | } |
| 1173 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1174 | } else { |
| 1175 | u8 op_reg_class, op_channel; |
| 1176 | |
| 1177 | if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 && |
| 1178 | p2p_supported_freq(p2p, p2p->best_freq_overall) && |
| 1179 | p2p_freq_to_channel(p2p->cfg->country, |
| 1180 | p2p->best_freq_overall, |
| 1181 | &op_reg_class, &op_channel) == 0) { |
| 1182 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1183 | "P2P: Select best overall channel as " |
| 1184 | "operating channel preference"); |
| 1185 | p2p->op_reg_class = op_reg_class; |
| 1186 | p2p->op_channel = op_channel; |
| 1187 | } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 && |
| 1188 | p2p_supported_freq(p2p, p2p->best_freq_5) && |
| 1189 | p2p_freq_to_channel(p2p->cfg->country, |
| 1190 | p2p->best_freq_5, |
| 1191 | &op_reg_class, &op_channel) == |
| 1192 | 0) { |
| 1193 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1194 | "P2P: Select best 5 GHz channel as " |
| 1195 | "operating channel preference"); |
| 1196 | p2p->op_reg_class = op_reg_class; |
| 1197 | p2p->op_channel = op_channel; |
| 1198 | } else if (!p2p->cfg->cfg_op_channel && |
| 1199 | p2p->best_freq_24 > 0 && |
| 1200 | p2p_supported_freq(p2p, p2p->best_freq_24) && |
| 1201 | p2p_freq_to_channel(p2p->cfg->country, |
| 1202 | p2p->best_freq_24, |
| 1203 | &op_reg_class, &op_channel) == |
| 1204 | 0) { |
| 1205 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1206 | "P2P: Select best 2.4 GHz channel as " |
| 1207 | "operating channel preference"); |
| 1208 | p2p->op_reg_class = op_reg_class; |
| 1209 | p2p->op_channel = op_channel; |
| 1210 | } else { |
| 1211 | p2p->op_reg_class = p2p->cfg->op_reg_class; |
| 1212 | p2p->op_channel = p2p->cfg->op_channel; |
| 1213 | } |
| 1214 | |
| 1215 | os_memcpy(&p2p->channels, &p2p->cfg->channels, |
| 1216 | sizeof(struct p2p_channels)); |
| 1217 | } |
| 1218 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1219 | "P2P: Own preference for operation channel: " |
| 1220 | "Operating Class %u Channel %u%s", |
| 1221 | p2p->op_reg_class, p2p->op_channel, |
| 1222 | force_freq ? " (forced)" : ""); |
| 1223 | |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1228 | static void p2p_set_dev_persistent(struct p2p_device *dev, |
| 1229 | int persistent_group) |
| 1230 | { |
| 1231 | switch (persistent_group) { |
| 1232 | case 0: |
| 1233 | dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP | |
| 1234 | P2P_DEV_PREFER_PERSISTENT_RECONN); |
| 1235 | break; |
| 1236 | case 1: |
| 1237 | dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP; |
| 1238 | dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN; |
| 1239 | break; |
| 1240 | case 2: |
| 1241 | dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP | |
| 1242 | P2P_DEV_PREFER_PERSISTENT_RECONN; |
| 1243 | break; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1248 | int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr, |
| 1249 | enum p2p_wps_method wps_method, |
| 1250 | int go_intent, const u8 *own_interface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1251 | unsigned int force_freq, int persistent_group, |
| 1252 | const u8 *force_ssid, size_t force_ssid_len, |
| 1253 | int pd_before_go_neg) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1254 | { |
| 1255 | struct p2p_device *dev; |
| 1256 | |
| 1257 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1258 | "P2P: Request to start group negotiation - peer=" MACSTR |
| 1259 | " GO Intent=%d Intended Interface Address=" MACSTR |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1260 | " wps_method=%d persistent_group=%d pd_before_go_neg=%d", |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1261 | MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr), |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1262 | wps_method, persistent_group, pd_before_go_neg); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1263 | |
| 1264 | if (p2p_prepare_channel(p2p, force_freq) < 0) |
| 1265 | return -1; |
| 1266 | |
| 1267 | dev = p2p_get_device(p2p, peer_addr); |
| 1268 | if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) { |
| 1269 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1270 | "P2P: Cannot connect to unknown P2P Device " MACSTR, |
| 1271 | MAC2STR(peer_addr)); |
| 1272 | return -1; |
| 1273 | } |
| 1274 | |
| 1275 | if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) { |
| 1276 | if (!(dev->info.dev_capab & |
| 1277 | P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) { |
| 1278 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1279 | "P2P: Cannot connect to P2P Device " MACSTR |
| 1280 | " that is in a group and is not discoverable", |
| 1281 | MAC2STR(peer_addr)); |
| 1282 | return -1; |
| 1283 | } |
| 1284 | if (dev->oper_freq <= 0) { |
| 1285 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1286 | "P2P: Cannot connect to P2P Device " MACSTR |
| 1287 | " with incomplete information", |
| 1288 | MAC2STR(peer_addr)); |
| 1289 | return -1; |
| 1290 | } |
| 1291 | |
| 1292 | /* |
| 1293 | * First, try to connect directly. If the peer does not |
| 1294 | * acknowledge frames, assume it is sleeping and use device |
| 1295 | * discoverability via the GO at that point. |
| 1296 | */ |
| 1297 | } |
| 1298 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1299 | p2p->ssid_set = 0; |
| 1300 | if (force_ssid) { |
| 1301 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID", |
| 1302 | force_ssid, force_ssid_len); |
| 1303 | os_memcpy(p2p->ssid, force_ssid, force_ssid_len); |
| 1304 | p2p->ssid_len = force_ssid_len; |
| 1305 | p2p->ssid_set = 1; |
| 1306 | } |
| 1307 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1308 | dev->flags &= ~P2P_DEV_NOT_YET_READY; |
| 1309 | dev->flags &= ~P2P_DEV_USER_REJECTED; |
| 1310 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; |
| 1311 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1312 | if (pd_before_go_neg) |
| 1313 | dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG; |
| 1314 | else |
| 1315 | dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1316 | dev->connect_reqs = 0; |
| 1317 | dev->go_neg_req_sent = 0; |
| 1318 | dev->go_state = UNKNOWN_GO; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1319 | p2p_set_dev_persistent(dev, persistent_group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1320 | p2p->go_intent = go_intent; |
| 1321 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); |
| 1322 | |
| 1323 | if (p2p->state != P2P_IDLE) |
| 1324 | p2p_stop_find(p2p); |
| 1325 | |
| 1326 | if (p2p->after_scan_tx) { |
| 1327 | /* |
| 1328 | * We need to drop the pending frame to avoid issues with the |
| 1329 | * new GO Negotiation, e.g., when the pending frame was from a |
| 1330 | * previous attempt at starting a GO Negotiation. |
| 1331 | */ |
| 1332 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped " |
| 1333 | "previous pending Action frame TX that was waiting " |
| 1334 | "for p2p_scan completion"); |
| 1335 | os_free(p2p->after_scan_tx); |
| 1336 | p2p->after_scan_tx = NULL; |
| 1337 | } |
| 1338 | |
| 1339 | dev->wps_method = wps_method; |
| 1340 | dev->status = P2P_SC_SUCCESS; |
| 1341 | |
| 1342 | if (force_freq) |
| 1343 | dev->flags |= P2P_DEV_FORCE_FREQ; |
| 1344 | else |
| 1345 | dev->flags &= ~P2P_DEV_FORCE_FREQ; |
| 1346 | |
| 1347 | if (p2p->p2p_scan_running) { |
| 1348 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1349 | "P2P: p2p_scan running - delay connect send"); |
| 1350 | p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT; |
| 1351 | os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN); |
| 1352 | return 0; |
| 1353 | } |
| 1354 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; |
| 1355 | |
| 1356 | return p2p_connect_send(p2p, dev); |
| 1357 | } |
| 1358 | |
| 1359 | |
| 1360 | int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr, |
| 1361 | enum p2p_wps_method wps_method, |
| 1362 | int go_intent, const u8 *own_interface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1363 | unsigned int force_freq, int persistent_group, |
| 1364 | const u8 *force_ssid, size_t force_ssid_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1365 | { |
| 1366 | struct p2p_device *dev; |
| 1367 | |
| 1368 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1369 | "P2P: Request to authorize group negotiation - peer=" MACSTR |
| 1370 | " GO Intent=%d Intended Interface Address=" MACSTR |
| 1371 | " wps_method=%d persistent_group=%d", |
| 1372 | MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr), |
| 1373 | wps_method, persistent_group); |
| 1374 | |
| 1375 | if (p2p_prepare_channel(p2p, force_freq) < 0) |
| 1376 | return -1; |
| 1377 | |
| 1378 | dev = p2p_get_device(p2p, peer_addr); |
| 1379 | if (dev == NULL) { |
| 1380 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1381 | "P2P: Cannot authorize unknown P2P Device " MACSTR, |
| 1382 | MAC2STR(peer_addr)); |
| 1383 | return -1; |
| 1384 | } |
| 1385 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1386 | p2p->ssid_set = 0; |
| 1387 | if (force_ssid) { |
| 1388 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID", |
| 1389 | force_ssid, force_ssid_len); |
| 1390 | os_memcpy(p2p->ssid, force_ssid, force_ssid_len); |
| 1391 | p2p->ssid_len = force_ssid_len; |
| 1392 | p2p->ssid_set = 1; |
| 1393 | } |
| 1394 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1395 | dev->flags &= ~P2P_DEV_NOT_YET_READY; |
| 1396 | dev->flags &= ~P2P_DEV_USER_REJECTED; |
| 1397 | dev->go_neg_req_sent = 0; |
| 1398 | dev->go_state = UNKNOWN_GO; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1399 | p2p_set_dev_persistent(dev, persistent_group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1400 | p2p->go_intent = go_intent; |
| 1401 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); |
| 1402 | |
| 1403 | dev->wps_method = wps_method; |
| 1404 | dev->status = P2P_SC_SUCCESS; |
| 1405 | |
| 1406 | if (force_freq) |
| 1407 | dev->flags |= P2P_DEV_FORCE_FREQ; |
| 1408 | else |
| 1409 | dev->flags &= ~P2P_DEV_FORCE_FREQ; |
| 1410 | |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr, |
| 1416 | struct p2p_device *dev, struct p2p_message *msg) |
| 1417 | { |
| 1418 | os_get_time(&dev->last_seen); |
| 1419 | |
| 1420 | p2p_copy_wps_info(dev, 0, msg); |
| 1421 | |
| 1422 | if (msg->listen_channel) { |
| 1423 | int freq; |
| 1424 | freq = p2p_channel_to_freq((char *) msg->listen_channel, |
| 1425 | msg->listen_channel[3], |
| 1426 | msg->listen_channel[4]); |
| 1427 | if (freq < 0) { |
| 1428 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1429 | "P2P: Unknown peer Listen channel: " |
| 1430 | "country=%c%c(0x%02x) reg_class=%u channel=%u", |
| 1431 | msg->listen_channel[0], |
| 1432 | msg->listen_channel[1], |
| 1433 | msg->listen_channel[2], |
| 1434 | msg->listen_channel[3], |
| 1435 | msg->listen_channel[4]); |
| 1436 | } else { |
| 1437 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update " |
| 1438 | "peer " MACSTR " Listen channel: %u -> %u MHz", |
| 1439 | MAC2STR(dev->info.p2p_device_addr), |
| 1440 | dev->listen_freq, freq); |
| 1441 | dev->listen_freq = freq; |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { |
| 1446 | dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY; |
| 1447 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1448 | "P2P: Completed device entry based on data from " |
| 1449 | "GO Negotiation Request"); |
| 1450 | } else { |
| 1451 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1452 | "P2P: Created device entry based on GO Neg Req: " |
| 1453 | MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' " |
| 1454 | "listen_freq=%d", |
| 1455 | MAC2STR(dev->info.p2p_device_addr), |
| 1456 | dev->info.dev_capab, dev->info.group_capab, |
| 1457 | dev->info.device_name, dev->listen_freq); |
| 1458 | } |
| 1459 | |
| 1460 | dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY; |
| 1461 | |
| 1462 | if (dev->flags & P2P_DEV_USER_REJECTED) { |
| 1463 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1464 | "P2P: Do not report rejected device"); |
| 1465 | return; |
| 1466 | } |
| 1467 | |
| 1468 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info, |
| 1469 | !(dev->flags & P2P_DEV_REPORTED_ONCE)); |
| 1470 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; |
| 1471 | } |
| 1472 | |
| 1473 | |
| 1474 | void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len) |
| 1475 | { |
| 1476 | os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); |
| 1477 | p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2); |
| 1478 | os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2], |
| 1479 | p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len); |
| 1480 | *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len; |
| 1481 | } |
| 1482 | |
| 1483 | |
| 1484 | int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params) |
| 1485 | { |
| 1486 | p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len); |
| 1487 | p2p_random(params->passphrase, 8); |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer) |
| 1493 | { |
| 1494 | struct p2p_go_neg_results res; |
| 1495 | int go = peer->go_state == LOCAL_GO; |
| 1496 | struct p2p_channels intersection; |
| 1497 | int freqs; |
| 1498 | size_t i, j; |
| 1499 | |
| 1500 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1501 | "P2P: GO Negotiation with " MACSTR " completed (%s will be " |
| 1502 | "GO)", MAC2STR(peer->info.p2p_device_addr), |
| 1503 | go ? "local end" : "peer"); |
| 1504 | |
| 1505 | os_memset(&res, 0, sizeof(res)); |
| 1506 | res.role_go = go; |
| 1507 | os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN); |
| 1508 | os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN); |
| 1509 | res.wps_method = peer->wps_method; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1510 | if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) { |
| 1511 | if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN) |
| 1512 | res.persistent_group = 2; |
| 1513 | else |
| 1514 | res.persistent_group = 1; |
| 1515 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1516 | |
| 1517 | if (go) { |
| 1518 | /* Setup AP mode for WPS provisioning */ |
| 1519 | res.freq = p2p_channel_to_freq(p2p->cfg->country, |
| 1520 | p2p->op_reg_class, |
| 1521 | p2p->op_channel); |
| 1522 | os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len); |
| 1523 | res.ssid_len = p2p->ssid_len; |
| 1524 | p2p_random(res.passphrase, 8); |
| 1525 | } else { |
| 1526 | res.freq = peer->oper_freq; |
| 1527 | if (p2p->ssid_len) { |
| 1528 | os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len); |
| 1529 | res.ssid_len = p2p->ssid_len; |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | p2p_channels_intersect(&p2p->channels, &peer->channels, |
| 1534 | &intersection); |
| 1535 | freqs = 0; |
| 1536 | for (i = 0; i < intersection.reg_classes; i++) { |
| 1537 | struct p2p_reg_class *c = &intersection.reg_class[i]; |
| 1538 | if (freqs + 1 == P2P_MAX_CHANNELS) |
| 1539 | break; |
| 1540 | for (j = 0; j < c->channels; j++) { |
| 1541 | int freq; |
| 1542 | if (freqs + 1 == P2P_MAX_CHANNELS) |
| 1543 | break; |
| 1544 | freq = p2p_channel_to_freq(peer->country, c->reg_class, |
| 1545 | c->channel[j]); |
| 1546 | if (freq < 0) |
| 1547 | continue; |
| 1548 | res.freq_list[freqs++] = freq; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout; |
| 1553 | |
| 1554 | p2p_clear_timeout(p2p); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1555 | p2p->ssid_set = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1556 | peer->go_neg_req_sent = 0; |
| 1557 | peer->wps_method = WPS_NOT_READY; |
| 1558 | |
| 1559 | p2p_set_state(p2p, P2P_PROVISIONING); |
| 1560 | p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res); |
| 1561 | } |
| 1562 | |
| 1563 | |
| 1564 | static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa, |
| 1565 | const u8 *data, size_t len, int rx_freq) |
| 1566 | { |
| 1567 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1568 | "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa)); |
| 1569 | wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len); |
| 1570 | |
| 1571 | if (len < 1) |
| 1572 | return; |
| 1573 | |
| 1574 | switch (data[0]) { |
| 1575 | case P2P_GO_NEG_REQ: |
| 1576 | p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq); |
| 1577 | break; |
| 1578 | case P2P_GO_NEG_RESP: |
| 1579 | p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq); |
| 1580 | break; |
| 1581 | case P2P_GO_NEG_CONF: |
| 1582 | p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1); |
| 1583 | break; |
| 1584 | case P2P_INVITATION_REQ: |
| 1585 | p2p_process_invitation_req(p2p, sa, data + 1, len - 1, |
| 1586 | rx_freq); |
| 1587 | break; |
| 1588 | case P2P_INVITATION_RESP: |
| 1589 | p2p_process_invitation_resp(p2p, sa, data + 1, len - 1); |
| 1590 | break; |
| 1591 | case P2P_PROV_DISC_REQ: |
| 1592 | p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq); |
| 1593 | break; |
| 1594 | case P2P_PROV_DISC_RESP: |
| 1595 | p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1); |
| 1596 | break; |
| 1597 | case P2P_DEV_DISC_REQ: |
| 1598 | p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq); |
| 1599 | break; |
| 1600 | case P2P_DEV_DISC_RESP: |
| 1601 | p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1); |
| 1602 | break; |
| 1603 | default: |
| 1604 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1605 | "P2P: Unsupported P2P Public Action frame type %d", |
| 1606 | data[0]); |
| 1607 | break; |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1612 | static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, |
| 1613 | const u8 *sa, const u8 *bssid, const u8 *data, |
| 1614 | size_t len, int freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1615 | { |
| 1616 | if (len < 1) |
| 1617 | return; |
| 1618 | |
| 1619 | switch (data[0]) { |
| 1620 | case WLAN_PA_VENDOR_SPECIFIC: |
| 1621 | data++; |
| 1622 | len--; |
| 1623 | if (len < 3) |
| 1624 | return; |
| 1625 | if (WPA_GET_BE24(data) != OUI_WFA) |
| 1626 | return; |
| 1627 | |
| 1628 | data += 3; |
| 1629 | len -= 3; |
| 1630 | if (len < 1) |
| 1631 | return; |
| 1632 | |
| 1633 | if (*data != P2P_OUI_TYPE) |
| 1634 | return; |
| 1635 | |
| 1636 | p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq); |
| 1637 | break; |
| 1638 | case WLAN_PA_GAS_INITIAL_REQ: |
| 1639 | p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq); |
| 1640 | break; |
| 1641 | case WLAN_PA_GAS_INITIAL_RESP: |
| 1642 | p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq); |
| 1643 | break; |
| 1644 | case WLAN_PA_GAS_COMEBACK_REQ: |
| 1645 | p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq); |
| 1646 | break; |
| 1647 | case WLAN_PA_GAS_COMEBACK_RESP: |
| 1648 | p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq); |
| 1649 | break; |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | |
| 1654 | void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa, |
| 1655 | const u8 *bssid, u8 category, |
| 1656 | const u8 *data, size_t len, int freq) |
| 1657 | { |
| 1658 | if (category == WLAN_ACTION_PUBLIC) { |
| 1659 | p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq); |
| 1660 | return; |
| 1661 | } |
| 1662 | |
| 1663 | if (category != WLAN_ACTION_VENDOR_SPECIFIC) |
| 1664 | return; |
| 1665 | |
| 1666 | if (len < 4) |
| 1667 | return; |
| 1668 | |
| 1669 | if (WPA_GET_BE24(data) != OUI_WFA) |
| 1670 | return; |
| 1671 | data += 3; |
| 1672 | len -= 3; |
| 1673 | |
| 1674 | if (*data != P2P_OUI_TYPE) |
| 1675 | return; |
| 1676 | data++; |
| 1677 | len--; |
| 1678 | |
| 1679 | /* P2P action frame */ |
| 1680 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1681 | "P2P: RX P2P Action from " MACSTR, MAC2STR(sa)); |
| 1682 | wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len); |
| 1683 | |
| 1684 | if (len < 1) |
| 1685 | return; |
| 1686 | switch (data[0]) { |
| 1687 | case P2P_NOA: |
| 1688 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1689 | "P2P: Received P2P Action - Notice of Absence"); |
| 1690 | /* TODO */ |
| 1691 | break; |
| 1692 | case P2P_PRESENCE_REQ: |
| 1693 | p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq); |
| 1694 | break; |
| 1695 | case P2P_PRESENCE_RESP: |
| 1696 | p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1); |
| 1697 | break; |
| 1698 | case P2P_GO_DISC_REQ: |
| 1699 | p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq); |
| 1700 | break; |
| 1701 | default: |
| 1702 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1703 | "P2P: Received P2P Action - unknown type %u", data[0]); |
| 1704 | break; |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | |
| 1709 | static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx) |
| 1710 | { |
| 1711 | struct p2p_data *p2p = eloop_ctx; |
| 1712 | if (p2p->go_neg_peer == NULL) |
| 1713 | return; |
| 1714 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
| 1715 | p2p->go_neg_peer->status = P2P_SC_SUCCESS; |
| 1716 | p2p_connect_send(p2p, p2p->go_neg_peer); |
| 1717 | } |
| 1718 | |
| 1719 | |
| 1720 | static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx) |
| 1721 | { |
| 1722 | struct p2p_data *p2p = eloop_ctx; |
| 1723 | if (p2p->invite_peer == NULL) |
| 1724 | return; |
| 1725 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
| 1726 | p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr); |
| 1727 | } |
| 1728 | |
| 1729 | |
| 1730 | static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr, |
| 1731 | const u8 *ie, size_t ie_len) |
| 1732 | { |
| 1733 | struct p2p_message msg; |
| 1734 | struct p2p_device *dev; |
| 1735 | |
| 1736 | os_memset(&msg, 0, sizeof(msg)); |
| 1737 | if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL) |
| 1738 | { |
| 1739 | p2p_parse_free(&msg); |
| 1740 | return; /* not a P2P probe */ |
| 1741 | } |
| 1742 | |
| 1743 | if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN || |
| 1744 | os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) |
| 1745 | != 0) { |
| 1746 | /* The Probe Request is not part of P2P Device Discovery. It is |
| 1747 | * not known whether the source address of the frame is the P2P |
| 1748 | * Device Address or P2P Interface Address. Do not add a new |
| 1749 | * peer entry based on this frames. |
| 1750 | */ |
| 1751 | p2p_parse_free(&msg); |
| 1752 | return; |
| 1753 | } |
| 1754 | |
| 1755 | dev = p2p_get_device(p2p, addr); |
| 1756 | if (dev) { |
| 1757 | if (dev->country[0] == 0 && msg.listen_channel) |
| 1758 | os_memcpy(dev->country, msg.listen_channel, 3); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1759 | os_get_time(&dev->last_seen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1760 | p2p_parse_free(&msg); |
| 1761 | return; /* already known */ |
| 1762 | } |
| 1763 | |
| 1764 | dev = p2p_create_device(p2p, addr); |
| 1765 | if (dev == NULL) { |
| 1766 | p2p_parse_free(&msg); |
| 1767 | return; |
| 1768 | } |
| 1769 | |
| 1770 | os_get_time(&dev->last_seen); |
| 1771 | dev->flags |= P2P_DEV_PROBE_REQ_ONLY; |
| 1772 | |
| 1773 | if (msg.listen_channel) { |
| 1774 | os_memcpy(dev->country, msg.listen_channel, 3); |
| 1775 | dev->listen_freq = p2p_channel_to_freq(dev->country, |
| 1776 | msg.listen_channel[3], |
| 1777 | msg.listen_channel[4]); |
| 1778 | } |
| 1779 | |
| 1780 | p2p_copy_wps_info(dev, 1, &msg); |
| 1781 | |
| 1782 | p2p_parse_free(&msg); |
| 1783 | |
| 1784 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 1785 | "P2P: Created device entry based on Probe Req: " MACSTR |
| 1786 | " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d", |
| 1787 | MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab, |
| 1788 | dev->info.group_capab, dev->info.device_name, |
| 1789 | dev->listen_freq); |
| 1790 | } |
| 1791 | |
| 1792 | |
| 1793 | struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p, |
| 1794 | const u8 *addr, |
| 1795 | struct p2p_message *msg) |
| 1796 | { |
| 1797 | struct p2p_device *dev; |
| 1798 | |
| 1799 | dev = p2p_get_device(p2p, addr); |
| 1800 | if (dev) { |
| 1801 | os_get_time(&dev->last_seen); |
| 1802 | return dev; /* already known */ |
| 1803 | } |
| 1804 | |
| 1805 | dev = p2p_create_device(p2p, addr); |
| 1806 | if (dev == NULL) |
| 1807 | return NULL; |
| 1808 | |
| 1809 | p2p_add_dev_info(p2p, addr, dev, msg); |
| 1810 | |
| 1811 | return dev; |
| 1812 | } |
| 1813 | |
| 1814 | |
| 1815 | static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type) |
| 1816 | { |
| 1817 | if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0) |
| 1818 | return 1; |
| 1819 | if (os_memcmp(dev_type, req_dev_type, 2) == 0 && |
| 1820 | WPA_GET_BE32(&req_dev_type[2]) == 0 && |
| 1821 | WPA_GET_BE16(&req_dev_type[6]) == 0) |
| 1822 | return 1; /* Category match with wildcard OUI/sub-category */ |
| 1823 | return 0; |
| 1824 | } |
| 1825 | |
| 1826 | |
| 1827 | int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[], |
| 1828 | size_t num_req_dev_type) |
| 1829 | { |
| 1830 | size_t i; |
| 1831 | for (i = 0; i < num_req_dev_type; i++) { |
| 1832 | if (dev_type_match(dev_type, req_dev_type[i])) |
| 1833 | return 1; |
| 1834 | } |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
| 1838 | |
| 1839 | /** |
| 1840 | * p2p_match_dev_type - Match local device type with requested type |
| 1841 | * @p2p: P2P module context from p2p_init() |
| 1842 | * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs) |
| 1843 | * Returns: 1 on match, 0 on mismatch |
| 1844 | * |
| 1845 | * This function can be used to match the Requested Device Type attribute in |
| 1846 | * WPS IE with the local device types for deciding whether to reply to a Probe |
| 1847 | * Request frame. |
| 1848 | */ |
| 1849 | int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps) |
| 1850 | { |
| 1851 | struct wps_parse_attr attr; |
| 1852 | size_t i; |
| 1853 | |
| 1854 | if (wps_parse_msg(wps, &attr)) |
| 1855 | return 1; /* assume no Requested Device Type attributes */ |
| 1856 | |
| 1857 | if (attr.num_req_dev_type == 0) |
| 1858 | return 1; /* no Requested Device Type attributes -> match */ |
| 1859 | |
| 1860 | if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type, |
| 1861 | attr.num_req_dev_type)) |
| 1862 | return 1; /* Own Primary Device Type matches */ |
| 1863 | |
| 1864 | for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) |
| 1865 | if (dev_type_list_match(p2p->cfg->sec_dev_type[i], |
| 1866 | attr.req_dev_type, |
| 1867 | attr.num_req_dev_type)) |
| 1868 | return 1; /* Own Secondary Device Type matches */ |
| 1869 | |
| 1870 | /* No matching device type found */ |
| 1871 | return 0; |
| 1872 | } |
| 1873 | |
| 1874 | |
| 1875 | struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p) |
| 1876 | { |
| 1877 | struct wpabuf *buf; |
| 1878 | u8 *len; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1879 | int pw_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1880 | |
| 1881 | buf = wpabuf_alloc(1000); |
| 1882 | if (buf == NULL) |
| 1883 | return NULL; |
| 1884 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1885 | if (p2p->go_neg_peer) { |
| 1886 | /* Advertise immediate availability of WPS credential */ |
| 1887 | pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method); |
| 1888 | } |
| 1889 | |
| 1890 | p2p_build_wps_ie(p2p, buf, pw_id, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1891 | |
| 1892 | /* P2P IE */ |
| 1893 | len = p2p_buf_add_ie_hdr(buf); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1894 | p2p_buf_add_capability(buf, p2p->dev_capab & |
| 1895 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1896 | if (p2p->ext_listen_interval) |
| 1897 | p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period, |
| 1898 | p2p->ext_listen_interval); |
| 1899 | p2p_buf_add_device_info(buf, p2p, NULL); |
| 1900 | p2p_buf_update_ie_hdr(buf, len); |
| 1901 | |
| 1902 | return buf; |
| 1903 | } |
| 1904 | |
| 1905 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1906 | static int is_11b(u8 rate) |
| 1907 | { |
| 1908 | return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16; |
| 1909 | } |
| 1910 | |
| 1911 | |
| 1912 | static int supp_rates_11b_only(struct ieee802_11_elems *elems) |
| 1913 | { |
| 1914 | int num_11b = 0, num_others = 0; |
| 1915 | int i; |
| 1916 | |
| 1917 | if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL) |
| 1918 | return 0; |
| 1919 | |
| 1920 | for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) { |
| 1921 | if (is_11b(elems->supp_rates[i])) |
| 1922 | num_11b++; |
| 1923 | else |
| 1924 | num_others++; |
| 1925 | } |
| 1926 | |
| 1927 | for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len; |
| 1928 | i++) { |
| 1929 | if (is_11b(elems->ext_supp_rates[i])) |
| 1930 | num_11b++; |
| 1931 | else |
| 1932 | num_others++; |
| 1933 | } |
| 1934 | |
| 1935 | return num_11b > 0 && num_others == 0; |
| 1936 | } |
| 1937 | |
| 1938 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1939 | static enum p2p_probe_req_status |
| 1940 | p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst, |
| 1941 | const u8 *bssid, const u8 *ie, size_t ie_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1942 | { |
| 1943 | struct ieee802_11_elems elems; |
| 1944 | struct wpabuf *buf; |
| 1945 | struct ieee80211_mgmt *resp; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1946 | struct p2p_message msg; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1947 | struct wpabuf *ies; |
| 1948 | |
| 1949 | if (!p2p->in_listen || !p2p->drv_in_listen) { |
| 1950 | /* not in Listen state - ignore Probe Request */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1951 | return P2P_PREQ_NOT_LISTEN; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) == |
| 1955 | ParseFailed) { |
| 1956 | /* Ignore invalid Probe Request frames */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1957 | return P2P_PREQ_MALFORMED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | if (elems.p2p == NULL) { |
| 1961 | /* not a P2P probe - ignore it */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1962 | return P2P_PREQ_NOT_P2P; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1965 | if (dst && !is_broadcast_ether_addr(dst) && |
| 1966 | os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) { |
| 1967 | /* Not sent to the broadcast address or our P2P Device Address |
| 1968 | */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1969 | return P2P_PREQ_NOT_PROCESSED; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | if (bssid && !is_broadcast_ether_addr(bssid)) { |
| 1973 | /* Not sent to the Wildcard BSSID */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1974 | return P2P_PREQ_NOT_PROCESSED; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1975 | } |
| 1976 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1977 | if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN || |
| 1978 | os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) != |
| 1979 | 0) { |
| 1980 | /* not using P2P Wildcard SSID - ignore */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1981 | return P2P_PREQ_NOT_PROCESSED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1984 | if (supp_rates_11b_only(&elems)) { |
| 1985 | /* Indicates support for 11b rates only */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1986 | return P2P_PREQ_NOT_P2P; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1987 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1988 | |
| 1989 | os_memset(&msg, 0, sizeof(msg)); |
| 1990 | if (p2p_parse_ies(ie, ie_len, &msg) < 0) { |
| 1991 | /* Could not parse P2P attributes */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1992 | return P2P_PREQ_NOT_P2P; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | if (msg.device_id && |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1996 | os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1997 | /* Device ID did not match */ |
| 1998 | p2p_parse_free(&msg); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1999 | return P2P_PREQ_NOT_PROCESSED; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /* Check Requested Device Type match */ |
| 2003 | if (msg.wps_attributes && |
| 2004 | !p2p_match_dev_type(p2p, msg.wps_attributes)) { |
| 2005 | /* No match with Requested Device Type */ |
| 2006 | p2p_parse_free(&msg); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2007 | return P2P_PREQ_NOT_PROCESSED; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2008 | } |
| 2009 | p2p_parse_free(&msg); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2010 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2011 | if (!p2p->cfg->send_probe_resp) { |
| 2012 | /* Response generated elsewhere */ |
| 2013 | return P2P_PREQ_NOT_PROCESSED; |
| 2014 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2015 | |
| 2016 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2017 | "P2P: Reply to P2P Probe Request in Listen state"); |
| 2018 | |
| 2019 | /* |
| 2020 | * We do not really have a specific BSS that this frame is advertising, |
| 2021 | * so build a frame that has some information in valid format. This is |
| 2022 | * really only used for discovery purposes, not to learn exact BSS |
| 2023 | * parameters. |
| 2024 | */ |
| 2025 | ies = p2p_build_probe_resp_ies(p2p); |
| 2026 | if (ies == NULL) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2027 | return P2P_PREQ_NOT_PROCESSED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2028 | |
| 2029 | buf = wpabuf_alloc(200 + wpabuf_len(ies)); |
| 2030 | if (buf == NULL) { |
| 2031 | wpabuf_free(ies); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2032 | return P2P_PREQ_NOT_PROCESSED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | resp = NULL; |
| 2036 | resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp); |
| 2037 | |
| 2038 | resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) | |
| 2039 | (WLAN_FC_STYPE_PROBE_RESP << 4)); |
| 2040 | os_memcpy(resp->da, addr, ETH_ALEN); |
| 2041 | os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN); |
| 2042 | os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN); |
| 2043 | resp->u.probe_resp.beacon_int = host_to_le16(100); |
| 2044 | /* hardware or low-level driver will setup seq_ctrl and timestamp */ |
| 2045 | resp->u.probe_resp.capab_info = |
| 2046 | host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE | |
| 2047 | WLAN_CAPABILITY_PRIVACY | |
| 2048 | WLAN_CAPABILITY_SHORT_SLOT_TIME); |
| 2049 | |
| 2050 | wpabuf_put_u8(buf, WLAN_EID_SSID); |
| 2051 | wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN); |
| 2052 | wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); |
| 2053 | |
| 2054 | wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES); |
| 2055 | wpabuf_put_u8(buf, 8); |
| 2056 | wpabuf_put_u8(buf, (60 / 5) | 0x80); |
| 2057 | wpabuf_put_u8(buf, 90 / 5); |
| 2058 | wpabuf_put_u8(buf, (120 / 5) | 0x80); |
| 2059 | wpabuf_put_u8(buf, 180 / 5); |
| 2060 | wpabuf_put_u8(buf, (240 / 5) | 0x80); |
| 2061 | wpabuf_put_u8(buf, 360 / 5); |
| 2062 | wpabuf_put_u8(buf, 480 / 5); |
| 2063 | wpabuf_put_u8(buf, 540 / 5); |
| 2064 | |
| 2065 | wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS); |
| 2066 | wpabuf_put_u8(buf, 1); |
| 2067 | wpabuf_put_u8(buf, p2p->cfg->channel); |
| 2068 | |
| 2069 | wpabuf_put_buf(buf, ies); |
| 2070 | wpabuf_free(ies); |
| 2071 | |
| 2072 | p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf); |
| 2073 | |
| 2074 | wpabuf_free(buf); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2075 | |
| 2076 | return P2P_PREQ_NOT_PROCESSED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2080 | enum p2p_probe_req_status |
| 2081 | p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst, |
| 2082 | const u8 *bssid, const u8 *ie, size_t ie_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2083 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2084 | enum p2p_probe_req_status res; |
| 2085 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2086 | p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len); |
| 2087 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2088 | res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2089 | |
| 2090 | if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) && |
| 2091 | p2p->go_neg_peer && |
| 2092 | os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN) |
| 2093 | == 0) { |
| 2094 | /* Received a Probe Request from GO Negotiation peer */ |
| 2095 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2096 | "P2P: Found GO Negotiation peer - try to start GO " |
| 2097 | "negotiation from timeout"); |
| 2098 | eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2099 | return P2P_PREQ_PROCESSED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) && |
| 2103 | p2p->invite_peer && |
| 2104 | os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN) |
| 2105 | == 0) { |
| 2106 | /* Received a Probe Request from Invite peer */ |
| 2107 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2108 | "P2P: Found Invite peer - try to start Invite from " |
| 2109 | "timeout"); |
| 2110 | eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2111 | return P2P_PREQ_PROCESSED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2112 | } |
| 2113 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2114 | return res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | |
| 2118 | static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid, |
| 2119 | u8 *buf, size_t len, struct wpabuf *p2p_ie) |
| 2120 | { |
| 2121 | struct wpabuf *tmp; |
| 2122 | u8 *lpos; |
| 2123 | size_t tmplen; |
| 2124 | int res; |
| 2125 | u8 group_capab; |
| 2126 | |
| 2127 | if (p2p_ie == NULL) |
| 2128 | return 0; /* WLAN AP is not a P2P manager */ |
| 2129 | |
| 2130 | /* |
| 2131 | * (Re)Association Request - P2P IE |
| 2132 | * P2P Capability attribute (shall be present) |
| 2133 | * P2P Interface attribute (present if concurrent device and |
| 2134 | * P2P Management is enabled) |
| 2135 | */ |
| 2136 | tmp = wpabuf_alloc(200); |
| 2137 | if (tmp == NULL) |
| 2138 | return -1; |
| 2139 | |
| 2140 | lpos = p2p_buf_add_ie_hdr(tmp); |
| 2141 | group_capab = 0; |
| 2142 | if (p2p->num_groups > 0) { |
| 2143 | group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER; |
| 2144 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && |
| 2145 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) && |
| 2146 | p2p->cross_connect) |
| 2147 | group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; |
| 2148 | } |
| 2149 | p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab); |
| 2150 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && |
| 2151 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED)) |
| 2152 | p2p_buf_add_p2p_interface(tmp, p2p); |
| 2153 | p2p_buf_update_ie_hdr(tmp, lpos); |
| 2154 | |
| 2155 | tmplen = wpabuf_len(tmp); |
| 2156 | if (tmplen > len) |
| 2157 | res = -1; |
| 2158 | else { |
| 2159 | os_memcpy(buf, wpabuf_head(tmp), tmplen); |
| 2160 | res = tmplen; |
| 2161 | } |
| 2162 | wpabuf_free(tmp); |
| 2163 | |
| 2164 | return res; |
| 2165 | } |
| 2166 | |
| 2167 | |
| 2168 | int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf, |
| 2169 | size_t len, int p2p_group, struct wpabuf *p2p_ie) |
| 2170 | { |
| 2171 | struct wpabuf *tmp; |
| 2172 | u8 *lpos; |
| 2173 | struct p2p_device *peer; |
| 2174 | size_t tmplen; |
| 2175 | int res; |
| 2176 | |
| 2177 | if (!p2p_group) |
| 2178 | return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie); |
| 2179 | |
| 2180 | /* |
| 2181 | * (Re)Association Request - P2P IE |
| 2182 | * P2P Capability attribute (shall be present) |
| 2183 | * Extended Listen Timing (may be present) |
| 2184 | * P2P Device Info attribute (shall be present) |
| 2185 | */ |
| 2186 | tmp = wpabuf_alloc(200); |
| 2187 | if (tmp == NULL) |
| 2188 | return -1; |
| 2189 | |
| 2190 | peer = bssid ? p2p_get_device(p2p, bssid) : NULL; |
| 2191 | |
| 2192 | lpos = p2p_buf_add_ie_hdr(tmp); |
| 2193 | p2p_buf_add_capability(tmp, p2p->dev_capab, 0); |
| 2194 | if (p2p->ext_listen_interval) |
| 2195 | p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period, |
| 2196 | p2p->ext_listen_interval); |
| 2197 | p2p_buf_add_device_info(tmp, p2p, peer); |
| 2198 | p2p_buf_update_ie_hdr(tmp, lpos); |
| 2199 | |
| 2200 | tmplen = wpabuf_len(tmp); |
| 2201 | if (tmplen > len) |
| 2202 | res = -1; |
| 2203 | else { |
| 2204 | os_memcpy(buf, wpabuf_head(tmp), tmplen); |
| 2205 | res = tmplen; |
| 2206 | } |
| 2207 | wpabuf_free(tmp); |
| 2208 | |
| 2209 | return res; |
| 2210 | } |
| 2211 | |
| 2212 | |
| 2213 | int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end) |
| 2214 | { |
| 2215 | struct wpabuf *p2p_ie; |
| 2216 | int ret; |
| 2217 | |
| 2218 | p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE); |
| 2219 | if (p2p_ie == NULL) |
| 2220 | return 0; |
| 2221 | |
| 2222 | ret = p2p_attr_text(p2p_ie, buf, end); |
| 2223 | wpabuf_free(p2p_ie); |
| 2224 | return ret; |
| 2225 | } |
| 2226 | |
| 2227 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2228 | int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr) |
| 2229 | { |
| 2230 | struct p2p_message msg; |
| 2231 | |
| 2232 | os_memset(&msg, 0, sizeof(msg)); |
| 2233 | if (p2p_parse_p2p_ie(p2p_ie, &msg)) |
| 2234 | return -1; |
| 2235 | |
| 2236 | if (msg.p2p_device_addr) { |
| 2237 | os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN); |
| 2238 | return 0; |
| 2239 | } else if (msg.device_id) { |
| 2240 | os_memcpy(dev_addr, msg.device_id, ETH_ALEN); |
| 2241 | return 0; |
| 2242 | } |
| 2243 | return -1; |
| 2244 | } |
| 2245 | |
| 2246 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2247 | int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr) |
| 2248 | { |
| 2249 | struct wpabuf *p2p_ie; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2250 | int ret; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2251 | |
| 2252 | p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, |
| 2253 | P2P_IE_VENDOR_TYPE); |
| 2254 | if (p2p_ie == NULL) |
| 2255 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2256 | ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2257 | wpabuf_free(p2p_ie); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2258 | return ret; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2262 | static void p2p_clear_go_neg(struct p2p_data *p2p) |
| 2263 | { |
| 2264 | p2p->go_neg_peer = NULL; |
| 2265 | p2p_clear_timeout(p2p); |
| 2266 | p2p_set_state(p2p, P2P_IDLE); |
| 2267 | } |
| 2268 | |
| 2269 | |
| 2270 | void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr) |
| 2271 | { |
| 2272 | if (p2p->go_neg_peer == NULL) { |
| 2273 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2274 | "P2P: No pending Group Formation - " |
| 2275 | "ignore WPS registration success notification"); |
| 2276 | return; /* No pending Group Formation */ |
| 2277 | } |
| 2278 | |
| 2279 | if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) != |
| 2280 | 0) { |
| 2281 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2282 | "P2P: Ignore WPS registration success notification " |
| 2283 | "for " MACSTR " (GO Negotiation peer " MACSTR ")", |
| 2284 | MAC2STR(mac_addr), |
| 2285 | MAC2STR(p2p->go_neg_peer->intended_addr)); |
| 2286 | return; /* Ignore unexpected peer address */ |
| 2287 | } |
| 2288 | |
| 2289 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2290 | "P2P: Group Formation completed successfully with " MACSTR, |
| 2291 | MAC2STR(mac_addr)); |
| 2292 | |
| 2293 | p2p_clear_go_neg(p2p); |
| 2294 | } |
| 2295 | |
| 2296 | |
| 2297 | void p2p_group_formation_failed(struct p2p_data *p2p) |
| 2298 | { |
| 2299 | if (p2p->go_neg_peer == NULL) { |
| 2300 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2301 | "P2P: No pending Group Formation - " |
| 2302 | "ignore group formation failure notification"); |
| 2303 | return; /* No pending Group Formation */ |
| 2304 | } |
| 2305 | |
| 2306 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2307 | "P2P: Group Formation failed with " MACSTR, |
| 2308 | MAC2STR(p2p->go_neg_peer->intended_addr)); |
| 2309 | |
| 2310 | p2p_clear_go_neg(p2p); |
| 2311 | } |
| 2312 | |
| 2313 | |
| 2314 | struct p2p_data * p2p_init(const struct p2p_config *cfg) |
| 2315 | { |
| 2316 | struct p2p_data *p2p; |
| 2317 | |
| 2318 | if (cfg->max_peers < 1) |
| 2319 | return NULL; |
| 2320 | |
| 2321 | p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg)); |
| 2322 | if (p2p == NULL) |
| 2323 | return NULL; |
| 2324 | p2p->cfg = (struct p2p_config *) (p2p + 1); |
| 2325 | os_memcpy(p2p->cfg, cfg, sizeof(*cfg)); |
| 2326 | if (cfg->dev_name) |
| 2327 | p2p->cfg->dev_name = os_strdup(cfg->dev_name); |
| 2328 | if (cfg->manufacturer) |
| 2329 | p2p->cfg->manufacturer = os_strdup(cfg->manufacturer); |
| 2330 | if (cfg->model_name) |
| 2331 | p2p->cfg->model_name = os_strdup(cfg->model_name); |
| 2332 | if (cfg->model_number) |
| 2333 | p2p->cfg->model_number = os_strdup(cfg->model_number); |
| 2334 | if (cfg->serial_number) |
| 2335 | p2p->cfg->serial_number = os_strdup(cfg->serial_number); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2336 | if (cfg->pref_chan) { |
| 2337 | p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan * |
| 2338 | sizeof(struct p2p_channel)); |
| 2339 | if (p2p->cfg->pref_chan) { |
| 2340 | os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan, |
| 2341 | cfg->num_pref_chan * |
| 2342 | sizeof(struct p2p_channel)); |
| 2343 | } else |
| 2344 | p2p->cfg->num_pref_chan = 0; |
| 2345 | } |
| 2346 | |
Irfan Sheriff | f44b9c4 | 2012-06-13 11:09:23 -0700 | [diff] [blame] | 2347 | #ifdef ANDROID_P2P |
| 2348 | /* 100ms listen time is too less to receive the response frames in some scenarios |
| 2349 | * increasing min listen time to 200ms. |
| 2350 | */ |
| 2351 | p2p->min_disc_int = 2; |
| 2352 | /* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */ |
| 2353 | p2p->sd_dev_list = NULL; |
| 2354 | #else |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2355 | p2p->min_disc_int = 1; |
Irfan Sheriff | f44b9c4 | 2012-06-13 11:09:23 -0700 | [diff] [blame] | 2356 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2357 | p2p->max_disc_int = 3; |
| 2358 | |
| 2359 | os_get_random(&p2p->next_tie_breaker, 1); |
| 2360 | p2p->next_tie_breaker &= 0x01; |
| 2361 | if (cfg->sd_request) |
| 2362 | p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY; |
| 2363 | p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE; |
| 2364 | if (cfg->concurrent_operations) |
| 2365 | p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER; |
| 2366 | p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
| 2367 | |
| 2368 | dl_list_init(&p2p->devices); |
| 2369 | |
| 2370 | eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0, |
| 2371 | p2p_expiration_timeout, p2p, NULL); |
| 2372 | |
| 2373 | return p2p; |
| 2374 | } |
| 2375 | |
| 2376 | |
| 2377 | void p2p_deinit(struct p2p_data *p2p) |
| 2378 | { |
| 2379 | eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL); |
| 2380 | eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL); |
| 2381 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); |
| 2382 | p2p_flush(p2p); |
| 2383 | p2p_free_req_dev_types(p2p); |
| 2384 | os_free(p2p->cfg->dev_name); |
| 2385 | os_free(p2p->cfg->manufacturer); |
| 2386 | os_free(p2p->cfg->model_name); |
| 2387 | os_free(p2p->cfg->model_number); |
| 2388 | os_free(p2p->cfg->serial_number); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2389 | os_free(p2p->cfg->pref_chan); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2390 | os_free(p2p->groups); |
| 2391 | wpabuf_free(p2p->sd_resp); |
| 2392 | os_free(p2p->after_scan_tx); |
| 2393 | p2p_remove_wps_vendor_extensions(p2p); |
| 2394 | os_free(p2p); |
| 2395 | } |
| 2396 | |
| 2397 | |
| 2398 | void p2p_flush(struct p2p_data *p2p) |
| 2399 | { |
| 2400 | struct p2p_device *dev, *prev; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2401 | p2p_stop_find(p2p); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2402 | dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device, |
| 2403 | list) { |
| 2404 | dl_list_del(&dev->list); |
| 2405 | p2p_device_free(p2p, dev); |
| 2406 | } |
Irfan Sheriff | f44b9c4 | 2012-06-13 11:09:23 -0700 | [diff] [blame] | 2407 | #ifdef ANDROID_P2P |
| 2408 | /* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */ |
| 2409 | p2p->sd_dev_list = NULL; |
| 2410 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2411 | p2p_free_sd_queries(p2p); |
| 2412 | os_free(p2p->after_scan_tx); |
| 2413 | p2p->after_scan_tx = NULL; |
| 2414 | } |
| 2415 | |
| 2416 | |
| 2417 | int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr) |
| 2418 | { |
| 2419 | struct p2p_device *dev; |
| 2420 | |
| 2421 | dev = p2p_get_device(p2p, addr); |
| 2422 | if (dev == NULL) |
| 2423 | return -1; |
| 2424 | |
| 2425 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR, |
| 2426 | MAC2STR(addr)); |
| 2427 | |
| 2428 | if (p2p->go_neg_peer == dev) |
| 2429 | p2p->go_neg_peer = NULL; |
| 2430 | |
| 2431 | dev->wps_method = WPS_NOT_READY; |
| 2432 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; |
| 2433 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; |
| 2434 | |
| 2435 | /* Check if after_scan_tx is for this peer. If so free it */ |
| 2436 | if (p2p->after_scan_tx && |
| 2437 | os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) { |
| 2438 | os_free(p2p->after_scan_tx); |
| 2439 | p2p->after_scan_tx = NULL; |
| 2440 | } |
| 2441 | |
| 2442 | return 0; |
| 2443 | } |
| 2444 | |
| 2445 | |
| 2446 | int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name) |
| 2447 | { |
| 2448 | os_free(p2p->cfg->dev_name); |
| 2449 | if (dev_name) { |
| 2450 | p2p->cfg->dev_name = os_strdup(dev_name); |
| 2451 | if (p2p->cfg->dev_name == NULL) |
| 2452 | return -1; |
| 2453 | } else |
| 2454 | p2p->cfg->dev_name = NULL; |
| 2455 | return 0; |
| 2456 | } |
| 2457 | |
| 2458 | |
| 2459 | int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer) |
| 2460 | { |
| 2461 | os_free(p2p->cfg->manufacturer); |
| 2462 | p2p->cfg->manufacturer = NULL; |
| 2463 | if (manufacturer) { |
| 2464 | p2p->cfg->manufacturer = os_strdup(manufacturer); |
| 2465 | if (p2p->cfg->manufacturer == NULL) |
| 2466 | return -1; |
| 2467 | } |
| 2468 | |
| 2469 | return 0; |
| 2470 | } |
| 2471 | |
| 2472 | |
| 2473 | int p2p_set_model_name(struct p2p_data *p2p, const char *model_name) |
| 2474 | { |
| 2475 | os_free(p2p->cfg->model_name); |
| 2476 | p2p->cfg->model_name = NULL; |
| 2477 | if (model_name) { |
| 2478 | p2p->cfg->model_name = os_strdup(model_name); |
| 2479 | if (p2p->cfg->model_name == NULL) |
| 2480 | return -1; |
| 2481 | } |
| 2482 | |
| 2483 | return 0; |
| 2484 | } |
| 2485 | |
| 2486 | |
| 2487 | int p2p_set_model_number(struct p2p_data *p2p, const char *model_number) |
| 2488 | { |
| 2489 | os_free(p2p->cfg->model_number); |
| 2490 | p2p->cfg->model_number = NULL; |
| 2491 | if (model_number) { |
| 2492 | p2p->cfg->model_number = os_strdup(model_number); |
| 2493 | if (p2p->cfg->model_number == NULL) |
| 2494 | return -1; |
| 2495 | } |
| 2496 | |
| 2497 | return 0; |
| 2498 | } |
| 2499 | |
| 2500 | |
| 2501 | int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number) |
| 2502 | { |
| 2503 | os_free(p2p->cfg->serial_number); |
| 2504 | p2p->cfg->serial_number = NULL; |
| 2505 | if (serial_number) { |
| 2506 | p2p->cfg->serial_number = os_strdup(serial_number); |
| 2507 | if (p2p->cfg->serial_number == NULL) |
| 2508 | return -1; |
| 2509 | } |
| 2510 | |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
| 2514 | |
| 2515 | void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods) |
| 2516 | { |
| 2517 | p2p->cfg->config_methods = config_methods; |
| 2518 | } |
| 2519 | |
| 2520 | |
| 2521 | void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid) |
| 2522 | { |
| 2523 | os_memcpy(p2p->cfg->uuid, uuid, 16); |
| 2524 | } |
| 2525 | |
| 2526 | |
| 2527 | int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type) |
| 2528 | { |
| 2529 | os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8); |
| 2530 | return 0; |
| 2531 | } |
| 2532 | |
| 2533 | |
| 2534 | int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8], |
| 2535 | size_t num_dev_types) |
| 2536 | { |
| 2537 | if (num_dev_types > P2P_SEC_DEVICE_TYPES) |
| 2538 | num_dev_types = P2P_SEC_DEVICE_TYPES; |
| 2539 | p2p->cfg->num_sec_dev_types = num_dev_types; |
| 2540 | os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8); |
| 2541 | return 0; |
| 2542 | } |
| 2543 | |
| 2544 | |
| 2545 | void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p) |
| 2546 | { |
| 2547 | int i; |
| 2548 | |
| 2549 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
| 2550 | wpabuf_free(p2p->wps_vendor_ext[i]); |
| 2551 | p2p->wps_vendor_ext[i] = NULL; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | |
| 2556 | int p2p_add_wps_vendor_extension(struct p2p_data *p2p, |
| 2557 | const struct wpabuf *vendor_ext) |
| 2558 | { |
| 2559 | int i; |
| 2560 | |
| 2561 | if (vendor_ext == NULL) |
| 2562 | return -1; |
| 2563 | |
| 2564 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
| 2565 | if (p2p->wps_vendor_ext[i] == NULL) |
| 2566 | break; |
| 2567 | } |
| 2568 | if (i >= P2P_MAX_WPS_VENDOR_EXT) |
| 2569 | return -1; |
| 2570 | |
| 2571 | p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext); |
| 2572 | if (p2p->wps_vendor_ext[i] == NULL) |
| 2573 | return -1; |
| 2574 | |
| 2575 | return 0; |
| 2576 | } |
| 2577 | |
| 2578 | |
| 2579 | int p2p_set_country(struct p2p_data *p2p, const char *country) |
| 2580 | { |
| 2581 | os_memcpy(p2p->cfg->country, country, 3); |
| 2582 | return 0; |
| 2583 | } |
| 2584 | |
| 2585 | |
| 2586 | void p2p_continue_find(struct p2p_data *p2p) |
| 2587 | { |
| 2588 | struct p2p_device *dev; |
Irfan Sheriff | f44b9c4 | 2012-06-13 11:09:23 -0700 | [diff] [blame] | 2589 | #ifdef ANDROID_P2P |
| 2590 | int skip=1; |
| 2591 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2592 | p2p_set_state(p2p, P2P_SEARCH); |
| 2593 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
Irfan Sheriff | f44b9c4 | 2012-06-13 11:09:23 -0700 | [diff] [blame] | 2594 | #ifdef ANDROID_P2P |
| 2595 | /* SD_FAIR_POLICY: We need to give chance to all devices in the device list |
| 2596 | * There may be a scenario, where a particular peer device have |
| 2597 | * not registered any query response. When we send a SD request to such device, |
| 2598 | * no response will be received. And if we continue to get probe responses from that device, |
| 2599 | * and if that device happens to be on top in our device list, |
| 2600 | * we will always continue to send SD requests always to that peer only. |
| 2601 | * We will not be able to send SD requests to other devices in that case. |
| 2602 | * This implementation keeps track of last serviced peer device. |
| 2603 | * And then takes the next one from the device list, in the next iteration. |
| 2604 | */ |
| 2605 | if (p2p->sd_dev_list && p2p->sd_dev_list != &p2p->devices) { |
| 2606 | if(skip) { |
| 2607 | if ((&dev->list == p2p->sd_dev_list) ) { |
| 2608 | skip = 0; |
| 2609 | if (dev->list.next == &p2p->devices) |
| 2610 | p2p->sd_dev_list = NULL; |
| 2611 | } |
| 2612 | continue; |
| 2613 | } |
| 2614 | } |
| 2615 | p2p->sd_dev_list = &dev->list; |
| 2616 | wpa_printf(MSG_DEBUG, "P2P: ### Servicing %p dev->flags 0x%x SD schedule %s devaddr " MACSTR, |
| 2617 | p2p->sd_dev_list, dev->flags, dev->flags & P2P_DEV_SD_SCHEDULE ? "TRUE": "FALSE", |
| 2618 | MAC2STR(dev->info.p2p_device_addr)); |
| 2619 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2620 | if (dev->flags & P2P_DEV_SD_SCHEDULE) { |
| 2621 | if (p2p_start_sd(p2p, dev) == 0) |
| 2622 | return; |
| 2623 | else |
| 2624 | break; |
| 2625 | } else if (dev->req_config_methods && |
| 2626 | !(dev->flags & P2P_DEV_PD_FOR_JOIN)) { |
| 2627 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send " |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2628 | "pending Provision Discovery Request to " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2629 | MACSTR " (config methods 0x%x)", |
| 2630 | MAC2STR(dev->info.p2p_device_addr), |
| 2631 | dev->req_config_methods); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2632 | if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2633 | return; |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | p2p_listen_in_find(p2p); |
| 2638 | } |
| 2639 | |
| 2640 | |
| 2641 | static void p2p_sd_cb(struct p2p_data *p2p, int success) |
| 2642 | { |
| 2643 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2644 | "P2P: Service Discovery Query TX callback: success=%d", |
| 2645 | success); |
| 2646 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
| 2647 | |
| 2648 | if (!success) { |
| 2649 | if (p2p->sd_peer) { |
| 2650 | p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE; |
| 2651 | p2p->sd_peer = NULL; |
| 2652 | } |
| 2653 | p2p_continue_find(p2p); |
| 2654 | return; |
| 2655 | } |
| 2656 | |
| 2657 | if (p2p->sd_peer == NULL) { |
| 2658 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2659 | "P2P: No SD peer entry known"); |
| 2660 | p2p_continue_find(p2p); |
| 2661 | return; |
| 2662 | } |
| 2663 | |
| 2664 | /* Wait for response from the peer */ |
| 2665 | p2p_set_state(p2p, P2P_SD_DURING_FIND); |
| 2666 | p2p_set_timeout(p2p, 0, 200000); |
| 2667 | } |
| 2668 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2669 | |
| 2670 | /** |
| 2671 | * p2p_retry_pd - Retry any pending provision disc requests in IDLE state |
| 2672 | * @p2p: P2P module context from p2p_init() |
| 2673 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2674 | static void p2p_retry_pd(struct p2p_data *p2p) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2675 | { |
| 2676 | struct p2p_device *dev; |
| 2677 | |
| 2678 | if (p2p->state != P2P_IDLE) |
| 2679 | return; |
| 2680 | |
| 2681 | /* |
| 2682 | * Retry the prov disc req attempt only for the peer that the user had |
| 2683 | * requested for and provided a join has not been initiated on it |
| 2684 | * in the meantime. |
| 2685 | */ |
| 2686 | |
| 2687 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
| 2688 | if (os_memcmp(p2p->pending_pd_devaddr, |
| 2689 | dev->info.p2p_device_addr, ETH_ALEN) != 0) |
| 2690 | continue; |
| 2691 | if (!dev->req_config_methods) |
| 2692 | continue; |
| 2693 | if (dev->flags & P2P_DEV_PD_FOR_JOIN) |
| 2694 | continue; |
| 2695 | |
| 2696 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send " |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2697 | "pending Provision Discovery Request to " |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2698 | MACSTR " (config methods 0x%x)", |
| 2699 | MAC2STR(dev->info.p2p_device_addr), |
| 2700 | dev->req_config_methods); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2701 | p2p_send_prov_disc_req(p2p, dev, 0, 0); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2702 | return; |
| 2703 | } |
| 2704 | } |
| 2705 | |
| 2706 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2707 | static void p2p_prov_disc_cb(struct p2p_data *p2p, int success) |
| 2708 | { |
| 2709 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2710 | "P2P: Provision Discovery Request TX callback: success=%d", |
| 2711 | success); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2712 | |
| 2713 | /* |
| 2714 | * Postpone resetting the pending action state till after we actually |
| 2715 | * time out. This allows us to take some action like notifying any |
| 2716 | * interested parties about no response to the request. |
| 2717 | * |
| 2718 | * When the timer (below) goes off we check in IDLE, SEARCH, or |
| 2719 | * LISTEN_ONLY state, which are the only allowed states to issue a PD |
| 2720 | * requests in, if this was still pending and then raise notification. |
| 2721 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2722 | |
| 2723 | if (!success) { |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2724 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
| 2725 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2726 | if (p2p->state != P2P_IDLE) |
| 2727 | p2p_continue_find(p2p); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2728 | else if (p2p->user_initiated_pd) { |
| 2729 | p2p->pending_action_state = P2P_PENDING_PD; |
| 2730 | p2p_set_timeout(p2p, 0, 300000); |
| 2731 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2732 | return; |
| 2733 | } |
| 2734 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2735 | /* |
| 2736 | * This postponing, of resetting pending_action_state, needs to be |
| 2737 | * done only for user initiated PD requests and not internal ones. |
| 2738 | */ |
| 2739 | if (p2p->user_initiated_pd) |
| 2740 | p2p->pending_action_state = P2P_PENDING_PD; |
| 2741 | else |
| 2742 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
| 2743 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2744 | /* Wait for response from the peer */ |
| 2745 | if (p2p->state == P2P_SEARCH) |
| 2746 | p2p_set_state(p2p, P2P_PD_DURING_FIND); |
| 2747 | p2p_set_timeout(p2p, 0, 200000); |
| 2748 | } |
| 2749 | |
| 2750 | |
| 2751 | int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq, |
| 2752 | int level, const u8 *ies, size_t ies_len) |
| 2753 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2754 | p2p_add_device(p2p, bssid, freq, level, ies, ies_len, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2755 | |
| 2756 | return 0; |
| 2757 | } |
| 2758 | |
| 2759 | |
| 2760 | void p2p_scan_res_handled(struct p2p_data *p2p) |
| 2761 | { |
| 2762 | if (!p2p->p2p_scan_running) { |
| 2763 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not " |
| 2764 | "running, but scan results received"); |
| 2765 | } |
| 2766 | p2p->p2p_scan_running = 0; |
| 2767 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); |
| 2768 | |
| 2769 | if (p2p_run_after_scan(p2p)) |
| 2770 | return; |
| 2771 | if (p2p->state == P2P_SEARCH) |
| 2772 | p2p_continue_find(p2p); |
| 2773 | } |
| 2774 | |
| 2775 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2776 | void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2777 | { |
| 2778 | u8 *len = p2p_buf_add_ie_hdr(ies); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2779 | p2p_buf_add_capability(ies, p2p->dev_capab & |
| 2780 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2781 | if (dev_id) |
| 2782 | p2p_buf_add_device_id(ies, dev_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2783 | if (p2p->cfg->reg_class && p2p->cfg->channel) |
| 2784 | p2p_buf_add_listen_channel(ies, p2p->cfg->country, |
| 2785 | p2p->cfg->reg_class, |
| 2786 | p2p->cfg->channel); |
| 2787 | if (p2p->ext_listen_interval) |
| 2788 | p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period, |
| 2789 | p2p->ext_listen_interval); |
| 2790 | /* TODO: p2p_buf_add_operating_channel() if GO */ |
| 2791 | p2p_buf_update_ie_hdr(ies, len); |
| 2792 | } |
| 2793 | |
| 2794 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2795 | size_t p2p_scan_ie_buf_len(struct p2p_data *p2p) |
| 2796 | { |
| 2797 | return 100; |
| 2798 | } |
| 2799 | |
| 2800 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2801 | int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end) |
| 2802 | { |
| 2803 | return p2p_attr_text(p2p_ie, buf, end); |
| 2804 | } |
| 2805 | |
| 2806 | |
| 2807 | static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success) |
| 2808 | { |
| 2809 | struct p2p_device *dev = p2p->go_neg_peer; |
| 2810 | |
| 2811 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2812 | "P2P: GO Negotiation Request TX callback: success=%d", |
| 2813 | success); |
| 2814 | |
| 2815 | if (dev == NULL) { |
| 2816 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2817 | "P2P: No pending GO Negotiation"); |
| 2818 | return; |
| 2819 | } |
| 2820 | |
| 2821 | if (success) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2822 | if (dev->flags & P2P_DEV_USER_REJECTED) { |
| 2823 | p2p_set_state(p2p, P2P_IDLE); |
| 2824 | return; |
| 2825 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2826 | } else if (dev->go_neg_req_sent) { |
| 2827 | /* Cancel the increment from p2p_connect_send() on failure */ |
Dmitry Shmidt | 98f9e76 | 2012-05-30 11:18:46 -0700 | [diff] [blame] | 2828 | dev->go_neg_req_sent--; |
| 2829 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2830 | |
| 2831 | if (!success && |
| 2832 | (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) && |
| 2833 | !is_zero_ether_addr(dev->member_in_go_dev)) { |
| 2834 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2835 | "P2P: Peer " MACSTR " did not acknowledge request - " |
| 2836 | "try to use device discoverability through its GO", |
| 2837 | MAC2STR(dev->info.p2p_device_addr)); |
| 2838 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
| 2839 | p2p_send_dev_disc_req(p2p, dev); |
| 2840 | return; |
| 2841 | } |
| 2842 | |
| 2843 | /* |
| 2844 | * Use P2P find, if needed, to find the other device from its listen |
| 2845 | * channel. |
| 2846 | */ |
| 2847 | p2p_set_state(p2p, P2P_CONNECT); |
| 2848 | p2p_set_timeout(p2p, 0, 100000); |
| 2849 | } |
| 2850 | |
| 2851 | |
| 2852 | static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success) |
| 2853 | { |
| 2854 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2855 | "P2P: GO Negotiation Response TX callback: success=%d", |
| 2856 | success); |
| 2857 | if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) { |
| 2858 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2859 | "P2P: Ignore TX callback event - GO Negotiation is " |
| 2860 | "not running anymore"); |
| 2861 | return; |
| 2862 | } |
| 2863 | p2p_set_state(p2p, P2P_CONNECT); |
| 2864 | p2p_set_timeout(p2p, 0, 100000); |
| 2865 | } |
| 2866 | |
| 2867 | |
| 2868 | static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success) |
| 2869 | { |
| 2870 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2871 | "P2P: GO Negotiation Response (failure) TX callback: " |
| 2872 | "success=%d", success); |
| 2873 | if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) { |
| 2874 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, |
| 2875 | p2p->go_neg_peer->status); |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | |
| 2880 | static void p2p_go_neg_conf_cb(struct p2p_data *p2p, |
| 2881 | enum p2p_send_action_result result) |
| 2882 | { |
| 2883 | struct p2p_device *dev; |
| 2884 | |
| 2885 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2886 | "P2P: GO Negotiation Confirm TX callback: result=%d", |
| 2887 | result); |
| 2888 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
| 2889 | if (result == P2P_SEND_ACTION_FAILED) { |
| 2890 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); |
| 2891 | return; |
| 2892 | } |
| 2893 | if (result == P2P_SEND_ACTION_NO_ACK) { |
| 2894 | /* |
| 2895 | * It looks like the TX status for GO Negotiation Confirm is |
| 2896 | * often showing failure even when the peer has actually |
| 2897 | * received the frame. Since the peer may change channels |
| 2898 | * immediately after having received the frame, we may not see |
| 2899 | * an Ack for retries, so just dropping a single frame may |
| 2900 | * trigger this. To allow the group formation to succeed if the |
| 2901 | * peer did indeed receive the frame, continue regardless of |
| 2902 | * the TX status. |
| 2903 | */ |
| 2904 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2905 | "P2P: Assume GO Negotiation Confirm TX was actually " |
| 2906 | "received by the peer even though Ack was not " |
| 2907 | "reported"); |
| 2908 | } |
| 2909 | |
| 2910 | dev = p2p->go_neg_peer; |
| 2911 | if (dev == NULL) |
| 2912 | return; |
| 2913 | |
| 2914 | p2p_go_complete(p2p, dev); |
| 2915 | } |
| 2916 | |
| 2917 | |
| 2918 | void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst, |
| 2919 | const u8 *src, const u8 *bssid, |
| 2920 | enum p2p_send_action_result result) |
| 2921 | { |
| 2922 | enum p2p_pending_action_state state; |
| 2923 | int success; |
| 2924 | |
| 2925 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2926 | "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR |
| 2927 | " src=" MACSTR " bssid=" MACSTR " result=%d", |
| 2928 | p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src), |
| 2929 | MAC2STR(bssid), result); |
| 2930 | success = result == P2P_SEND_ACTION_SUCCESS; |
| 2931 | state = p2p->pending_action_state; |
| 2932 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
| 2933 | switch (state) { |
| 2934 | case P2P_NO_PENDING_ACTION: |
| 2935 | break; |
| 2936 | case P2P_PENDING_GO_NEG_REQUEST: |
| 2937 | p2p_go_neg_req_cb(p2p, success); |
| 2938 | break; |
| 2939 | case P2P_PENDING_GO_NEG_RESPONSE: |
| 2940 | p2p_go_neg_resp_cb(p2p, success); |
| 2941 | break; |
| 2942 | case P2P_PENDING_GO_NEG_RESPONSE_FAILURE: |
| 2943 | p2p_go_neg_resp_failure_cb(p2p, success); |
| 2944 | break; |
| 2945 | case P2P_PENDING_GO_NEG_CONFIRM: |
| 2946 | p2p_go_neg_conf_cb(p2p, result); |
| 2947 | break; |
| 2948 | case P2P_PENDING_SD: |
| 2949 | p2p_sd_cb(p2p, success); |
| 2950 | break; |
| 2951 | case P2P_PENDING_PD: |
| 2952 | p2p_prov_disc_cb(p2p, success); |
| 2953 | break; |
| 2954 | case P2P_PENDING_INVITATION_REQUEST: |
| 2955 | p2p_invitation_req_cb(p2p, success); |
| 2956 | break; |
| 2957 | case P2P_PENDING_INVITATION_RESPONSE: |
| 2958 | p2p_invitation_resp_cb(p2p, success); |
| 2959 | break; |
| 2960 | case P2P_PENDING_DEV_DISC_REQUEST: |
| 2961 | p2p_dev_disc_req_cb(p2p, success); |
| 2962 | break; |
| 2963 | case P2P_PENDING_DEV_DISC_RESPONSE: |
| 2964 | p2p_dev_disc_resp_cb(p2p, success); |
| 2965 | break; |
| 2966 | case P2P_PENDING_GO_DISC_REQ: |
| 2967 | p2p_go_disc_req_cb(p2p, success); |
| 2968 | break; |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | |
| 2973 | void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq, |
| 2974 | unsigned int duration) |
| 2975 | { |
| 2976 | if (freq == p2p->pending_client_disc_freq) { |
| 2977 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2978 | "P2P: Client discoverability remain-awake completed"); |
| 2979 | p2p->pending_client_disc_freq = 0; |
| 2980 | return; |
| 2981 | } |
| 2982 | |
| 2983 | if (freq != p2p->pending_listen_freq) { |
| 2984 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2985 | "P2P: Unexpected listen callback for freq=%u " |
| 2986 | "duration=%u (pending_listen_freq=%u)", |
| 2987 | freq, duration, p2p->pending_listen_freq); |
| 2988 | return; |
| 2989 | } |
| 2990 | |
| 2991 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 2992 | "P2P: Starting Listen timeout(%u,%u) on freq=%u based on " |
| 2993 | "callback", |
| 2994 | p2p->pending_listen_sec, p2p->pending_listen_usec, |
| 2995 | p2p->pending_listen_freq); |
| 2996 | p2p->in_listen = 1; |
| 2997 | p2p->drv_in_listen = freq; |
| 2998 | if (p2p->pending_listen_sec || p2p->pending_listen_usec) { |
| 2999 | /* |
| 3000 | * Add 20 msec extra wait to avoid race condition with driver |
| 3001 | * remain-on-channel end event, i.e., give driver more time to |
| 3002 | * complete the operation before our timeout expires. |
| 3003 | */ |
| 3004 | p2p_set_timeout(p2p, p2p->pending_listen_sec, |
| 3005 | p2p->pending_listen_usec + 20000); |
| 3006 | } |
| 3007 | |
| 3008 | p2p->pending_listen_freq = 0; |
| 3009 | } |
| 3010 | |
| 3011 | |
| 3012 | int p2p_listen_end(struct p2p_data *p2p, unsigned int freq) |
| 3013 | { |
| 3014 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen " |
| 3015 | "state (freq=%u)", freq); |
| 3016 | p2p->drv_in_listen = 0; |
| 3017 | if (p2p->in_listen) |
| 3018 | return 0; /* Internal timeout will trigger the next step */ |
| 3019 | |
| 3020 | if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) { |
| 3021 | if (p2p->go_neg_peer->connect_reqs >= 120) { |
| 3022 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3023 | "P2P: Timeout on sending GO Negotiation " |
| 3024 | "Request without getting response"); |
| 3025 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); |
| 3026 | return 0; |
| 3027 | } |
| 3028 | |
| 3029 | p2p_set_state(p2p, P2P_CONNECT); |
| 3030 | p2p_connect_send(p2p, p2p->go_neg_peer); |
| 3031 | return 1; |
| 3032 | } else if (p2p->state == P2P_SEARCH) { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3033 | if (p2p->p2p_scan_running) { |
| 3034 | /* |
| 3035 | * Search is already in progress. This can happen if |
| 3036 | * an Action frame RX is reported immediately after |
| 3037 | * the end of a remain-on-channel operation and the |
| 3038 | * response frame to that is sent using an offchannel |
| 3039 | * operation while in p2p_find. Avoid an attempt to |
| 3040 | * restart a scan here. |
| 3041 | */ |
| 3042 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan " |
| 3043 | "already in progress - do not try to start a " |
| 3044 | "new one"); |
| 3045 | return 1; |
| 3046 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3047 | if (p2p->pending_listen_freq) { |
| 3048 | /* |
| 3049 | * Better wait a bit if the driver is unable to start |
| 3050 | * offchannel operation for some reason. p2p_search() |
| 3051 | * will be started from internal timeout. |
| 3052 | */ |
| 3053 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Listen " |
| 3054 | "operation did not seem to start - delay " |
| 3055 | "search phase to avoid busy loop"); |
| 3056 | p2p_set_timeout(p2p, 0, 100000); |
| 3057 | return 1; |
| 3058 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3059 | p2p_search(p2p); |
| 3060 | return 1; |
| 3061 | } |
| 3062 | |
| 3063 | return 0; |
| 3064 | } |
| 3065 | |
| 3066 | |
| 3067 | static void p2p_timeout_connect(struct p2p_data *p2p) |
| 3068 | { |
| 3069 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3070 | if (p2p->go_neg_peer && |
| 3071 | (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) { |
| 3072 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Wait for GO " |
| 3073 | "Negotiation Confirm timed out - assume GO " |
| 3074 | "Negotiation failed"); |
| 3075 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); |
| 3076 | return; |
| 3077 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3078 | p2p_set_state(p2p, P2P_CONNECT_LISTEN); |
| 3079 | p2p_listen_in_find(p2p); |
| 3080 | } |
| 3081 | |
| 3082 | |
| 3083 | static void p2p_timeout_connect_listen(struct p2p_data *p2p) |
| 3084 | { |
| 3085 | if (p2p->go_neg_peer) { |
| 3086 | if (p2p->drv_in_listen) { |
| 3087 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is " |
| 3088 | "still in Listen state; wait for it to " |
| 3089 | "complete"); |
| 3090 | return; |
| 3091 | } |
| 3092 | |
| 3093 | if (p2p->go_neg_peer->connect_reqs >= 120) { |
| 3094 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3095 | "P2P: Timeout on sending GO Negotiation " |
| 3096 | "Request without getting response"); |
| 3097 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); |
| 3098 | return; |
| 3099 | } |
| 3100 | |
| 3101 | p2p_set_state(p2p, P2P_CONNECT); |
| 3102 | p2p_connect_send(p2p, p2p->go_neg_peer); |
| 3103 | } else |
| 3104 | p2p_set_state(p2p, P2P_IDLE); |
| 3105 | } |
| 3106 | |
| 3107 | |
| 3108 | static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p) |
| 3109 | { |
| 3110 | /* |
| 3111 | * TODO: could remain constantly in Listen state for some time if there |
| 3112 | * are no other concurrent uses for the radio. For now, go to listen |
| 3113 | * state once per second to give other uses a chance to use the radio. |
| 3114 | */ |
| 3115 | p2p_set_state(p2p, P2P_WAIT_PEER_IDLE); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3116 | p2p_set_timeout(p2p, 0, 500000); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | |
| 3120 | static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p) |
| 3121 | { |
| 3122 | struct p2p_device *dev = p2p->go_neg_peer; |
| 3123 | |
| 3124 | if (dev == NULL) { |
| 3125 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3126 | "P2P: Unknown GO Neg peer - stop GO Neg wait"); |
| 3127 | return; |
| 3128 | } |
| 3129 | |
| 3130 | dev->wait_count++; |
| 3131 | if (dev->wait_count >= 120) { |
| 3132 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3133 | "P2P: Timeout on waiting peer to become ready for GO " |
| 3134 | "Negotiation"); |
| 3135 | p2p_go_neg_failed(p2p, dev, -1); |
| 3136 | return; |
| 3137 | } |
| 3138 | |
| 3139 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3140 | "P2P: Go to Listen state while waiting for the peer to become " |
| 3141 | "ready for GO Negotiation"); |
| 3142 | p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT); |
| 3143 | p2p_listen_in_find(p2p); |
| 3144 | } |
| 3145 | |
| 3146 | |
| 3147 | static void p2p_timeout_sd_during_find(struct p2p_data *p2p) |
| 3148 | { |
| 3149 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3150 | "P2P: Service Discovery Query timeout"); |
| 3151 | if (p2p->sd_peer) { |
| 3152 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
| 3153 | p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE; |
| 3154 | p2p->sd_peer = NULL; |
| 3155 | } |
| 3156 | p2p_continue_find(p2p); |
| 3157 | } |
| 3158 | |
| 3159 | |
| 3160 | static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p) |
| 3161 | { |
| 3162 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3163 | "P2P: Provision Discovery Request timeout"); |
| 3164 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
| 3165 | p2p_continue_find(p2p); |
| 3166 | } |
| 3167 | |
| 3168 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 3169 | static void p2p_timeout_prov_disc_req(struct p2p_data *p2p) |
| 3170 | { |
| 3171 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
| 3172 | |
| 3173 | /* |
| 3174 | * For user initiated PD requests that we have not gotten any responses |
| 3175 | * for while in IDLE state, we retry them a couple of times before |
| 3176 | * giving up. |
| 3177 | */ |
| 3178 | if (!p2p->user_initiated_pd) |
| 3179 | return; |
| 3180 | |
| 3181 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3182 | "P2P: User initiated Provision Discovery Request timeout"); |
| 3183 | |
| 3184 | if (p2p->pd_retries) { |
| 3185 | p2p->pd_retries--; |
| 3186 | p2p_retry_pd(p2p); |
| 3187 | } else { |
| 3188 | if (p2p->cfg->prov_disc_fail) |
| 3189 | p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, |
| 3190 | p2p->pending_pd_devaddr, |
| 3191 | P2P_PROV_DISC_TIMEOUT); |
| 3192 | p2p_reset_pending_pd(p2p); |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3197 | static void p2p_timeout_invite(struct p2p_data *p2p) |
| 3198 | { |
| 3199 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
| 3200 | p2p_set_state(p2p, P2P_INVITE_LISTEN); |
| 3201 | if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) { |
| 3202 | /* |
| 3203 | * Better remain on operating channel instead of listen channel |
| 3204 | * when running a group. |
| 3205 | */ |
| 3206 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in " |
| 3207 | "active GO role - wait on operating channel"); |
| 3208 | p2p_set_timeout(p2p, 0, 100000); |
| 3209 | return; |
| 3210 | } |
| 3211 | p2p_listen_in_find(p2p); |
| 3212 | } |
| 3213 | |
| 3214 | |
| 3215 | static void p2p_timeout_invite_listen(struct p2p_data *p2p) |
| 3216 | { |
| 3217 | if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) { |
| 3218 | p2p_set_state(p2p, P2P_INVITE); |
| 3219 | p2p_invite_send(p2p, p2p->invite_peer, |
| 3220 | p2p->invite_go_dev_addr); |
| 3221 | } else { |
| 3222 | if (p2p->invite_peer) { |
| 3223 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3224 | "P2P: Invitation Request retry limit reached"); |
| 3225 | if (p2p->cfg->invitation_result) |
| 3226 | p2p->cfg->invitation_result( |
| 3227 | p2p->cfg->cb_ctx, -1, NULL); |
| 3228 | } |
| 3229 | p2p_set_state(p2p, P2P_IDLE); |
| 3230 | } |
| 3231 | } |
| 3232 | |
| 3233 | |
| 3234 | static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx) |
| 3235 | { |
| 3236 | struct p2p_data *p2p = eloop_ctx; |
| 3237 | |
| 3238 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)", |
| 3239 | p2p_state_txt(p2p->state)); |
| 3240 | |
| 3241 | p2p->in_listen = 0; |
| 3242 | |
| 3243 | switch (p2p->state) { |
| 3244 | case P2P_IDLE: |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 3245 | /* Check if we timed out waiting for PD req */ |
| 3246 | if (p2p->pending_action_state == P2P_PENDING_PD) |
| 3247 | p2p_timeout_prov_disc_req(p2p); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3248 | break; |
| 3249 | case P2P_SEARCH: |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 3250 | /* Check if we timed out waiting for PD req */ |
| 3251 | if (p2p->pending_action_state == P2P_PENDING_PD) |
| 3252 | p2p_timeout_prov_disc_req(p2p); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3253 | p2p_search(p2p); |
| 3254 | break; |
| 3255 | case P2P_CONNECT: |
| 3256 | p2p_timeout_connect(p2p); |
| 3257 | break; |
| 3258 | case P2P_CONNECT_LISTEN: |
| 3259 | p2p_timeout_connect_listen(p2p); |
| 3260 | break; |
| 3261 | case P2P_GO_NEG: |
| 3262 | break; |
| 3263 | case P2P_LISTEN_ONLY: |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 3264 | /* Check if we timed out waiting for PD req */ |
| 3265 | if (p2p->pending_action_state == P2P_PENDING_PD) |
| 3266 | p2p_timeout_prov_disc_req(p2p); |
| 3267 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3268 | if (p2p->ext_listen_only) { |
| 3269 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3270 | "P2P: Extended Listen Timing - Listen State " |
| 3271 | "completed"); |
| 3272 | p2p->ext_listen_only = 0; |
| 3273 | p2p_set_state(p2p, P2P_IDLE); |
| 3274 | } |
| 3275 | break; |
| 3276 | case P2P_WAIT_PEER_CONNECT: |
| 3277 | p2p_timeout_wait_peer_connect(p2p); |
| 3278 | break; |
| 3279 | case P2P_WAIT_PEER_IDLE: |
| 3280 | p2p_timeout_wait_peer_idle(p2p); |
| 3281 | break; |
| 3282 | case P2P_SD_DURING_FIND: |
| 3283 | p2p_timeout_sd_during_find(p2p); |
| 3284 | break; |
| 3285 | case P2P_PROVISIONING: |
| 3286 | break; |
| 3287 | case P2P_PD_DURING_FIND: |
| 3288 | p2p_timeout_prov_disc_during_find(p2p); |
| 3289 | break; |
| 3290 | case P2P_INVITE: |
| 3291 | p2p_timeout_invite(p2p); |
| 3292 | break; |
| 3293 | case P2P_INVITE_LISTEN: |
| 3294 | p2p_timeout_invite_listen(p2p); |
| 3295 | break; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3296 | case P2P_SEARCH_WHEN_READY: |
| 3297 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | |
| 3302 | int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr) |
| 3303 | { |
| 3304 | struct p2p_device *dev; |
| 3305 | |
| 3306 | dev = p2p_get_device(p2p, peer_addr); |
| 3307 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject " |
| 3308 | "connection attempts by peer " MACSTR, MAC2STR(peer_addr)); |
| 3309 | if (dev == NULL) { |
| 3310 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR |
| 3311 | " unknown", MAC2STR(peer_addr)); |
| 3312 | return -1; |
| 3313 | } |
| 3314 | dev->status = P2P_SC_FAIL_REJECTED_BY_USER; |
| 3315 | dev->flags |= P2P_DEV_USER_REJECTED; |
| 3316 | return 0; |
| 3317 | } |
| 3318 | |
| 3319 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3320 | const char * p2p_wps_method_text(enum p2p_wps_method method) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3321 | { |
| 3322 | switch (method) { |
| 3323 | case WPS_NOT_READY: |
| 3324 | return "not-ready"; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3325 | case WPS_PIN_DISPLAY: |
| 3326 | return "Display"; |
| 3327 | case WPS_PIN_KEYPAD: |
| 3328 | return "Keypad"; |
| 3329 | case WPS_PBC: |
| 3330 | return "PBC"; |
| 3331 | } |
| 3332 | |
| 3333 | return "??"; |
| 3334 | } |
| 3335 | |
| 3336 | |
| 3337 | static const char * p2p_go_state_text(enum p2p_go_state go_state) |
| 3338 | { |
| 3339 | switch (go_state) { |
| 3340 | case UNKNOWN_GO: |
| 3341 | return "unknown"; |
| 3342 | case LOCAL_GO: |
| 3343 | return "local"; |
| 3344 | case REMOTE_GO: |
| 3345 | return "remote"; |
| 3346 | } |
| 3347 | |
| 3348 | return "??"; |
| 3349 | } |
| 3350 | |
| 3351 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3352 | const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p, |
| 3353 | const u8 *addr, int next) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3354 | { |
| 3355 | struct p2p_device *dev; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3356 | |
| 3357 | if (addr) |
| 3358 | dev = p2p_get_device(p2p, addr); |
| 3359 | else |
| 3360 | dev = dl_list_first(&p2p->devices, struct p2p_device, list); |
| 3361 | |
| 3362 | if (dev && next) { |
| 3363 | dev = dl_list_first(&dev->list, struct p2p_device, list); |
| 3364 | if (&dev->list == &p2p->devices) |
| 3365 | dev = NULL; |
| 3366 | } |
| 3367 | |
| 3368 | if (dev == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3369 | return NULL; |
| 3370 | |
| 3371 | return &dev->info; |
| 3372 | } |
| 3373 | |
| 3374 | |
| 3375 | int p2p_get_peer_info_txt(const struct p2p_peer_info *info, |
| 3376 | char *buf, size_t buflen) |
| 3377 | { |
| 3378 | struct p2p_device *dev; |
| 3379 | int res; |
| 3380 | char *pos, *end; |
| 3381 | struct os_time now; |
| 3382 | |
| 3383 | if (info == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3384 | return -1; |
| 3385 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3386 | dev = (struct p2p_device *) (((u8 *) info) - |
| 3387 | offsetof(struct p2p_device, info)); |
| 3388 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3389 | pos = buf; |
| 3390 | end = buf + buflen; |
| 3391 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3392 | os_get_time(&now); |
| 3393 | res = os_snprintf(pos, end - pos, |
| 3394 | "age=%d\n" |
| 3395 | "listen_freq=%d\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3396 | "wps_method=%s\n" |
| 3397 | "interface_addr=" MACSTR "\n" |
| 3398 | "member_in_go_dev=" MACSTR "\n" |
| 3399 | "member_in_go_iface=" MACSTR "\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3400 | "go_neg_req_sent=%d\n" |
| 3401 | "go_state=%s\n" |
| 3402 | "dialog_token=%u\n" |
| 3403 | "intended_addr=" MACSTR "\n" |
| 3404 | "country=%c%c\n" |
| 3405 | "oper_freq=%d\n" |
| 3406 | "req_config_methods=0x%x\n" |
| 3407 | "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n" |
| 3408 | "status=%d\n" |
| 3409 | "wait_count=%u\n" |
| 3410 | "invitation_reqs=%u\n", |
| 3411 | (int) (now.sec - dev->last_seen.sec), |
| 3412 | dev->listen_freq, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3413 | p2p_wps_method_text(dev->wps_method), |
| 3414 | MAC2STR(dev->interface_addr), |
| 3415 | MAC2STR(dev->member_in_go_dev), |
| 3416 | MAC2STR(dev->member_in_go_iface), |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3417 | dev->go_neg_req_sent, |
| 3418 | p2p_go_state_text(dev->go_state), |
| 3419 | dev->dialog_token, |
| 3420 | MAC2STR(dev->intended_addr), |
| 3421 | dev->country[0] ? dev->country[0] : '_', |
| 3422 | dev->country[1] ? dev->country[1] : '_', |
| 3423 | dev->oper_freq, |
| 3424 | dev->req_config_methods, |
| 3425 | dev->flags & P2P_DEV_PROBE_REQ_ONLY ? |
| 3426 | "[PROBE_REQ_ONLY]" : "", |
| 3427 | dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "", |
| 3428 | dev->flags & P2P_DEV_NOT_YET_READY ? |
| 3429 | "[NOT_YET_READY]" : "", |
| 3430 | dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "", |
| 3431 | dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" : |
| 3432 | "", |
| 3433 | dev->flags & P2P_DEV_PD_PEER_DISPLAY ? |
| 3434 | "[PD_PEER_DISPLAY]" : "", |
| 3435 | dev->flags & P2P_DEV_PD_PEER_KEYPAD ? |
| 3436 | "[PD_PEER_KEYPAD]" : "", |
| 3437 | dev->flags & P2P_DEV_USER_REJECTED ? |
| 3438 | "[USER_REJECTED]" : "", |
| 3439 | dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ? |
| 3440 | "[PEER_WAITING_RESPONSE]" : "", |
| 3441 | dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ? |
| 3442 | "[PREFER_PERSISTENT_GROUP]" : "", |
| 3443 | dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ? |
| 3444 | "[WAIT_GO_NEG_RESPONSE]" : "", |
| 3445 | dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ? |
| 3446 | "[WAIT_GO_NEG_CONFIRM]" : "", |
| 3447 | dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ? |
| 3448 | "[GROUP_CLIENT_ONLY]" : "", |
| 3449 | dev->flags & P2P_DEV_FORCE_FREQ ? |
| 3450 | "[FORCE_FREQ]" : "", |
| 3451 | dev->flags & P2P_DEV_PD_FOR_JOIN ? |
| 3452 | "[PD_FOR_JOIN]" : "", |
| 3453 | dev->status, |
| 3454 | dev->wait_count, |
| 3455 | dev->invitation_reqs); |
| 3456 | if (res < 0 || res >= end - pos) |
| 3457 | return pos - buf; |
| 3458 | pos += res; |
| 3459 | |
| 3460 | if (dev->ext_listen_period) { |
| 3461 | res = os_snprintf(pos, end - pos, |
| 3462 | "ext_listen_period=%u\n" |
| 3463 | "ext_listen_interval=%u\n", |
| 3464 | dev->ext_listen_period, |
| 3465 | dev->ext_listen_interval); |
| 3466 | if (res < 0 || res >= end - pos) |
| 3467 | return pos - buf; |
| 3468 | pos += res; |
| 3469 | } |
| 3470 | |
| 3471 | if (dev->oper_ssid_len) { |
| 3472 | res = os_snprintf(pos, end - pos, |
| 3473 | "oper_ssid=%s\n", |
| 3474 | wpa_ssid_txt(dev->oper_ssid, |
| 3475 | dev->oper_ssid_len)); |
| 3476 | if (res < 0 || res >= end - pos) |
| 3477 | return pos - buf; |
| 3478 | pos += res; |
| 3479 | } |
| 3480 | |
| 3481 | return pos - buf; |
| 3482 | } |
| 3483 | |
| 3484 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3485 | int p2p_peer_known(struct p2p_data *p2p, const u8 *addr) |
| 3486 | { |
| 3487 | return p2p_get_device(p2p, addr) != NULL; |
| 3488 | } |
| 3489 | |
| 3490 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3491 | void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled) |
| 3492 | { |
| 3493 | if (enabled) { |
| 3494 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client " |
| 3495 | "discoverability enabled"); |
| 3496 | p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
| 3497 | } else { |
| 3498 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client " |
| 3499 | "discoverability disabled"); |
| 3500 | p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
| 3501 | } |
| 3502 | } |
| 3503 | |
| 3504 | |
| 3505 | static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1, |
| 3506 | u32 duration2, u32 interval2) |
| 3507 | { |
| 3508 | struct wpabuf *req; |
| 3509 | struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL; |
| 3510 | u8 *len; |
| 3511 | |
| 3512 | req = wpabuf_alloc(100); |
| 3513 | if (req == NULL) |
| 3514 | return NULL; |
| 3515 | |
| 3516 | if (duration1 || interval1) { |
| 3517 | os_memset(&desc1, 0, sizeof(desc1)); |
| 3518 | desc1.count_type = 1; |
| 3519 | desc1.duration = duration1; |
| 3520 | desc1.interval = interval1; |
| 3521 | ptr1 = &desc1; |
| 3522 | |
| 3523 | if (duration2 || interval2) { |
| 3524 | os_memset(&desc2, 0, sizeof(desc2)); |
| 3525 | desc2.count_type = 2; |
| 3526 | desc2.duration = duration2; |
| 3527 | desc2.interval = interval2; |
| 3528 | ptr2 = &desc2; |
| 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1); |
| 3533 | len = p2p_buf_add_ie_hdr(req); |
| 3534 | p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2); |
| 3535 | p2p_buf_update_ie_hdr(req, len); |
| 3536 | |
| 3537 | return req; |
| 3538 | } |
| 3539 | |
| 3540 | |
| 3541 | int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr, |
| 3542 | const u8 *own_interface_addr, unsigned int freq, |
| 3543 | u32 duration1, u32 interval1, u32 duration2, |
| 3544 | u32 interval2) |
| 3545 | { |
| 3546 | struct wpabuf *req; |
| 3547 | |
| 3548 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to " |
| 3549 | "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u " |
| 3550 | "int1=%u dur2=%u int2=%u", |
| 3551 | MAC2STR(go_interface_addr), MAC2STR(own_interface_addr), |
| 3552 | freq, duration1, interval1, duration2, interval2); |
| 3553 | |
| 3554 | req = p2p_build_presence_req(duration1, interval1, duration2, |
| 3555 | interval2); |
| 3556 | if (req == NULL) |
| 3557 | return -1; |
| 3558 | |
| 3559 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
| 3560 | if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr, |
| 3561 | go_interface_addr, |
| 3562 | wpabuf_head(req), wpabuf_len(req), 200) < 0) { |
| 3563 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3564 | "P2P: Failed to send Action frame"); |
| 3565 | } |
| 3566 | wpabuf_free(req); |
| 3567 | |
| 3568 | return 0; |
| 3569 | } |
| 3570 | |
| 3571 | |
| 3572 | static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa, |
| 3573 | size_t noa_len, u8 dialog_token) |
| 3574 | { |
| 3575 | struct wpabuf *resp; |
| 3576 | u8 *len; |
| 3577 | |
| 3578 | resp = wpabuf_alloc(100 + noa_len); |
| 3579 | if (resp == NULL) |
| 3580 | return NULL; |
| 3581 | |
| 3582 | p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token); |
| 3583 | len = p2p_buf_add_ie_hdr(resp); |
| 3584 | p2p_buf_add_status(resp, status); |
| 3585 | if (noa) { |
| 3586 | wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE); |
| 3587 | wpabuf_put_le16(resp, noa_len); |
| 3588 | wpabuf_put_data(resp, noa, noa_len); |
| 3589 | } else |
| 3590 | p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL); |
| 3591 | p2p_buf_update_ie_hdr(resp, len); |
| 3592 | |
| 3593 | return resp; |
| 3594 | } |
| 3595 | |
| 3596 | |
| 3597 | static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, |
| 3598 | const u8 *sa, const u8 *data, size_t len, |
| 3599 | int rx_freq) |
| 3600 | { |
| 3601 | struct p2p_message msg; |
| 3602 | u8 status; |
| 3603 | struct wpabuf *resp; |
| 3604 | size_t g; |
| 3605 | struct p2p_group *group = NULL; |
| 3606 | int parsed = 0; |
| 3607 | u8 noa[50]; |
| 3608 | int noa_len; |
| 3609 | |
| 3610 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3611 | "P2P: Received P2P Action - P2P Presence Request"); |
| 3612 | |
| 3613 | for (g = 0; g < p2p->num_groups; g++) { |
| 3614 | if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]), |
| 3615 | ETH_ALEN) == 0) { |
| 3616 | group = p2p->groups[g]; |
| 3617 | break; |
| 3618 | } |
| 3619 | } |
| 3620 | if (group == NULL) { |
| 3621 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3622 | "P2P: Ignore P2P Presence Request for unknown group " |
| 3623 | MACSTR, MAC2STR(da)); |
| 3624 | return; |
| 3625 | } |
| 3626 | |
| 3627 | if (p2p_parse(data, len, &msg) < 0) { |
| 3628 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3629 | "P2P: Failed to parse P2P Presence Request"); |
| 3630 | status = P2P_SC_FAIL_INVALID_PARAMS; |
| 3631 | goto fail; |
| 3632 | } |
| 3633 | parsed = 1; |
| 3634 | |
| 3635 | if (msg.noa == NULL) { |
| 3636 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3637 | "P2P: No NoA attribute in P2P Presence Request"); |
| 3638 | status = P2P_SC_FAIL_INVALID_PARAMS; |
| 3639 | goto fail; |
| 3640 | } |
| 3641 | |
| 3642 | status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len); |
| 3643 | |
| 3644 | fail: |
| 3645 | if (p2p->cfg->get_noa) |
| 3646 | noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa, |
| 3647 | sizeof(noa)); |
| 3648 | else |
| 3649 | noa_len = -1; |
| 3650 | resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL, |
| 3651 | noa_len > 0 ? noa_len : 0, |
| 3652 | msg.dialog_token); |
| 3653 | if (parsed) |
| 3654 | p2p_parse_free(&msg); |
| 3655 | if (resp == NULL) |
| 3656 | return; |
| 3657 | |
| 3658 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
| 3659 | if (p2p_send_action(p2p, rx_freq, sa, da, da, |
| 3660 | wpabuf_head(resp), wpabuf_len(resp), 200) < 0) { |
| 3661 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3662 | "P2P: Failed to send Action frame"); |
| 3663 | } |
| 3664 | wpabuf_free(resp); |
| 3665 | } |
| 3666 | |
| 3667 | |
| 3668 | static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, |
| 3669 | const u8 *sa, const u8 *data, size_t len) |
| 3670 | { |
| 3671 | struct p2p_message msg; |
| 3672 | |
| 3673 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3674 | "P2P: Received P2P Action - P2P Presence Response"); |
| 3675 | |
| 3676 | if (p2p_parse(data, len, &msg) < 0) { |
| 3677 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3678 | "P2P: Failed to parse P2P Presence Response"); |
| 3679 | return; |
| 3680 | } |
| 3681 | |
| 3682 | if (msg.status == NULL || msg.noa == NULL) { |
| 3683 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3684 | "P2P: No Status or NoA attribute in P2P Presence " |
| 3685 | "Response"); |
| 3686 | p2p_parse_free(&msg); |
| 3687 | return; |
| 3688 | } |
| 3689 | |
| 3690 | if (*msg.status) { |
| 3691 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3692 | "P2P: P2P Presence Request was rejected: status %u", |
| 3693 | *msg.status); |
| 3694 | p2p_parse_free(&msg); |
| 3695 | return; |
| 3696 | } |
| 3697 | |
| 3698 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3699 | "P2P: P2P Presence Request was accepted"); |
| 3700 | wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA", |
| 3701 | msg.noa, msg.noa_len); |
| 3702 | /* TODO: process NoA */ |
| 3703 | p2p_parse_free(&msg); |
| 3704 | } |
| 3705 | |
| 3706 | |
| 3707 | static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx) |
| 3708 | { |
| 3709 | struct p2p_data *p2p = eloop_ctx; |
| 3710 | |
| 3711 | if (p2p->ext_listen_interval) { |
| 3712 | /* Schedule next extended listen timeout */ |
| 3713 | eloop_register_timeout(p2p->ext_listen_interval_sec, |
| 3714 | p2p->ext_listen_interval_usec, |
| 3715 | p2p_ext_listen_timeout, p2p, NULL); |
| 3716 | } |
| 3717 | |
| 3718 | if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) { |
| 3719 | /* |
| 3720 | * This should not really happen, but it looks like the Listen |
| 3721 | * command may fail is something else (e.g., a scan) was |
| 3722 | * running at an inconvenient time. As a workaround, allow new |
| 3723 | * Extended Listen operation to be started. |
| 3724 | */ |
| 3725 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous " |
| 3726 | "Extended Listen operation had not been completed - " |
| 3727 | "try again"); |
| 3728 | p2p->ext_listen_only = 0; |
| 3729 | p2p_set_state(p2p, P2P_IDLE); |
| 3730 | } |
| 3731 | |
| 3732 | if (p2p->state != P2P_IDLE) { |
| 3733 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended " |
| 3734 | "Listen timeout in active state (%s)", |
| 3735 | p2p_state_txt(p2p->state)); |
| 3736 | return; |
| 3737 | } |
| 3738 | |
| 3739 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout"); |
| 3740 | p2p->ext_listen_only = 1; |
| 3741 | if (p2p_listen(p2p, p2p->ext_listen_period) < 0) { |
| 3742 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start " |
| 3743 | "Listen state for Extended Listen Timing"); |
| 3744 | p2p->ext_listen_only = 0; |
| 3745 | } |
| 3746 | } |
| 3747 | |
| 3748 | |
| 3749 | int p2p_ext_listen(struct p2p_data *p2p, unsigned int period, |
| 3750 | unsigned int interval) |
| 3751 | { |
| 3752 | if (period > 65535 || interval > 65535 || period > interval || |
| 3753 | (period == 0 && interval > 0) || (period > 0 && interval == 0)) { |
| 3754 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3755 | "P2P: Invalid Extended Listen Timing request: " |
| 3756 | "period=%u interval=%u", period, interval); |
| 3757 | return -1; |
| 3758 | } |
| 3759 | |
| 3760 | eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL); |
| 3761 | |
| 3762 | if (interval == 0) { |
| 3763 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3764 | "P2P: Disabling Extended Listen Timing"); |
| 3765 | p2p->ext_listen_period = 0; |
| 3766 | p2p->ext_listen_interval = 0; |
| 3767 | return 0; |
| 3768 | } |
| 3769 | |
| 3770 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
| 3771 | "P2P: Enabling Extended Listen Timing: period %u msec, " |
| 3772 | "interval %u msec", period, interval); |
| 3773 | p2p->ext_listen_period = period; |
| 3774 | p2p->ext_listen_interval = interval; |
| 3775 | p2p->ext_listen_interval_sec = interval / 1000; |
| 3776 | p2p->ext_listen_interval_usec = (interval % 1000) * 1000; |
| 3777 | |
| 3778 | eloop_register_timeout(p2p->ext_listen_interval_sec, |
| 3779 | p2p->ext_listen_interval_usec, |
| 3780 | p2p_ext_listen_timeout, p2p, NULL); |
| 3781 | |
| 3782 | return 0; |
| 3783 | } |
| 3784 | |
| 3785 | |
| 3786 | void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, |
| 3787 | const u8 *ie, size_t ie_len) |
| 3788 | { |
| 3789 | struct p2p_message msg; |
| 3790 | |
| 3791 | if (bssid == NULL || ie == NULL) |
| 3792 | return; |
| 3793 | |
| 3794 | os_memset(&msg, 0, sizeof(msg)); |
| 3795 | if (p2p_parse_ies(ie, ie_len, &msg)) |
| 3796 | return; |
| 3797 | if (msg.minor_reason_code == NULL) |
| 3798 | return; |
| 3799 | |
| 3800 | wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, |
| 3801 | "P2P: Deauthentication notification BSSID " MACSTR |
| 3802 | " reason_code=%u minor_reason_code=%u", |
| 3803 | MAC2STR(bssid), reason_code, *msg.minor_reason_code); |
| 3804 | |
| 3805 | p2p_parse_free(&msg); |
| 3806 | } |
| 3807 | |
| 3808 | |
| 3809 | void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, |
| 3810 | const u8 *ie, size_t ie_len) |
| 3811 | { |
| 3812 | struct p2p_message msg; |
| 3813 | |
| 3814 | if (bssid == NULL || ie == NULL) |
| 3815 | return; |
| 3816 | |
| 3817 | os_memset(&msg, 0, sizeof(msg)); |
| 3818 | if (p2p_parse_ies(ie, ie_len, &msg)) |
| 3819 | return; |
| 3820 | if (msg.minor_reason_code == NULL) |
| 3821 | return; |
| 3822 | |
| 3823 | wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, |
| 3824 | "P2P: Disassociation notification BSSID " MACSTR |
| 3825 | " reason_code=%u minor_reason_code=%u", |
| 3826 | MAC2STR(bssid), reason_code, *msg.minor_reason_code); |
| 3827 | |
| 3828 | p2p_parse_free(&msg); |
| 3829 | } |
| 3830 | |
| 3831 | |
| 3832 | void p2p_set_managed_oper(struct p2p_data *p2p, int enabled) |
| 3833 | { |
| 3834 | if (enabled) { |
| 3835 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P " |
| 3836 | "Device operations enabled"); |
| 3837 | p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED; |
| 3838 | } else { |
| 3839 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P " |
| 3840 | "Device operations disabled"); |
| 3841 | p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED; |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | |
| 3846 | int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel) |
| 3847 | { |
| 3848 | if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0) |
| 3849 | return -1; |
| 3850 | |
| 3851 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: " |
| 3852 | "reg_class %u channel %u", reg_class, channel); |
| 3853 | p2p->cfg->reg_class = reg_class; |
| 3854 | p2p->cfg->channel = channel; |
| 3855 | |
| 3856 | return 0; |
| 3857 | } |
| 3858 | |
| 3859 | |
| 3860 | int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len) |
| 3861 | { |
| 3862 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len); |
| 3863 | if (postfix == NULL) { |
| 3864 | p2p->cfg->ssid_postfix_len = 0; |
| 3865 | return 0; |
| 3866 | } |
| 3867 | if (len > sizeof(p2p->cfg->ssid_postfix)) |
| 3868 | return -1; |
| 3869 | os_memcpy(p2p->cfg->ssid_postfix, postfix, len); |
| 3870 | p2p->cfg->ssid_postfix_len = len; |
| 3871 | return 0; |
| 3872 | } |
| 3873 | |
| 3874 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 3875 | int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel, |
| 3876 | int cfg_op_channel) |
| 3877 | { |
| 3878 | if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel) |
| 3879 | < 0) |
| 3880 | return -1; |
| 3881 | |
| 3882 | wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: " |
| 3883 | "reg_class %u channel %u", op_reg_class, op_channel); |
| 3884 | p2p->cfg->op_reg_class = op_reg_class; |
| 3885 | p2p->cfg->op_channel = op_channel; |
| 3886 | p2p->cfg->cfg_op_channel = cfg_op_channel; |
| 3887 | return 0; |
| 3888 | } |
| 3889 | |
| 3890 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3891 | int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan, |
| 3892 | const struct p2p_channel *pref_chan) |
| 3893 | { |
| 3894 | struct p2p_channel *n; |
| 3895 | |
| 3896 | if (pref_chan) { |
| 3897 | n = os_malloc(num_pref_chan * sizeof(struct p2p_channel)); |
| 3898 | if (n == NULL) |
| 3899 | return -1; |
| 3900 | os_memcpy(n, pref_chan, |
| 3901 | num_pref_chan * sizeof(struct p2p_channel)); |
| 3902 | } else |
| 3903 | n = NULL; |
| 3904 | |
| 3905 | os_free(p2p->cfg->pref_chan); |
| 3906 | p2p->cfg->pref_chan = n; |
| 3907 | p2p->cfg->num_pref_chan = num_pref_chan; |
| 3908 | |
| 3909 | return 0; |
| 3910 | } |
| 3911 | |
| 3912 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3913 | int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr, |
| 3914 | u8 *iface_addr) |
| 3915 | { |
| 3916 | struct p2p_device *dev = p2p_get_device(p2p, dev_addr); |
| 3917 | if (dev == NULL || is_zero_ether_addr(dev->interface_addr)) |
| 3918 | return -1; |
| 3919 | os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN); |
| 3920 | return 0; |
| 3921 | } |
| 3922 | |
| 3923 | |
| 3924 | int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr, |
| 3925 | u8 *dev_addr) |
| 3926 | { |
| 3927 | struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); |
| 3928 | if (dev == NULL) |
| 3929 | return -1; |
| 3930 | os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN); |
| 3931 | return 0; |
| 3932 | } |
| 3933 | |
| 3934 | |
| 3935 | void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr) |
| 3936 | { |
| 3937 | os_memcpy(p2p->peer_filter, addr, ETH_ALEN); |
| 3938 | if (is_zero_ether_addr(p2p->peer_filter)) |
| 3939 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer " |
| 3940 | "filter"); |
| 3941 | else |
| 3942 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer " |
| 3943 | "filter for " MACSTR, MAC2STR(p2p->peer_filter)); |
| 3944 | } |
| 3945 | |
| 3946 | |
| 3947 | void p2p_set_cross_connect(struct p2p_data *p2p, int enabled) |
| 3948 | { |
| 3949 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s", |
| 3950 | enabled ? "enabled" : "disabled"); |
| 3951 | if (p2p->cross_connect == enabled) |
| 3952 | return; |
| 3953 | p2p->cross_connect = enabled; |
| 3954 | /* TODO: may need to tear down any action group where we are GO(?) */ |
| 3955 | } |
| 3956 | |
| 3957 | |
| 3958 | int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr) |
| 3959 | { |
| 3960 | struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); |
| 3961 | if (dev == NULL) |
| 3962 | return -1; |
| 3963 | if (dev->oper_freq <= 0) |
| 3964 | return -1; |
| 3965 | return dev->oper_freq; |
| 3966 | } |
| 3967 | |
| 3968 | |
| 3969 | void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled) |
| 3970 | { |
| 3971 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s", |
| 3972 | enabled ? "enabled" : "disabled"); |
| 3973 | p2p->cfg->p2p_intra_bss = enabled; |
| 3974 | } |
| 3975 | |
| 3976 | |
| 3977 | void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan) |
| 3978 | { |
| 3979 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list"); |
| 3980 | os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels)); |
| 3981 | } |
| 3982 | |
| 3983 | |
| 3984 | int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst, |
| 3985 | const u8 *src, const u8 *bssid, const u8 *buf, |
| 3986 | size_t len, unsigned int wait_time) |
| 3987 | { |
| 3988 | if (p2p->p2p_scan_running) { |
| 3989 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action " |
| 3990 | "frame TX until p2p_scan completes"); |
| 3991 | if (p2p->after_scan_tx) { |
| 3992 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped " |
| 3993 | "previous pending Action frame TX"); |
| 3994 | os_free(p2p->after_scan_tx); |
| 3995 | } |
| 3996 | p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) + |
| 3997 | len); |
| 3998 | if (p2p->after_scan_tx == NULL) |
| 3999 | return -1; |
| 4000 | p2p->after_scan_tx->freq = freq; |
| 4001 | os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN); |
| 4002 | os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN); |
| 4003 | os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN); |
| 4004 | p2p->after_scan_tx->len = len; |
| 4005 | p2p->after_scan_tx->wait_time = wait_time; |
| 4006 | os_memcpy(p2p->after_scan_tx + 1, buf, len); |
| 4007 | return 0; |
| 4008 | } |
| 4009 | |
| 4010 | return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid, |
| 4011 | buf, len, wait_time); |
| 4012 | } |
| 4013 | |
| 4014 | |
| 4015 | void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5, |
| 4016 | int freq_overall) |
| 4017 | { |
| 4018 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d," |
| 4019 | " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall); |
| 4020 | p2p->best_freq_24 = freq_24; |
| 4021 | p2p->best_freq_5 = freq_5; |
| 4022 | p2p->best_freq_overall = freq_overall; |
| 4023 | } |
| 4024 | |
| 4025 | |
| 4026 | const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p) |
| 4027 | { |
| 4028 | if (p2p == NULL || p2p->go_neg_peer == NULL) |
| 4029 | return NULL; |
| 4030 | return p2p->go_neg_peer->info.p2p_device_addr; |
| 4031 | } |
| 4032 | |
| 4033 | |
| 4034 | const struct p2p_peer_info * |
| 4035 | p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next) |
| 4036 | { |
| 4037 | struct p2p_device *dev; |
| 4038 | |
| 4039 | if (addr) { |
| 4040 | dev = p2p_get_device(p2p, addr); |
| 4041 | if (!dev) |
| 4042 | return NULL; |
| 4043 | |
| 4044 | if (!next) { |
| 4045 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) |
| 4046 | return NULL; |
| 4047 | |
| 4048 | return &dev->info; |
| 4049 | } else { |
| 4050 | do { |
| 4051 | dev = dl_list_first(&dev->list, |
| 4052 | struct p2p_device, |
| 4053 | list); |
| 4054 | if (&dev->list == &p2p->devices) |
| 4055 | return NULL; |
| 4056 | } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY); |
| 4057 | } |
| 4058 | } else { |
| 4059 | dev = dl_list_first(&p2p->devices, struct p2p_device, list); |
| 4060 | if (!dev) |
| 4061 | return NULL; |
| 4062 | while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { |
| 4063 | dev = dl_list_first(&dev->list, |
| 4064 | struct p2p_device, |
| 4065 | list); |
| 4066 | if (&dev->list == &p2p->devices) |
| 4067 | return NULL; |
| 4068 | } |
| 4069 | } |
| 4070 | |
| 4071 | return &dev->info; |
| 4072 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4073 | |
| 4074 | #ifdef ANDROID_P2P |
| 4075 | int p2p_search_in_progress(struct p2p_data *p2p) |
| 4076 | { |
| 4077 | if (p2p == NULL) |
| 4078 | return 0; |
| 4079 | |
| 4080 | return p2p->state == P2P_SEARCH; |
| 4081 | } |
| 4082 | #endif |
| 4083 | |
| 4084 | int p2p_in_progress(struct p2p_data *p2p) |
| 4085 | { |
| 4086 | if (p2p == NULL) |
| 4087 | return 0; |
| 4088 | return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING; |
| 4089 | } |