]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/p2p_supplicant.c
P2P: Implement power save configuration
[thirdparty/hostap.git] / wpa_supplicant / p2p_supplicant.c
CommitLineData
b22128ef
JM
1/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
17#include "common.h"
18#include "eloop.h"
19#include "common/ieee802_11_common.h"
20#include "common/ieee802_11_defs.h"
21#include "common/wpa_ctrl.h"
22#include "wps/wps_i.h"
23#include "p2p/p2p.h"
24#include "ap/hostapd.h"
25#include "wpa_supplicant_i.h"
26#include "driver_i.h"
27#include "ap.h"
28#include "config_ssid.h"
29#include "config.h"
30#include "mlme.h"
31#include "notify.h"
32#include "scan.h"
33#include "bss.h"
34#include "wps_supplicant.h"
35#include "p2p_supplicant.h"
36
37
38static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
39static struct wpa_supplicant *
40wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
41 int go);
42static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
43static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
44
45
46static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
47 struct wpa_scan_results *scan_res)
48{
49 size_t i;
50
51 if (wpa_s->global->p2p_disabled)
52 return;
53
54 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
55 (int) scan_res->num);
56
57 for (i = 0; i < scan_res->num; i++) {
58 struct wpa_scan_res *bss = scan_res->res[i];
59 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
60 bss->freq, bss->level,
61 (const u8 *) (bss + 1),
62 bss->ie_len) > 0)
63 return;
64 }
65
66 p2p_scan_res_handled(wpa_s->global->p2p);
67}
68
69
70static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq)
71{
72 struct wpa_supplicant *wpa_s = ctx;
73 struct wpa_driver_scan_params params;
74 int ret;
75 struct wpabuf *wps_ie, *ies;
76 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
77
78 os_memset(&params, 0, sizeof(params));
79
80 /* P2P Wildcard SSID */
81 params.num_ssids = 1;
82 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
83 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
84
85 wpa_s->wps->dev.p2p = 1;
86 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
87 WPS_REQ_ENROLLEE);
88 if (wps_ie == NULL)
89 return -1;
90
91 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
92 if (ies == NULL) {
93 wpabuf_free(wps_ie);
94 return -1;
95 }
96 wpabuf_put_buf(ies, wps_ie);
97 wpabuf_free(wps_ie);
98
99 p2p_scan_ie(wpa_s->global->p2p, ies);
100
101 params.extra_ies = wpabuf_head(ies);
102 params.extra_ies_len = wpabuf_len(ies);
103
104 switch (type) {
105 case P2P_SCAN_SOCIAL:
106 params.freqs = social_channels;
107 break;
108 case P2P_SCAN_FULL:
109 break;
110 case P2P_SCAN_SPECIFIC:
111 social_channels[0] = freq;
112 social_channels[1] = 0;
113 params.freqs = social_channels;
114 break;
115 case P2P_SCAN_SOCIAL_PLUS_ONE:
116 social_channels[3] = freq;
117 params.freqs = social_channels;
118 break;
119 }
120
121 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
122 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
123 ret = ieee80211_sta_req_scan(wpa_s, &params);
124 else
125 ret = wpa_drv_scan(wpa_s, &params);
126
127 wpabuf_free(ies);
128
129 return ret;
130}
131
132
133#ifdef CONFIG_CLIENT_MLME
134static void p2p_rx_action_mlme(void *ctx, const u8 *buf, size_t len, int freq)
135{
136 struct wpa_supplicant *wpa_s = ctx;
137 const struct ieee80211_mgmt *mgmt;
138 size_t hdr_len;
139
140 if (wpa_s->global->p2p_disabled)
141 return;
142 mgmt = (const struct ieee80211_mgmt *) buf;
143 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
144 if (hdr_len > len)
145 return;
146 p2p_rx_action(wpa_s->global->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
147 mgmt->u.action.category,
148 &mgmt->u.action.u.vs_public_action.action,
149 len - hdr_len, freq);
150}
151#endif /* CONFIG_CLIENT_MLME */
152
153
154static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
155{
156 switch (p2p_group_interface) {
157 case P2P_GROUP_INTERFACE_PENDING:
158 return WPA_IF_P2P_GROUP;
159 case P2P_GROUP_INTERFACE_GO:
160 return WPA_IF_P2P_GO;
161 case P2P_GROUP_INTERFACE_CLIENT:
162 return WPA_IF_P2P_CLIENT;
163 }
164
165 return WPA_IF_P2P_GROUP;
166}
167
168
169static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
170{
171 struct wpa_ssid *ssid;
172 char *gtype;
173
174 ssid = wpa_s->current_ssid;
175 if (ssid == NULL) {
176 /*
177 * The current SSID was not known, but there may still be a
178 * pending P2P group interface waiting for provisioning.
179 */
180 ssid = wpa_s->conf->ssid;
181 while (ssid) {
182 if (ssid->p2p_group &&
183 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
184 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
185 break;
186 ssid = ssid->next;
187 }
188 }
189 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
190 gtype = "GO";
191 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
192 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
193 wpa_s->reassociate = 0;
194 wpa_s->disconnected = 1;
195 wpa_supplicant_deauthenticate(wpa_s,
196 WLAN_REASON_DEAUTH_LEAVING);
197 gtype = "client";
198 } else
199 gtype = "GO";
200 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s",
201 wpa_s->ifname, gtype);
202 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
203 struct wpa_global *global;
204 char *ifname;
205 enum wpa_driver_if_type type;
206 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
207 wpa_s->ifname);
208 global = wpa_s->global;
209 ifname = os_strdup(wpa_s->ifname);
210 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
211 wpa_supplicant_remove_iface(wpa_s->global, wpa_s);
212 wpa_s = global->ifaces;
213 if (wpa_s && ifname)
214 wpa_drv_if_remove(wpa_s, type, ifname);
215 os_free(ifname);
216 return;
217 }
218
219 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
220 if (ssid && (ssid->p2p_group ||
221 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
222 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
223 int id = ssid->id;
224 if (ssid == wpa_s->current_ssid)
225 wpa_s->current_ssid = NULL;
226 wpas_notify_network_removed(wpa_s, ssid);
227 wpa_config_remove_network(wpa_s->conf, id);
228 wpa_supplicant_clear_status(wpa_s);
229 } else {
230 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
231 "found");
232 }
233 wpa_supplicant_ap_deinit(wpa_s);
234}
235
236
237static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
238 u8 *go_dev_addr,
239 const u8 *ssid, size_t ssid_len)
240{
241 struct wpa_bss *bss;
242 const u8 *bssid;
243 struct wpabuf *p2p;
244 u8 group_capab;
245 const u8 *addr;
246
247 if (wpa_s->go_params)
248 bssid = wpa_s->go_params->peer_interface_addr;
249 else
250 bssid = wpa_s->bssid;
251
252 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
253 if (bss == NULL) {
254 u8 iface_addr[ETH_ALEN];
255 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
256 iface_addr) == 0)
257 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
258 }
259 if (bss == NULL) {
260 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
261 "group is persistent - BSS " MACSTR " not found",
262 MAC2STR(bssid));
263 return 0;
264 }
265
266 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
267 if (p2p == NULL) {
268 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
269 "group is persistent - BSS " MACSTR
270 " did not include P2P IE", MAC2STR(bssid));
271 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
272 (u8 *) (bss + 1), bss->ie_len);
273 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
274 ((u8 *) bss + 1) + bss->ie_len,
275 bss->beacon_ie_len);
276 return 0;
277 }
278
279 group_capab = p2p_get_group_capab(p2p);
280 addr = p2p_get_go_dev_addr(p2p);
281 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
282 "group_capab=0x%x", group_capab);
283 if (addr) {
284 os_memcpy(go_dev_addr, addr, ETH_ALEN);
285 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
286 MAC2STR(addr));
287 } else
288 os_memset(go_dev_addr, 0, ETH_ALEN);
289 wpabuf_free(p2p);
290
291 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
292 "go_dev_addr=" MACSTR,
293 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
294
295 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
296}
297
298
299static void wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
300 struct wpa_ssid *ssid,
301 const u8 *go_dev_addr)
302{
303 struct wpa_ssid *s;
304 int changed = 0;
305
306 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
307 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
308 for (s = wpa_s->conf->ssid; s; s = s->next) {
309 if (s->disabled == 2 &&
310 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
311 s->ssid_len == ssid->ssid_len &&
312 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
313 break;
314 }
315
316 if (s) {
317 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
318 "entry");
319 if (ssid->passphrase && !s->passphrase)
320 changed = 1;
321 else if (ssid->passphrase && s->passphrase &&
322 os_strcmp(ssid->passphrase, s->passphrase) != 0)
323 changed = 1;
324 } else {
325 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
326 "entry");
327 changed = 1;
328 s = wpa_config_add_network(wpa_s->conf);
329 if (s == NULL)
330 return;
331 wpa_config_set_network_defaults(s);
332 }
333
334 s->p2p_group = 1;
335 s->p2p_persistent_group = 1;
336 s->disabled = 2;
337 s->bssid_set = 1;
338 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
339 s->mode = ssid->mode;
340 s->auth_alg = WPA_AUTH_ALG_OPEN;
341 s->key_mgmt = WPA_KEY_MGMT_PSK;
342 s->proto = WPA_PROTO_RSN;
343 s->pairwise_cipher = WPA_CIPHER_CCMP;
344 if (ssid->passphrase) {
345 os_free(s->passphrase);
346 s->passphrase = os_strdup(ssid->passphrase);
347 }
348 if (ssid->psk_set) {
349 s->psk_set = 1;
350 os_memcpy(s->psk, ssid->psk, 32);
351 }
352 if (s->passphrase && !s->psk_set)
353 wpa_config_update_psk(s);
354 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
355 os_free(s->ssid);
356 s->ssid = os_malloc(ssid->ssid_len);
357 }
358 if (s->ssid) {
359 s->ssid_len = ssid->ssid_len;
360 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
361 }
362
363#ifndef CONFIG_NO_CONFIG_WRITE
364 if (changed && wpa_s->conf->update_config &&
365 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
366 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
367 }
368#endif /* CONFIG_NO_CONFIG_WRITE */
369}
370
371
372static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
373 int success)
374{
375 struct wpa_ssid *ssid;
376 const char *ssid_txt;
377 int client;
378 int persistent;
379 u8 go_dev_addr[ETH_ALEN];
380
381 /*
382 * This callback is likely called for the main interface. Update wpa_s
383 * to use the group interface if a new interface was created for the
384 * group.
385 */
386 if (wpa_s->global->p2p_group_formation)
387 wpa_s = wpa_s->global->p2p_group_formation;
388 wpa_s->p2p_in_provisioning = 0;
389
390 if (!success) {
391 wpa_msg(wpa_s->parent, MSG_INFO,
392 P2P_EVENT_GROUP_FORMATION_FAILURE);
393 wpas_p2p_group_delete(wpa_s);
394 return;
395 }
396
397 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
398
399 ssid = wpa_s->current_ssid;
400 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
401 ssid->mode = WPAS_MODE_P2P_GO;
402 p2p_group_notif_formation_done(wpa_s->p2p_group);
403 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
404 }
405
406 persistent = 0;
407 if (ssid) {
408 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
409 client = ssid->mode == WPAS_MODE_INFRA;
410 if (ssid->mode == WPAS_MODE_P2P_GO) {
411 persistent = ssid->p2p_persistent_group;
412 os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
413 ETH_ALEN);
414 } else
415 persistent = wpas_p2p_persistent_group(wpa_s,
416 go_dev_addr,
417 ssid->ssid,
418 ssid->ssid_len);
419 } else {
420 ssid_txt = "";
421 client = wpa_s->p2p_group_interface ==
422 P2P_GROUP_INTERFACE_CLIENT;
423 }
424
425 wpa_s->show_group_started = 0;
426 if (client) {
427 /*
428 * Indicate event only after successfully completed 4-way
429 * handshake, i.e., when the interface is ready for data
430 * packets.
431 */
432 wpa_s->show_group_started = 1;
433 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
434 char psk[65];
435 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
436 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
437 "%s GO ssid=\"%s\" psk=%s go_dev_addr=" MACSTR "%s",
438 wpa_s->ifname, ssid_txt, psk, MAC2STR(go_dev_addr),
439 persistent ? " [PERSISTENT]" : "");
440 } else {
441 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
442 "%s GO ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
443 MACSTR "%s",
444 wpa_s->ifname, ssid_txt,
445 ssid && ssid->passphrase ? ssid->passphrase : "",
446 MAC2STR(go_dev_addr),
447 persistent ? " [PERSISTENT]" : "");
448 }
449
450 if (persistent)
451 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
452 go_dev_addr);
453}
454
455
456static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
457{
458 struct wpa_supplicant *wpa_s = eloop_ctx;
459 struct wpa_supplicant *iface;
460 int res;
461 int without_roc;
462
463 without_roc = wpa_s->pending_action_without_roc;
464 wpa_s->pending_action_without_roc = 0;
465
466 if (wpa_s->pending_action_tx == NULL)
467 return;
468
469 if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
470 wpa_s->pending_action_freq != 0) {
471 wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
472 "waiting for another freq=%u (off_channel_freq=%u)",
473 wpa_s->pending_action_freq,
474 wpa_s->off_channel_freq);
475 if (without_roc && wpa_s->off_channel_freq == 0) {
476 /*
477 * We may get here if wpas_send_action() found us to be
478 * on the correct channel, but remain-on-channel cancel
479 * event was received before getting here.
480 */
481 wpa_printf(MSG_DEBUG, "P2P: Schedule "
482 "remain-on-channel to send Action frame");
483 if (wpa_drv_remain_on_channel(
484 wpa_s, wpa_s->pending_action_freq, 200) <
485 0) {
486 wpa_printf(MSG_DEBUG, "P2P: Failed to request "
487 "driver to remain on channel (%u "
488 "MHz) for Action Frame TX",
489 wpa_s->pending_action_freq);
07a30a66
JM
490 } else
491 wpa_s->roc_waiting_drv_freq =
492 wpa_s->pending_action_freq;
b22128ef
JM
493 }
494 return;
495 }
496
497 /*
498 * This call is likely going to be on the P2P device instance if the
499 * driver uses a separate interface for that purpose. However, some
500 * Action frames are actually sent within a P2P Group and when that is
501 * the case, we need to follow power saving (e.g., GO buffering the
d6ae9950 502 * frame for a client in PS mode or a client following the advertised
b22128ef
JM
503 * NoA from its GO). To make that easier for the driver, select the
504 * correct group interface here.
505 */
506 if (os_memcmp(wpa_s->pending_action_src, wpa_s->own_addr, ETH_ALEN) !=
507 0) {
508 /*
509 * Try to find a group interface that matches with the source
510 * address.
511 */
512 iface = wpa_s->global->ifaces;
513 while (iface) {
514 if (os_memcmp(wpa_s->pending_action_src,
515 iface->own_addr, ETH_ALEN) == 0)
516 break;
d6ae9950 517 iface = iface->next;
b22128ef
JM
518 }
519 if (iface) {
520 wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
521 "instead of interface %s for Action TX",
522 iface->ifname, wpa_s->ifname);
523 } else
524 iface = wpa_s;
525 } else
526 iface = wpa_s;
527
528 wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
529 MACSTR " using interface %s",
530 MAC2STR(wpa_s->pending_action_dst), iface->ifname);
531 res = wpa_drv_send_action(iface, wpa_s->pending_action_freq,
532 wpa_s->pending_action_dst,
533 wpa_s->pending_action_src,
534 wpa_s->pending_action_bssid,
535 wpabuf_head(wpa_s->pending_action_tx),
536 wpabuf_len(wpa_s->pending_action_tx));
537 if (res) {
538 wpa_printf(MSG_DEBUG, "P2P: Failed to send the pending "
539 "Action frame");
540 /*
541 * Use fake TX status event to allow P2P state machine to
542 * continue.
543 */
544 wpas_send_action_tx_status(
545 wpa_s, wpa_s->pending_action_dst,
546 wpabuf_head(wpa_s->pending_action_tx),
547 wpabuf_len(wpa_s->pending_action_tx), 0);
548 }
549}
550
551
552void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst,
553 const u8 *data, size_t data_len, int ack)
554{
555 if (wpa_s->global->p2p_disabled)
556 return;
557
558 if (wpa_s->pending_action_tx == NULL) {
559 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - no "
560 "pending operation");
561 return;
562 }
563
564 if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) {
565 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - unknown "
566 "destination address");
567 return;
568 }
569
570 wpabuf_free(wpa_s->pending_action_tx);
571 wpa_s->pending_action_tx = NULL;
572
573 p2p_send_action_cb(wpa_s->global->p2p, wpa_s->pending_action_freq,
574 wpa_s->pending_action_dst,
575 wpa_s->pending_action_src,
576 wpa_s->pending_action_bssid,
577 ack);
578
579 if (wpa_s->pending_pd_before_join &&
580 (os_memcmp(wpa_s->pending_action_dst, wpa_s->pending_join_dev_addr,
581 ETH_ALEN) == 0 ||
582 os_memcmp(wpa_s->pending_action_dst,
583 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
584 wpa_s->pending_pd_before_join = 0;
585 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
586 "join-existing-group operation");
587 wpas_p2p_join_start(wpa_s);
588 }
589}
590
591
592static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
593 const u8 *src, const u8 *bssid, const u8 *buf,
594 size_t len, unsigned int wait_time)
595{
596 struct wpa_supplicant *wpa_s = ctx;
597
598 wpa_printf(MSG_DEBUG, "P2P: Send action frame: freq=%d dst=" MACSTR
599 " src=" MACSTR " bssid=" MACSTR,
600 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid));
601
602 if (wpa_s->pending_action_tx) {
603 wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
604 "to " MACSTR, MAC2STR(wpa_s->pending_action_dst));
605 wpabuf_free(wpa_s->pending_action_tx);
606 }
607 wpa_s->pending_action_tx = wpabuf_alloc(len);
608 if (wpa_s->pending_action_tx == NULL)
609 return -1;
610 wpabuf_put_data(wpa_s->pending_action_tx, buf, len);
611 os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN);
612 os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN);
613 os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
614 wpa_s->pending_action_freq = freq;
615
616 if (wpa_s->off_channel_freq == freq || freq == 0) {
617 /* Already on requested channel; send immediately */
618 /* TODO: Would there ever be need to extend the current
619 * duration on the channel? */
620 wpa_s->pending_action_without_roc = 1;
621 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
622 eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL);
623 return 0;
624 }
625 wpa_s->pending_action_without_roc = 0;
626
07a30a66
JM
627 if (wpa_s->roc_waiting_drv_freq == freq) {
628 wpa_printf(MSG_DEBUG, "P2P: Already waiting for driver to get "
629 "to frequency %u MHz; continue waiting to send the "
630 "Action frame", freq);
631 return 0;
632 }
633
b22128ef
JM
634 wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
635 "once the driver gets to the requested channel");
636 if (wait_time > wpa_s->max_remain_on_chan)
637 wait_time = wpa_s->max_remain_on_chan;
638 if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) {
639 wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
640 "to remain on channel (%u MHz) for Action "
641 "Frame TX", freq);
642 return -1;
643 }
07a30a66 644 wpa_s->roc_waiting_drv_freq = freq;
b22128ef
JM
645
646 return 0;
647}
648
649
650static void wpas_send_action_done(void *ctx)
651{
652 struct wpa_supplicant *wpa_s = ctx;
653 wpa_printf(MSG_DEBUG, "P2P: Action frame sequence done notification");
654 wpabuf_free(wpa_s->pending_action_tx);
655 wpa_s->pending_action_tx = NULL;
656 if (wpa_s->off_channel_freq) {
657 wpa_drv_cancel_remain_on_channel(wpa_s);
658 wpa_s->off_channel_freq = 0;
07a30a66 659 wpa_s->roc_waiting_drv_freq = 0;
b22128ef
JM
660 }
661}
662
663
664static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
665 struct p2p_go_neg_results *params)
666{
667 if (wpa_s->go_params == NULL) {
668 wpa_s->go_params = os_malloc(sizeof(*params));
669 if (wpa_s->go_params == NULL)
670 return -1;
671 }
672 os_memcpy(wpa_s->go_params, params, sizeof(*params));
673 return 0;
674}
675
676
677static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
678 struct p2p_go_neg_results *res)
679{
680 wpa_supplicant_ap_deinit(wpa_s);
681 wpas_copy_go_neg_results(wpa_s, res);
682 if (res->wps_method == WPS_PBC)
683 wpas_wps_start_pbc(wpa_s, NULL /* res->peer_interface_addr */,
684 1);
685 else
686 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
687 wpa_s->p2p_pin, 1);
688}
689
690
691static void p2p_go_configured(void *ctx, void *data)
692{
693 struct wpa_supplicant *wpa_s = ctx;
694 struct p2p_go_neg_results *params = data;
695 struct wpa_ssid *ssid;
696
697 ssid = wpa_s->current_ssid;
698 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
699 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
700 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
701 "%s GO ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
702 MACSTR "%s",
703 wpa_s->ifname,
704 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
705 params->passphrase ? params->passphrase : "",
706 MAC2STR(wpa_s->parent->own_addr),
707 params->persistent_group ? " [PERSISTENT]" : "");
708 if (params->persistent_group)
709 wpas_p2p_store_persistent_group(
710 wpa_s->parent, ssid,
711 wpa_s->parent->own_addr);
712 return;
713 }
714
715 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
716 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
717 params->peer_interface_addr)) {
718 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
719 "filtering");
720 return;
721 }
722 if (params->wps_method == WPS_PBC)
723 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr);
724 else if (wpa_s->p2p_pin[0])
725 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
726 wpa_s->p2p_pin, NULL, 0);
727 os_free(wpa_s->go_params);
728 wpa_s->go_params = NULL;
729}
730
731
732static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
733 struct p2p_go_neg_results *params,
734 int group_formation)
735{
736 struct wpa_ssid *ssid;
737
738 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
739 return;
740
741 ssid = wpa_config_add_network(wpa_s->conf);
742 if (ssid == NULL)
743 return;
744
745 wpa_config_set_network_defaults(ssid);
746 ssid->temporary = 1;
747 ssid->p2p_group = 1;
748 ssid->p2p_persistent_group = params->persistent_group;
749 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
750 WPAS_MODE_P2P_GO;
751 ssid->frequency = params->freq;
752 ssid->ssid = os_zalloc(params->ssid_len + 1);
753 if (ssid->ssid) {
754 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
755 ssid->ssid_len = params->ssid_len;
756 }
757 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
758 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
759 ssid->proto = WPA_PROTO_RSN;
760 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
761 ssid->passphrase = os_strdup(params->passphrase);
762
763 wpa_s->ap_configured_cb = p2p_go_configured;
764 wpa_s->ap_configured_cb_ctx = wpa_s;
765 wpa_s->ap_configured_cb_data = wpa_s->go_params;
766 wpa_s->connect_without_scan = 1;
767 wpa_s->reassociate = 1;
768 wpa_s->disconnected = 0;
769 wpa_supplicant_req_scan(wpa_s, 0, 0);
770}
771
772
773static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
774 const struct wpa_supplicant *src)
775{
776 struct wpa_config *d;
777 const struct wpa_config *s;
778
779 d = dst->conf;
780 s = src->conf;
781
782#define C(n) if (s->n) d->n = os_strdup(s->n)
783 C(device_name);
784 C(manufacturer);
785 C(model_name);
786 C(model_number);
787 C(serial_number);
788 C(device_type);
789 C(config_methods);
790#undef C
791}
792
793
794static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
795 enum wpa_driver_if_type type)
796{
797 char ifname[120], force_ifname[120];
798
799 if (wpa_s->pending_interface_name[0]) {
800 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
801 "- skip creation of a new one");
802 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
803 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
804 "unknown?! ifname='%s'",
805 wpa_s->pending_interface_name);
806 return -1;
807 }
808 return 0;
809 }
810
811 os_snprintf(ifname, sizeof(ifname), "%s-p2p-%d", wpa_s->ifname,
812 wpa_s->p2p_group_idx);
813 force_ifname[0] = '\0';
814
815 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
816 ifname);
817 wpa_s->p2p_group_idx++;
818
819 wpa_s->pending_interface_type = type;
820 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
821 wpa_s->pending_interface_addr) < 0) {
822 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
823 "interface");
824 return -1;
825 }
826
827 if (force_ifname[0]) {
828 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
829 force_ifname);
830 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
831 sizeof(wpa_s->pending_interface_name));
832 } else
833 os_strlcpy(wpa_s->pending_interface_name, ifname,
834 sizeof(wpa_s->pending_interface_name));
835 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
836 MACSTR, wpa_s->pending_interface_name,
837 MAC2STR(wpa_s->pending_interface_addr));
838
839 return 0;
840}
841
842
843static void wpas_p2p_remove_pending_group_interface(
844 struct wpa_supplicant *wpa_s)
845{
846 if (!wpa_s->pending_interface_name[0] ||
847 is_zero_ether_addr(wpa_s->pending_interface_addr))
848 return; /* No pending virtual interface */
849
850 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
851 wpa_s->pending_interface_name);
852 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
853 wpa_s->pending_interface_name);
854 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
855 wpa_s->pending_interface_name[0] = '\0';
856}
857
858
859static struct wpa_supplicant *
860wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
861{
862 struct wpa_interface iface;
863 struct wpa_supplicant *group_wpa_s;
864
865 if (!wpa_s->pending_interface_name[0]) {
866 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
867 return NULL;
868 }
869
870 os_memset(&iface, 0, sizeof(iface));
871 iface.ifname = wpa_s->pending_interface_name;
872 iface.driver = wpa_s->driver->name;
873 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
874 iface.driver_param = wpa_s->conf->driver_param;
875 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
876 if (group_wpa_s == NULL) {
877 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
878 "wpa_supplicant interface");
879 return NULL;
880 }
881 wpa_s->pending_interface_name[0] = '\0';
882 group_wpa_s->parent = wpa_s;
883 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
884 P2P_GROUP_INTERFACE_CLIENT;
885 wpa_s->global->p2p_group_formation = group_wpa_s;
886
887 wpas_p2p_clone_config(group_wpa_s, wpa_s);
888
889 return group_wpa_s;
890}
891
892
893static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
894 void *timeout_ctx)
895{
896 struct wpa_supplicant *wpa_s = eloop_ctx;
897 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
898 if (wpa_s->global->p2p)
899 p2p_group_formation_failed(wpa_s->global->p2p);
900 wpas_group_formation_completed(wpa_s, 0);
901}
902
903
904void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
905{
906 struct wpa_supplicant *wpa_s = ctx;
907
908 if (wpa_s->off_channel_freq) {
909 wpa_drv_cancel_remain_on_channel(wpa_s);
910 wpa_s->off_channel_freq = 0;
07a30a66 911 wpa_s->roc_waiting_drv_freq = 0;
b22128ef
JM
912 }
913
914 if (res->status) {
915 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
916 res->status);
917 wpas_p2p_remove_pending_group_interface(wpa_s);
918 return;
919 }
920
921 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
922
923 if (wpa_s->create_p2p_iface) {
924 struct wpa_supplicant *group_wpa_s =
925 wpas_p2p_init_group_interface(wpa_s, res->role_go);
926 if (group_wpa_s == NULL) {
927 wpas_p2p_remove_pending_group_interface(wpa_s);
928 return;
929 }
930 if (group_wpa_s != wpa_s)
931 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
932 sizeof(group_wpa_s->p2p_pin));
933 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
934 wpa_s->pending_interface_name[0] = '\0';
935 group_wpa_s->p2p_in_provisioning = 1;
936
937 if (res->role_go)
938 wpas_start_wps_go(group_wpa_s, res, 1);
939 else
940 wpas_start_wps_enrollee(group_wpa_s, res);
941 } else {
942 wpa_s->p2p_in_provisioning = 1;
943 wpa_s->global->p2p_group_formation = wpa_s;
944
945 if (res->role_go)
946 wpas_start_wps_go(wpa_s, res, 1);
947 else
948 wpas_start_wps_enrollee(ctx, res);
949 }
950
951 wpa_s->p2p_long_listen = 0;
952 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
953
954 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
955 /* TODO: add peer Config Timeout */
956 eloop_register_timeout(15, 0, wpas_p2p_group_formation_timeout, wpa_s,
957 NULL);
958}
959
960
961void wpas_go_neg_req_rx(void *ctx, const u8 *src)
962{
963 struct wpa_supplicant *wpa_s = ctx;
964 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR,
965 MAC2STR(src));
966}
967
968
969void wpas_dev_found(void *ctx, const u8 *addr, const u8 *dev_addr,
970 const u8 *pri_dev_type, const char *dev_name,
971 u16 config_methods, u8 dev_capab, u8 group_capab)
972{
973 struct wpa_supplicant *wpa_s = ctx;
974 char devtype[WPS_DEV_TYPE_BUFSIZE];
975 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
976 " p2p_dev_addr=" MACSTR
977 " pri_dev_type=%s name='%s' config_methods=0x%x "
978 "dev_capab=0x%x group_capab=0x%x",
979 MAC2STR(addr), MAC2STR(dev_addr),
980 wps_dev_type_bin2str(pri_dev_type, devtype, sizeof(devtype)),
981 dev_name, config_methods, dev_capab, group_capab);
982}
983
984
985static int wpas_start_listen(void *ctx, unsigned int freq,
986 unsigned int duration,
987 const struct wpabuf *probe_resp_ie)
988{
989 struct wpa_supplicant *wpa_s = ctx;
990
991 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie);
992
993 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
994 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
995 "report received Probe Request frames");
996 return -1;
997 }
998
999 wpa_s->pending_listen_freq = freq;
1000 wpa_s->pending_listen_duration = duration;
1001
1002 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1003 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1004 "to remain on channel (%u MHz) for Listen "
1005 "state", freq);
1006 wpa_s->pending_listen_freq = 0;
1007 return -1;
1008 }
07a30a66 1009 wpa_s->roc_waiting_drv_freq = freq;
b22128ef
JM
1010
1011 return 0;
1012}
1013
1014
1015static void wpas_stop_listen(void *ctx)
1016{
1017 struct wpa_supplicant *wpa_s = ctx;
1018 if (wpa_s->off_channel_freq) {
1019 wpa_drv_cancel_remain_on_channel(wpa_s);
1020 wpa_s->off_channel_freq = 0;
07a30a66 1021 wpa_s->roc_waiting_drv_freq = 0;
b22128ef
JM
1022 }
1023 wpa_drv_probe_req_report(wpa_s, 0);
1024}
1025
1026
1027static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1028{
1029 struct wpa_supplicant *wpa_s = ctx;
1030 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1031}
1032
1033
1034static struct p2p_srv_bonjour *
1035wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1036 const struct wpabuf *query)
1037{
1038 struct p2p_srv_bonjour *bsrv;
1039 size_t len;
1040
1041 len = wpabuf_len(query);
1042 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1043 struct p2p_srv_bonjour, list) {
1044 if (len == wpabuf_len(bsrv->query) &&
1045 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1046 len) == 0)
1047 return bsrv;
1048 }
1049 return NULL;
1050}
1051
1052
1053static struct p2p_srv_upnp *
1054wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1055 const char *service)
1056{
1057 struct p2p_srv_upnp *usrv;
1058
1059 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1060 struct p2p_srv_upnp, list) {
1061 if (version == usrv->version &&
1062 os_strcmp(service, usrv->service) == 0)
1063 return usrv;
1064 }
1065 return NULL;
1066}
1067
1068
1069static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1070 u8 srv_trans_id)
1071{
1072 u8 *len_pos;
1073
1074 if (wpabuf_tailroom(resp) < 5)
1075 return;
1076
1077 /* Length (to be filled) */
1078 len_pos = wpabuf_put(resp, 2);
1079 wpabuf_put_u8(resp, srv_proto);
1080 wpabuf_put_u8(resp, srv_trans_id);
1081 /* Status Code */
1082 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1083 /* Response Data: empty */
1084 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1085}
1086
1087
1088static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1089 struct wpabuf *resp, u8 srv_trans_id)
1090{
1091 struct p2p_srv_bonjour *bsrv;
1092 u8 *len_pos;
1093
1094 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1095
1096 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1097 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1098 return;
1099 }
1100
1101 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1102 struct p2p_srv_bonjour, list) {
1103 if (wpabuf_tailroom(resp) <
1104 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1105 return;
1106 /* Length (to be filled) */
1107 len_pos = wpabuf_put(resp, 2);
1108 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1109 wpabuf_put_u8(resp, srv_trans_id);
1110 /* Status Code */
1111 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1112 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1113 wpabuf_head(bsrv->resp),
1114 wpabuf_len(bsrv->resp));
1115 /* Response Data */
1116 wpabuf_put_buf(resp, bsrv->query); /* Key */
1117 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1118 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1119 2);
1120 }
1121}
1122
1123
1124static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1125 struct wpabuf *resp, u8 srv_trans_id,
1126 const u8 *query, size_t query_len)
1127{
1128 struct p2p_srv_bonjour *bsrv;
1129 struct wpabuf buf;
1130 u8 *len_pos;
1131
1132 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1133 query, query_len);
1134 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1135 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1136 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1137 srv_trans_id);
1138 return;
1139 }
1140
1141 if (query_len == 0) {
1142 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1143 return;
1144 }
1145
1146 if (wpabuf_tailroom(resp) < 5)
1147 return;
1148 /* Length (to be filled) */
1149 len_pos = wpabuf_put(resp, 2);
1150 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1151 wpabuf_put_u8(resp, srv_trans_id);
1152
1153 wpabuf_set(&buf, query, query_len);
1154 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1155 if (bsrv == NULL) {
1156 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1157 "available");
1158
1159 /* Status Code */
1160 wpabuf_put_u8(resp, P2P_SD_QUERY_DATA_NOT_AVAILABLE);
1161 /* Response Data: empty */
1162 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1163 2);
1164 return;
1165 }
1166
1167 /* Status Code */
1168 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1169 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1170 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1171
1172 if (wpabuf_tailroom(resp) >=
1173 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1174 /* Response Data */
1175 wpabuf_put_buf(resp, bsrv->query); /* Key */
1176 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1177 }
1178 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1179}
1180
1181
1182static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1183 struct wpabuf *resp, u8 srv_trans_id)
1184{
1185 struct p2p_srv_upnp *usrv;
1186 u8 *len_pos;
1187
1188 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1189
1190 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1191 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1192 return;
1193 }
1194
1195 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1196 struct p2p_srv_upnp, list) {
1197 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1198 return;
1199
1200 /* Length (to be filled) */
1201 len_pos = wpabuf_put(resp, 2);
1202 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1203 wpabuf_put_u8(resp, srv_trans_id);
1204
1205 /* Status Code */
1206 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1207 /* Response Data */
1208 wpabuf_put_u8(resp, usrv->version);
1209 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1210 usrv->service);
1211 wpabuf_put_str(resp, usrv->service);
1212 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1213 2);
1214 }
1215}
1216
1217
1218static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1219 struct wpabuf *resp, u8 srv_trans_id,
1220 const u8 *query, size_t query_len)
1221{
1222 struct p2p_srv_upnp *usrv;
1223 u8 *len_pos;
1224 u8 version;
1225 char *str;
1226 int count = 0;
1227
1228 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1229 query, query_len);
1230
1231 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1232 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1233 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1234 srv_trans_id);
1235 return;
1236 }
1237
1238 if (query_len == 0) {
1239 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1240 return;
1241 }
1242
1243 version = query[0];
1244 str = os_malloc(query_len);
1245 if (str == NULL)
1246 return;
1247 os_memcpy(str, query + 1, query_len - 1);
1248 str[query_len - 1] = '\0';
1249
1250 if (wpabuf_tailroom(resp) < 5)
1251 return;
1252
1253 /* Length (to be filled) */
1254 len_pos = wpabuf_put(resp, 2);
1255 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1256 wpabuf_put_u8(resp, srv_trans_id);
1257
1258 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1259 struct p2p_srv_upnp, list) {
1260 if (version != usrv->version)
1261 continue;
1262
1263 if (os_strcmp(str, "ssdp:all") != 0 &&
1264 os_strstr(usrv->service, str) == NULL)
1265 continue;
1266
1267 if (wpabuf_tailroom(resp) < 2)
1268 break;
1269 if (count == 0) {
1270 /* Status Code */
1271 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1272 /* Response Data */
1273 wpabuf_put_u8(resp, version);
1274 } else
1275 wpabuf_put_u8(resp, ',');
1276
1277 count++;
1278
1279 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1280 usrv->service);
1281 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1282 break;
1283 wpabuf_put_str(resp, usrv->service);
1284 }
1285
1286 if (count == 0) {
1287 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1288 "available");
1289 /* Status Code */
1290 wpabuf_put_u8(resp, P2P_SD_QUERY_DATA_NOT_AVAILABLE);
1291 /* Response Data: empty */
1292 }
1293
1294 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1295}
1296
1297
1298void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1299 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1300{
1301 struct wpa_supplicant *wpa_s = ctx;
1302 const u8 *pos = tlvs;
1303 const u8 *end = tlvs + tlvs_len;
1304 const u8 *tlv_end;
1305 u16 slen;
1306 struct wpabuf *resp;
1307 u8 srv_proto, srv_trans_id;
1308 size_t buf_len;
1309 char *buf;
1310
1311 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1312 tlvs, tlvs_len);
1313 buf_len = 2 * tlvs_len + 1;
1314 buf = os_malloc(buf_len);
1315 if (buf) {
1316 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1317 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1318 MACSTR " %u %u %s",
1319 freq, MAC2STR(sa), dialog_token, update_indic,
1320 buf);
1321 os_free(buf);
1322 }
1323
1324 if (wpa_s->p2p_sd_over_ctrl_iface)
1325 return; /* to be processed by an external program */
1326
1327 resp = wpabuf_alloc(10000);
1328 if (resp == NULL)
1329 return;
1330
1331 while (pos + 1 < end) {
1332 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1333 slen = WPA_GET_LE16(pos);
1334 pos += 2;
1335 if (pos + slen > end || slen < 2) {
1336 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1337 "length");
1338 wpabuf_free(resp);
1339 return;
1340 }
1341 tlv_end = pos + slen;
1342
1343 srv_proto = *pos++;
1344 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1345 srv_proto);
1346 srv_trans_id = *pos++;
1347 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1348 srv_trans_id);
1349
1350 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1351 pos, tlv_end - pos);
1352
1353 switch (srv_proto) {
1354 case P2P_SERV_ALL_SERVICES:
1355 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1356 "for all services");
1357 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1358 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1359 wpa_printf(MSG_DEBUG, "P2P: No service "
1360 "discovery protocols available");
1361 wpas_sd_add_proto_not_avail(
1362 resp, P2P_SERV_ALL_SERVICES,
1363 srv_trans_id);
1364 break;
1365 }
1366 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1367 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1368 break;
1369 case P2P_SERV_BONJOUR:
1370 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1371 pos, tlv_end - pos);
1372 break;
1373 case P2P_SERV_UPNP:
1374 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1375 pos, tlv_end - pos);
1376 break;
1377 default:
1378 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1379 "protocol %u", srv_proto);
1380 wpas_sd_add_proto_not_avail(resp, srv_proto,
1381 srv_trans_id);
1382 break;
1383 }
1384
1385 pos = tlv_end;
1386 }
1387
1388 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1389
1390 wpabuf_free(resp);
1391}
1392
1393
1394void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1395 const u8 *tlvs, size_t tlvs_len)
1396{
1397 struct wpa_supplicant *wpa_s = ctx;
1398 const u8 *pos = tlvs;
1399 const u8 *end = tlvs + tlvs_len;
1400 const u8 *tlv_end;
1401 u16 slen;
1402 size_t buf_len;
1403 char *buf;
1404
1405 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1406 tlvs, tlvs_len);
1407 buf_len = 2 * tlvs_len + 1;
1408 buf = os_malloc(buf_len);
1409 if (buf) {
1410 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1411 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_RESP MACSTR
1412 " %u %s",
1413 MAC2STR(sa), update_indic, buf);
1414 os_free(buf);
1415 }
1416
1417 while (pos < end) {
1418 u8 srv_proto, srv_trans_id, status;
1419
1420 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1421 slen = WPA_GET_LE16(pos);
1422 pos += 2;
1423 if (pos + slen > end || slen < 3) {
1424 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1425 "length");
1426 return;
1427 }
1428 tlv_end = pos + slen;
1429
1430 srv_proto = *pos++;
1431 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1432 srv_proto);
1433 srv_trans_id = *pos++;
1434 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1435 srv_trans_id);
1436 status = *pos++;
1437 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1438 status);
1439
1440 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1441 pos, tlv_end - pos);
1442
1443 pos = tlv_end;
1444 }
1445}
1446
1447
1448void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1449 const struct wpabuf *tlvs)
1450{
1451 return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1452}
1453
1454
1455void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1456 u8 version, const char *query)
1457{
1458 struct wpabuf *tlvs;
1459 void *ret;
1460
1461 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1462 if (tlvs == NULL)
1463 return NULL;
1464 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1465 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1466 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1467 wpabuf_put_u8(tlvs, version);
1468 wpabuf_put_str(tlvs, query);
1469 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1470 wpabuf_free(tlvs);
1471 return ret;
1472}
1473
1474
1475int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1476{
1477 return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1478}
1479
1480
1481void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1482 const u8 *dst, u8 dialog_token,
1483 const struct wpabuf *resp_tlvs)
1484{
1485 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1486 resp_tlvs);
1487}
1488
1489
1490void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1491{
1492 p2p_sd_service_update(wpa_s->global->p2p);
1493}
1494
1495
1496static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1497{
1498 dl_list_del(&bsrv->list);
1499 wpabuf_free(bsrv->query);
1500 wpabuf_free(bsrv->resp);
1501 os_free(bsrv);
1502}
1503
1504
1505static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1506{
1507 dl_list_del(&usrv->list);
1508 os_free(usrv->service);
1509 os_free(usrv);
1510}
1511
1512
1513void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1514{
1515 struct p2p_srv_bonjour *bsrv, *bn;
1516 struct p2p_srv_upnp *usrv, *un;
1517
1518 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1519 struct p2p_srv_bonjour, list)
1520 wpas_p2p_srv_bonjour_free(bsrv);
1521
1522 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1523 struct p2p_srv_upnp, list)
1524 wpas_p2p_srv_upnp_free(usrv);
1525
1526 wpas_p2p_sd_service_update(wpa_s);
1527}
1528
1529
1530int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1531 struct wpabuf *query, struct wpabuf *resp)
1532{
1533 struct p2p_srv_bonjour *bsrv;
1534
1535 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1536 if (bsrv) {
1537 wpabuf_free(query);
1538 wpabuf_free(bsrv->resp);
1539 bsrv->resp = resp;
1540 return 0;
1541 }
1542
1543 bsrv = os_zalloc(sizeof(*bsrv));
1544 if (bsrv == NULL)
1545 return -1;
1546 bsrv->query = query;
1547 bsrv->resp = resp;
1548 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1549
1550 wpas_p2p_sd_service_update(wpa_s);
1551 return 0;
1552}
1553
1554
1555int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1556 const struct wpabuf *query)
1557{
1558 struct p2p_srv_bonjour *bsrv;
1559
1560 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1561 if (bsrv == NULL)
1562 return -1;
1563 wpas_p2p_srv_bonjour_free(bsrv);
1564 wpas_p2p_sd_service_update(wpa_s);
1565 return 0;
1566}
1567
1568
1569int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1570 const char *service)
1571{
1572 struct p2p_srv_upnp *usrv;
1573
1574 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1575 return 0; /* Already listed */
1576 usrv = os_zalloc(sizeof(*usrv));
1577 if (usrv == NULL)
1578 return -1;
1579 usrv->version = version;
1580 usrv->service = os_strdup(service);
1581 if (usrv->service == NULL) {
1582 os_free(usrv);
1583 return -1;
1584 }
1585 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1586
1587 wpas_p2p_sd_service_update(wpa_s);
1588 return 0;
1589}
1590
1591
1592int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1593 const char *service)
1594{
1595 struct p2p_srv_upnp *usrv;
1596
1597 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1598 if (usrv == NULL)
1599 return -1;
1600 wpas_p2p_srv_upnp_free(usrv);
1601 wpas_p2p_sd_service_update(wpa_s);
1602 return 0;
1603}
1604
1605
1606static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1607 const u8 *peer, const char *params)
1608{
1609 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1610 MAC2STR(peer), wps_generate_pin(), params);
1611}
1612
1613
1614static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1615 const u8 *peer, const char *params)
1616{
1617 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1618 MAC2STR(peer), params);
1619}
1620
1621
1622void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1623 const u8 *dev_addr, const u8 *pri_dev_type,
1624 const char *dev_name, u16 supp_config_methods,
1625 u8 dev_capab, u8 group_capab)
1626{
1627 struct wpa_supplicant *wpa_s = ctx;
1628 char devtype[WPS_DEV_TYPE_BUFSIZE];
1629 char params[200];
1630 u8 empty_dev_type[8];
1631
1632 if (pri_dev_type == NULL) {
1633 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1634 pri_dev_type = empty_dev_type;
1635 }
1636 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1637 " pri_dev_type=%s name='%s' config_methods=0x%x "
1638 "dev_capab=0x%x group_capab=0x%x",
1639 MAC2STR(dev_addr),
1640 wps_dev_type_bin2str(pri_dev_type, devtype,
1641 sizeof(devtype)),
1642 dev_name, supp_config_methods, dev_capab, group_capab);
1643 params[sizeof(params) - 1] = '\0';
1644
1645 if (config_methods & WPS_CONFIG_DISPLAY)
1646 wpas_prov_disc_local_display(wpa_s, peer, params);
1647 else if (config_methods & WPS_CONFIG_KEYPAD)
1648 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1649 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1650 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1651 "%s", MAC2STR(peer), params);
1652}
1653
1654
1655void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1656{
1657 struct wpa_supplicant *wpa_s = ctx;
1658 if (config_methods & WPS_CONFIG_DISPLAY)
1659 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1660 else if (config_methods & WPS_CONFIG_KEYPAD)
1661 wpas_prov_disc_local_display(wpa_s, peer, "");
1662 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1663 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1664 MAC2STR(peer));
1665}
1666
1667
1668static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1669 const u8 *go_dev_addr, const u8 *ssid,
1670 size_t ssid_len, int *go, u8 *group_bssid,
1671 int *force_freq, int persistent_group)
1672{
1673 struct wpa_supplicant *wpa_s = ctx;
1674 struct wpa_ssid *s;
1675 u8 cur_bssid[ETH_ALEN];
1676 int res;
1677
1678 if (!persistent_group) {
1679 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1680 " to join an active group", MAC2STR(sa));
1681 /*
1682 * Do not accept the invitation automatically; notify user and
1683 * request approval.
1684 */
1685 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1686 }
1687
1688 if (!wpa_s->conf->persistent_reconnect)
1689 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1690
1691 for (s = wpa_s->conf->ssid; s; s = s->next) {
1692 if (s->disabled == 2 &&
1693 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1694 s->ssid_len == ssid_len &&
1695 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1696 break;
1697 }
1698
1699 if (!s) {
1700 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1701 " requested reinvocation of an unknown group",
1702 MAC2STR(sa));
1703 return P2P_SC_FAIL_UNKNOWN_GROUP;
1704 }
1705
1706 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1707 *go = 1;
1708 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1709 wpa_printf(MSG_DEBUG, "P2P: The only available "
1710 "interface is already in use - reject "
1711 "invitation");
1712 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1713 }
1714 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1715 } else if (s->mode == WPAS_MODE_P2P_GO) {
1716 *go = 1;
1717 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1718 {
1719 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1720 "interface address for the group");
1721 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1722 }
1723 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1724 ETH_ALEN);
1725 }
1726
1727 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1728 wpa_s->assoc_freq) {
1729 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1730 "the channel we are already using");
1731 *force_freq = wpa_s->assoc_freq;
1732 }
1733
1734 res = wpa_drv_shared_freq(wpa_s);
1735 if (res > 0) {
1736 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1737 "with the channel we are already using on a "
1738 "shared interface");
1739 *force_freq = res;
1740 }
1741
1742 return P2P_SC_SUCCESS;
1743}
1744
1745
1746static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
1747 const u8 *ssid, size_t ssid_len,
1748 const u8 *go_dev_addr, u8 status,
1749 int op_freq)
1750{
1751 struct wpa_supplicant *wpa_s = ctx;
1752 struct wpa_ssid *s;
1753
1754 for (s = wpa_s->conf->ssid; s; s = s->next) {
1755 if (s->disabled == 2 &&
1756 s->ssid_len == ssid_len &&
1757 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1758 break;
1759 }
1760
1761 if (status == P2P_SC_SUCCESS) {
1762 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1763 " was accepted; op_freq=%d MHz",
1764 MAC2STR(sa), op_freq);
1765 if (s) {
1766 wpas_p2p_group_add_persistent(
1767 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
1768 }
1769 return;
1770 }
1771
1772 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1773 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1774 " was rejected (status %u)", MAC2STR(sa), status);
1775 return;
1776 }
1777
1778 if (!s) {
1779 if (bssid) {
1780 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1781 "sa=" MACSTR " go_dev_addr=" MACSTR
1782 " bssid=" MACSTR " unknown-network",
1783 MAC2STR(sa), MAC2STR(go_dev_addr),
1784 MAC2STR(bssid));
1785 } else {
1786 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1787 "sa=" MACSTR " go_dev_addr=" MACSTR
1788 " unknown-network",
1789 MAC2STR(sa), MAC2STR(go_dev_addr));
1790 }
1791 return;
1792 }
1793
1794 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
1795 " persistent=%d", MAC2STR(sa), s->id);
1796}
1797
1798
1799static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
1800{
1801 struct wpa_supplicant *wpa_s = ctx;
1802 struct wpa_ssid *ssid;
1803
1804 if (bssid) {
1805 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1806 "status=%d " MACSTR,
1807 status, MAC2STR(bssid));
1808 } else {
1809 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1810 "status=%d ", status);
1811 }
1812
1813 if (status != P2P_SC_SUCCESS) {
1814 wpas_p2p_remove_pending_group_interface(wpa_s);
1815 return;
1816 }
1817
1818 ssid = wpa_config_get_network(wpa_s->conf,
1819 wpa_s->pending_invite_ssid_id);
1820 if (ssid == NULL) {
1821 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
1822 "data matching with invitation");
1823 return;
1824 }
1825
1826 wpas_p2p_group_add_persistent(wpa_s, ssid,
1827 ssid->mode == WPAS_MODE_P2P_GO, 0);
1828}
1829
1830
1831static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
1832 struct p2p_config *p2p)
1833{
1834 struct hostapd_hw_modes *modes;
1835 u16 num_modes, flags;
1836 int i, cla;
1837 int band24 = 0, band5_low = 0, band5_high = 0;
1838
1839 /* TODO: more detailed selection of channels within reg_class based on
1840 * driver capabilities */
1841
1842 modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
1843 if (modes == NULL) {
1844 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
1845 "of all supported channels; assume dualband "
1846 "support");
1847 band24 = band5_low = band5_high = 1;
1848 } else {
1849 for (i = 0; i < num_modes; i++) {
1850 struct hostapd_hw_modes *mode;
1851 mode = &modes[i];
1852 if (mode->mode == HOSTAPD_MODE_IEEE80211G) {
1853 band24 = 1;
1854 } else if (mode->mode == HOSTAPD_MODE_IEEE80211A) {
1855 int j;
1856 for (j = 0; j < mode->num_channels; j++) {
1857 struct hostapd_channel_data *ch;
1858 ch = &mode->channels[j];
1859 if (ch->chan == 36)
1860 band5_low = 1;
1861 else if (ch->chan == 157)
1862 band5_high = 1;
1863 }
1864 }
1865 }
1866 }
1867
1868 cla = 0;
1869
1870 if (band24) {
1871 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1872 "2.4 GHz band");
1873
1874 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1875 p2p->channels.reg_class[cla].reg_class = 81;
1876 p2p->channels.reg_class[cla].channels = 13;
1877 for (i = 0; i < 13; i++)
1878 p2p->channels.reg_class[cla].channel[i] = i + 1;
1879 cla++;
1880
1881 /* Operating class 82 - 2.4 GHz band channel 14 */
1882 p2p->channels.reg_class[cla].reg_class = 82;
1883 p2p->channels.reg_class[cla].channels = 1;
1884 p2p->channels.reg_class[cla].channel[0] = 14;
1885 cla++;
1886
1887#if 0
1888 /* Operating class 83 - 2.4 GHz band channels 1..9; 40 MHz */
1889 p2p->channels.reg_class[cla].reg_class = 83;
1890 p2p->channels.reg_class[cla].channels = 9;
1891 for (i = 0; i < 9; i++)
1892 p2p->channels.reg_class[cla].channel[i] = i + 1;
1893 cla++;
1894
1895 /* Operating class 84 - 2.4 GHz band channels 5..13; 40 MHz */
1896 p2p->channels.reg_class[cla].reg_class = 84;
1897 p2p->channels.reg_class[cla].channels = 9;
1898 for (i = 0; i < 9; i++)
1899 p2p->channels.reg_class[cla].channel[i] = i + 5;
1900 cla++;
1901#endif
1902 }
1903
1904 if (band5_low) {
1905 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1906 "lower 5 GHz band");
1907
1908 /* Operating class 115 - 5 GHz, channels 36-48 */
1909 p2p->channels.reg_class[cla].reg_class = 115;
1910 p2p->channels.reg_class[cla].channels = 4;
1911 p2p->channels.reg_class[cla].channel[0] = 36;
1912 p2p->channels.reg_class[cla].channel[1] = 40;
1913 p2p->channels.reg_class[cla].channel[2] = 44;
1914 p2p->channels.reg_class[cla].channel[3] = 48;
1915 cla++;
1916
1917#if 0
1918 /* Operating class 116 - 5 GHz, channels 36,44; 40 MHz */
1919 p2p->channels.reg_class[cla].reg_class = 116;
1920 p2p->channels.reg_class[cla].channels = 2;
1921 p2p->channels.reg_class[cla].channel[0] = 36;
1922 p2p->channels.reg_class[cla].channel[1] = 44;
1923 cla++;
1924
1925 /* Operating class 117 - 5 GHz, channels 40,48; 40 MHz */
1926 p2p->channels.reg_class[cla].reg_class = 117;
1927 p2p->channels.reg_class[cla].channels = 2;
1928 p2p->channels.reg_class[cla].channel[0] = 40;
1929 p2p->channels.reg_class[cla].channel[1] = 48;
1930 cla++;
1931#endif
1932 }
1933
1934 if (band5_high) {
1935 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1936 "higher 5 GHz band");
1937
1938 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1939 p2p->channels.reg_class[cla].reg_class = 124;
1940 p2p->channels.reg_class[cla].channels = 4;
1941 p2p->channels.reg_class[cla].channel[0] = 149;
1942 p2p->channels.reg_class[cla].channel[1] = 153;
1943 p2p->channels.reg_class[cla].channel[2] = 157;
1944 p2p->channels.reg_class[cla].channel[3] = 161;
1945 cla++;
1946
1947#if 0
1948 /* Operating class 126 - 5 GHz, channels 149,157; 40 MHz */
1949 p2p->channels.reg_class[cla].reg_class = 126;
1950 p2p->channels.reg_class[cla].channels = 2;
1951 p2p->channels.reg_class[cla].channel[0] = 149;
1952 p2p->channels.reg_class[cla].channel[1] = 157;
1953 cla++;
1954
1955 /* Operating class 127 - 5 GHz, channels 153,161; 40 MHz */
1956 p2p->channels.reg_class[cla].reg_class = 127;
1957 p2p->channels.reg_class[cla].channels = 2;
1958 p2p->channels.reg_class[cla].channel[0] = 153;
1959 p2p->channels.reg_class[cla].channel[1] = 161;
1960 cla++;
1961#endif
1962 }
1963
1964 p2p->channels.reg_classes = cla;
1965
1966 if (modes)
1967 ieee80211_sta_free_hw_features(modes, num_modes);
1968
1969 return 0;
1970}
1971
1972
1973static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
1974 size_t buf_len)
1975{
1976 struct wpa_supplicant *wpa_s = ctx;
1977
1978 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1979 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
1980 break;
1981 }
1982 if (wpa_s == NULL)
1983 return -1;
1984
1985 return wpa_drv_get_noa(wpa_s, buf, buf_len);
1986}
1987
1988
1989/**
1990 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
1991 * @global: Pointer to global data from wpa_supplicant_init()
1992 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
1993 * Returns: 0 on success, -1 on failure
1994 */
1995int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
1996{
1997 struct p2p_config p2p;
1998 unsigned int r;
1999 int i;
2000
2001 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2002 return 0;
2003
2004#ifdef CONFIG_CLIENT_MLME
2005 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
2006 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
2007 wpa_s->mlme.public_action_cb_ctx = wpa_s;
2008 }
2009#endif /* CONFIG_CLIENT_MLME */
2010
2011 if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
2012 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
2013 /* Continue anyway; this is not really a fatal error */
2014 }
2015
2016 if (global->p2p)
2017 return 0;
2018
2019 os_memset(&p2p, 0, sizeof(p2p));
2020 p2p.msg_ctx = wpa_s;
2021 p2p.cb_ctx = wpa_s;
2022 p2p.p2p_scan = wpas_p2p_scan;
2023 p2p.send_action = wpas_send_action;
2024 p2p.send_action_done = wpas_send_action_done;
2025 p2p.go_neg_completed = wpas_go_neg_completed;
2026 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2027 p2p.dev_found = wpas_dev_found;
2028 p2p.start_listen = wpas_start_listen;
2029 p2p.stop_listen = wpas_stop_listen;
2030 p2p.send_probe_resp = wpas_send_probe_resp;
2031 p2p.sd_request = wpas_sd_request;
2032 p2p.sd_response = wpas_sd_response;
2033 p2p.prov_disc_req = wpas_prov_disc_req;
2034 p2p.prov_disc_resp = wpas_prov_disc_resp;
2035 p2p.invitation_process = wpas_invitation_process;
2036 p2p.invitation_received = wpas_invitation_received;
2037 p2p.invitation_result = wpas_invitation_result;
2038 p2p.get_noa = wpas_get_noa;
2039
2040 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2041 os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2042 p2p.dev_name = wpa_s->conf->device_name;
2043
2044 if (wpa_s->conf->p2p_listen_reg_class &&
2045 wpa_s->conf->p2p_listen_channel) {
2046 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2047 p2p.channel = wpa_s->conf->p2p_listen_channel;
2048 } else {
2049 p2p.reg_class = 81;
2050 /*
2051 * Pick one of the social channels randomly as the listen
2052 * channel.
2053 */
2054 os_get_random((u8 *) &r, sizeof(r));
2055 p2p.channel = 1 + (r % 3) * 5;
2056 }
2057
2058 if (wpa_s->conf->p2p_oper_reg_class &&
2059 wpa_s->conf->p2p_oper_channel) {
2060 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2061 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2062 } else {
2063 p2p.op_reg_class = 81;
2064 /*
2065 * For initial tests, pick the operation channel randomly.
2066 * TODO: Use scan results (etc.) to select the best channel.
2067 */
2068 p2p.op_channel = 1 + r % 11;
2069 }
2070 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d "
2071 "Own preferred operation channel: %d",
2072 p2p.channel, p2p.op_channel);
2073 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2074 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2075 p2p.country[2] = 0x04;
2076 } else
2077 os_memcpy(p2p.country, "US\x04", 3);
2078
2079 if (wpas_p2p_setup_channels(wpa_s, &p2p)) {
2080 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2081 "channel list");
2082 return -1;
2083 }
2084
2085 if (wpa_s->conf->device_type &&
2086 wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) <
2087 0) {
2088 wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
2089 return -1;
2090 }
2091
2092 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
2093 if (wpa_s->conf->sec_device_type[i] == NULL)
2094 continue;
2095 if (wps_dev_type_str2bin(
2096 wpa_s->conf->sec_device_type[i],
2097 p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) {
2098 wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type");
2099 return -1;
2100 }
2101 p2p.num_sec_dev_types++;
2102 if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES)
2103 break;
2104 }
2105
2106 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2107 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2108
2109 p2p.max_peers = 100;
2110
2111 if (wpa_s->conf->p2p_ssid_postfix) {
2112 p2p.ssid_postfix_len =
2113 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2114 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2115 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2116 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2117 p2p.ssid_postfix_len);
2118 }
2119
2120 global->p2p = p2p_init(&p2p);
2121 if (global->p2p == NULL)
2122 return -1;
2123
2124 return 0;
2125}
2126
2127
2128/**
2129 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2130 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2131 *
2132 * This function deinitialize per-interface P2P data.
2133 */
2134void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2135{
2136 if (wpa_s->driver && wpa_s->drv_priv)
2137 wpa_drv_probe_req_report(wpa_s, 0);
2138 os_free(wpa_s->go_params);
2139 wpa_s->go_params = NULL;
2140 wpabuf_free(wpa_s->pending_action_tx);
2141 wpa_s->pending_action_tx = NULL;
2142 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2143 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2144 wpa_s->p2p_long_listen = 0;
2145 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2146 wpas_p2p_remove_pending_group_interface(wpa_s);
2147
2148 /* TODO: remove group interface from the driver if this wpa_s instance
2149 * is on top of a P2P group interface */
2150}
2151
2152
2153/**
2154 * wpas_p2p_deinit_global - Deinitialize global P2P module
2155 * @global: Pointer to global data from wpa_supplicant_init()
2156 *
2157 * This function deinitializes the global (per device) P2P module.
2158 */
2159void wpas_p2p_deinit_global(struct wpa_global *global)
2160{
2161 struct wpa_supplicant *wpa_s, *tmp;
2162 char *ifname;
2163
2164 if (global->p2p == NULL)
2165 return;
2166
2167 /* Remove remaining P2P group interfaces */
2168 wpa_s = global->ifaces;
2169 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2170 wpa_s = wpa_s->next;
2171 while (wpa_s) {
2172 enum wpa_driver_if_type type;
2173 tmp = global->ifaces;
2174 while (tmp &&
2175 (tmp == wpa_s ||
2176 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2177 tmp = tmp->next;
2178 }
2179 if (tmp == NULL)
2180 break;
2181 ifname = os_strdup(tmp->ifname);
2182 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2183 wpa_supplicant_remove_iface(global, tmp);
2184 if (ifname)
2185 wpa_drv_if_remove(wpa_s, type, ifname);
2186 os_free(ifname);
2187 }
2188
2189 p2p_deinit(global->p2p);
2190 global->p2p = NULL;
2191}
2192
2193
2194static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2195{
2196 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
2197 return 1; /* P2P group requires a new interface in every case
2198 */
2199 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2200 return 0; /* driver does not support concurrent operations */
2201 if (wpa_s->global->ifaces->next)
2202 return 1; /* more that one interface already in use */
2203 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2204 return 1; /* this interface is already in use */
2205 return 0;
2206}
2207
2208
2209static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2210 const u8 *peer_addr,
2211 enum p2p_wps_method wps_method,
2212 int go_intent, const u8 *own_interface_addr,
2213 unsigned int force_freq, int persistent_group)
2214{
2215 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2216 go_intent, own_interface_addr, force_freq,
2217 persistent_group);
2218}
2219
2220
2221static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2222 const u8 *peer_addr,
2223 enum p2p_wps_method wps_method,
2224 int go_intent, const u8 *own_interface_addr,
2225 unsigned int force_freq, int persistent_group)
2226{
2227 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2228 go_intent, own_interface_addr, force_freq,
2229 persistent_group);
2230}
2231
2232
2233static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2234 const u8 *dev_addr, enum p2p_wps_method wps_method)
2235{
2236 struct wpa_bss *bss;
2237
2238 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2239 MACSTR " dev " MACSTR ")",
2240 MAC2STR(iface_addr), MAC2STR(dev_addr));
2241
2242 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2243 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2244 wpa_s->pending_join_wps_method = wps_method;
2245
2246 /* Make sure we are not running find during connection establishment */
2247 wpas_p2p_stop_find(wpa_s);
2248
2249 bss = wpa_bss_get_bssid(wpa_s, iface_addr);
2250 if (bss) {
2251 u16 method;
2252
2253 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2254 "prior to joining an existing group (GO " MACSTR
2255 " freq=%u MHz)",
2256 MAC2STR(dev_addr), bss->freq);
2257 wpa_s->pending_pd_before_join = 1;
2258
2259 switch (wps_method) {
2260 case WPS_PIN_LABEL:
2261 case WPS_PIN_DISPLAY:
2262 method = WPS_CONFIG_KEYPAD;
2263 break;
2264 case WPS_PIN_KEYPAD:
2265 method = WPS_CONFIG_DISPLAY;
2266 break;
2267 case WPS_PBC:
2268 method = WPS_CONFIG_PUSHBUTTON;
2269 break;
2270 default:
2271 method = 0;
2272 break;
2273 }
2274
2275 if (p2p_prov_disc_req(wpa_s->global->p2p, dev_addr, method, 1)
2276 < 0) {
2277 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2278 "Discovery Request before joining an "
2279 "existing group");
2280 wpa_s->pending_pd_before_join = 0;
2281 goto start;
2282 }
2283
2284 /*
2285 * Actual join operation will be started from the Action frame
2286 * TX status callback.
2287 */
2288 return 0;
2289 }
2290
2291 wpa_printf(MSG_DEBUG, "P2P: Target BSS/GO not yet in BSS table - "
2292 "cannot send Provision Discovery Request");
2293
2294start:
2295 /* Start join operation immediately */
2296 return wpas_p2p_join_start(wpa_s);
2297}
2298
2299
2300static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2301{
2302 struct wpa_supplicant *group;
2303 struct p2p_go_neg_results res;
2304
2305 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2306 if (group == NULL)
2307 return -1;
2308 if (group != wpa_s)
2309 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2310 sizeof(group->p2p_pin));
2311
2312 group->p2p_in_provisioning = 1;
2313
2314 os_memset(&res, 0, sizeof(res));
2315 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2316 ETH_ALEN);
2317 res.wps_method = wpa_s->pending_join_wps_method;
2318 wpas_start_wps_enrollee(group, &res);
2319
2320 return 0;
2321}
2322
2323
2324/**
2325 * wpas_p2p_connect - Request P2P Group Formation to be started
2326 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2327 * @peer_addr: Address of the peer P2P Device
2328 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2329 * @persistent_group: Whether to create a persistent group
2330 * @join: Whether to join an existing group (as a client) instead of starting
2331 * Group Owner negotiation; @peer_addr is BSSID in that case
2332 * @auth: Whether to only authorize the connection instead of doing that and
2333 * initiating Group Owner negotiation
2334 * @go_intent: GO Intent or -1 to use default
2335 * @freq: Frequency for the group or 0 for auto-selection
2336 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on failure
2337 */
2338int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2339 const char *pin, enum p2p_wps_method wps_method,
2340 int persistent_group, int join, int auth, int go_intent,
2341 int freq)
2342{
2343 int force_freq = 0;
2344 u8 bssid[ETH_ALEN];
2345 int ret = 0;
2346 enum wpa_driver_if_type iftype;
2347
2348 if (go_intent < 0)
2349 go_intent = wpa_s->conf->p2p_go_intent;
2350
2351 if (!auth)
2352 wpa_s->p2p_long_listen = 0;
2353
2354 if (pin)
2355 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2356 else if (wps_method == WPS_PIN_DISPLAY) {
2357 ret = wps_generate_pin();
2358 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2359 ret);
2360 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2361 wpa_s->p2p_pin);
2362 } else
2363 wpa_s->p2p_pin[0] = '\0';
2364
2365 if (join) {
2366 u8 iface_addr[ETH_ALEN];
2367 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2368 iface_addr) < 0)
2369 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2370 if (wpas_p2p_join(wpa_s, iface_addr, peer_addr, wps_method) <
2371 0)
2372 return -1;
2373 return ret;
2374 }
2375
2376 if (freq > 0)
2377 force_freq = freq;
2378 else if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2379 wpa_s->assoc_freq)
2380 force_freq = wpa_s->assoc_freq;
2381 else {
2382 force_freq = wpa_drv_shared_freq(wpa_s);
2383 if (force_freq < 0)
2384 force_freq = 0;
2385 }
2386
2387 if (force_freq > 0) {
2388 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2389 "channel we are already using (%u MHz) on another "
2390 "interface", force_freq);
2391 }
2392
2393 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2394
2395 if (!wpa_s->create_p2p_iface) {
2396 if (auth) {
2397 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2398 go_intent, wpa_s->own_addr,
2399 force_freq, persistent_group)
2400 < 0)
2401 return -1;
2402 return ret;
2403 }
2404 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
2405 go_intent, wpa_s->own_addr,
2406 force_freq, persistent_group) < 0)
2407 return -1;
2408 return ret;
2409 }
2410
2411 /* Prepare to add a new interface for the group */
2412 iftype = WPA_IF_P2P_GROUP;
2413 if (join)
2414 iftype = WPA_IF_P2P_CLIENT;
2415 else if (go_intent == 15)
2416 iftype = WPA_IF_P2P_GO;
2417 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
2418 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2419 "interface for the group");
2420 return -1;
2421 }
2422
2423 if (auth) {
2424 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2425 go_intent,
2426 wpa_s->pending_interface_addr,
2427 force_freq, persistent_group) < 0)
2428 return -1;
2429 return ret;
2430 }
2431 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
2432 wpa_s->pending_interface_addr,
2433 force_freq, persistent_group) < 0) {
2434 wpas_p2p_remove_pending_group_interface(wpa_s);
2435 return -1;
2436 }
2437 return ret;
2438}
2439
2440
2441/**
2442 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2443 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2444 * @freq: Frequency of the channel in MHz
2445 * @duration: Duration of the stay on the channel in milliseconds
2446 *
2447 * This callback is called when the driver indicates that it has started the
2448 * requested remain-on-channel duration.
2449 */
2450void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2451 unsigned int freq, unsigned int duration)
2452{
07a30a66 2453 wpa_s->roc_waiting_drv_freq = 0;
b22128ef
JM
2454 wpa_s->off_channel_freq = freq;
2455 wpas_send_action_cb(wpa_s, NULL);
2456 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
2457 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
2458 wpa_s->pending_listen_duration);
2459 wpa_s->pending_listen_freq = 0;
2460 }
2461}
2462
2463
2464static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
2465 unsigned int timeout)
2466{
2467 /* Limit maximum Listen state time based on driver limitation. */
2468 if (timeout > wpa_s->max_remain_on_chan)
2469 timeout = wpa_s->max_remain_on_chan;
2470
2471 return p2p_listen(wpa_s->global->p2p, timeout);
2472}
2473
2474
2475/**
2476 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
2477 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2478 * @freq: Frequency of the channel in MHz
2479 *
2480 * This callback is called when the driver indicates that a remain-on-channel
2481 * operation has been completed, i.e., the duration on the requested channel
2482 * has timed out.
2483 */
2484void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2485 unsigned int freq)
2486{
2487 wpa_s->off_channel_freq = 0;
2488 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
2489 return; /* P2P module started a new operation */
2490 if (wpa_s->pending_action_tx)
2491 return;
2492 if (wpa_s->p2p_long_listen > 0)
2493 wpa_s->p2p_long_listen -= 5;
2494 if (wpa_s->p2p_long_listen > 0) {
2495 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
2496 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen * 1000);
2497 }
2498}
2499
2500
2501/**
2502 * wpas_p2p_group_remove - Remove a P2P group
2503 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2504 * @ifname: Network interface name of the group interface or "*" to remove all
2505 * groups
2506 * Returns: 0 on success, -1 on failure
2507 *
2508 * This function is used to remove a P2P group. This can be used to disconnect
2509 * from a group in which the local end is a P2P Client or to end a P2P Group in
2510 * case the local end is the Group Owner. If a virtual network interface was
2511 * created for this group, that interface will be removed. Otherwise, only the
2512 * configured P2P group network will be removed from the interface.
2513 */
2514int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
2515{
2516 struct wpa_global *global = wpa_s->global;
2517
2518 if (os_strcmp(ifname, "*") == 0) {
2519 struct wpa_supplicant *prev;
2520 wpa_s = global->ifaces;
2521 while (wpa_s) {
2522 prev = wpa_s;
2523 wpa_s = wpa_s->next;
2524 wpas_p2p_group_delete(prev);
2525 }
2526 return 0;
2527 }
2528
2529 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2530 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2531 break;
2532 }
2533
2534 if (wpa_s == NULL)
2535 return -1;
2536
2537 wpas_p2p_group_delete(wpa_s);
2538
2539 return 0;
2540}
2541
2542
2543static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
2544 struct p2p_go_neg_results *params,
2545 int freq)
2546{
2547 u8 bssid[ETH_ALEN];
2548 int res;
2549
2550 os_memset(params, 0, sizeof(*params));
2551 params->role_go = 1;
2552 params->freq = 2412;
2553 if (freq)
2554 params->freq = freq;
2555 else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
2556 wpa_s->conf->p2p_oper_channel >= 1 &&
2557 wpa_s->conf->p2p_oper_channel <= 11)
2558 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
2559 else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
2560 wpa_s->conf->p2p_oper_reg_class == 118)
2561 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
2562 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2563 wpa_s->assoc_freq && !freq) {
2564 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2565 "already using");
2566 params->freq = wpa_s->assoc_freq;
2567 }
2568
2569 res = wpa_drv_shared_freq(wpa_s);
2570 if (res > 0 && !freq) {
2571 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2572 "already using on a shared interface");
2573 params->freq = res;
2574 }
2575}
2576
2577
2578static struct wpa_supplicant *
2579wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
2580 int go)
2581{
2582 struct wpa_supplicant *group_wpa_s;
2583
2584 if (!wpas_p2p_create_iface(wpa_s))
2585 return wpa_s;
2586
2587 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2588 WPA_IF_P2P_CLIENT) < 0)
2589 return NULL;
2590 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
2591 if (group_wpa_s == NULL) {
2592 wpas_p2p_remove_pending_group_interface(wpa_s);
2593 return NULL;
2594 }
2595
2596 return group_wpa_s;
2597}
2598
2599
2600/**
2601 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
2602 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2603 * @persistent_group: Whether to create a persistent group
2604 * @freq: Frequency for the group or 0 to indicate no hardcoding
2605 * Returns: 0 on success, -1 on failure
2606 *
2607 * This function creates a new P2P group with the local end as the Group Owner,
2608 * i.e., without using Group Owner Negotiation.
2609 */
2610int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
2611 int freq)
2612{
2613 struct p2p_go_neg_results params;
2614
2615 wpas_p2p_init_go_params(wpa_s, &params, freq);
2616 p2p_go_params(wpa_s->global->p2p, &params);
2617 params.persistent_group = persistent_group;
2618
2619 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
2620 if (wpa_s == NULL)
2621 return -1;
2622 wpas_start_wps_go(wpa_s, &params, 0);
2623
2624 return 0;
2625}
2626
2627
2628static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
2629 struct wpa_ssid *params, int addr_allocated)
2630{
2631 struct wpa_ssid *ssid;
2632
2633 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
2634 if (wpa_s == NULL)
2635 return -1;
2636
2637 wpa_supplicant_ap_deinit(wpa_s);
2638
2639 ssid = wpa_config_add_network(wpa_s->conf);
2640 if (ssid == NULL)
2641 return -1;
2642 wpas_notify_network_added(wpa_s, ssid);
2643 wpa_config_set_network_defaults(ssid);
2644 ssid->temporary = 1;
2645 ssid->proto = WPA_PROTO_RSN;
2646 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
2647 ssid->group_cipher = WPA_CIPHER_CCMP;
2648 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2649 ssid->ssid = os_malloc(params->ssid_len);
2650 if (ssid->ssid == NULL) {
2651 wpas_notify_network_removed(wpa_s, ssid);
2652 wpa_config_remove_network(wpa_s->conf, ssid->id);
2653 return -1;
2654 }
2655 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2656 ssid->ssid_len = params->ssid_len;
2657 ssid->p2p_group = 1;
2658 if (params->psk_set) {
2659 os_memcpy(ssid->psk, params->psk, 32);
2660 ssid->psk_set = 1;
2661 }
2662 if (params->passphrase)
2663 ssid->passphrase = os_strdup(params->passphrase);
2664
2665 wpa_supplicant_select_network(wpa_s, ssid);
2666
2667 wpa_s->show_group_started = 1;
2668
2669 return 0;
2670}
2671
2672
2673int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
2674 struct wpa_ssid *ssid, int addr_allocated,
2675 int freq)
2676{
2677 struct p2p_go_neg_results params;
2678
2679 if (ssid->disabled != 2 || ssid->ssid == NULL)
2680 return -1;
2681
2682 wpa_s->p2p_long_listen = 0;
2683 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2684
2685 if (ssid->mode == WPAS_MODE_INFRA)
2686 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
2687
2688 if (ssid->mode != WPAS_MODE_P2P_GO)
2689 return -1;
2690
2691 wpas_p2p_init_go_params(wpa_s, &params, freq);
2692
2693 params.role_go = 1;
2694 if (ssid->passphrase == NULL ||
2695 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
2696 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
2697 "group");
2698 return -1;
2699 }
2700 os_strlcpy(params.passphrase, ssid->passphrase,
2701 sizeof(params.passphrase));
2702 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
2703 params.ssid_len = ssid->ssid_len;
2704 params.persistent_group = 1;
2705
2706 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
2707 if (wpa_s == NULL)
2708 return -1;
2709
2710 wpas_start_wps_go(wpa_s, &params, 0);
2711
2712 return 0;
2713}
2714
2715
2716static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
2717 struct wpabuf *proberesp_ies)
2718{
2719 struct wpa_supplicant *wpa_s = ctx;
2720 if (wpa_s->ap_iface) {
2721 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
2722 if (beacon_ies) {
2723 wpabuf_free(hapd->p2p_beacon_ie);
2724 hapd->p2p_beacon_ie = beacon_ies;
2725 }
2726 wpabuf_free(hapd->p2p_probe_resp_ie);
2727 hapd->p2p_probe_resp_ie = proberesp_ies;
2728 } else {
2729 wpabuf_free(beacon_ies);
2730 wpabuf_free(proberesp_ies);
2731 }
2732 wpa_supplicant_ap_update_beacon(wpa_s);
2733}
2734
2735
2736struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
2737 int persistent_group,
2738 int group_formation)
2739{
2740 struct p2p_group *group;
2741 struct p2p_group_config *cfg;
2742
2743 cfg = os_zalloc(sizeof(*cfg));
2744 if (cfg == NULL)
2745 return NULL;
2746
2747 cfg->persistent_group = persistent_group;
2748 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
2749 cfg->cb_ctx = wpa_s;
2750 cfg->ie_update = wpas_p2p_ie_update;
2751
2752 group = p2p_group_init(wpa_s->global->p2p, cfg);
2753 if (group == NULL)
2754 os_free(cfg);
2755 if (!group_formation)
2756 p2p_group_notif_formation_done(group);
2757 wpa_s->p2p_group = group;
2758 return group;
2759}
2760
2761
2762void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2763 int registrar)
2764{
2765 if (!wpa_s->p2p_in_provisioning) {
2766 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
2767 "provisioning not in progress");
2768 return;
2769 }
2770
2771 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
2772 NULL);
2773 if (wpa_s->global->p2p)
2774 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
2775 wpas_group_formation_completed(wpa_s, 1);
2776}
2777
2778
2779int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2780 const char *config_method)
2781{
2782 u16 config_methods;
2783
2784 if (os_strcmp(config_method, "display") == 0)
2785 config_methods = WPS_CONFIG_DISPLAY;
2786 else if (os_strcmp(config_method, "keypad") == 0)
2787 config_methods = WPS_CONFIG_KEYPAD;
2788 else if (os_strcmp(config_method, "pbc") == 0 ||
2789 os_strcmp(config_method, "pushbutton") == 0)
2790 config_methods = WPS_CONFIG_PUSHBUTTON;
2791 else
2792 return -1;
2793
2794 if (wpa_s->global->p2p == NULL)
2795 return -1;
2796
2797 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
2798 config_methods, 0);
2799}
2800
2801
2802int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
2803 char *end)
2804{
2805 return p2p_scan_result_text(ies, ies_len, buf, end);
2806}
2807
2808
2809int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
2810 enum p2p_discovery_type type)
2811{
2812 wpa_s->p2p_long_listen = 0;
2813
2814 return p2p_find(wpa_s->global->p2p, timeout, type);
2815}
2816
2817
2818void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
2819{
2820 wpa_s->p2p_long_listen = 0;
2821
2822 p2p_stop_find(wpa_s->global->p2p);
2823
2824 wpas_p2p_remove_pending_group_interface(wpa_s);
2825}
2826
2827
2828static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
2829{
2830 struct wpa_supplicant *wpa_s = eloop_ctx;
2831 wpa_s->p2p_long_listen = 0;
2832}
2833
2834
2835int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
2836{
2837 int res;
2838
2839 if (timeout == 0) {
2840 /*
2841 * This is a request for unlimited Listen state. However, at
2842 * least for now, this is mapped to a Listen state for one
2843 * hour.
2844 */
2845 timeout = 3600;
2846 }
2847 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2848 wpa_s->p2p_long_listen = 0;
2849
2850 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
2851 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
2852 wpa_s->p2p_long_listen = timeout;
2853 eloop_register_timeout(timeout, 0,
2854 wpas_p2p_long_listen_timeout,
2855 wpa_s, NULL);
2856 }
2857
2858 return res;
2859}
2860
2861
2862int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, const u8 *bssid,
2863 u8 *buf, size_t len, int p2p_group)
2864{
2865 if (wpa_s->global->p2p_disabled)
2866 return -1;
2867 if (wpa_s->global->p2p == NULL)
2868 return -1;
2869
2870 return p2p_assoc_req_ie(wpa_s->global->p2p, bssid, buf, len,
2871 p2p_group);
2872}
2873
2874
2875int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
2876 const u8 *ie, size_t ie_len)
2877{
2878 if (wpa_s->global->p2p_disabled)
2879 return 0;
2880 if (wpa_s->global->p2p == NULL)
2881 return 0;
2882
2883 return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
2884}
2885
2886
2887void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
2888 const u8 *sa, const u8 *bssid,
2889 u8 category, const u8 *data, size_t len, int freq)
2890{
2891 if (wpa_s->global->p2p_disabled)
2892 return;
2893 if (wpa_s->global->p2p == NULL)
2894 return;
2895
2896 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
2897 freq);
2898}
2899
2900
2901void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
2902{
2903 if (wpa_s->global->p2p_disabled)
2904 return;
2905 if (wpa_s->global->p2p == NULL)
2906 return;
2907
2908 p2p_scan_ie(wpa_s->global->p2p, ies);
2909}
2910
2911
2912void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
2913{
2914 p2p_group_deinit(wpa_s->p2p_group);
2915 wpa_s->p2p_group = NULL;
2916}
2917
2918
2919int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
2920{
2921 wpa_s->p2p_long_listen = 0;
2922
2923 return p2p_reject(wpa_s->global->p2p, addr);
2924}
2925
2926
2927/* Invite to reinvoke a persistent group */
2928int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2929 struct wpa_ssid *ssid, const u8 *go_dev_addr)
2930{
2931 enum p2p_invite_role role;
2932 u8 *bssid = NULL;
2933
2934 if (ssid->mode == WPAS_MODE_P2P_GO) {
2935 role = P2P_INVITE_ROLE_GO;
2936 if (peer_addr == NULL) {
2937 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
2938 "address in invitation command");
2939 return -1;
2940 }
2941 if (wpas_p2p_create_iface(wpa_s)) {
2942 if (wpas_p2p_add_group_interface(wpa_s,
2943 WPA_IF_P2P_GO) < 0) {
2944 wpa_printf(MSG_ERROR, "P2P: Failed to "
2945 "allocate a new interface for the "
2946 "group");
2947 return -1;
2948 }
2949 bssid = wpa_s->pending_interface_addr;
2950 } else
2951 bssid = wpa_s->own_addr;
2952 } else {
2953 role = P2P_INVITE_ROLE_CLIENT;
2954 peer_addr = ssid->bssid;
2955 }
2956 wpa_s->pending_invite_ssid_id = ssid->id;
2957
2958 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
2959 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
2960}
2961
2962
2963/* Invite to join an active group */
2964int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
2965 const u8 *peer_addr, const u8 *go_dev_addr)
2966{
2967 struct wpa_global *global = wpa_s->global;
2968 enum p2p_invite_role role;
2969 u8 *bssid = NULL;
2970 struct wpa_ssid *ssid;
2971
2972 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2973 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2974 break;
2975 }
2976 if (wpa_s == NULL) {
2977 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
2978 return -1;
2979 }
2980
2981 ssid = wpa_s->current_ssid;
2982 if (ssid == NULL) {
2983 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
2984 "invitation");
2985 return -1;
2986 }
2987
2988 if (ssid->mode == WPAS_MODE_P2P_GO) {
2989 role = P2P_INVITE_ROLE_ACTIVE_GO;
2990 bssid = wpa_s->own_addr;
2991 if (go_dev_addr == NULL)
2992 go_dev_addr = wpa_s->own_addr;
2993 } else {
2994 role = P2P_INVITE_ROLE_CLIENT;
2995 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
2996 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
2997 "invite to current group");
2998 return -1;
2999 }
3000 bssid = wpa_s->bssid;
3001 if (go_dev_addr == NULL &&
3002 !is_zero_ether_addr(wpa_s->go_dev_addr))
3003 go_dev_addr = wpa_s->go_dev_addr;
3004 }
3005 wpa_s->pending_invite_ssid_id = -1;
3006
3007 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3008 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 0);
3009}
3010
3011
3012void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3013{
3014 struct wpa_ssid *ssid = wpa_s->current_ssid;
3015 const char *ssid_txt;
3016 u8 go_dev_addr[ETH_ALEN];
3017 int persistent;
3018
3019 if (!wpa_s->show_group_started || !ssid)
3020 return;
3021
3022 wpa_s->show_group_started = 0;
3023
3024 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3025 os_memset(go_dev_addr, 0, ETH_ALEN);
3026 if (ssid->bssid_set)
3027 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3028 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3029 ssid->ssid_len);
3030 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3031
3032 if (ssid->passphrase == NULL && ssid->psk_set) {
3033 char psk[65];
3034 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3035 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3036 "%s client ssid=\"%s\" psk=%s go_dev_addr=" MACSTR
3037 "%s",
3038 wpa_s->ifname, ssid_txt, psk, MAC2STR(go_dev_addr),
3039 persistent ? " [PERSISTENT]" : "");
3040 } else {
3041 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3042 "%s client ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
3043 MACSTR "%s",
3044 wpa_s->ifname, ssid_txt,
3045 ssid->passphrase ? ssid->passphrase : "",
3046 MAC2STR(go_dev_addr),
3047 persistent ? " [PERSISTENT]" : "");
3048 }
3049
3050 if (persistent)
3051 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
3052 go_dev_addr);
3053}
3054
3055
3056int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3057 u32 interval1, u32 duration2, u32 interval2)
3058{
3059 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3060 wpa_s->current_ssid == NULL ||
3061 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3062 return -1;
3063
3064 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3065 wpa_s->own_addr, wpa_s->assoc_freq,
3066 duration1, interval1, duration2, interval2);
3067}
3068
3069
3070int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3071 unsigned int interval)
3072{
3073 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3074}
3075
3076
3077void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3078 u16 reason_code, const u8 *ie, size_t ie_len)
3079{
3080 if (wpa_s->global->p2p_disabled)
3081 return;
3082
3083 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3084}
3085
3086
3087void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3088 u16 reason_code, const u8 *ie, size_t ie_len)
3089{
3090 if (wpa_s->global->p2p_disabled)
3091 return;
3092
3093 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3094}
3095
3096
3097void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3098{
3099 struct p2p_data *p2p = wpa_s->global->p2p;
3100
3101 if (p2p == NULL)
3102 return;
3103
3104 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
3105 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
3106
3107 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
3108 u8 pri_dev_type[8];
3109 if (wpa_s->conf->device_type) {
3110 if (wps_dev_type_str2bin(wpa_s->conf->device_type,
3111 pri_dev_type) < 0) {
3112 wpa_printf(MSG_ERROR, "P2P: Invalid "
3113 "device_type");
3114 } else
3115 p2p_set_pri_dev_type(p2p, pri_dev_type);
3116 }
3117 }
3118
3119 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
3120 u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
3121 size_t num = 0;
3122 int i;
3123 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
3124 if (wpa_s->conf->sec_device_type[i] == NULL)
3125 continue;
3126 if (wps_dev_type_str2bin(
3127 wpa_s->conf->sec_device_type[i],
3128 sec_dev_type[num]) < 0) {
3129 wpa_printf(MSG_ERROR, "P2P: Invalid "
3130 "sec_device_type");
3131 continue;
3132 }
3133 num++;
3134 if (num == P2P_SEC_DEVICE_TYPES)
3135 break;
3136 }
3137 p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num);
3138 }
3139
3140 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3141 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3142 char country[3];
3143 country[0] = wpa_s->conf->country[0];
3144 country[1] = wpa_s->conf->country[1];
3145 country[2] = 0x04;
3146 p2p_set_country(p2p, country);
3147 }
3148
3149 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
3150 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
3151 wpa_s->conf->p2p_ssid_postfix ?
3152 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
3153 0);
3154 }
3155}