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