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