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