3 * Copyright (c) 2009-2010, Atheros Communications
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
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"
24 #include "ap/hostapd.h"
25 #include "ap/p2p_hostapd.h"
26 #include "wpa_supplicant_i.h"
29 #include "config_ssid.h"
35 #include "wps_supplicant.h"
36 #include "p2p_supplicant.h"
39 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
40 static struct wpa_supplicant *
41 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
43 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
44 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
45 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
46 const u8 *dev_addr, enum p2p_wps_method wps_method);
47 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
48 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
49 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
50 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
53 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
54 struct wpa_scan_results *scan_res)
58 if (wpa_s->global->p2p_disabled)
61 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
64 for (i = 0; i < scan_res->num; i++) {
65 struct wpa_scan_res *bss = scan_res->res[i];
66 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
67 bss->freq, bss->level,
68 (const u8 *) (bss + 1),
73 p2p_scan_res_handled(wpa_s->global->p2p);
77 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq)
79 struct wpa_supplicant *wpa_s = ctx;
80 struct wpa_driver_scan_params params;
82 struct wpabuf *wps_ie, *ies;
83 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
85 os_memset(¶ms, 0, sizeof(params));
87 /* P2P Wildcard SSID */
89 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
90 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
92 wpa_s->wps->dev.p2p = 1;
93 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
98 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
103 wpabuf_put_buf(ies, wps_ie);
106 p2p_scan_ie(wpa_s->global->p2p, ies);
108 params.extra_ies = wpabuf_head(ies);
109 params.extra_ies_len = wpabuf_len(ies);
112 case P2P_SCAN_SOCIAL:
113 params.freqs = social_channels;
117 case P2P_SCAN_SPECIFIC:
118 social_channels[0] = freq;
119 social_channels[1] = 0;
120 params.freqs = social_channels;
122 case P2P_SCAN_SOCIAL_PLUS_ONE:
123 social_channels[3] = freq;
124 params.freqs = social_channels;
128 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
129 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
130 ret = ieee80211_sta_req_scan(wpa_s, ¶ms);
132 ret = wpa_drv_scan(wpa_s, ¶ms);
140 #ifdef CONFIG_CLIENT_MLME
141 static void p2p_rx_action_mlme(void *ctx, const u8 *buf, size_t len, int freq)
143 struct wpa_supplicant *wpa_s = ctx;
144 const struct ieee80211_mgmt *mgmt;
147 if (wpa_s->global->p2p_disabled)
149 mgmt = (const struct ieee80211_mgmt *) buf;
150 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
153 p2p_rx_action(wpa_s->global->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
154 mgmt->u.action.category,
155 &mgmt->u.action.u.vs_public_action.action,
156 len - hdr_len, freq);
158 #endif /* CONFIG_CLIENT_MLME */
161 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
163 switch (p2p_group_interface) {
164 case P2P_GROUP_INTERFACE_PENDING:
165 return WPA_IF_P2P_GROUP;
166 case P2P_GROUP_INTERFACE_GO:
167 return WPA_IF_P2P_GO;
168 case P2P_GROUP_INTERFACE_CLIENT:
169 return WPA_IF_P2P_CLIENT;
172 return WPA_IF_P2P_GROUP;
176 static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
178 struct wpa_ssid *ssid;
182 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
184 ssid = wpa_s->current_ssid;
187 * The current SSID was not known, but there may still be a
188 * pending P2P group interface waiting for provisioning.
190 ssid = wpa_s->conf->ssid;
192 if (ssid->p2p_group &&
193 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
194 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
199 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
201 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
202 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
203 wpa_s->reassociate = 0;
204 wpa_s->disconnected = 1;
205 wpa_supplicant_deauthenticate(wpa_s,
206 WLAN_REASON_DEAUTH_LEAVING);
210 if (wpa_s->cross_connect_in_use) {
211 wpa_s->cross_connect_in_use = 0;
212 wpa_msg(wpa_s->parent, MSG_INFO,
213 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
214 wpa_s->ifname, wpa_s->cross_connect_uplink);
216 switch (wpa_s->removal_reason) {
217 case P2P_GROUP_REMOVAL_REQUESTED:
218 reason = " reason=REQUESTED";
220 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
221 reason = " reason=IDLE";
223 case P2P_GROUP_REMOVAL_UNAVAILABLE:
224 reason = " reason=UNAVAILABLE";
230 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s",
231 wpa_s->ifname, gtype, reason);
232 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
233 struct wpa_global *global;
235 enum wpa_driver_if_type type;
236 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
238 global = wpa_s->global;
239 ifname = os_strdup(wpa_s->ifname);
240 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
241 wpa_supplicant_remove_iface(wpa_s->global, wpa_s);
242 wpa_s = global->ifaces;
244 wpa_drv_if_remove(wpa_s, type, ifname);
249 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
250 if (ssid && (ssid->p2p_group ||
251 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
252 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
254 if (ssid == wpa_s->current_ssid)
255 wpa_s->current_ssid = NULL;
256 wpas_notify_network_removed(wpa_s, ssid);
257 wpa_config_remove_network(wpa_s->conf, id);
258 wpa_supplicant_clear_status(wpa_s);
260 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
263 wpa_supplicant_ap_deinit(wpa_s);
267 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
269 const u8 *ssid, size_t ssid_len)
277 if (wpa_s->go_params)
278 bssid = wpa_s->go_params->peer_interface_addr;
280 bssid = wpa_s->bssid;
282 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
284 u8 iface_addr[ETH_ALEN];
285 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
287 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
290 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
291 "group is persistent - BSS " MACSTR " not found",
296 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
298 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
299 "group is persistent - BSS " MACSTR
300 " did not include P2P IE", MAC2STR(bssid));
301 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
302 (u8 *) (bss + 1), bss->ie_len);
303 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
304 ((u8 *) bss + 1) + bss->ie_len,
309 group_capab = p2p_get_group_capab(p2p);
310 addr = p2p_get_go_dev_addr(p2p);
311 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
312 "group_capab=0x%x", group_capab);
314 os_memcpy(go_dev_addr, addr, ETH_ALEN);
315 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
318 os_memset(go_dev_addr, 0, ETH_ALEN);
321 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
322 "go_dev_addr=" MACSTR,
323 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
325 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
329 static void wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
330 struct wpa_ssid *ssid,
331 const u8 *go_dev_addr)
336 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
337 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
338 for (s = wpa_s->conf->ssid; s; s = s->next) {
339 if (s->disabled == 2 &&
340 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
341 s->ssid_len == ssid->ssid_len &&
342 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
347 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
349 if (ssid->passphrase && !s->passphrase)
351 else if (ssid->passphrase && s->passphrase &&
352 os_strcmp(ssid->passphrase, s->passphrase) != 0)
355 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
358 s = wpa_config_add_network(wpa_s->conf);
361 wpa_config_set_network_defaults(s);
365 s->p2p_persistent_group = 1;
368 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
369 s->mode = ssid->mode;
370 s->auth_alg = WPA_AUTH_ALG_OPEN;
371 s->key_mgmt = WPA_KEY_MGMT_PSK;
372 s->proto = WPA_PROTO_RSN;
373 s->pairwise_cipher = WPA_CIPHER_CCMP;
374 if (ssid->passphrase) {
375 os_free(s->passphrase);
376 s->passphrase = os_strdup(ssid->passphrase);
380 os_memcpy(s->psk, ssid->psk, 32);
382 if (s->passphrase && !s->psk_set)
383 wpa_config_update_psk(s);
384 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
386 s->ssid = os_malloc(ssid->ssid_len);
389 s->ssid_len = ssid->ssid_len;
390 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
393 #ifndef CONFIG_NO_CONFIG_WRITE
394 if (changed && wpa_s->conf->update_config &&
395 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
396 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
398 #endif /* CONFIG_NO_CONFIG_WRITE */
402 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
405 struct wpa_ssid *ssid;
406 const char *ssid_txt;
409 u8 go_dev_addr[ETH_ALEN];
412 * This callback is likely called for the main interface. Update wpa_s
413 * to use the group interface if a new interface was created for the
416 if (wpa_s->global->p2p_group_formation)
417 wpa_s = wpa_s->global->p2p_group_formation;
418 wpa_s->p2p_in_provisioning = 0;
421 wpa_msg(wpa_s->parent, MSG_INFO,
422 P2P_EVENT_GROUP_FORMATION_FAILURE);
423 wpas_p2p_group_delete(wpa_s);
427 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
429 ssid = wpa_s->current_ssid;
430 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
431 ssid->mode = WPAS_MODE_P2P_GO;
432 p2p_group_notif_formation_done(wpa_s->p2p_group);
433 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
438 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
439 client = ssid->mode == WPAS_MODE_INFRA;
440 if (ssid->mode == WPAS_MODE_P2P_GO) {
441 persistent = ssid->p2p_persistent_group;
442 os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
445 persistent = wpas_p2p_persistent_group(wpa_s,
451 client = wpa_s->p2p_group_interface ==
452 P2P_GROUP_INTERFACE_CLIENT;
455 wpa_s->show_group_started = 0;
458 * Indicate event only after successfully completed 4-way
459 * handshake, i.e., when the interface is ready for data
462 wpa_s->show_group_started = 1;
463 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
465 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
466 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
467 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
469 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
470 MAC2STR(go_dev_addr),
471 persistent ? " [PERSISTENT]" : "");
472 wpas_p2p_cross_connect_setup(wpa_s);
473 wpas_p2p_set_group_idle_timeout(wpa_s);
475 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
476 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
477 "go_dev_addr=" MACSTR "%s",
478 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
479 ssid && ssid->passphrase ? ssid->passphrase : "",
480 MAC2STR(go_dev_addr),
481 persistent ? " [PERSISTENT]" : "");
482 wpas_p2p_cross_connect_setup(wpa_s);
483 wpas_p2p_set_group_idle_timeout(wpa_s);
487 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
492 static struct wpa_supplicant *
493 wpas_get_tx_interface(struct wpa_supplicant *wpa_s, const u8 *src)
495 struct wpa_supplicant *iface;
497 if (os_memcmp(src, wpa_s->own_addr, ETH_ALEN) == 0)
501 * Try to find a group interface that matches with the source address.
503 iface = wpa_s->global->ifaces;
505 if (os_memcmp(wpa_s->pending_action_src,
506 iface->own_addr, ETH_ALEN) == 0)
511 wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
512 "instead of interface %s for Action TX",
513 iface->ifname, wpa_s->ifname);
521 static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
523 struct wpa_supplicant *wpa_s = eloop_ctx;
524 struct wpa_supplicant *iface;
528 without_roc = wpa_s->pending_action_without_roc;
529 wpa_s->pending_action_without_roc = 0;
530 wpa_printf(MSG_DEBUG, "P2P: Send Action callback (without_roc=%d "
531 "pending_action_tx=%p)",
532 without_roc, wpa_s->pending_action_tx);
534 if (wpa_s->pending_action_tx == NULL)
538 * This call is likely going to be on the P2P device instance if the
539 * driver uses a separate interface for that purpose. However, some
540 * Action frames are actually sent within a P2P Group and when that is
541 * the case, we need to follow power saving (e.g., GO buffering the
542 * frame for a client in PS mode or a client following the advertised
543 * NoA from its GO). To make that easier for the driver, select the
544 * correct group interface here.
546 iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src);
548 if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
549 wpa_s->pending_action_freq != 0 &&
550 wpa_s->pending_action_freq != iface->assoc_freq) {
551 wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
552 "waiting for another freq=%u (off_channel_freq=%u "
554 wpa_s->pending_action_freq,
555 wpa_s->off_channel_freq,
557 if (without_roc && wpa_s->off_channel_freq == 0) {
559 * We may get here if wpas_send_action() found us to be
560 * on the correct channel, but remain-on-channel cancel
561 * event was received before getting here.
563 wpa_printf(MSG_DEBUG, "P2P: Schedule "
564 "remain-on-channel to send Action frame");
565 if (wpa_drv_remain_on_channel(
566 wpa_s, wpa_s->pending_action_freq, 200) <
568 wpa_printf(MSG_DEBUG, "P2P: Failed to request "
569 "driver to remain on channel (%u "
570 "MHz) for Action Frame TX",
571 wpa_s->pending_action_freq);
573 wpa_s->off_channel_freq = 0;
574 wpa_s->roc_waiting_drv_freq =
575 wpa_s->pending_action_freq;
581 wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
582 MACSTR " using interface %s",
583 MAC2STR(wpa_s->pending_action_dst), iface->ifname);
584 res = wpa_drv_send_action(iface, wpa_s->pending_action_freq,
585 wpa_s->pending_action_dst,
586 wpa_s->pending_action_src,
587 wpa_s->pending_action_bssid,
588 wpabuf_head(wpa_s->pending_action_tx),
589 wpabuf_len(wpa_s->pending_action_tx));
591 wpa_printf(MSG_DEBUG, "P2P: Failed to send the pending "
594 * Use fake TX status event to allow P2P state machine to
597 wpas_send_action_tx_status(
598 wpa_s, wpa_s->pending_action_dst,
599 wpabuf_head(wpa_s->pending_action_tx),
600 wpabuf_len(wpa_s->pending_action_tx),
601 P2P_SEND_ACTION_FAILED);
606 void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst,
607 const u8 *data, size_t data_len,
608 enum p2p_send_action_result result)
610 if (wpa_s->global->p2p_disabled)
613 if (wpa_s->pending_action_tx == NULL) {
614 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - no "
615 "pending operation");
619 if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) {
620 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - unknown "
621 "destination address");
625 wpabuf_free(wpa_s->pending_action_tx);
626 wpa_s->pending_action_tx = NULL;
628 p2p_send_action_cb(wpa_s->global->p2p, wpa_s->pending_action_freq,
629 wpa_s->pending_action_dst,
630 wpa_s->pending_action_src,
631 wpa_s->pending_action_bssid,
634 if (wpa_s->pending_pd_before_join &&
635 (os_memcmp(wpa_s->pending_action_dst, wpa_s->pending_join_dev_addr,
637 os_memcmp(wpa_s->pending_action_dst,
638 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
639 wpa_s->pending_pd_before_join = 0;
640 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
641 "join-existing-group operation");
642 wpas_p2p_join_start(wpa_s);
647 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
648 const u8 *src, const u8 *bssid, const u8 *buf,
649 size_t len, unsigned int wait_time)
651 struct wpa_supplicant *wpa_s = ctx;
653 wpa_printf(MSG_DEBUG, "P2P: Send action frame: freq=%d dst=" MACSTR
654 " src=" MACSTR " bssid=" MACSTR " len=%d",
655 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
658 if (wpa_s->pending_action_tx) {
659 wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
660 "to " MACSTR, MAC2STR(wpa_s->pending_action_dst));
661 wpabuf_free(wpa_s->pending_action_tx);
663 wpa_s->pending_action_tx = wpabuf_alloc(len);
664 if (wpa_s->pending_action_tx == NULL) {
665 wpa_printf(MSG_DEBUG, "P2P: Failed to allocate Action frame "
666 "TX buffer (len=%llu)", (unsigned long long) len);
669 wpabuf_put_data(wpa_s->pending_action_tx, buf, len);
670 os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN);
671 os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN);
672 os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
673 wpa_s->pending_action_freq = freq;
676 struct wpa_supplicant *tx_iface;
677 tx_iface = wpas_get_tx_interface(wpa_s, src);
678 if (tx_iface->assoc_freq == freq) {
679 wpa_printf(MSG_DEBUG, "P2P: Already on requested "
680 "channel (TX interface operating channel)");
685 if (wpa_s->off_channel_freq == freq || freq == 0) {
686 wpa_printf(MSG_DEBUG, "P2P: Already on requested channel; "
687 "send Action frame immediately");
688 /* TODO: Would there ever be need to extend the current
689 * duration on the channel? */
690 wpa_s->pending_action_without_roc = 1;
691 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
692 eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL);
695 wpa_s->pending_action_without_roc = 0;
697 if (wpa_s->roc_waiting_drv_freq == freq) {
698 wpa_printf(MSG_DEBUG, "P2P: Already waiting for driver to get "
699 "to frequency %u MHz; continue waiting to send the "
700 "Action frame", freq);
704 wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
705 "once the driver gets to the requested channel");
706 if (wait_time > wpa_s->max_remain_on_chan)
707 wait_time = wpa_s->max_remain_on_chan;
708 if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) {
709 wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
710 "to remain on channel (%u MHz) for Action "
714 wpa_s->off_channel_freq = 0;
715 wpa_s->roc_waiting_drv_freq = freq;
721 static void wpas_send_action_done(void *ctx)
723 struct wpa_supplicant *wpa_s = ctx;
724 wpa_printf(MSG_DEBUG, "P2P: Action frame sequence done notification");
725 wpabuf_free(wpa_s->pending_action_tx);
726 wpa_s->pending_action_tx = NULL;
727 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
728 wpa_drv_cancel_remain_on_channel(wpa_s);
729 wpa_s->off_channel_freq = 0;
730 wpa_s->roc_waiting_drv_freq = 0;
735 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
736 struct p2p_go_neg_results *params)
738 if (wpa_s->go_params == NULL) {
739 wpa_s->go_params = os_malloc(sizeof(*params));
740 if (wpa_s->go_params == NULL)
743 os_memcpy(wpa_s->go_params, params, sizeof(*params));
748 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
749 struct p2p_go_neg_results *res)
751 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
752 MAC2STR(res->peer_interface_addr));
753 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
754 res->ssid, res->ssid_len);
755 wpa_supplicant_ap_deinit(wpa_s);
756 wpas_copy_go_neg_results(wpa_s, res);
757 if (res->wps_method == WPS_PBC)
758 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
760 u16 dev_pw_id = DEV_PW_DEFAULT;
761 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
762 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
763 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
764 wpa_s->p2p_pin, 1, dev_pw_id);
769 static void p2p_go_configured(void *ctx, void *data)
771 struct wpa_supplicant *wpa_s = ctx;
772 struct p2p_go_neg_results *params = data;
773 struct wpa_ssid *ssid;
775 ssid = wpa_s->current_ssid;
776 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
777 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
778 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
779 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
780 "go_dev_addr=" MACSTR "%s",
782 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
784 params->passphrase ? params->passphrase : "",
785 MAC2STR(wpa_s->parent->own_addr),
786 params->persistent_group ? " [PERSISTENT]" : "");
787 if (params->persistent_group)
788 wpas_p2p_store_persistent_group(
790 wpa_s->parent->own_addr);
791 wpas_p2p_cross_connect_setup(wpa_s);
792 wpas_p2p_set_group_idle_timeout(wpa_s);
796 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
797 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
798 params->peer_interface_addr)) {
799 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
803 if (params->wps_method == WPS_PBC)
804 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr);
805 else if (wpa_s->p2p_pin[0])
806 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
807 wpa_s->p2p_pin, NULL, 0);
808 os_free(wpa_s->go_params);
809 wpa_s->go_params = NULL;
813 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
814 struct p2p_go_neg_results *params,
817 struct wpa_ssid *ssid;
819 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
822 ssid = wpa_config_add_network(wpa_s->conf);
826 wpa_config_set_network_defaults(ssid);
829 ssid->p2p_persistent_group = params->persistent_group;
830 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
832 ssid->frequency = params->freq;
833 ssid->ssid = os_zalloc(params->ssid_len + 1);
835 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
836 ssid->ssid_len = params->ssid_len;
838 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
839 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
840 ssid->proto = WPA_PROTO_RSN;
841 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
842 ssid->passphrase = os_strdup(params->passphrase);
844 wpa_s->ap_configured_cb = p2p_go_configured;
845 wpa_s->ap_configured_cb_ctx = wpa_s;
846 wpa_s->ap_configured_cb_data = wpa_s->go_params;
847 wpa_s->connect_without_scan = 1;
848 wpa_s->reassociate = 1;
849 wpa_s->disconnected = 0;
850 wpa_supplicant_req_scan(wpa_s, 0, 0);
854 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
855 const struct wpa_supplicant *src)
857 struct wpa_config *d;
858 const struct wpa_config *s;
863 #define C(n) if (s->n) d->n = os_strdup(s->n)
873 d->p2p_group_idle = s->p2p_group_idle;
874 d->p2p_intra_bss = s->p2p_intra_bss;
878 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
879 enum wpa_driver_if_type type)
881 char ifname[120], force_ifname[120];
883 if (wpa_s->pending_interface_name[0]) {
884 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
885 "- skip creation of a new one");
886 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
887 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
888 "unknown?! ifname='%s'",
889 wpa_s->pending_interface_name);
895 os_snprintf(ifname, sizeof(ifname), "%s-p2p-%d", wpa_s->ifname,
896 wpa_s->p2p_group_idx);
897 force_ifname[0] = '\0';
899 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
901 wpa_s->p2p_group_idx++;
903 wpa_s->pending_interface_type = type;
904 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
905 wpa_s->pending_interface_addr) < 0) {
906 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
911 if (force_ifname[0]) {
912 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
914 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
915 sizeof(wpa_s->pending_interface_name));
917 os_strlcpy(wpa_s->pending_interface_name, ifname,
918 sizeof(wpa_s->pending_interface_name));
919 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
920 MACSTR, wpa_s->pending_interface_name,
921 MAC2STR(wpa_s->pending_interface_addr));
927 static void wpas_p2p_remove_pending_group_interface(
928 struct wpa_supplicant *wpa_s)
930 if (!wpa_s->pending_interface_name[0] ||
931 is_zero_ether_addr(wpa_s->pending_interface_addr))
932 return; /* No pending virtual interface */
934 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
935 wpa_s->pending_interface_name);
936 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
937 wpa_s->pending_interface_name);
938 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
939 wpa_s->pending_interface_name[0] = '\0';
943 static struct wpa_supplicant *
944 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
946 struct wpa_interface iface;
947 struct wpa_supplicant *group_wpa_s;
949 if (!wpa_s->pending_interface_name[0]) {
950 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
951 if (!wpas_p2p_create_iface(wpa_s))
954 * Something has forced us to remove the pending interface; try
955 * to create a new one and hope for the best that we will get
956 * the same local address.
958 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
959 WPA_IF_P2P_CLIENT) < 0)
963 os_memset(&iface, 0, sizeof(iface));
964 iface.ifname = wpa_s->pending_interface_name;
965 iface.driver = wpa_s->driver->name;
966 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
967 iface.driver_param = wpa_s->conf->driver_param;
968 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
969 if (group_wpa_s == NULL) {
970 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
971 "wpa_supplicant interface");
974 wpa_s->pending_interface_name[0] = '\0';
975 group_wpa_s->parent = wpa_s;
976 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
977 P2P_GROUP_INTERFACE_CLIENT;
978 wpa_s->global->p2p_group_formation = group_wpa_s;
980 wpas_p2p_clone_config(group_wpa_s, wpa_s);
986 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
989 struct wpa_supplicant *wpa_s = eloop_ctx;
990 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
991 if (wpa_s->global->p2p)
992 p2p_group_formation_failed(wpa_s->global->p2p);
993 wpas_group_formation_completed(wpa_s, 0);
997 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
999 struct wpa_supplicant *wpa_s = ctx;
1001 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1002 wpa_drv_cancel_remain_on_channel(wpa_s);
1003 wpa_s->off_channel_freq = 0;
1004 wpa_s->roc_waiting_drv_freq = 0;
1008 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1010 wpas_p2p_remove_pending_group_interface(wpa_s);
1014 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1016 if (wpa_s->create_p2p_iface) {
1017 struct wpa_supplicant *group_wpa_s =
1018 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1019 if (group_wpa_s == NULL) {
1020 wpas_p2p_remove_pending_group_interface(wpa_s);
1023 if (group_wpa_s != wpa_s) {
1024 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1025 sizeof(group_wpa_s->p2p_pin));
1026 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1028 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1029 wpa_s->pending_interface_name[0] = '\0';
1030 group_wpa_s->p2p_in_provisioning = 1;
1033 wpas_start_wps_go(group_wpa_s, res, 1);
1035 wpas_start_wps_enrollee(group_wpa_s, res);
1037 wpa_s->p2p_in_provisioning = 1;
1038 wpa_s->global->p2p_group_formation = wpa_s;
1041 wpas_start_wps_go(wpa_s, res, 1);
1043 wpas_start_wps_enrollee(ctx, res);
1046 wpa_s->p2p_long_listen = 0;
1047 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1049 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1050 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1051 (res->peer_config_timeout % 100) * 10000,
1052 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1056 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1058 struct wpa_supplicant *wpa_s = ctx;
1059 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1060 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1064 void wpas_dev_found(void *ctx, const u8 *addr, const u8 *dev_addr,
1065 const u8 *pri_dev_type, const char *dev_name,
1066 u16 config_methods, u8 dev_capab, u8 group_capab)
1068 struct wpa_supplicant *wpa_s = ctx;
1069 char devtype[WPS_DEV_TYPE_BUFSIZE];
1070 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1071 " p2p_dev_addr=" MACSTR
1072 " pri_dev_type=%s name='%s' config_methods=0x%x "
1073 "dev_capab=0x%x group_capab=0x%x",
1074 MAC2STR(addr), MAC2STR(dev_addr),
1075 wps_dev_type_bin2str(pri_dev_type, devtype, sizeof(devtype)),
1076 dev_name, config_methods, dev_capab, group_capab);
1080 static int wpas_start_listen(void *ctx, unsigned int freq,
1081 unsigned int duration,
1082 const struct wpabuf *probe_resp_ie)
1084 struct wpa_supplicant *wpa_s = ctx;
1086 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1088 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1089 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1090 "report received Probe Request frames");
1094 wpa_s->pending_listen_freq = freq;
1095 wpa_s->pending_listen_duration = duration;
1097 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1098 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1099 "to remain on channel (%u MHz) for Listen "
1101 wpa_s->pending_listen_freq = 0;
1104 wpa_s->off_channel_freq = 0;
1105 wpa_s->roc_waiting_drv_freq = freq;
1111 static void wpas_stop_listen(void *ctx)
1113 struct wpa_supplicant *wpa_s = ctx;
1114 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1115 wpa_drv_cancel_remain_on_channel(wpa_s);
1116 wpa_s->off_channel_freq = 0;
1117 wpa_s->roc_waiting_drv_freq = 0;
1119 wpa_drv_probe_req_report(wpa_s, 0);
1123 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1125 struct wpa_supplicant *wpa_s = ctx;
1126 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1130 static struct p2p_srv_bonjour *
1131 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1132 const struct wpabuf *query)
1134 struct p2p_srv_bonjour *bsrv;
1137 len = wpabuf_len(query);
1138 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1139 struct p2p_srv_bonjour, list) {
1140 if (len == wpabuf_len(bsrv->query) &&
1141 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1149 static struct p2p_srv_upnp *
1150 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1151 const char *service)
1153 struct p2p_srv_upnp *usrv;
1155 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1156 struct p2p_srv_upnp, list) {
1157 if (version == usrv->version &&
1158 os_strcmp(service, usrv->service) == 0)
1165 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1170 if (wpabuf_tailroom(resp) < 5)
1173 /* Length (to be filled) */
1174 len_pos = wpabuf_put(resp, 2);
1175 wpabuf_put_u8(resp, srv_proto);
1176 wpabuf_put_u8(resp, srv_trans_id);
1178 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1179 /* Response Data: empty */
1180 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1184 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1185 struct wpabuf *resp, u8 srv_trans_id)
1187 struct p2p_srv_bonjour *bsrv;
1190 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1192 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1193 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1197 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1198 struct p2p_srv_bonjour, list) {
1199 if (wpabuf_tailroom(resp) <
1200 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1202 /* Length (to be filled) */
1203 len_pos = wpabuf_put(resp, 2);
1204 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1205 wpabuf_put_u8(resp, srv_trans_id);
1207 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1208 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1209 wpabuf_head(bsrv->resp),
1210 wpabuf_len(bsrv->resp));
1212 wpabuf_put_buf(resp, bsrv->query); /* Key */
1213 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1214 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1220 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1221 struct wpabuf *resp, u8 srv_trans_id,
1222 const u8 *query, size_t query_len)
1224 struct p2p_srv_bonjour *bsrv;
1228 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1230 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1231 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1232 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1237 if (query_len == 0) {
1238 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1242 if (wpabuf_tailroom(resp) < 5)
1244 /* Length (to be filled) */
1245 len_pos = wpabuf_put(resp, 2);
1246 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1247 wpabuf_put_u8(resp, srv_trans_id);
1249 wpabuf_set(&buf, query, query_len);
1250 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1252 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1256 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1257 /* Response Data: empty */
1258 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1264 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1265 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1266 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1268 if (wpabuf_tailroom(resp) >=
1269 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1271 wpabuf_put_buf(resp, bsrv->query); /* Key */
1272 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1274 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1278 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1279 struct wpabuf *resp, u8 srv_trans_id)
1281 struct p2p_srv_upnp *usrv;
1284 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1286 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1287 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1291 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1292 struct p2p_srv_upnp, list) {
1293 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1296 /* Length (to be filled) */
1297 len_pos = wpabuf_put(resp, 2);
1298 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1299 wpabuf_put_u8(resp, srv_trans_id);
1302 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1304 wpabuf_put_u8(resp, usrv->version);
1305 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1307 wpabuf_put_str(resp, usrv->service);
1308 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1314 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1315 struct wpabuf *resp, u8 srv_trans_id,
1316 const u8 *query, size_t query_len)
1318 struct p2p_srv_upnp *usrv;
1324 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1327 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1328 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1329 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1334 if (query_len == 0) {
1335 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1339 if (wpabuf_tailroom(resp) < 5)
1342 /* Length (to be filled) */
1343 len_pos = wpabuf_put(resp, 2);
1344 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1345 wpabuf_put_u8(resp, srv_trans_id);
1348 str = os_malloc(query_len);
1351 os_memcpy(str, query + 1, query_len - 1);
1352 str[query_len - 1] = '\0';
1354 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1355 struct p2p_srv_upnp, list) {
1356 if (version != usrv->version)
1359 if (os_strcmp(str, "ssdp:all") != 0 &&
1360 os_strstr(usrv->service, str) == NULL)
1363 if (wpabuf_tailroom(resp) < 2)
1367 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1369 wpabuf_put_u8(resp, version);
1371 wpabuf_put_u8(resp, ',');
1375 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1377 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1379 wpabuf_put_str(resp, usrv->service);
1383 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1386 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1387 /* Response Data: empty */
1390 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1394 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1395 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1397 struct wpa_supplicant *wpa_s = ctx;
1398 const u8 *pos = tlvs;
1399 const u8 *end = tlvs + tlvs_len;
1402 struct wpabuf *resp;
1403 u8 srv_proto, srv_trans_id;
1407 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1409 buf_len = 2 * tlvs_len + 1;
1410 buf = os_malloc(buf_len);
1412 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1413 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1415 freq, MAC2STR(sa), dialog_token, update_indic,
1420 if (wpa_s->p2p_sd_over_ctrl_iface)
1421 return; /* to be processed by an external program */
1423 resp = wpabuf_alloc(10000);
1427 while (pos + 1 < end) {
1428 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1429 slen = WPA_GET_LE16(pos);
1431 if (pos + slen > end || slen < 2) {
1432 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1437 tlv_end = pos + slen;
1440 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1442 srv_trans_id = *pos++;
1443 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1446 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1447 pos, tlv_end - pos);
1450 if (wpa_s->force_long_sd) {
1451 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1453 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1454 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1458 switch (srv_proto) {
1459 case P2P_SERV_ALL_SERVICES:
1460 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1461 "for all services");
1462 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1463 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1464 wpa_printf(MSG_DEBUG, "P2P: No service "
1465 "discovery protocols available");
1466 wpas_sd_add_proto_not_avail(
1467 resp, P2P_SERV_ALL_SERVICES,
1471 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1472 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1474 case P2P_SERV_BONJOUR:
1475 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1476 pos, tlv_end - pos);
1479 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1480 pos, tlv_end - pos);
1483 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1484 "protocol %u", srv_proto);
1485 wpas_sd_add_proto_not_avail(resp, srv_proto,
1494 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1500 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1501 const u8 *tlvs, size_t tlvs_len)
1503 struct wpa_supplicant *wpa_s = ctx;
1504 const u8 *pos = tlvs;
1505 const u8 *end = tlvs + tlvs_len;
1511 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1513 if (tlvs_len > 1500) {
1514 /* TODO: better way for handling this */
1515 wpa_msg_ctrl(wpa_s, MSG_INFO,
1516 P2P_EVENT_SERV_DISC_RESP MACSTR
1517 " %u <long response: %u bytes>",
1518 MAC2STR(sa), update_indic,
1519 (unsigned int) tlvs_len);
1521 buf_len = 2 * tlvs_len + 1;
1522 buf = os_malloc(buf_len);
1524 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1525 wpa_msg_ctrl(wpa_s, MSG_INFO,
1526 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1527 MAC2STR(sa), update_indic, buf);
1533 u8 srv_proto, srv_trans_id, status;
1535 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1536 slen = WPA_GET_LE16(pos);
1538 if (pos + slen > end || slen < 3) {
1539 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1543 tlv_end = pos + slen;
1546 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1548 srv_trans_id = *pos++;
1549 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1552 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1555 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1556 pos, tlv_end - pos);
1563 void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1564 const struct wpabuf *tlvs)
1566 return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1570 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1571 u8 version, const char *query)
1573 struct wpabuf *tlvs;
1576 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1579 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1580 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1581 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1582 wpabuf_put_u8(tlvs, version);
1583 wpabuf_put_str(tlvs, query);
1584 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1590 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1592 return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1596 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1597 const u8 *dst, u8 dialog_token,
1598 const struct wpabuf *resp_tlvs)
1600 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1605 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1607 p2p_sd_service_update(wpa_s->global->p2p);
1611 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1613 dl_list_del(&bsrv->list);
1614 wpabuf_free(bsrv->query);
1615 wpabuf_free(bsrv->resp);
1620 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1622 dl_list_del(&usrv->list);
1623 os_free(usrv->service);
1628 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1630 struct p2p_srv_bonjour *bsrv, *bn;
1631 struct p2p_srv_upnp *usrv, *un;
1633 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1634 struct p2p_srv_bonjour, list)
1635 wpas_p2p_srv_bonjour_free(bsrv);
1637 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1638 struct p2p_srv_upnp, list)
1639 wpas_p2p_srv_upnp_free(usrv);
1641 wpas_p2p_sd_service_update(wpa_s);
1645 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1646 struct wpabuf *query, struct wpabuf *resp)
1648 struct p2p_srv_bonjour *bsrv;
1650 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1653 wpabuf_free(bsrv->resp);
1658 bsrv = os_zalloc(sizeof(*bsrv));
1661 bsrv->query = query;
1663 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1665 wpas_p2p_sd_service_update(wpa_s);
1670 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1671 const struct wpabuf *query)
1673 struct p2p_srv_bonjour *bsrv;
1675 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1678 wpas_p2p_srv_bonjour_free(bsrv);
1679 wpas_p2p_sd_service_update(wpa_s);
1684 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1685 const char *service)
1687 struct p2p_srv_upnp *usrv;
1689 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1690 return 0; /* Already listed */
1691 usrv = os_zalloc(sizeof(*usrv));
1694 usrv->version = version;
1695 usrv->service = os_strdup(service);
1696 if (usrv->service == NULL) {
1700 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1702 wpas_p2p_sd_service_update(wpa_s);
1707 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1708 const char *service)
1710 struct p2p_srv_upnp *usrv;
1712 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1715 wpas_p2p_srv_upnp_free(usrv);
1716 wpas_p2p_sd_service_update(wpa_s);
1721 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1722 const u8 *peer, const char *params)
1724 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1725 MAC2STR(peer), wps_generate_pin(), params);
1729 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1730 const u8 *peer, const char *params)
1732 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1733 MAC2STR(peer), params);
1737 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1738 const u8 *dev_addr, const u8 *pri_dev_type,
1739 const char *dev_name, u16 supp_config_methods,
1740 u8 dev_capab, u8 group_capab)
1742 struct wpa_supplicant *wpa_s = ctx;
1743 char devtype[WPS_DEV_TYPE_BUFSIZE];
1745 u8 empty_dev_type[8];
1747 if (pri_dev_type == NULL) {
1748 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1749 pri_dev_type = empty_dev_type;
1751 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1752 " pri_dev_type=%s name='%s' config_methods=0x%x "
1753 "dev_capab=0x%x group_capab=0x%x",
1755 wps_dev_type_bin2str(pri_dev_type, devtype,
1757 dev_name, supp_config_methods, dev_capab, group_capab);
1758 params[sizeof(params) - 1] = '\0';
1760 if (config_methods & WPS_CONFIG_DISPLAY)
1761 wpas_prov_disc_local_display(wpa_s, peer, params);
1762 else if (config_methods & WPS_CONFIG_KEYPAD)
1763 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1764 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1765 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1766 "%s", MAC2STR(peer), params);
1770 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1772 struct wpa_supplicant *wpa_s = ctx;
1773 if (config_methods & WPS_CONFIG_DISPLAY)
1774 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1775 else if (config_methods & WPS_CONFIG_KEYPAD)
1776 wpas_prov_disc_local_display(wpa_s, peer, "");
1777 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1778 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1781 if (wpa_s->pending_pd_before_join &&
1782 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1783 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1784 wpa_s->pending_pd_before_join = 0;
1785 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1786 "join-existing-group operation");
1787 wpas_p2p_join_start(wpa_s);
1792 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1793 const u8 *go_dev_addr, const u8 *ssid,
1794 size_t ssid_len, int *go, u8 *group_bssid,
1795 int *force_freq, int persistent_group)
1797 struct wpa_supplicant *wpa_s = ctx;
1799 u8 cur_bssid[ETH_ALEN];
1802 if (!persistent_group) {
1803 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1804 " to join an active group", MAC2STR(sa));
1805 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1806 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1808 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1809 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1810 "authorized invitation");
1814 * Do not accept the invitation automatically; notify user and
1817 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1820 if (!wpa_s->conf->persistent_reconnect)
1821 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1823 for (s = wpa_s->conf->ssid; s; s = s->next) {
1824 if (s->disabled == 2 &&
1825 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1826 s->ssid_len == ssid_len &&
1827 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1832 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1833 " requested reinvocation of an unknown group",
1835 return P2P_SC_FAIL_UNKNOWN_GROUP;
1838 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1840 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1841 wpa_printf(MSG_DEBUG, "P2P: The only available "
1842 "interface is already in use - reject "
1844 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1846 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1847 } else if (s->mode == WPAS_MODE_P2P_GO) {
1849 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1851 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1852 "interface address for the group");
1853 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1855 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1860 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1861 wpa_s->assoc_freq) {
1862 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1863 "the channel we are already using");
1864 *force_freq = wpa_s->assoc_freq;
1867 res = wpa_drv_shared_freq(wpa_s);
1869 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1870 "with the channel we are already using on a "
1871 "shared interface");
1875 return P2P_SC_SUCCESS;
1879 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
1880 const u8 *ssid, size_t ssid_len,
1881 const u8 *go_dev_addr, u8 status,
1884 struct wpa_supplicant *wpa_s = ctx;
1887 for (s = wpa_s->conf->ssid; s; s = s->next) {
1888 if (s->disabled == 2 &&
1889 s->ssid_len == ssid_len &&
1890 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1894 if (status == P2P_SC_SUCCESS) {
1895 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1896 " was accepted; op_freq=%d MHz",
1897 MAC2STR(sa), op_freq);
1899 wpas_p2p_group_add_persistent(
1900 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
1902 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
1903 wpa_s->p2p_wps_method);
1908 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1909 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1910 " was rejected (status %u)", MAC2STR(sa), status);
1916 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1917 "sa=" MACSTR " go_dev_addr=" MACSTR
1918 " bssid=" MACSTR " unknown-network",
1919 MAC2STR(sa), MAC2STR(go_dev_addr),
1922 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1923 "sa=" MACSTR " go_dev_addr=" MACSTR
1925 MAC2STR(sa), MAC2STR(go_dev_addr));
1930 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
1931 " persistent=%d", MAC2STR(sa), s->id);
1935 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
1937 struct wpa_supplicant *wpa_s = ctx;
1938 struct wpa_ssid *ssid;
1941 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1942 "status=%d " MACSTR,
1943 status, MAC2STR(bssid));
1945 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1946 "status=%d ", status);
1949 if (wpa_s->pending_invite_ssid_id == -1)
1950 return; /* Invitation to active group */
1952 if (status != P2P_SC_SUCCESS) {
1953 wpas_p2p_remove_pending_group_interface(wpa_s);
1957 ssid = wpa_config_get_network(wpa_s->conf,
1958 wpa_s->pending_invite_ssid_id);
1960 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
1961 "data matching with invitation");
1965 wpas_p2p_group_add_persistent(wpa_s, ssid,
1966 ssid->mode == WPAS_MODE_P2P_GO, 0);
1970 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
1971 struct p2p_channels *chan)
1975 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
1978 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1979 chan->reg_class[cla].reg_class = 81;
1980 chan->reg_class[cla].channels = 11;
1981 for (i = 0; i < 11; i++)
1982 chan->reg_class[cla].channel[i] = i + 1;
1985 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
1988 /* Operating class 115 - 5 GHz, channels 36-48 */
1989 chan->reg_class[cla].reg_class = 115;
1990 chan->reg_class[cla].channels = 4;
1991 chan->reg_class[cla].channel[0] = 36;
1992 chan->reg_class[cla].channel[1] = 40;
1993 chan->reg_class[cla].channel[2] = 44;
1994 chan->reg_class[cla].channel[3] = 48;
1997 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2000 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2001 chan->reg_class[cla].reg_class = 124;
2002 chan->reg_class[cla].channels = 4;
2003 chan->reg_class[cla].channel[0] = 149;
2004 chan->reg_class[cla].channel[1] = 153;
2005 chan->reg_class[cla].channel[2] = 157;
2006 chan->reg_class[cla].channel[3] = 161;
2009 chan->reg_classes = cla;
2014 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2016 enum hostapd_hw_mode mode)
2020 for (i = 0; i < num_modes; i++) {
2021 if (modes[i].mode == mode)
2029 static int has_channel(struct hostapd_hw_modes *mode, u8 chan)
2033 for (i = 0; i < mode->num_channels; i++) {
2034 if (mode->channels[i].chan == chan) {
2035 return !(mode->channels[i].flag &
2036 (HOSTAPD_CHAN_DISABLED |
2037 HOSTAPD_CHAN_PASSIVE_SCAN |
2038 HOSTAPD_CHAN_NO_IBSS |
2039 HOSTAPD_CHAN_RADAR));
2047 struct p2p_oper_class_map {
2048 enum hostapd_hw_mode mode;
2055 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2056 struct p2p_channels *chan)
2058 struct hostapd_hw_modes *modes, *mode;
2059 u16 num_modes, flags;
2061 struct p2p_oper_class_map op_class[] = {
2062 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1 },
2063 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1 },
2064 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4 },
2065 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4 },
2066 #if 0 /* TODO: 40 MHz channels */
2067 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1 },
2068 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1 },
2069 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8 },
2070 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8 },
2071 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8 },
2072 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8 },
2077 modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
2078 if (modes == NULL) {
2079 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2080 "of all supported channels; assume dualband "
2082 return wpas_p2p_default_channels(wpa_s, chan);
2087 for (op = 0; op_class[op].op_class; op++) {
2088 struct p2p_oper_class_map *o = &op_class[op];
2090 struct p2p_reg_class *reg = NULL;
2092 mode = get_mode(modes, num_modes, o->mode);
2095 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2096 if (!has_channel(mode, ch))
2099 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2100 "class %u", o->op_class);
2101 reg = &chan->reg_class[cla];
2103 reg->reg_class = o->op_class;
2105 reg->channel[reg->channels] = ch;
2109 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2110 reg->channel, reg->channels);
2114 chan->reg_classes = cla;
2116 ieee80211_sta_free_hw_features(modes, num_modes);
2122 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2125 struct wpa_supplicant *wpa_s = ctx;
2127 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2128 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2134 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2139 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2140 * @global: Pointer to global data from wpa_supplicant_init()
2141 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2142 * Returns: 0 on success, -1 on failure
2144 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2146 struct p2p_config p2p;
2150 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2153 #ifdef CONFIG_CLIENT_MLME
2154 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
2155 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
2156 wpa_s->mlme.public_action_cb_ctx = wpa_s;
2158 #endif /* CONFIG_CLIENT_MLME */
2160 if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
2161 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
2162 /* Continue anyway; this is not really a fatal error */
2168 os_memset(&p2p, 0, sizeof(p2p));
2169 p2p.msg_ctx = wpa_s;
2171 p2p.p2p_scan = wpas_p2p_scan;
2172 p2p.send_action = wpas_send_action;
2173 p2p.send_action_done = wpas_send_action_done;
2174 p2p.go_neg_completed = wpas_go_neg_completed;
2175 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2176 p2p.dev_found = wpas_dev_found;
2177 p2p.start_listen = wpas_start_listen;
2178 p2p.stop_listen = wpas_stop_listen;
2179 p2p.send_probe_resp = wpas_send_probe_resp;
2180 p2p.sd_request = wpas_sd_request;
2181 p2p.sd_response = wpas_sd_response;
2182 p2p.prov_disc_req = wpas_prov_disc_req;
2183 p2p.prov_disc_resp = wpas_prov_disc_resp;
2184 p2p.invitation_process = wpas_invitation_process;
2185 p2p.invitation_received = wpas_invitation_received;
2186 p2p.invitation_result = wpas_invitation_result;
2187 p2p.get_noa = wpas_get_noa;
2189 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2190 os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2191 p2p.dev_name = wpa_s->conf->device_name;
2193 if (wpa_s->conf->p2p_listen_reg_class &&
2194 wpa_s->conf->p2p_listen_channel) {
2195 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2196 p2p.channel = wpa_s->conf->p2p_listen_channel;
2200 * Pick one of the social channels randomly as the listen
2203 os_get_random((u8 *) &r, sizeof(r));
2204 p2p.channel = 1 + (r % 3) * 5;
2207 if (wpa_s->conf->p2p_oper_reg_class &&
2208 wpa_s->conf->p2p_oper_channel) {
2209 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2210 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2212 p2p.op_reg_class = 81;
2214 * For initial tests, pick the operation channel randomly.
2215 * TODO: Use scan results (etc.) to select the best channel.
2217 os_get_random((u8 *) &r, sizeof(r));
2218 p2p.op_channel = 1 + r % 11;
2220 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d "
2221 "Own preferred operation channel: %d",
2222 p2p.channel, p2p.op_channel);
2223 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2224 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2225 p2p.country[2] = 0x04;
2227 os_memcpy(p2p.country, "US\x04", 3);
2229 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2230 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2235 if (wpa_s->conf->device_type &&
2236 wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) <
2238 wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
2242 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
2243 if (wpa_s->conf->sec_device_type[i] == NULL)
2245 if (wps_dev_type_str2bin(
2246 wpa_s->conf->sec_device_type[i],
2247 p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) {
2248 wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type");
2251 p2p.num_sec_dev_types++;
2252 if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES)
2256 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2257 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2259 p2p.max_peers = 100;
2261 if (wpa_s->conf->p2p_ssid_postfix) {
2262 p2p.ssid_postfix_len =
2263 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2264 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2265 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2266 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2267 p2p.ssid_postfix_len);
2270 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2272 global->p2p = p2p_init(&p2p);
2273 if (global->p2p == NULL)
2281 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2282 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2284 * This function deinitialize per-interface P2P data.
2286 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2288 if (wpa_s->driver && wpa_s->drv_priv)
2289 wpa_drv_probe_req_report(wpa_s, 0);
2290 os_free(wpa_s->go_params);
2291 wpa_s->go_params = NULL;
2292 wpabuf_free(wpa_s->pending_action_tx);
2293 wpa_s->pending_action_tx = NULL;
2294 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2295 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2296 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2297 wpa_s->p2p_long_listen = 0;
2298 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2299 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2300 wpas_p2p_remove_pending_group_interface(wpa_s);
2302 /* TODO: remove group interface from the driver if this wpa_s instance
2303 * is on top of a P2P group interface */
2308 * wpas_p2p_deinit_global - Deinitialize global P2P module
2309 * @global: Pointer to global data from wpa_supplicant_init()
2311 * This function deinitializes the global (per device) P2P module.
2313 void wpas_p2p_deinit_global(struct wpa_global *global)
2315 struct wpa_supplicant *wpa_s, *tmp;
2318 if (global->p2p == NULL)
2321 /* Remove remaining P2P group interfaces */
2322 wpa_s = global->ifaces;
2324 wpas_p2p_service_flush(wpa_s);
2325 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2326 wpa_s = wpa_s->next;
2328 enum wpa_driver_if_type type;
2329 tmp = global->ifaces;
2332 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2337 ifname = os_strdup(tmp->ifname);
2338 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2339 wpa_supplicant_remove_iface(global, tmp);
2341 wpa_drv_if_remove(wpa_s, type, ifname);
2346 * Deinit GO data on any possibly remaining interface (if main
2347 * interface is used as GO).
2349 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2350 if (wpa_s->ap_iface)
2351 wpas_p2p_group_deinit(wpa_s);
2354 p2p_deinit(global->p2p);
2359 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2361 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
2362 return 1; /* P2P group requires a new interface in every case
2364 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2365 return 0; /* driver does not support concurrent operations */
2366 if (wpa_s->global->ifaces->next)
2367 return 1; /* more that one interface already in use */
2368 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2369 return 1; /* this interface is already in use */
2374 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2375 const u8 *peer_addr,
2376 enum p2p_wps_method wps_method,
2377 int go_intent, const u8 *own_interface_addr,
2378 unsigned int force_freq, int persistent_group)
2380 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2381 go_intent, own_interface_addr, force_freq,
2386 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2387 const u8 *peer_addr,
2388 enum p2p_wps_method wps_method,
2389 int go_intent, const u8 *own_interface_addr,
2390 unsigned int force_freq, int persistent_group)
2392 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2393 go_intent, own_interface_addr, force_freq,
2398 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2399 struct wpa_scan_results *scan_res)
2401 struct wpa_bss *bss;
2404 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2406 if (wpa_s->global->p2p_disabled)
2409 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
2410 scan_res ? (int) scan_res->num : -1);
2413 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2415 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2416 wpa_s->pending_join_iface_addr);
2418 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2419 "from P2P peer table: %d MHz", freq);
2421 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2424 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2425 "from BSS table: %d MHz", freq);
2430 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2431 "prior to joining an existing group (GO " MACSTR
2433 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2434 wpa_s->pending_pd_before_join = 1;
2436 switch (wpa_s->pending_join_wps_method) {
2438 case WPS_PIN_DISPLAY:
2439 method = WPS_CONFIG_KEYPAD;
2441 case WPS_PIN_KEYPAD:
2442 method = WPS_CONFIG_DISPLAY;
2445 method = WPS_CONFIG_PUSHBUTTON;
2452 if (p2p_prov_disc_req(wpa_s->global->p2p,
2453 wpa_s->pending_join_dev_addr, method, 1)
2455 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2456 "Discovery Request before joining an "
2458 wpa_s->pending_pd_before_join = 0;
2463 * Actual join operation will be started from the Action frame
2464 * TX status callback.
2469 wpa_printf(MSG_DEBUG, "P2P: Target BSS/GO not yet in BSS table - "
2470 "cannot send Provision Discovery Request");
2473 /* Start join operation immediately */
2474 wpas_p2p_join_start(wpa_s);
2478 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2480 struct wpa_supplicant *wpa_s = eloop_ctx;
2482 struct wpa_driver_scan_params params;
2483 struct wpabuf *wps_ie, *ies;
2485 os_memset(¶ms, 0, sizeof(params));
2487 /* P2P Wildcard SSID */
2488 params.num_ssids = 1;
2489 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2490 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2492 wpa_s->wps->dev.p2p = 1;
2493 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2495 if (wps_ie == NULL) {
2496 wpas_p2p_scan_res_join(wpa_s, NULL);
2500 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
2502 wpabuf_free(wps_ie);
2503 wpas_p2p_scan_res_join(wpa_s, NULL);
2506 wpabuf_put_buf(ies, wps_ie);
2507 wpabuf_free(wps_ie);
2509 p2p_scan_ie(wpa_s->global->p2p, ies);
2511 params.extra_ies = wpabuf_head(ies);
2512 params.extra_ies_len = wpabuf_len(ies);
2515 * Run a scan to update BSS table and start Provision Discovery once
2516 * the new scan results become available.
2518 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2519 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
2520 ret = ieee80211_sta_req_scan(wpa_s, ¶ms);
2522 ret = wpa_drv_scan(wpa_s, ¶ms);
2527 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2529 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2530 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2535 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2536 const u8 *dev_addr, enum p2p_wps_method wps_method)
2538 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2539 MACSTR " dev " MACSTR ")",
2540 MAC2STR(iface_addr), MAC2STR(dev_addr));
2542 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2543 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2544 wpa_s->pending_join_wps_method = wps_method;
2546 /* Make sure we are not running find during connection establishment */
2547 wpas_p2p_stop_find(wpa_s);
2549 wpas_p2p_join_scan(wpa_s, NULL);
2554 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2556 struct wpa_supplicant *group;
2557 struct p2p_go_neg_results res;
2559 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2562 if (group != wpa_s) {
2563 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2564 sizeof(group->p2p_pin));
2565 group->p2p_wps_method = wpa_s->p2p_wps_method;
2568 group->p2p_in_provisioning = 1;
2570 os_memset(&res, 0, sizeof(res));
2571 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2573 res.wps_method = wpa_s->pending_join_wps_method;
2574 wpas_start_wps_enrollee(group, &res);
2577 * Allow a longer timeout for join-a-running-group than normal 15
2578 * second group formation timeout since the GO may not have authorized
2579 * our connection yet.
2581 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2582 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
2590 * wpas_p2p_connect - Request P2P Group Formation to be started
2591 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2592 * @peer_addr: Address of the peer P2P Device
2593 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2594 * @persistent_group: Whether to create a persistent group
2595 * @join: Whether to join an existing group (as a client) instead of starting
2596 * Group Owner negotiation; @peer_addr is BSSID in that case
2597 * @auth: Whether to only authorize the connection instead of doing that and
2598 * initiating Group Owner negotiation
2599 * @go_intent: GO Intent or -1 to use default
2600 * @freq: Frequency for the group or 0 for auto-selection
2601 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2602 * failure, -2 on failure due to channel not currently available,
2603 * -3 if forced channel is not supported
2605 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2606 const char *pin, enum p2p_wps_method wps_method,
2607 int persistent_group, int join, int auth, int go_intent,
2610 int force_freq = 0, oper_freq = 0;
2613 enum wpa_driver_if_type iftype;
2616 go_intent = wpa_s->conf->p2p_go_intent;
2619 wpa_s->p2p_long_listen = 0;
2621 wpa_s->p2p_wps_method = wps_method;
2624 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2625 else if (wps_method == WPS_PIN_DISPLAY) {
2626 ret = wps_generate_pin();
2627 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2629 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2632 wpa_s->p2p_pin[0] = '\0';
2635 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
2637 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
2638 "connect a running group from " MACSTR,
2639 MAC2STR(peer_addr));
2640 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
2643 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
2644 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2646 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2647 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
2650 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
2656 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2658 oper_freq = wpa_s->assoc_freq;
2660 oper_freq = wpa_drv_shared_freq(wpa_s);
2666 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
2667 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
2668 "(%u MHz) is not supported for P2P uses",
2673 if (oper_freq > 0 && freq != oper_freq &&
2674 !(wpa_s->drv_flags &
2675 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2676 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2677 "on %u MHz while connected on another "
2678 "channel (%u MHz)", freq, oper_freq);
2681 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2682 "requested channel (%u MHz)", freq);
2684 } else if (oper_freq > 0 &&
2685 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
2686 if (!(wpa_s->drv_flags &
2687 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2688 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2689 "while connected on non-P2P supported "
2690 "channel (%u MHz)", oper_freq);
2693 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
2694 "(%u MHz) not available for P2P - try to use "
2695 "another channel", oper_freq);
2697 } else if (oper_freq > 0) {
2698 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2699 "channel we are already using (%u MHz) on another "
2700 "interface", oper_freq);
2701 force_freq = oper_freq;
2704 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2706 if (!wpa_s->create_p2p_iface) {
2708 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2709 go_intent, wpa_s->own_addr,
2710 force_freq, persistent_group)
2715 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
2716 go_intent, wpa_s->own_addr,
2717 force_freq, persistent_group) < 0)
2722 /* Prepare to add a new interface for the group */
2723 iftype = WPA_IF_P2P_GROUP;
2725 iftype = WPA_IF_P2P_CLIENT;
2726 else if (go_intent == 15)
2727 iftype = WPA_IF_P2P_GO;
2728 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
2729 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2730 "interface for the group");
2735 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2737 wpa_s->pending_interface_addr,
2738 force_freq, persistent_group) < 0)
2742 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
2743 wpa_s->pending_interface_addr,
2744 force_freq, persistent_group) < 0) {
2745 wpas_p2p_remove_pending_group_interface(wpa_s);
2753 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2754 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2755 * @freq: Frequency of the channel in MHz
2756 * @duration: Duration of the stay on the channel in milliseconds
2758 * This callback is called when the driver indicates that it has started the
2759 * requested remain-on-channel duration.
2761 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2762 unsigned int freq, unsigned int duration)
2764 wpa_s->roc_waiting_drv_freq = 0;
2765 wpa_s->off_channel_freq = freq;
2766 wpas_send_action_cb(wpa_s, NULL);
2767 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
2768 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
2769 wpa_s->pending_listen_duration);
2770 wpa_s->pending_listen_freq = 0;
2775 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
2776 unsigned int timeout)
2778 /* Limit maximum Listen state time based on driver limitation. */
2779 if (timeout > wpa_s->max_remain_on_chan)
2780 timeout = wpa_s->max_remain_on_chan;
2782 return p2p_listen(wpa_s->global->p2p, timeout);
2787 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
2788 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2789 * @freq: Frequency of the channel in MHz
2791 * This callback is called when the driver indicates that a remain-on-channel
2792 * operation has been completed, i.e., the duration on the requested channel
2795 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2798 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
2799 "(p2p_long_listen=%d pending_action_tx=%p)",
2800 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
2801 wpa_s->off_channel_freq = 0;
2802 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
2803 return; /* P2P module started a new operation */
2804 if (wpa_s->pending_action_tx)
2806 if (wpa_s->p2p_long_listen > 0)
2807 wpa_s->p2p_long_listen -= 5;
2808 if (wpa_s->p2p_long_listen > 0) {
2809 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
2810 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen * 1000);
2816 * wpas_p2p_group_remove - Remove a P2P group
2817 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2818 * @ifname: Network interface name of the group interface or "*" to remove all
2820 * Returns: 0 on success, -1 on failure
2822 * This function is used to remove a P2P group. This can be used to disconnect
2823 * from a group in which the local end is a P2P Client or to end a P2P Group in
2824 * case the local end is the Group Owner. If a virtual network interface was
2825 * created for this group, that interface will be removed. Otherwise, only the
2826 * configured P2P group network will be removed from the interface.
2828 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
2830 struct wpa_global *global = wpa_s->global;
2832 if (os_strcmp(ifname, "*") == 0) {
2833 struct wpa_supplicant *prev;
2834 wpa_s = global->ifaces;
2837 wpa_s = wpa_s->next;
2838 prev->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
2839 wpas_p2p_group_delete(prev);
2844 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2845 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2852 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
2853 wpas_p2p_group_delete(wpa_s);
2859 static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
2860 struct p2p_go_neg_results *params,
2866 os_memset(params, 0, sizeof(*params));
2867 params->role_go = 1;
2868 params->freq = 2412;
2870 params->freq = freq;
2871 else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
2872 wpa_s->conf->p2p_oper_channel >= 1 &&
2873 wpa_s->conf->p2p_oper_channel <= 11)
2874 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
2875 else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
2876 wpa_s->conf->p2p_oper_reg_class == 118)
2877 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
2878 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2879 wpa_s->assoc_freq && !freq) {
2880 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2882 params->freq = wpa_s->assoc_freq;
2885 res = wpa_drv_shared_freq(wpa_s);
2886 if (res > 0 && !freq) {
2887 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2888 "already using on a shared interface");
2894 static struct wpa_supplicant *
2895 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
2898 struct wpa_supplicant *group_wpa_s;
2900 if (!wpas_p2p_create_iface(wpa_s))
2903 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2904 WPA_IF_P2P_CLIENT) < 0)
2906 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
2907 if (group_wpa_s == NULL) {
2908 wpas_p2p_remove_pending_group_interface(wpa_s);
2917 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
2918 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2919 * @persistent_group: Whether to create a persistent group
2920 * @freq: Frequency for the group or 0 to indicate no hardcoding
2921 * Returns: 0 on success, -1 on failure
2923 * This function creates a new P2P group with the local end as the Group Owner,
2924 * i.e., without using Group Owner Negotiation.
2926 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
2929 struct p2p_go_neg_results params;
2931 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
2932 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
2933 "(%u MHz) is not supported for P2P uses",
2938 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
2939 p2p_go_params(wpa_s->global->p2p, ¶ms);
2940 params.persistent_group = persistent_group;
2942 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
2945 wpas_start_wps_go(wpa_s, ¶ms, 0);
2951 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
2952 struct wpa_ssid *params, int addr_allocated)
2954 struct wpa_ssid *ssid;
2956 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
2960 wpa_supplicant_ap_deinit(wpa_s);
2962 ssid = wpa_config_add_network(wpa_s->conf);
2965 wpas_notify_network_added(wpa_s, ssid);
2966 wpa_config_set_network_defaults(ssid);
2967 ssid->temporary = 1;
2968 ssid->proto = WPA_PROTO_RSN;
2969 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
2970 ssid->group_cipher = WPA_CIPHER_CCMP;
2971 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2972 ssid->ssid = os_malloc(params->ssid_len);
2973 if (ssid->ssid == NULL) {
2974 wpas_notify_network_removed(wpa_s, ssid);
2975 wpa_config_remove_network(wpa_s->conf, ssid->id);
2978 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2979 ssid->ssid_len = params->ssid_len;
2980 ssid->p2p_group = 1;
2981 if (params->psk_set) {
2982 os_memcpy(ssid->psk, params->psk, 32);
2985 if (params->passphrase)
2986 ssid->passphrase = os_strdup(params->passphrase);
2988 wpa_supplicant_select_network(wpa_s, ssid);
2990 wpa_s->show_group_started = 1;
2996 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
2997 struct wpa_ssid *ssid, int addr_allocated,
3000 struct p2p_go_neg_results params;
3002 if (ssid->disabled != 2 || ssid->ssid == NULL)
3005 wpa_s->p2p_long_listen = 0;
3006 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3008 if (ssid->mode == WPAS_MODE_INFRA)
3009 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3011 if (ssid->mode != WPAS_MODE_P2P_GO)
3014 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
3017 if (ssid->passphrase == NULL ||
3018 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3019 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3023 os_strlcpy(params.passphrase, ssid->passphrase,
3024 sizeof(params.passphrase));
3025 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3026 params.ssid_len = ssid->ssid_len;
3027 params.persistent_group = 1;
3029 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3033 wpas_start_wps_go(wpa_s, ¶ms, 0);
3039 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3040 struct wpabuf *proberesp_ies)
3042 struct wpa_supplicant *wpa_s = ctx;
3043 if (wpa_s->ap_iface) {
3044 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3046 wpabuf_free(hapd->p2p_beacon_ie);
3047 hapd->p2p_beacon_ie = beacon_ies;
3049 wpabuf_free(hapd->p2p_probe_resp_ie);
3050 hapd->p2p_probe_resp_ie = proberesp_ies;
3052 wpabuf_free(beacon_ies);
3053 wpabuf_free(proberesp_ies);
3055 wpa_supplicant_ap_update_beacon(wpa_s);
3059 static void wpas_p2p_idle_update(void *ctx, int idle)
3061 struct wpa_supplicant *wpa_s = ctx;
3062 if (!wpa_s->ap_iface)
3064 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3066 wpas_p2p_set_group_idle_timeout(wpa_s);
3068 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3072 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3073 int persistent_group,
3074 int group_formation)
3076 struct p2p_group *group;
3077 struct p2p_group_config *cfg;
3079 cfg = os_zalloc(sizeof(*cfg));
3083 cfg->persistent_group = persistent_group;
3084 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3085 if (wpa_s->max_stations &&
3086 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3087 cfg->max_clients = wpa_s->max_stations;
3089 cfg->max_clients = wpa_s->conf->max_num_sta;
3090 cfg->cb_ctx = wpa_s;
3091 cfg->ie_update = wpas_p2p_ie_update;
3092 cfg->idle_update = wpas_p2p_idle_update;
3094 group = p2p_group_init(wpa_s->global->p2p, cfg);
3097 if (!group_formation)
3098 p2p_group_notif_formation_done(group);
3099 wpa_s->p2p_group = group;
3104 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3107 if (!wpa_s->p2p_in_provisioning) {
3108 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3109 "provisioning not in progress");
3113 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3115 if (wpa_s->global->p2p)
3116 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3117 wpas_group_formation_completed(wpa_s, 1);
3121 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3122 const char *config_method)
3126 if (os_strcmp(config_method, "display") == 0)
3127 config_methods = WPS_CONFIG_DISPLAY;
3128 else if (os_strcmp(config_method, "keypad") == 0)
3129 config_methods = WPS_CONFIG_KEYPAD;
3130 else if (os_strcmp(config_method, "pbc") == 0 ||
3131 os_strcmp(config_method, "pushbutton") == 0)
3132 config_methods = WPS_CONFIG_PUSHBUTTON;
3136 if (wpa_s->global->p2p == NULL)
3139 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3144 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3147 return p2p_scan_result_text(ies, ies_len, buf, end);
3151 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3153 if (!wpa_s->pending_action_tx)
3156 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3157 "operation request");
3158 wpabuf_free(wpa_s->pending_action_tx);
3159 wpa_s->pending_action_tx = NULL;
3163 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3164 enum p2p_discovery_type type)
3166 wpas_p2p_clear_pending_action_tx(wpa_s);
3167 wpa_s->p2p_long_listen = 0;
3169 return p2p_find(wpa_s->global->p2p, timeout, type);
3173 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3175 wpas_p2p_clear_pending_action_tx(wpa_s);
3176 wpa_s->p2p_long_listen = 0;
3177 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3179 p2p_stop_find(wpa_s->global->p2p);
3181 wpas_p2p_remove_pending_group_interface(wpa_s);
3185 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3187 struct wpa_supplicant *wpa_s = eloop_ctx;
3188 wpa_s->p2p_long_listen = 0;
3192 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3196 wpas_p2p_clear_pending_action_tx(wpa_s);
3200 * This is a request for unlimited Listen state. However, at
3201 * least for now, this is mapped to a Listen state for one
3206 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3207 wpa_s->p2p_long_listen = 0;
3209 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3210 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
3211 wpa_s->p2p_long_listen = timeout;
3212 eloop_register_timeout(timeout, 0,
3213 wpas_p2p_long_listen_timeout,
3221 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3222 u8 *buf, size_t len, int p2p_group)
3224 struct wpabuf *p2p_ie;
3227 if (wpa_s->global->p2p_disabled)
3229 if (wpa_s->global->p2p == NULL)
3234 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3235 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3237 wpabuf_free(p2p_ie);
3243 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3244 const u8 *ie, size_t ie_len)
3246 if (wpa_s->global->p2p_disabled)
3248 if (wpa_s->global->p2p == NULL)
3251 return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
3255 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3256 const u8 *sa, const u8 *bssid,
3257 u8 category, const u8 *data, size_t len, int freq)
3259 if (wpa_s->global->p2p_disabled)
3261 if (wpa_s->global->p2p == NULL)
3264 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3269 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3271 if (wpa_s->global->p2p_disabled)
3273 if (wpa_s->global->p2p == NULL)
3276 p2p_scan_ie(wpa_s->global->p2p, ies);
3280 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3282 p2p_group_deinit(wpa_s->p2p_group);
3283 wpa_s->p2p_group = NULL;
3287 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3289 wpa_s->p2p_long_listen = 0;
3291 return p2p_reject(wpa_s->global->p2p, addr);
3295 /* Invite to reinvoke a persistent group */
3296 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3297 struct wpa_ssid *ssid, const u8 *go_dev_addr)
3299 enum p2p_invite_role role;
3302 if (ssid->mode == WPAS_MODE_P2P_GO) {
3303 role = P2P_INVITE_ROLE_GO;
3304 if (peer_addr == NULL) {
3305 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3306 "address in invitation command");
3309 if (wpas_p2p_create_iface(wpa_s)) {
3310 if (wpas_p2p_add_group_interface(wpa_s,
3311 WPA_IF_P2P_GO) < 0) {
3312 wpa_printf(MSG_ERROR, "P2P: Failed to "
3313 "allocate a new interface for the "
3317 bssid = wpa_s->pending_interface_addr;
3319 bssid = wpa_s->own_addr;
3321 role = P2P_INVITE_ROLE_CLIENT;
3322 peer_addr = ssid->bssid;
3324 wpa_s->pending_invite_ssid_id = ssid->id;
3326 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3327 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
3331 /* Invite to join an active group */
3332 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
3333 const u8 *peer_addr, const u8 *go_dev_addr)
3335 struct wpa_global *global = wpa_s->global;
3336 enum p2p_invite_role role;
3338 struct wpa_ssid *ssid;
3340 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3341 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3344 if (wpa_s == NULL) {
3345 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
3349 ssid = wpa_s->current_ssid;
3351 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
3356 if (ssid->mode == WPAS_MODE_P2P_GO) {
3357 role = P2P_INVITE_ROLE_ACTIVE_GO;
3358 bssid = wpa_s->own_addr;
3359 if (go_dev_addr == NULL)
3360 go_dev_addr = wpa_s->parent->own_addr;
3362 role = P2P_INVITE_ROLE_CLIENT;
3363 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
3364 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
3365 "invite to current group");
3368 bssid = wpa_s->bssid;
3369 if (go_dev_addr == NULL &&
3370 !is_zero_ether_addr(wpa_s->go_dev_addr))
3371 go_dev_addr = wpa_s->go_dev_addr;
3373 wpa_s->pending_invite_ssid_id = -1;
3375 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3376 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
3381 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3383 struct wpa_ssid *ssid = wpa_s->current_ssid;
3384 const char *ssid_txt;
3385 u8 go_dev_addr[ETH_ALEN];
3388 if (!wpa_s->show_group_started || !ssid)
3391 wpa_s->show_group_started = 0;
3393 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3394 os_memset(go_dev_addr, 0, ETH_ALEN);
3395 if (ssid->bssid_set)
3396 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3397 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3399 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3401 if (wpa_s->global->p2p_group_formation == wpa_s)
3402 wpa_s->global->p2p_group_formation = NULL;
3404 if (ssid->passphrase == NULL && ssid->psk_set) {
3406 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3407 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3408 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3410 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
3411 MAC2STR(go_dev_addr),
3412 persistent ? " [PERSISTENT]" : "");
3414 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3415 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3416 "go_dev_addr=" MACSTR "%s",
3417 wpa_s->ifname, ssid_txt, ssid->frequency,
3418 ssid->passphrase ? ssid->passphrase : "",
3419 MAC2STR(go_dev_addr),
3420 persistent ? " [PERSISTENT]" : "");
3424 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
3429 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3430 u32 interval1, u32 duration2, u32 interval2)
3432 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3433 wpa_s->current_ssid == NULL ||
3434 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3437 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3438 wpa_s->own_addr, wpa_s->assoc_freq,
3439 duration1, interval1, duration2, interval2);
3443 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3444 unsigned int interval)
3446 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3450 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
3452 struct wpa_supplicant *wpa_s = eloop_ctx;
3454 if (wpa_s->conf->p2p_group_idle == 0) {
3455 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
3460 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
3462 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
3463 wpas_p2p_group_delete(wpa_s);
3467 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
3469 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3470 if (wpa_s->conf->p2p_group_idle == 0)
3473 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3476 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
3477 wpa_s->conf->p2p_group_idle);
3478 eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
3479 wpas_p2p_group_idle_timeout, wpa_s, NULL);
3483 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3484 u16 reason_code, const u8 *ie, size_t ie_len)
3486 if (wpa_s->global->p2p_disabled)
3489 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3493 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3494 u16 reason_code, const u8 *ie, size_t ie_len)
3496 if (wpa_s->global->p2p_disabled)
3499 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3503 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3505 struct p2p_data *p2p = wpa_s->global->p2p;
3510 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3513 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
3514 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
3516 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
3518 if (wpa_s->conf->device_type) {
3519 if (wps_dev_type_str2bin(wpa_s->conf->device_type,
3520 pri_dev_type) < 0) {
3521 wpa_printf(MSG_ERROR, "P2P: Invalid "
3524 p2p_set_pri_dev_type(p2p, pri_dev_type);
3528 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
3529 u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
3532 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
3533 if (wpa_s->conf->sec_device_type[i] == NULL)
3535 if (wps_dev_type_str2bin(
3536 wpa_s->conf->sec_device_type[i],
3537 sec_dev_type[num]) < 0) {
3538 wpa_printf(MSG_ERROR, "P2P: Invalid "
3543 if (num == P2P_SEC_DEVICE_TYPES)
3546 p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num);
3549 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3550 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3552 country[0] = wpa_s->conf->country[0];
3553 country[1] = wpa_s->conf->country[1];
3555 p2p_set_country(p2p, country);
3558 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
3559 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
3560 wpa_s->conf->p2p_ssid_postfix ?
3561 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
3565 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
3566 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
3570 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
3573 if (!wpa_s->ap_iface)
3575 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
3580 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
3582 if (wpa_s->global->p2p_disabled)
3584 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3587 wpa_s->global->cross_connection = enabled;
3588 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
3591 struct wpa_supplicant *iface;
3593 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
3595 if (iface->cross_connect_enabled == 0)
3598 iface->cross_connect_enabled = 0;
3599 iface->cross_connect_in_use = 0;
3600 wpa_msg(iface->parent, MSG_INFO,
3601 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
3602 iface->ifname, iface->cross_connect_uplink);
3610 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
3612 struct wpa_supplicant *iface;
3614 if (!uplink->global->cross_connection)
3617 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
3618 if (!iface->cross_connect_enabled)
3620 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
3623 if (iface->ap_iface == NULL)
3625 if (iface->cross_connect_in_use)
3628 iface->cross_connect_in_use = 1;
3629 wpa_msg(iface->parent, MSG_INFO,
3630 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
3631 iface->ifname, iface->cross_connect_uplink);
3636 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
3638 struct wpa_supplicant *iface;
3640 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
3641 if (!iface->cross_connect_enabled)
3643 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
3646 if (!iface->cross_connect_in_use)
3649 wpa_msg(iface->parent, MSG_INFO,
3650 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
3651 iface->ifname, iface->cross_connect_uplink);
3652 iface->cross_connect_in_use = 0;
3657 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
3659 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
3660 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
3661 wpa_s->cross_connect_disallowed)
3662 wpas_p2p_disable_cross_connect(wpa_s);
3664 wpas_p2p_enable_cross_connect(wpa_s);
3665 if (!wpa_s->ap_iface)
3666 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3670 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
3672 wpas_p2p_disable_cross_connect(wpa_s);
3673 if (!wpa_s->ap_iface)
3674 wpas_p2p_set_group_idle_timeout(wpa_s);
3678 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
3680 struct wpa_supplicant *iface;
3682 if (!wpa_s->global->cross_connection)
3685 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3688 if (iface->drv_flags &
3689 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
3691 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
3694 wpa_s->cross_connect_enabled = 1;
3695 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
3696 sizeof(wpa_s->cross_connect_uplink));
3697 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
3698 "%s to %s whenever uplink is available",
3699 wpa_s->ifname, wpa_s->cross_connect_uplink);
3701 if (iface->ap_iface || iface->current_ssid == NULL ||
3702 iface->current_ssid->mode != WPAS_MODE_INFRA ||
3703 iface->cross_connect_disallowed ||
3704 iface->wpa_state != WPA_COMPLETED)
3707 wpa_s->cross_connect_in_use = 1;
3708 wpa_msg(wpa_s->parent, MSG_INFO,
3709 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
3710 wpa_s->ifname, wpa_s->cross_connect_uplink);
3716 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
3718 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
3719 !wpa_s->p2p_in_provisioning)
3720 return 0; /* not P2P client operation */
3722 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
3724 wpas_group_formation_completed(wpa_s, 0);
3729 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
3731 struct p2p_channels chan;
3733 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
3736 os_memset(&chan, 0, sizeof(chan));
3737 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
3738 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
3743 p2p_update_channel_list(wpa_s->global->p2p, &chan);
3747 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
3749 struct wpa_global *global = wpa_s->global;
3752 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
3754 if (wpa_s->pending_interface_name[0] &&
3755 !is_zero_ether_addr(wpa_s->pending_interface_addr))
3758 wpas_p2p_stop_find(wpa_s);
3760 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3761 if (wpa_s == global->p2p_group_formation &&
3762 (wpa_s->p2p_in_provisioning ||
3763 wpa_s->parent->pending_interface_type ==
3764 WPA_IF_P2P_CLIENT)) {
3765 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
3766 "formation found - cancelling",
3769 wpas_p2p_group_delete(wpa_s);
3775 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
3783 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
3785 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3788 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
3789 "being available anymore");
3790 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
3791 wpas_p2p_group_delete(wpa_s);