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