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