P2P: Allow pre-authorization of invitation to apply to src addr
[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
1363                 if (wpa_s->force_long_sd) {
1364                         wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1365                                    "response");
1366                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1367                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1368                         goto done;
1369                 }
1370
1371                 switch (srv_proto) {
1372                 case P2P_SERV_ALL_SERVICES:
1373                         wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1374                                    "for all services");
1375                         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1376                             dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1377                                 wpa_printf(MSG_DEBUG, "P2P: No service "
1378                                            "discovery protocols available");
1379                                 wpas_sd_add_proto_not_avail(
1380                                         resp, P2P_SERV_ALL_SERVICES,
1381                                         srv_trans_id);
1382                                 break;
1383                         }
1384                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1385                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1386                         break;
1387                 case P2P_SERV_BONJOUR:
1388                         wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1389                                             pos, tlv_end - pos);
1390                         break;
1391                 case P2P_SERV_UPNP:
1392                         wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1393                                          pos, tlv_end - pos);
1394                         break;
1395                 default:
1396                         wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1397                                    "protocol %u", srv_proto);
1398                         wpas_sd_add_proto_not_avail(resp, srv_proto,
1399                                                     srv_trans_id);
1400                         break;
1401                 }
1402
1403                 pos = tlv_end;
1404         }
1405
1406 done:
1407         wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1408
1409         wpabuf_free(resp);
1410 }
1411
1412
1413 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1414                       const u8 *tlvs, size_t tlvs_len)
1415 {
1416         struct wpa_supplicant *wpa_s = ctx;
1417         const u8 *pos = tlvs;
1418         const u8 *end = tlvs + tlvs_len;
1419         const u8 *tlv_end;
1420         u16 slen;
1421         size_t buf_len;
1422         char *buf;
1423
1424         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1425                     tlvs, tlvs_len);
1426         if (tlvs_len > 1500) {
1427                 /* TODO: better way for handling this */
1428                 wpa_msg_ctrl(wpa_s, MSG_INFO,
1429                              P2P_EVENT_SERV_DISC_RESP MACSTR
1430                              " %u <long response: %u bytes>",
1431                              MAC2STR(sa), update_indic,
1432                              (unsigned int) tlvs_len);
1433         } else {
1434                 buf_len = 2 * tlvs_len + 1;
1435                 buf = os_malloc(buf_len);
1436                 if (buf) {
1437                         wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1438                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1439                                      P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1440                                      MAC2STR(sa), update_indic, buf);
1441                         os_free(buf);
1442                 }
1443         }
1444
1445         while (pos < end) {
1446                 u8 srv_proto, srv_trans_id, status;
1447
1448                 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1449                 slen = WPA_GET_LE16(pos);
1450                 pos += 2;
1451                 if (pos + slen > end || slen < 3) {
1452                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1453                                    "length");
1454                         return;
1455                 }
1456                 tlv_end = pos + slen;
1457
1458                 srv_proto = *pos++;
1459                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1460                            srv_proto);
1461                 srv_trans_id = *pos++;
1462                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1463                            srv_trans_id);
1464                 status = *pos++;
1465                 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1466                            status);
1467
1468                 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1469                             pos, tlv_end - pos);
1470
1471                 pos = tlv_end;
1472         }
1473 }
1474
1475
1476 void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1477                            const struct wpabuf *tlvs)
1478 {
1479         return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1480 }
1481
1482
1483 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1484                                 u8 version, const char *query)
1485 {
1486         struct wpabuf *tlvs;
1487         void *ret;
1488
1489         tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1490         if (tlvs == NULL)
1491                 return NULL;
1492         wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1493         wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1494         wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1495         wpabuf_put_u8(tlvs, version);
1496         wpabuf_put_str(tlvs, query);
1497         ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1498         wpabuf_free(tlvs);
1499         return ret;
1500 }
1501
1502
1503 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1504 {
1505         return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1506 }
1507
1508
1509 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1510                           const u8 *dst, u8 dialog_token,
1511                           const struct wpabuf *resp_tlvs)
1512 {
1513         p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1514                         resp_tlvs);
1515 }
1516
1517
1518 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1519 {
1520         p2p_sd_service_update(wpa_s->global->p2p);
1521 }
1522
1523
1524 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1525 {
1526         dl_list_del(&bsrv->list);
1527         wpabuf_free(bsrv->query);
1528         wpabuf_free(bsrv->resp);
1529         os_free(bsrv);
1530 }
1531
1532
1533 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1534 {
1535         dl_list_del(&usrv->list);
1536         os_free(usrv->service);
1537         os_free(usrv);
1538 }
1539
1540
1541 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1542 {
1543         struct p2p_srv_bonjour *bsrv, *bn;
1544         struct p2p_srv_upnp *usrv, *un;
1545
1546         dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1547                               struct p2p_srv_bonjour, list)
1548                 wpas_p2p_srv_bonjour_free(bsrv);
1549
1550         dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1551                               struct p2p_srv_upnp, list)
1552                 wpas_p2p_srv_upnp_free(usrv);
1553
1554         wpas_p2p_sd_service_update(wpa_s);
1555 }
1556
1557
1558 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1559                                  struct wpabuf *query, struct wpabuf *resp)
1560 {
1561         struct p2p_srv_bonjour *bsrv;
1562
1563         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1564         if (bsrv) {
1565                 wpabuf_free(query);
1566                 wpabuf_free(bsrv->resp);
1567                 bsrv->resp = resp;
1568                 return 0;
1569         }
1570
1571         bsrv = os_zalloc(sizeof(*bsrv));
1572         if (bsrv == NULL)
1573                 return -1;
1574         bsrv->query = query;
1575         bsrv->resp = resp;
1576         dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1577
1578         wpas_p2p_sd_service_update(wpa_s);
1579         return 0;
1580 }
1581
1582
1583 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1584                                  const struct wpabuf *query)
1585 {
1586         struct p2p_srv_bonjour *bsrv;
1587
1588         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1589         if (bsrv == NULL)
1590                 return -1;
1591         wpas_p2p_srv_bonjour_free(bsrv);
1592         wpas_p2p_sd_service_update(wpa_s);
1593         return 0;
1594 }
1595
1596
1597 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1598                               const char *service)
1599 {
1600         struct p2p_srv_upnp *usrv;
1601
1602         if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1603                 return 0; /* Already listed */
1604         usrv = os_zalloc(sizeof(*usrv));
1605         if (usrv == NULL)
1606                 return -1;
1607         usrv->version = version;
1608         usrv->service = os_strdup(service);
1609         if (usrv->service == NULL) {
1610                 os_free(usrv);
1611                 return -1;
1612         }
1613         dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1614
1615         wpas_p2p_sd_service_update(wpa_s);
1616         return 0;
1617 }
1618
1619
1620 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1621                               const char *service)
1622 {
1623         struct p2p_srv_upnp *usrv;
1624
1625         usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1626         if (usrv == NULL)
1627                 return -1;
1628         wpas_p2p_srv_upnp_free(usrv);
1629         wpas_p2p_sd_service_update(wpa_s);
1630         return 0;
1631 }
1632
1633
1634 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1635                                          const u8 *peer, const char *params)
1636 {
1637         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1638                 MAC2STR(peer), wps_generate_pin(), params);
1639 }
1640
1641
1642 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1643                                         const u8 *peer, const char *params)
1644 {
1645         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1646                 MAC2STR(peer), params);
1647 }
1648
1649
1650 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1651                         const u8 *dev_addr, const u8 *pri_dev_type,
1652                         const char *dev_name, u16 supp_config_methods,
1653                         u8 dev_capab, u8 group_capab)
1654 {
1655         struct wpa_supplicant *wpa_s = ctx;
1656         char devtype[WPS_DEV_TYPE_BUFSIZE];
1657         char params[200];
1658         u8 empty_dev_type[8];
1659
1660         if (pri_dev_type == NULL) {
1661                 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1662                 pri_dev_type = empty_dev_type;
1663         }
1664         os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1665                     " pri_dev_type=%s name='%s' config_methods=0x%x "
1666                     "dev_capab=0x%x group_capab=0x%x",
1667                     MAC2STR(dev_addr),
1668                     wps_dev_type_bin2str(pri_dev_type, devtype,
1669                                          sizeof(devtype)),
1670                     dev_name, supp_config_methods, dev_capab, group_capab);
1671         params[sizeof(params) - 1] = '\0';
1672
1673         if (config_methods & WPS_CONFIG_DISPLAY)
1674                 wpas_prov_disc_local_display(wpa_s, peer, params);
1675         else if (config_methods & WPS_CONFIG_KEYPAD)
1676                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1677         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1678                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1679                         "%s", MAC2STR(peer), params);
1680 }
1681
1682
1683 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1684 {
1685         struct wpa_supplicant *wpa_s = ctx;
1686         if (config_methods & WPS_CONFIG_DISPLAY)
1687                 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1688         else if (config_methods & WPS_CONFIG_KEYPAD)
1689                 wpas_prov_disc_local_display(wpa_s, peer, "");
1690         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1691                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1692                         MAC2STR(peer));
1693 }
1694
1695
1696 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1697                                   const u8 *go_dev_addr, const u8 *ssid,
1698                                   size_t ssid_len, int *go, u8 *group_bssid,
1699                                   int *force_freq, int persistent_group)
1700 {
1701         struct wpa_supplicant *wpa_s = ctx;
1702         struct wpa_ssid *s;
1703         u8 cur_bssid[ETH_ALEN];
1704         int res;
1705
1706         if (!persistent_group) {
1707                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1708                            " to join an active group", MAC2STR(sa));
1709                 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1710                     (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1711                      == 0 ||
1712                      os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1713                         wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1714                                    "authorized invitation");
1715                         goto accept_inv;
1716                 }
1717                 /*
1718                  * Do not accept the invitation automatically; notify user and
1719                  * request approval.
1720                  */
1721                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1722         }
1723
1724         if (!wpa_s->conf->persistent_reconnect)
1725                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1726
1727         for (s = wpa_s->conf->ssid; s; s = s->next) {
1728                 if (s->disabled == 2 &&
1729                     os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1730                     s->ssid_len == ssid_len &&
1731                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
1732                         break;
1733         }
1734
1735         if (!s) {
1736                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1737                            " requested reinvocation of an unknown group",
1738                            MAC2STR(sa));
1739                 return P2P_SC_FAIL_UNKNOWN_GROUP;
1740         }
1741
1742         if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1743                 *go = 1;
1744                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1745                         wpa_printf(MSG_DEBUG, "P2P: The only available "
1746                                    "interface is already in use - reject "
1747                                    "invitation");
1748                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1749                 }
1750                 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1751         } else if (s->mode == WPAS_MODE_P2P_GO) {
1752                 *go = 1;
1753                 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1754                 {
1755                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1756                                    "interface address for the group");
1757                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1758                 }
1759                 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1760                           ETH_ALEN);
1761         }
1762
1763 accept_inv:
1764         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1765             wpa_s->assoc_freq) {
1766                 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1767                            "the channel we are already using");
1768                 *force_freq = wpa_s->assoc_freq;
1769         }
1770
1771         res = wpa_drv_shared_freq(wpa_s);
1772         if (res > 0) {
1773                 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1774                            "with the channel we are already using on a "
1775                            "shared interface");
1776                 *force_freq = res;
1777         }
1778
1779         return P2P_SC_SUCCESS;
1780 }
1781
1782
1783 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
1784                                      const u8 *ssid, size_t ssid_len,
1785                                      const u8 *go_dev_addr, u8 status,
1786                                      int op_freq)
1787 {
1788         struct wpa_supplicant *wpa_s = ctx;
1789         struct wpa_ssid *s;
1790
1791         for (s = wpa_s->conf->ssid; s; s = s->next) {
1792                 if (s->disabled == 2 &&
1793                     s->ssid_len == ssid_len &&
1794                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
1795                         break;
1796         }
1797
1798         if (status == P2P_SC_SUCCESS) {
1799                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1800                            " was accepted; op_freq=%d MHz",
1801                            MAC2STR(sa), op_freq);
1802                 if (s) {
1803                         wpas_p2p_group_add_persistent(
1804                                 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
1805                 } else {
1806                         wpas_p2p_join(wpa_s, bssid, go_dev_addr,
1807                                       wpa_s->p2p_wps_method);
1808                 }
1809                 return;
1810         }
1811
1812         if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1813                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1814                            " was rejected (status %u)", MAC2STR(sa), status);
1815                 return;
1816         }
1817
1818         if (!s) {
1819                 if (bssid) {
1820                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1821                                 "sa=" MACSTR " go_dev_addr=" MACSTR
1822                                 " bssid=" MACSTR " unknown-network",
1823                                 MAC2STR(sa), MAC2STR(go_dev_addr),
1824                                 MAC2STR(bssid));
1825                 } else {
1826                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1827                                 "sa=" MACSTR " go_dev_addr=" MACSTR
1828                                 " unknown-network",
1829                                 MAC2STR(sa), MAC2STR(go_dev_addr));
1830                 }
1831                 return;
1832         }
1833
1834         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
1835                 " persistent=%d", MAC2STR(sa), s->id);
1836 }
1837
1838
1839 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
1840 {
1841         struct wpa_supplicant *wpa_s = ctx;
1842         struct wpa_ssid *ssid;
1843
1844         if (bssid) {
1845                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1846                         "status=%d " MACSTR,
1847                         status, MAC2STR(bssid));
1848         } else {
1849                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1850                         "status=%d ", status);
1851         }
1852
1853         if (wpa_s->pending_invite_ssid_id == -1)
1854                 return; /* Invitation to active group */
1855
1856         if (status != P2P_SC_SUCCESS) {
1857                 wpas_p2p_remove_pending_group_interface(wpa_s);
1858                 return;
1859         }
1860
1861         ssid = wpa_config_get_network(wpa_s->conf,
1862                                       wpa_s->pending_invite_ssid_id);
1863         if (ssid == NULL) {
1864                 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
1865                            "data matching with invitation");
1866                 return;
1867         }
1868
1869         wpas_p2p_group_add_persistent(wpa_s, ssid,
1870                                       ssid->mode == WPAS_MODE_P2P_GO, 0);
1871 }
1872
1873
1874 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
1875                                    struct p2p_config *p2p)
1876 {
1877         struct hostapd_hw_modes *modes;
1878         u16 num_modes, flags;
1879         int i, cla;
1880         int band24 = 0, band5_low = 0, band5_high = 0;
1881
1882         /* TODO: more detailed selection of channels within reg_class based on
1883          * driver capabilities */
1884
1885         modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
1886         if (modes == NULL) {
1887                 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
1888                            "of all supported channels; assume dualband "
1889                            "support");
1890                 band24 = band5_low = band5_high = 1;
1891         } else {
1892                 for (i = 0; i < num_modes; i++) {
1893                         struct hostapd_hw_modes *mode;
1894                         mode = &modes[i];
1895                         if (mode->mode == HOSTAPD_MODE_IEEE80211G) {
1896                                 band24 = 1;
1897                         } else if (mode->mode == HOSTAPD_MODE_IEEE80211A) {
1898                                 int j;
1899                                 for (j = 0; j < mode->num_channels; j++) {
1900                                         struct hostapd_channel_data *ch;
1901                                         ch = &mode->channels[j];
1902                                         if (ch->chan == 36)
1903                                                 band5_low = 1;
1904                                         else if (ch->chan == 157)
1905                                                 band5_high = 1;
1906                                 }
1907                         }
1908                 }
1909         }
1910
1911         cla = 0;
1912
1913         if (band24) {
1914                 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1915                            "2.4 GHz band");
1916
1917                 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1918                 p2p->channels.reg_class[cla].reg_class = 81;
1919                 p2p->channels.reg_class[cla].channels = 13;
1920                 for (i = 0; i < 13; i++)
1921                         p2p->channels.reg_class[cla].channel[i] = i + 1;
1922                 cla++;
1923
1924                 /* Operating class 82 - 2.4 GHz band channel 14 */
1925                 p2p->channels.reg_class[cla].reg_class = 82;
1926                 p2p->channels.reg_class[cla].channels = 1;
1927                 p2p->channels.reg_class[cla].channel[0] = 14;
1928                 cla++;
1929
1930 #if 0
1931                 /* Operating class 83 - 2.4 GHz band channels 1..9; 40 MHz */
1932                 p2p->channels.reg_class[cla].reg_class = 83;
1933                 p2p->channels.reg_class[cla].channels = 9;
1934                 for (i = 0; i < 9; i++)
1935                         p2p->channels.reg_class[cla].channel[i] = i + 1;
1936                 cla++;
1937
1938                 /* Operating class 84 - 2.4 GHz band channels 5..13; 40 MHz */
1939                 p2p->channels.reg_class[cla].reg_class = 84;
1940                 p2p->channels.reg_class[cla].channels = 9;
1941                 for (i = 0; i < 9; i++)
1942                         p2p->channels.reg_class[cla].channel[i] = i + 5;
1943                 cla++;
1944 #endif
1945         }
1946
1947         if (band5_low) {
1948                 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1949                            "lower 5 GHz band");
1950
1951                 /* Operating class 115 - 5 GHz, channels 36-48 */
1952                 p2p->channels.reg_class[cla].reg_class = 115;
1953                 p2p->channels.reg_class[cla].channels = 4;
1954                 p2p->channels.reg_class[cla].channel[0] = 36;
1955                 p2p->channels.reg_class[cla].channel[1] = 40;
1956                 p2p->channels.reg_class[cla].channel[2] = 44;
1957                 p2p->channels.reg_class[cla].channel[3] = 48;
1958                 cla++;
1959
1960 #if 0
1961                 /* Operating class 116 - 5 GHz, channels 36,44; 40 MHz */
1962                 p2p->channels.reg_class[cla].reg_class = 116;
1963                 p2p->channels.reg_class[cla].channels = 2;
1964                 p2p->channels.reg_class[cla].channel[0] = 36;
1965                 p2p->channels.reg_class[cla].channel[1] = 44;
1966                 cla++;
1967
1968                 /* Operating class 117 - 5 GHz, channels 40,48; 40 MHz */
1969                 p2p->channels.reg_class[cla].reg_class = 117;
1970                 p2p->channels.reg_class[cla].channels = 2;
1971                 p2p->channels.reg_class[cla].channel[0] = 40;
1972                 p2p->channels.reg_class[cla].channel[1] = 48;
1973                 cla++;
1974 #endif
1975         }
1976
1977         if (band5_high) {
1978                 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1979                            "higher 5 GHz band");
1980
1981                 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1982                 p2p->channels.reg_class[cla].reg_class = 124;
1983                 p2p->channels.reg_class[cla].channels = 4;
1984                 p2p->channels.reg_class[cla].channel[0] = 149;
1985                 p2p->channels.reg_class[cla].channel[1] = 153;
1986                 p2p->channels.reg_class[cla].channel[2] = 157;
1987                 p2p->channels.reg_class[cla].channel[3] = 161;
1988                 cla++;
1989
1990 #if 0
1991                 /* Operating class 126 - 5 GHz, channels 149,157; 40 MHz */
1992                 p2p->channels.reg_class[cla].reg_class = 126;
1993                 p2p->channels.reg_class[cla].channels = 2;
1994                 p2p->channels.reg_class[cla].channel[0] = 149;
1995                 p2p->channels.reg_class[cla].channel[1] = 157;
1996                 cla++;
1997
1998                 /* Operating class 127 - 5 GHz, channels 153,161; 40 MHz */
1999                 p2p->channels.reg_class[cla].reg_class = 127;
2000                 p2p->channels.reg_class[cla].channels = 2;
2001                 p2p->channels.reg_class[cla].channel[0] = 153;
2002                 p2p->channels.reg_class[cla].channel[1] = 161;
2003                 cla++;
2004 #endif
2005         }
2006
2007         p2p->channels.reg_classes = cla;
2008
2009         if (modes)
2010                 ieee80211_sta_free_hw_features(modes, num_modes);
2011
2012         return 0;
2013 }
2014
2015
2016 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2017                         size_t buf_len)
2018 {
2019         struct wpa_supplicant *wpa_s = ctx;
2020
2021         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2022                 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2023                         break;
2024         }
2025         if (wpa_s == NULL)
2026                 return -1;
2027
2028         return wpa_drv_get_noa(wpa_s, buf, buf_len);
2029 }
2030
2031
2032 /**
2033  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2034  * @global: Pointer to global data from wpa_supplicant_init()
2035  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2036  * Returns: 0 on success, -1 on failure
2037  */
2038 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2039 {
2040         struct p2p_config p2p;
2041         unsigned int r;
2042         int i;
2043
2044         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2045                 return 0;
2046
2047 #ifdef CONFIG_CLIENT_MLME
2048         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
2049                 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
2050                 wpa_s->mlme.public_action_cb_ctx = wpa_s;
2051         }
2052 #endif /* CONFIG_CLIENT_MLME */
2053
2054         if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
2055                 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
2056                 /* Continue anyway; this is not really a fatal error */
2057         }
2058
2059         if (global->p2p)
2060                 return 0;
2061
2062         os_memset(&p2p, 0, sizeof(p2p));
2063         p2p.msg_ctx = wpa_s;
2064         p2p.cb_ctx = wpa_s;
2065         p2p.p2p_scan = wpas_p2p_scan;
2066         p2p.send_action = wpas_send_action;
2067         p2p.send_action_done = wpas_send_action_done;
2068         p2p.go_neg_completed = wpas_go_neg_completed;
2069         p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2070         p2p.dev_found = wpas_dev_found;
2071         p2p.start_listen = wpas_start_listen;
2072         p2p.stop_listen = wpas_stop_listen;
2073         p2p.send_probe_resp = wpas_send_probe_resp;
2074         p2p.sd_request = wpas_sd_request;
2075         p2p.sd_response = wpas_sd_response;
2076         p2p.prov_disc_req = wpas_prov_disc_req;
2077         p2p.prov_disc_resp = wpas_prov_disc_resp;
2078         p2p.invitation_process = wpas_invitation_process;
2079         p2p.invitation_received = wpas_invitation_received;
2080         p2p.invitation_result = wpas_invitation_result;
2081         p2p.get_noa = wpas_get_noa;
2082
2083         os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2084         os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2085         p2p.dev_name = wpa_s->conf->device_name;
2086
2087         if (wpa_s->conf->p2p_listen_reg_class &&
2088             wpa_s->conf->p2p_listen_channel) {
2089                 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2090                 p2p.channel = wpa_s->conf->p2p_listen_channel;
2091         } else {
2092                 p2p.reg_class = 81;
2093                 /*
2094                  * Pick one of the social channels randomly as the listen
2095                  * channel.
2096                  */
2097                 os_get_random((u8 *) &r, sizeof(r));
2098                 p2p.channel = 1 + (r % 3) * 5;
2099         }
2100
2101         if (wpa_s->conf->p2p_oper_reg_class &&
2102             wpa_s->conf->p2p_oper_channel) {
2103                 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2104                 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2105         } else {
2106                 p2p.op_reg_class = 81;
2107                 /*
2108                  * For initial tests, pick the operation channel randomly.
2109                  * TODO: Use scan results (etc.) to select the best channel.
2110                  */
2111                 p2p.op_channel = 1 + r % 11;
2112         }
2113         wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d  "
2114                    "Own preferred operation channel: %d",
2115                    p2p.channel, p2p.op_channel);
2116         if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2117                 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2118                 p2p.country[2] = 0x04;
2119         } else
2120                 os_memcpy(p2p.country, "US\x04", 3);
2121
2122         if (wpas_p2p_setup_channels(wpa_s, &p2p)) {
2123                 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2124                            "channel list");
2125                 return -1;
2126         }
2127
2128         if (wpa_s->conf->device_type &&
2129             wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) <
2130             0) {
2131                 wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
2132                 return -1;
2133         }
2134
2135         for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
2136                 if (wpa_s->conf->sec_device_type[i] == NULL)
2137                         continue;
2138                 if (wps_dev_type_str2bin(
2139                             wpa_s->conf->sec_device_type[i],
2140                             p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) {
2141                         wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type");
2142                         return -1;
2143                 }
2144                 p2p.num_sec_dev_types++;
2145                 if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES)
2146                         break;
2147         }
2148
2149         p2p.concurrent_operations = !!(wpa_s->drv_flags &
2150                                        WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2151
2152         p2p.max_peers = 100;
2153
2154         if (wpa_s->conf->p2p_ssid_postfix) {
2155                 p2p.ssid_postfix_len =
2156                         os_strlen(wpa_s->conf->p2p_ssid_postfix);
2157                 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2158                         p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2159                 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2160                           p2p.ssid_postfix_len);
2161         }
2162
2163         global->p2p = p2p_init(&p2p);
2164         if (global->p2p == NULL)
2165                 return -1;
2166
2167         return 0;
2168 }
2169
2170
2171 /**
2172  * wpas_p2p_deinit - Deinitialize per-interface P2P data
2173  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2174  *
2175  * This function deinitialize per-interface P2P data.
2176  */
2177 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2178 {
2179         if (wpa_s->driver && wpa_s->drv_priv)
2180                 wpa_drv_probe_req_report(wpa_s, 0);
2181         os_free(wpa_s->go_params);
2182         wpa_s->go_params = NULL;
2183         wpabuf_free(wpa_s->pending_action_tx);
2184         wpa_s->pending_action_tx = NULL;
2185         eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2186         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2187         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2188         wpa_s->p2p_long_listen = 0;
2189         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2190         wpas_p2p_remove_pending_group_interface(wpa_s);
2191
2192         /* TODO: remove group interface from the driver if this wpa_s instance
2193          * is on top of a P2P group interface */
2194 }
2195
2196
2197 /**
2198  * wpas_p2p_deinit_global - Deinitialize global P2P module
2199  * @global: Pointer to global data from wpa_supplicant_init()
2200  *
2201  * This function deinitializes the global (per device) P2P module.
2202  */
2203 void wpas_p2p_deinit_global(struct wpa_global *global)
2204 {
2205         struct wpa_supplicant *wpa_s, *tmp;
2206         char *ifname;
2207
2208         if (global->p2p == NULL)
2209                 return;
2210
2211         /* Remove remaining P2P group interfaces */
2212         wpa_s = global->ifaces;
2213         if (wpa_s)
2214                 wpas_p2p_service_flush(wpa_s);
2215         while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2216                 wpa_s = wpa_s->next;
2217         while (wpa_s) {
2218                 enum wpa_driver_if_type type;
2219                 tmp = global->ifaces;
2220                 while (tmp &&
2221                        (tmp == wpa_s ||
2222                         tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2223                         tmp = tmp->next;
2224                 }
2225                 if (tmp == NULL)
2226                         break;
2227                 ifname = os_strdup(tmp->ifname);
2228                 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2229                 wpa_supplicant_remove_iface(global, tmp);
2230                 if (ifname)
2231                         wpa_drv_if_remove(wpa_s, type, ifname);
2232                 os_free(ifname);
2233         }
2234
2235         p2p_deinit(global->p2p);
2236         global->p2p = NULL;
2237 }
2238
2239
2240 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2241 {
2242         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
2243                 return 1; /* P2P group requires a new interface in every case
2244                            */
2245         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2246                 return 0; /* driver does not support concurrent operations */
2247         if (wpa_s->global->ifaces->next)
2248                 return 1; /* more that one interface already in use */
2249         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2250                 return 1; /* this interface is already in use */
2251         return 0;
2252 }
2253
2254
2255 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2256                                  const u8 *peer_addr,
2257                                  enum p2p_wps_method wps_method,
2258                                  int go_intent, const u8 *own_interface_addr,
2259                                  unsigned int force_freq, int persistent_group)
2260 {
2261         return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2262                            go_intent, own_interface_addr, force_freq,
2263                            persistent_group);
2264 }
2265
2266
2267 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2268                                 const u8 *peer_addr,
2269                                 enum p2p_wps_method wps_method,
2270                                 int go_intent, const u8 *own_interface_addr,
2271                                 unsigned int force_freq, int persistent_group)
2272 {
2273         return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2274                              go_intent, own_interface_addr, force_freq,
2275                              persistent_group);
2276 }
2277
2278
2279 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2280                                    struct wpa_scan_results *scan_res)
2281 {
2282         struct wpa_bss *bss;
2283
2284         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2285
2286         if (wpa_s->global->p2p_disabled)
2287                 return;
2288
2289         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
2290                    scan_res ? (int) scan_res->num : -1);
2291
2292         if (scan_res)
2293                 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2294
2295         bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2296         if (bss) {
2297                 u16 method;
2298
2299                 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2300                            "prior to joining an existing group (GO " MACSTR
2301                            " freq=%u MHz)",
2302                            MAC2STR(wpa_s->pending_join_dev_addr), bss->freq);
2303                 wpa_s->pending_pd_before_join = 1;
2304
2305                 switch (wpa_s->pending_join_wps_method) {
2306                 case WPS_PIN_LABEL:
2307                 case WPS_PIN_DISPLAY:
2308                         method = WPS_CONFIG_KEYPAD;
2309                         break;
2310                 case WPS_PIN_KEYPAD:
2311                         method = WPS_CONFIG_DISPLAY;
2312                         break;
2313                 case WPS_PBC:
2314                         method = WPS_CONFIG_PUSHBUTTON;
2315                         break;
2316                 default:
2317                         method = 0;
2318                         break;
2319                 }
2320
2321                 if (p2p_prov_disc_req(wpa_s->global->p2p,
2322                                       wpa_s->pending_join_dev_addr, method, 1)
2323                     < 0) {
2324                         wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2325                                    "Discovery Request before joining an "
2326                                    "existing group");
2327                         wpa_s->pending_pd_before_join = 0;
2328                         goto start;
2329                 }
2330
2331                 /*
2332                  * Actual join operation will be started from the Action frame
2333                  * TX status callback.
2334                  */
2335                 return;
2336         }
2337
2338         wpa_printf(MSG_DEBUG, "P2P: Target BSS/GO not yet in BSS table - "
2339                    "cannot send Provision Discovery Request");
2340
2341 start:
2342         /* Start join operation immediately */
2343         wpas_p2p_join_start(wpa_s);
2344 }
2345
2346
2347 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2348 {
2349         struct wpa_supplicant *wpa_s = eloop_ctx;
2350         int ret;
2351         struct wpa_driver_scan_params params;
2352         struct wpabuf *wps_ie, *ies;
2353
2354         os_memset(&params, 0, sizeof(params));
2355
2356         /* P2P Wildcard SSID */
2357         params.num_ssids = 1;
2358         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2359         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2360
2361         wpa_s->wps->dev.p2p = 1;
2362         wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2363                                         WPS_REQ_ENROLLEE);
2364         if (wps_ie == NULL) {
2365                 wpas_p2p_scan_res_join(wpa_s, NULL);
2366                 return;
2367         }
2368
2369         ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
2370         if (ies == NULL) {
2371                 wpabuf_free(wps_ie);
2372                 wpas_p2p_scan_res_join(wpa_s, NULL);
2373                 return;
2374         }
2375         wpabuf_put_buf(ies, wps_ie);
2376         wpabuf_free(wps_ie);
2377
2378         p2p_scan_ie(wpa_s->global->p2p, ies);
2379
2380         params.extra_ies = wpabuf_head(ies);
2381         params.extra_ies_len = wpabuf_len(ies);
2382
2383         /*
2384          * Run a scan to update BSS table and start Provision Discovery once
2385          * the new scan results become available.
2386          */
2387         wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2388         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
2389                 ret = ieee80211_sta_req_scan(wpa_s, &params);
2390         else
2391                 ret = wpa_drv_scan(wpa_s, &params);
2392
2393         wpabuf_free(ies);
2394
2395         if (ret) {
2396                 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2397                            "try again later");
2398                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2399                 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2400         }
2401 }
2402
2403
2404 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2405                          const u8 *dev_addr, enum p2p_wps_method wps_method)
2406 {
2407         wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2408                    MACSTR " dev " MACSTR ")",
2409                    MAC2STR(iface_addr), MAC2STR(dev_addr));
2410
2411         os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2412         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2413         wpa_s->pending_join_wps_method = wps_method;
2414
2415         /* Make sure we are not running find during connection establishment */
2416         wpas_p2p_stop_find(wpa_s);
2417
2418         wpas_p2p_join_scan(wpa_s, NULL);
2419         return 0;
2420 }
2421
2422
2423 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2424 {
2425         struct wpa_supplicant *group;
2426         struct p2p_go_neg_results res;
2427
2428         group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2429         if (group == NULL)
2430                 return -1;
2431         if (group != wpa_s) {
2432                 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2433                           sizeof(group->p2p_pin));
2434                 group->p2p_wps_method = wpa_s->p2p_wps_method;
2435         }
2436
2437         group->p2p_in_provisioning = 1;
2438
2439         os_memset(&res, 0, sizeof(res));
2440         os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2441                   ETH_ALEN);
2442         res.wps_method = wpa_s->pending_join_wps_method;
2443         wpas_start_wps_enrollee(group, &res);
2444
2445         return 0;
2446 }
2447
2448
2449 /**
2450  * wpas_p2p_connect - Request P2P Group Formation to be started
2451  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2452  * @peer_addr: Address of the peer P2P Device
2453  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2454  * @persistent_group: Whether to create a persistent group
2455  * @join: Whether to join an existing group (as a client) instead of starting
2456  *      Group Owner negotiation; @peer_addr is BSSID in that case
2457  * @auth: Whether to only authorize the connection instead of doing that and
2458  *      initiating Group Owner negotiation
2459  * @go_intent: GO Intent or -1 to use default
2460  * @freq: Frequency for the group or 0 for auto-selection
2461  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on failure
2462  */
2463 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2464                      const char *pin, enum p2p_wps_method wps_method,
2465                      int persistent_group, int join, int auth, int go_intent,
2466                      int freq)
2467 {
2468         int force_freq = 0;
2469         u8 bssid[ETH_ALEN];
2470         int ret = 0;
2471         enum wpa_driver_if_type iftype;
2472
2473         if (go_intent < 0)
2474                 go_intent = wpa_s->conf->p2p_go_intent;
2475
2476         if (!auth)
2477                 wpa_s->p2p_long_listen = 0;
2478
2479         wpa_s->p2p_wps_method = wps_method;
2480
2481         if (pin)
2482                 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2483         else if (wps_method == WPS_PIN_DISPLAY) {
2484                 ret = wps_generate_pin();
2485                 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2486                             ret);
2487                 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2488                            wpa_s->p2p_pin);
2489         } else
2490                 wpa_s->p2p_pin[0] = '\0';
2491
2492         if (join) {
2493                 u8 iface_addr[ETH_ALEN];
2494                 if (auth) {
2495                         wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
2496                                    "connect a running group from " MACSTR,
2497                                    MAC2STR(peer_addr));
2498                         os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
2499                         return ret;
2500                 }
2501                 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2502                                            iface_addr) < 0)
2503                         os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2504                 if (wpas_p2p_join(wpa_s, iface_addr, peer_addr, wps_method) <
2505                     0)
2506                         return -1;
2507                 return ret;
2508         }
2509
2510         if (freq > 0)
2511                 force_freq = freq;
2512         else if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2513                  wpa_s->assoc_freq)
2514                 force_freq = wpa_s->assoc_freq;
2515         else {
2516                 force_freq = wpa_drv_shared_freq(wpa_s);
2517                 if (force_freq < 0)
2518                         force_freq = 0;
2519         }
2520
2521         if (force_freq > 0) {
2522                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2523                            "channel we are already using (%u MHz) on another "
2524                            "interface", force_freq);
2525         }
2526
2527         wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2528
2529         if (!wpa_s->create_p2p_iface) {
2530                 if (auth) {
2531                         if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2532                                                  go_intent, wpa_s->own_addr,
2533                                                  force_freq, persistent_group)
2534                             < 0)
2535                                 return -1;
2536                         return ret;
2537                 }
2538                 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
2539                                           go_intent, wpa_s->own_addr,
2540                                           force_freq, persistent_group) < 0)
2541                         return -1;
2542                 return ret;
2543         }
2544
2545         /* Prepare to add a new interface for the group */
2546         iftype = WPA_IF_P2P_GROUP;
2547         if (join)
2548                 iftype = WPA_IF_P2P_CLIENT;
2549         else if (go_intent == 15)
2550                 iftype = WPA_IF_P2P_GO;
2551         if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
2552                 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2553                            "interface for the group");
2554                 return -1;
2555         }
2556
2557         if (auth) {
2558                 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2559                                          go_intent,
2560                                          wpa_s->pending_interface_addr,
2561                                          force_freq, persistent_group) < 0)
2562                         return -1;
2563                 return ret;
2564         }
2565         if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
2566                                   wpa_s->pending_interface_addr,
2567                                   force_freq, persistent_group) < 0) {
2568                 wpas_p2p_remove_pending_group_interface(wpa_s);
2569                 return -1;
2570         }
2571         return ret;
2572 }
2573
2574
2575 /**
2576  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2577  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2578  * @freq: Frequency of the channel in MHz
2579  * @duration: Duration of the stay on the channel in milliseconds
2580  *
2581  * This callback is called when the driver indicates that it has started the
2582  * requested remain-on-channel duration.
2583  */
2584 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2585                                    unsigned int freq, unsigned int duration)
2586 {
2587         wpa_s->roc_waiting_drv_freq = 0;
2588         wpa_s->off_channel_freq = freq;
2589         wpas_send_action_cb(wpa_s, NULL);
2590         if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
2591                 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
2592                               wpa_s->pending_listen_duration);
2593                 wpa_s->pending_listen_freq = 0;
2594         }
2595 }
2596
2597
2598 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
2599                                  unsigned int timeout)
2600 {
2601         /* Limit maximum Listen state time based on driver limitation. */
2602         if (timeout > wpa_s->max_remain_on_chan)
2603                 timeout = wpa_s->max_remain_on_chan;
2604
2605         return p2p_listen(wpa_s->global->p2p, timeout);
2606 }
2607
2608
2609 /**
2610  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
2611  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2612  * @freq: Frequency of the channel in MHz
2613  *
2614  * This callback is called when the driver indicates that a remain-on-channel
2615  * operation has been completed, i.e., the duration on the requested channel
2616  * has timed out.
2617  */
2618 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2619                                           unsigned int freq)
2620 {
2621         wpa_s->off_channel_freq = 0;
2622         if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
2623                 return; /* P2P module started a new operation */
2624         if (wpa_s->pending_action_tx)
2625                 return;
2626         if (wpa_s->p2p_long_listen > 0)
2627                 wpa_s->p2p_long_listen -= 5;
2628         if (wpa_s->p2p_long_listen > 0) {
2629                 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
2630                 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen * 1000);
2631         }
2632 }
2633
2634
2635 /**
2636  * wpas_p2p_group_remove - Remove a P2P group
2637  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2638  * @ifname: Network interface name of the group interface or "*" to remove all
2639  *      groups
2640  * Returns: 0 on success, -1 on failure
2641  *
2642  * This function is used to remove a P2P group. This can be used to disconnect
2643  * from a group in which the local end is a P2P Client or to end a P2P Group in
2644  * case the local end is the Group Owner. If a virtual network interface was
2645  * created for this group, that interface will be removed. Otherwise, only the
2646  * configured P2P group network will be removed from the interface.
2647  */
2648 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
2649 {
2650         struct wpa_global *global = wpa_s->global;
2651
2652         if (os_strcmp(ifname, "*") == 0) {
2653                 struct wpa_supplicant *prev;
2654                 wpa_s = global->ifaces;
2655                 while (wpa_s) {
2656                         prev = wpa_s;
2657                         wpa_s = wpa_s->next;
2658                         wpas_p2p_group_delete(prev);
2659                 }
2660                 return 0;
2661         }
2662
2663         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2664                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2665                         break;
2666         }
2667
2668         if (wpa_s == NULL)
2669                 return -1;
2670
2671         wpas_p2p_group_delete(wpa_s);
2672
2673         return 0;
2674 }
2675
2676
2677 static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
2678                                     struct p2p_go_neg_results *params,
2679                                     int freq)
2680 {
2681         u8 bssid[ETH_ALEN];
2682         int res;
2683
2684         os_memset(params, 0, sizeof(*params));
2685         params->role_go = 1;
2686         params->freq = 2412;
2687         if (freq)
2688                 params->freq = freq;
2689         else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
2690                  wpa_s->conf->p2p_oper_channel >= 1 &&
2691                  wpa_s->conf->p2p_oper_channel <= 11)
2692                 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
2693         else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
2694                  wpa_s->conf->p2p_oper_reg_class == 118)
2695                 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
2696         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2697             wpa_s->assoc_freq && !freq) {
2698                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2699                            "already using");
2700                 params->freq = wpa_s->assoc_freq;
2701         }
2702
2703         res = wpa_drv_shared_freq(wpa_s);
2704         if (res > 0 && !freq) {
2705                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2706                            "already using on a shared interface");
2707                 params->freq = res;
2708         }
2709 }
2710
2711
2712 static struct wpa_supplicant *
2713 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
2714                          int go)
2715 {
2716         struct wpa_supplicant *group_wpa_s;
2717
2718         if (!wpas_p2p_create_iface(wpa_s))
2719                 return wpa_s;
2720
2721         if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2722                                          WPA_IF_P2P_CLIENT) < 0)
2723                 return NULL;
2724         group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
2725         if (group_wpa_s == NULL) {
2726                 wpas_p2p_remove_pending_group_interface(wpa_s);
2727                 return NULL;
2728         }
2729
2730         return group_wpa_s;
2731 }
2732
2733
2734 /**
2735  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
2736  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2737  * @persistent_group: Whether to create a persistent group
2738  * @freq: Frequency for the group or 0 to indicate no hardcoding
2739  * Returns: 0 on success, -1 on failure
2740  *
2741  * This function creates a new P2P group with the local end as the Group Owner,
2742  * i.e., without using Group Owner Negotiation.
2743  */
2744 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
2745                        int freq)
2746 {
2747         struct p2p_go_neg_results params;
2748
2749         wpas_p2p_init_go_params(wpa_s, &params, freq);
2750         p2p_go_params(wpa_s->global->p2p, &params);
2751         params.persistent_group = persistent_group;
2752
2753         wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
2754         if (wpa_s == NULL)
2755                 return -1;
2756         wpas_start_wps_go(wpa_s, &params, 0);
2757
2758         return 0;
2759 }
2760
2761
2762 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
2763                                  struct wpa_ssid *params, int addr_allocated)
2764 {
2765         struct wpa_ssid *ssid;
2766
2767         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
2768         if (wpa_s == NULL)
2769                 return -1;
2770
2771         wpa_supplicant_ap_deinit(wpa_s);
2772
2773         ssid = wpa_config_add_network(wpa_s->conf);
2774         if (ssid == NULL)
2775                 return -1;
2776         wpas_notify_network_added(wpa_s, ssid);
2777         wpa_config_set_network_defaults(ssid);
2778         ssid->temporary = 1;
2779         ssid->proto = WPA_PROTO_RSN;
2780         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
2781         ssid->group_cipher = WPA_CIPHER_CCMP;
2782         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2783         ssid->ssid = os_malloc(params->ssid_len);
2784         if (ssid->ssid == NULL) {
2785                 wpas_notify_network_removed(wpa_s, ssid);
2786                 wpa_config_remove_network(wpa_s->conf, ssid->id);
2787                 return -1;
2788         }
2789         os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2790         ssid->ssid_len = params->ssid_len;
2791         ssid->p2p_group = 1;
2792         if (params->psk_set) {
2793                 os_memcpy(ssid->psk, params->psk, 32);
2794                 ssid->psk_set = 1;
2795         }
2796         if (params->passphrase)
2797                 ssid->passphrase = os_strdup(params->passphrase);
2798
2799         wpa_supplicant_select_network(wpa_s, ssid);
2800
2801         wpa_s->show_group_started = 1;
2802
2803         return 0;
2804 }
2805
2806
2807 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
2808                                   struct wpa_ssid *ssid, int addr_allocated,
2809                                   int freq)
2810 {
2811         struct p2p_go_neg_results params;
2812
2813         if (ssid->disabled != 2 || ssid->ssid == NULL)
2814                 return -1;
2815
2816         wpa_s->p2p_long_listen = 0;
2817         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2818
2819         if (ssid->mode == WPAS_MODE_INFRA)
2820                 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
2821
2822         if (ssid->mode != WPAS_MODE_P2P_GO)
2823                 return -1;
2824
2825         wpas_p2p_init_go_params(wpa_s, &params, freq);
2826
2827         params.role_go = 1;
2828         if (ssid->passphrase == NULL ||
2829             os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
2830                 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
2831                            "group");
2832                 return -1;
2833         }
2834         os_strlcpy(params.passphrase, ssid->passphrase,
2835                    sizeof(params.passphrase));
2836         os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
2837         params.ssid_len = ssid->ssid_len;
2838         params.persistent_group = 1;
2839
2840         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
2841         if (wpa_s == NULL)
2842                 return -1;
2843
2844         wpas_start_wps_go(wpa_s, &params, 0);
2845
2846         return 0;
2847 }
2848
2849
2850 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
2851                                struct wpabuf *proberesp_ies)
2852 {
2853         struct wpa_supplicant *wpa_s = ctx;
2854         if (wpa_s->ap_iface) {
2855                 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
2856                 if (beacon_ies) {
2857                         wpabuf_free(hapd->p2p_beacon_ie);
2858                         hapd->p2p_beacon_ie = beacon_ies;
2859                 }
2860                 wpabuf_free(hapd->p2p_probe_resp_ie);
2861                 hapd->p2p_probe_resp_ie = proberesp_ies;
2862         } else {
2863                 wpabuf_free(beacon_ies);
2864                 wpabuf_free(proberesp_ies);
2865         }
2866         wpa_supplicant_ap_update_beacon(wpa_s);
2867 }
2868
2869
2870 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
2871                                        int persistent_group,
2872                                        int group_formation)
2873 {
2874         struct p2p_group *group;
2875         struct p2p_group_config *cfg;
2876
2877         cfg = os_zalloc(sizeof(*cfg));
2878         if (cfg == NULL)
2879                 return NULL;
2880
2881         cfg->persistent_group = persistent_group;
2882         os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
2883         cfg->cb_ctx = wpa_s;
2884         cfg->ie_update = wpas_p2p_ie_update;
2885
2886         group = p2p_group_init(wpa_s->global->p2p, cfg);
2887         if (group == NULL)
2888                 os_free(cfg);
2889         if (!group_formation)
2890                 p2p_group_notif_formation_done(group);
2891         wpa_s->p2p_group = group;
2892         return group;
2893 }
2894
2895
2896 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2897                           int registrar)
2898 {
2899         if (!wpa_s->p2p_in_provisioning) {
2900                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
2901                            "provisioning not in progress");
2902                 return;
2903         }
2904
2905         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
2906                              NULL);
2907         if (wpa_s->global->p2p)
2908                 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
2909         wpas_group_formation_completed(wpa_s, 1);
2910 }
2911
2912
2913 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2914                        const char *config_method)
2915 {
2916         u16 config_methods;
2917
2918         if (os_strcmp(config_method, "display") == 0)
2919                 config_methods = WPS_CONFIG_DISPLAY;
2920         else if (os_strcmp(config_method, "keypad") == 0)
2921                 config_methods = WPS_CONFIG_KEYPAD;
2922         else if (os_strcmp(config_method, "pbc") == 0 ||
2923                  os_strcmp(config_method, "pushbutton") == 0)
2924                 config_methods = WPS_CONFIG_PUSHBUTTON;
2925         else
2926                 return -1;
2927
2928         if (wpa_s->global->p2p == NULL)
2929                 return -1;
2930
2931         return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
2932                                  config_methods, 0);
2933 }
2934
2935
2936 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
2937                               char *end)
2938 {
2939         return p2p_scan_result_text(ies, ies_len, buf, end);
2940 }
2941
2942
2943 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
2944                   enum p2p_discovery_type type)
2945 {
2946         wpa_s->p2p_long_listen = 0;
2947
2948         return p2p_find(wpa_s->global->p2p, timeout, type);
2949 }
2950
2951
2952 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
2953 {
2954         wpa_s->p2p_long_listen = 0;
2955         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2956
2957         p2p_stop_find(wpa_s->global->p2p);
2958
2959         wpas_p2p_remove_pending_group_interface(wpa_s);
2960 }
2961
2962
2963 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
2964 {
2965         struct wpa_supplicant *wpa_s = eloop_ctx;
2966         wpa_s->p2p_long_listen = 0;
2967 }
2968
2969
2970 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
2971 {
2972         int res;
2973
2974         if (timeout == 0) {
2975                 /*
2976                  * This is a request for unlimited Listen state. However, at
2977                  * least for now, this is mapped to a Listen state for one
2978                  * hour.
2979                  */
2980                 timeout = 3600;
2981         }
2982         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2983         wpa_s->p2p_long_listen = 0;
2984
2985         res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
2986         if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
2987                 wpa_s->p2p_long_listen = timeout;
2988                 eloop_register_timeout(timeout, 0,
2989                                        wpas_p2p_long_listen_timeout,
2990                                        wpa_s, NULL);
2991         }
2992
2993         return res;
2994 }
2995
2996
2997 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, const u8 *bssid,
2998                           u8 *buf, size_t len, int p2p_group)
2999 {
3000         if (wpa_s->global->p2p_disabled)
3001                 return -1;
3002         if (wpa_s->global->p2p == NULL)
3003                 return -1;
3004
3005         return p2p_assoc_req_ie(wpa_s->global->p2p, bssid, buf, len,
3006                                 p2p_group);
3007 }
3008
3009
3010 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3011                           const u8 *ie, size_t ie_len)
3012 {
3013         if (wpa_s->global->p2p_disabled)
3014                 return 0;
3015         if (wpa_s->global->p2p == NULL)
3016                 return 0;
3017
3018         return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
3019 }
3020
3021
3022 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3023                         const u8 *sa, const u8 *bssid,
3024                         u8 category, const u8 *data, size_t len, int freq)
3025 {
3026         if (wpa_s->global->p2p_disabled)
3027                 return;
3028         if (wpa_s->global->p2p == NULL)
3029                 return;
3030
3031         p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3032                       freq);
3033 }
3034
3035
3036 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3037 {
3038         if (wpa_s->global->p2p_disabled)
3039                 return;
3040         if (wpa_s->global->p2p == NULL)
3041                 return;
3042
3043         p2p_scan_ie(wpa_s->global->p2p, ies);
3044 }
3045
3046
3047 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3048 {
3049         p2p_group_deinit(wpa_s->p2p_group);
3050         wpa_s->p2p_group = NULL;
3051 }
3052
3053
3054 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3055 {
3056         wpa_s->p2p_long_listen = 0;
3057
3058         return p2p_reject(wpa_s->global->p2p, addr);
3059 }
3060
3061
3062 /* Invite to reinvoke a persistent group */
3063 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3064                     struct wpa_ssid *ssid, const u8 *go_dev_addr)
3065 {
3066         enum p2p_invite_role role;
3067         u8 *bssid = NULL;
3068
3069         if (ssid->mode == WPAS_MODE_P2P_GO) {
3070                 role = P2P_INVITE_ROLE_GO;
3071                 if (peer_addr == NULL) {
3072                         wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3073                                    "address in invitation command");
3074                         return -1;
3075                 }
3076                 if (wpas_p2p_create_iface(wpa_s)) {
3077                         if (wpas_p2p_add_group_interface(wpa_s,
3078                                                          WPA_IF_P2P_GO) < 0) {
3079                                 wpa_printf(MSG_ERROR, "P2P: Failed to "
3080                                            "allocate a new interface for the "
3081                                            "group");
3082                                 return -1;
3083                         }
3084                         bssid = wpa_s->pending_interface_addr;
3085                 } else
3086                         bssid = wpa_s->own_addr;
3087         } else {
3088                 role = P2P_INVITE_ROLE_CLIENT;
3089                 peer_addr = ssid->bssid;
3090         }
3091         wpa_s->pending_invite_ssid_id = ssid->id;
3092
3093         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3094                           ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
3095 }
3096
3097
3098 /* Invite to join an active group */
3099 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
3100                           const u8 *peer_addr, const u8 *go_dev_addr)
3101 {
3102         struct wpa_global *global = wpa_s->global;
3103         enum p2p_invite_role role;
3104         u8 *bssid = NULL;
3105         struct wpa_ssid *ssid;
3106
3107         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3108                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3109                         break;
3110         }
3111         if (wpa_s == NULL) {
3112                 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
3113                 return -1;
3114         }
3115
3116         ssid = wpa_s->current_ssid;
3117         if (ssid == NULL) {
3118                 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
3119                            "invitation");
3120                 return -1;
3121         }
3122
3123         if (ssid->mode == WPAS_MODE_P2P_GO) {
3124                 role = P2P_INVITE_ROLE_ACTIVE_GO;
3125                 bssid = wpa_s->own_addr;
3126                 if (go_dev_addr == NULL)
3127                         go_dev_addr = wpa_s->parent->own_addr;
3128         } else {
3129                 role = P2P_INVITE_ROLE_CLIENT;
3130                 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
3131                         wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
3132                                    "invite to current group");
3133                         return -1;
3134                 }
3135                 bssid = wpa_s->bssid;
3136                 if (go_dev_addr == NULL &&
3137                     !is_zero_ether_addr(wpa_s->go_dev_addr))
3138                         go_dev_addr = wpa_s->go_dev_addr;
3139         }
3140         wpa_s->pending_invite_ssid_id = -1;
3141
3142         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3143                           ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 0);
3144 }
3145
3146
3147 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3148 {
3149         struct wpa_ssid *ssid = wpa_s->current_ssid;
3150         const char *ssid_txt;
3151         u8 go_dev_addr[ETH_ALEN];
3152         int persistent;
3153
3154         if (!wpa_s->show_group_started || !ssid)
3155                 return;
3156
3157         wpa_s->show_group_started = 0;
3158
3159         ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3160         os_memset(go_dev_addr, 0, ETH_ALEN);
3161         if (ssid->bssid_set)
3162                 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3163         persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3164                                                ssid->ssid_len);
3165         os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3166
3167         if (ssid->passphrase == NULL && ssid->psk_set) {
3168                 char psk[65];
3169                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3170                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3171                         "%s client ssid=\"%s\" psk=%s go_dev_addr=" MACSTR
3172                         "%s",
3173                         wpa_s->ifname, ssid_txt, psk, MAC2STR(go_dev_addr),
3174                         persistent ? " [PERSISTENT]" : "");
3175         } else {
3176                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3177                         "%s client ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
3178                         MACSTR "%s",
3179                         wpa_s->ifname, ssid_txt,
3180                         ssid->passphrase ? ssid->passphrase : "",
3181                         MAC2STR(go_dev_addr),
3182                         persistent ? " [PERSISTENT]" : "");
3183         }
3184
3185         if (persistent)
3186                 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
3187                                                 go_dev_addr);
3188 }
3189
3190
3191 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3192                           u32 interval1, u32 duration2, u32 interval2)
3193 {
3194         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3195             wpa_s->current_ssid == NULL ||
3196             wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3197                 return -1;
3198
3199         return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3200                                 wpa_s->own_addr, wpa_s->assoc_freq,
3201                                 duration1, interval1, duration2, interval2);
3202 }
3203
3204
3205 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3206                         unsigned int interval)
3207 {
3208         return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3209 }
3210
3211
3212 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3213                            u16 reason_code, const u8 *ie, size_t ie_len)
3214 {
3215         if (wpa_s->global->p2p_disabled)
3216                 return;
3217
3218         p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3219 }
3220
3221
3222 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3223                              u16 reason_code, const u8 *ie, size_t ie_len)
3224 {
3225         if (wpa_s->global->p2p_disabled)
3226                 return;
3227
3228         p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3229 }
3230
3231
3232 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3233 {
3234         struct p2p_data *p2p = wpa_s->global->p2p;
3235
3236         if (p2p == NULL)
3237                 return;
3238
3239         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
3240                 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
3241
3242         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
3243                 u8 pri_dev_type[8];
3244                 if (wpa_s->conf->device_type) {
3245                         if (wps_dev_type_str2bin(wpa_s->conf->device_type,
3246                                                  pri_dev_type) < 0) {
3247                                 wpa_printf(MSG_ERROR, "P2P: Invalid "
3248                                            "device_type");
3249                         } else
3250                                 p2p_set_pri_dev_type(p2p, pri_dev_type);
3251                 }
3252         }
3253
3254         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
3255                 u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
3256                 size_t num = 0;
3257                 int i;
3258                 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
3259                         if (wpa_s->conf->sec_device_type[i] == NULL)
3260                                 continue;
3261                         if (wps_dev_type_str2bin(
3262                                     wpa_s->conf->sec_device_type[i],
3263                                     sec_dev_type[num]) < 0) {
3264                                 wpa_printf(MSG_ERROR, "P2P: Invalid "
3265                                            "sec_device_type");
3266                                 continue;
3267                         }
3268                         num++;
3269                         if (num == P2P_SEC_DEVICE_TYPES)
3270                                 break;
3271                 }
3272                 p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num);
3273         }
3274
3275         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3276             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3277                 char country[3];
3278                 country[0] = wpa_s->conf->country[0];
3279                 country[1] = wpa_s->conf->country[1];
3280                 country[2] = 0x04;
3281                 p2p_set_country(p2p, country);
3282         }
3283
3284         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
3285                 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
3286                                      wpa_s->conf->p2p_ssid_postfix ?
3287                                      os_strlen(wpa_s->conf->p2p_ssid_postfix) :
3288                                      0);
3289         }
3290 }