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