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