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