P2P: Use operating frequency from peer table as backup for join
[libeap.git] / src / p2p / p2p.h
1 /*
2  * Wi-Fi Direct - P2P module
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 #ifndef P2P_H
16 #define P2P_H
17
18 /**
19  * P2P_MAX_REG_CLASSES - Maximum number of regulatory classes
20  */
21 #define P2P_MAX_REG_CLASSES 10
22
23 /**
24  * P2P_MAX_REG_CLASS_CHANNELS - Maximum number of channels per regulatory class
25  */
26 #define P2P_MAX_REG_CLASS_CHANNELS 20
27
28 /**
29  * struct p2p_channels - List of supported channels
30  */
31 struct p2p_channels {
32         /**
33          * struct p2p_reg_class - Supported regulatory class
34          */
35         struct p2p_reg_class {
36                 /**
37                  * reg_class - Regulatory class (IEEE 802.11-2007, Annex J)
38                  */
39                 u8 reg_class;
40
41                 /**
42                  * channel - Supported channels
43                  */
44                 u8 channel[P2P_MAX_REG_CLASS_CHANNELS];
45
46                 /**
47                  * channels - Number of channel entries in use
48                  */
49                 size_t channels;
50         } reg_class[P2P_MAX_REG_CLASSES];
51
52         /**
53          * reg_classes - Number of reg_class entries in use
54          */
55         size_t reg_classes;
56 };
57
58 enum p2p_wps_method {
59         WPS_NOT_READY, WPS_PIN_LABEL, WPS_PIN_DISPLAY, WPS_PIN_KEYPAD, WPS_PBC
60 };
61
62 /**
63  * struct p2p_go_neg_results - P2P Group Owner Negotiation results
64  */
65 struct p2p_go_neg_results {
66         /**
67          * status - Negotiation result (Status Code)
68          *
69          * 0 (P2P_SC_SUCCESS) indicates success. Non-zero values indicate
70          * failed negotiation.
71          */
72         int status;
73
74         /**
75          * role_go - Whether local end is Group Owner
76          */
77         int role_go;
78
79         /**
80          * freq - Frequency of the group operational channel in MHz
81          */
82         int freq;
83
84         /**
85          * ssid - SSID of the group
86          */
87         u8 ssid[32];
88
89         /**
90          * ssid_len - Length of SSID in octets
91          */
92         size_t ssid_len;
93
94         /**
95          * passphrase - WPA2-Personal passphrase for the group (GO only)
96          */
97         char passphrase[64];
98
99         /**
100          * peer_device_addr - P2P Device Address of the peer
101          */
102         u8 peer_device_addr[ETH_ALEN];
103
104         /**
105          * peer_interface_addr - P2P Interface Address of the peer
106          */
107         u8 peer_interface_addr[ETH_ALEN];
108
109         /**
110          * wps_method - WPS method to be used during provisioning
111          */
112         enum p2p_wps_method wps_method;
113
114 #define P2P_MAX_CHANNELS 50
115
116         /**
117          * freq_list - Zero-terminated list of possible operational channels
118          */
119         int freq_list[P2P_MAX_CHANNELS];
120
121         /**
122          * persistent_group - Whether the group should be made persistent
123          */
124         int persistent_group;
125 };
126
127 struct p2p_data;
128
129 enum p2p_scan_type {
130         P2P_SCAN_SOCIAL,
131         P2P_SCAN_FULL,
132         P2P_SCAN_SPECIFIC,
133         P2P_SCAN_SOCIAL_PLUS_ONE
134 };
135
136 /**
137  * struct p2p_config - P2P configuration
138  *
139  * This configuration is provided to the P2P module during initialization with
140  * p2p_init().
141  */
142 struct p2p_config {
143         /**
144          * country - Country code to use in P2P operations
145          */
146         char country[3];
147
148         /**
149          * reg_class - Regulatory class for own listen channel
150          */
151         u8 reg_class;
152
153         /**
154          * channel - Own listen channel
155          */
156         u8 channel;
157
158         /**
159          * Regulatory class for own operational channel
160          */
161         u8 op_reg_class;
162
163         /**
164          * op_channel - Own operational channel
165          */
166         u8 op_channel;
167
168         /**
169          * channels - Own supported regulatory classes and channels
170          *
171          * List of supposerted channels per regulatory class. The regulatory
172          * classes are defined in IEEE Std 802.11-2007 Annex J and the
173          * numbering of the clases depends on the configured country code.
174          */
175         struct p2p_channels channels;
176
177         /**
178          * pri_dev_type - Primary Device Type (see WPS)
179          */
180         u8 pri_dev_type[8];
181
182         /**
183          * P2P_SEC_DEVICE_TYPES - Maximum number of secondary device types
184          */
185 #define P2P_SEC_DEVICE_TYPES 5
186
187         /**
188          * sec_dev_type - Optional secondary device types
189          */
190         u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
191
192         /**
193          * dev_addr - P2P Device Address
194          */
195         u8 dev_addr[ETH_ALEN];
196
197         /**
198          * dev_name - Device Name
199          */
200         char *dev_name;
201
202         /**
203          * num_sec_dev_types - Number of sec_dev_type entries
204          */
205         size_t num_sec_dev_types;
206
207         /**
208          * concurrent_operations - Whether concurrent operations are supported
209          */
210         int concurrent_operations;
211
212         /**
213          * max_peers - Maximum number of discovered peers to remember
214          *
215          * If more peers are discovered, older entries will be removed to make
216          * room for the new ones.
217          */
218         size_t max_peers;
219
220         /**
221          * ssid_postfix - Postfix data to add to the SSID
222          *
223          * This data will be added to the end of the SSID after the
224          * DIRECT-<random two octets> prefix.
225          */
226         u8 ssid_postfix[32 - 9];
227
228         /**
229          * ssid_postfix_len - Length of the ssid_postfix data
230          */
231         size_t ssid_postfix_len;
232
233         /**
234          * msg_ctx - Context to use with wpa_msg() calls
235          */
236         void *msg_ctx;
237
238         /**
239          * cb_ctx - Context to use with callback functions
240          */
241         void *cb_ctx;
242
243
244         /* Callbacks to request lower layer driver operations */
245
246         /**
247          * p2p_scan - Request a P2P scan/search
248          * @ctx: Callback context from cb_ctx
249          * @type: Scan type
250          * @freq: Specific frequency (MHz) to scan or 0 for no restriction
251          * Returns: 0 on success, -1 on failure
252          *
253          * This callback function is used to request a P2P scan or search
254          * operation to be completed. Type type argument specifies which type
255          * of scan is to be done. @P2P_SCAN_SOCIAL indicates that only the
256          * social channels (1, 6, 11) should be scanned. @P2P_SCAN_FULL
257          * indicates that all channels are to be scanned. @P2P_SCAN_SPECIFIC
258          * request a scan of a single channel specified by freq.
259          * @P2P_SCAN_SOCIAL_PLUS_ONE request scan of all the social channels
260          * plus one extra channel specified by freq.
261          *
262          * The full scan is used for the initial scan to find group owners from
263          * all. The other types are used during search phase scan of the social
264          * channels (with potential variation if the Listen channel of the
265          * target peer is known or if other channels are scanned in steps).
266          *
267          * The scan results are returned after this call by calling
268          * p2p_scan_res_handler() for each scan result that has a P2P IE and
269          * then calling p2p_scan_res_handled() to indicate that all scan
270          * results have been indicated.
271          */
272         int (*p2p_scan)(void *ctx, enum p2p_scan_type type, int freq);
273
274         /**
275          * send_probe_resp - Transmit a Probe Response frame
276          * @ctx: Callback context from cb_ctx
277          * @buf: Probe Response frame (including the header and body)
278          * Returns: 0 on success, -1 on failure
279          *
280          * This function is used to reply to Probe Request frames that were
281          * indicated with a call to p2p_probe_req_rx(). The response is to be
282          * sent on the same channel or to be dropped if the driver is not
283          * anymore listening to Probe Request frames.
284          *
285          * Alternatively, the responsibility for building the Probe Response
286          * frames in Listen state may be in another system component in which
287          * case this function need to be implemented (i.e., the function
288          * pointer can be %NULL). The WPS and P2P IEs to be added for Probe
289          * Response frames in such a case are available from the
290          * start_listen() callback. It should be noted that the received Probe
291          * Request frames must be indicated by calling p2p_probe_req_rx() even
292          * if this send_probe_resp() is not used.
293          */
294         int (*send_probe_resp)(void *ctx, const struct wpabuf *buf);
295
296         /**
297          * send_action - Transmit an Action frame
298          * @ctx: Callback context from cb_ctx
299          * @freq: Frequency in MHz for the channel on which to transmit
300          * @dst: Destination MAC address (Address 1)
301          * @src: Source MAC address (Address 2)
302          * @bssid: BSSID (Address 3)
303          * @buf: Frame body (starting from Category field)
304          * @len: Length of buf in octets
305          * @wait_time: How many msec to wait for a response frame
306          * Returns: 0 on success, -1 on failure
307          *
308          * The Action frame may not be transmitted immediately and the status
309          * of the transmission must be reported by calling
310          * p2p_send_action_cb() once the frame has either been transmitted or
311          * it has been dropped due to excessive retries or other failure to
312          * transmit.
313          */
314         int (*send_action)(void *ctx, unsigned int freq, const u8 *dst,
315                            const u8 *src, const u8 *bssid, const u8 *buf,
316                            size_t len, unsigned int wait_time);
317
318         /**
319          * send_action_done - Notify that Action frame sequence was completed
320          * @ctx: Callback context from cb_ctx
321          *
322          * This function is called when the Action frame sequence that was
323          * started with send_action() has been completed, i.e., when there is
324          * no need to wait for a response from the destination peer anymore.
325          */
326         void (*send_action_done)(void *ctx);
327
328         /**
329          * start_listen - Start Listen state
330          * @ctx: Callback context from cb_ctx
331          * @freq: Frequency of the listen channel in MHz
332          * @duration: Duration for the Listen state in milliseconds
333          * @probe_resp_ie: IE(s) to be added to Probe Response frames
334          * Returns: 0 on success, -1 on failure
335          *
336          * This Listen state may not start immediately since the driver may
337          * have other pending operations to complete first. Once the Listen
338          * state has started, p2p_listen_cb() must be called to notify the P2P
339          * module. Once the Listen state is stopped, p2p_listen_end() must be
340          * called to notify the P2P module that the driver is not in the Listen
341          * state anymore.
342          *
343          * If the send_probe_resp() is not used for generating the response,
344          * the IEs from probe_resp_ie need to be added to the end of the Probe
345          * Response frame body. If send_probe_resp() is used, the probe_resp_ie
346          * information can be ignored.
347          */
348         int (*start_listen)(void *ctx, unsigned int freq,
349                             unsigned int duration,
350                             const struct wpabuf *probe_resp_ie);
351         /**
352          * stop_listen - Stop Listen state
353          * @ctx: Callback context from cb_ctx
354          *
355          * This callback can be used to stop a Listen state operation that was
356          * previously requested with start_listen().
357          */
358         void (*stop_listen)(void *ctx);
359
360         /**
361          * get_noa - Get current Notice of Absence attribute payload
362          * @ctx: Callback context from cb_ctx
363          * @interface_addr: P2P Interface Address of the GO
364          * @buf: Buffer for returning NoA
365          * @buf_len: Buffer length in octets
366          * Returns: Number of octets used in buf, 0 to indicate no NoA is being
367          * advertized, or -1 on failure
368          *
369          * This function is used to fetch the current Notice of Absence
370          * attribute value from GO.
371          */
372         int (*get_noa)(void *ctx, const u8 *interface_addr, u8 *buf,
373                        size_t buf_len);
374
375         /* Callbacks to notify events to upper layer management entity */
376
377         /**
378          * dev_found - Notification of a found P2P Device
379          * @ctx: Callback context from cb_ctx
380          * @addr: Source address of the message triggering this notification
381          * @dev_addr: P2P Device Address of the found P2P Device
382          * @pri_dev_type: Primary Device Type
383          * @dev_name: Device Name
384          * @config_methods: Configuration Methods
385          * @dev_capab: Device Capabilities
386          * @group_capab: Group Capabilities
387          *
388          * This callback is used to notify that a new P2P Device has been
389          * found. This may happen, e.g., during Search state based on scan
390          * results or during Listen state based on receive Probe Request and
391          * Group Owner Negotiation Request.
392          */
393         void (*dev_found)(void *ctx, const u8 *addr, const u8 *dev_addr,
394                           const u8 *pri_dev_type, const char *dev_name,
395                           u16 config_methods, u8 dev_capab, u8 group_capab);
396
397         /**
398          * go_neg_req_rx - Notification of a receive GO Negotiation Request
399          * @ctx: Callback context from cb_ctx
400          * @src: Source address of the message triggering this notification
401          * @dev_passwd_id: WPS Device Password ID
402          *
403          * This callback is used to notify that a P2P Device is requesting
404          * group owner negotiation with us, but we do not have all the
405          * necessary information to start GO Negotiation. This indicates that
406          * the local user has not authorized the connection yet by providing a
407          * PIN or PBC button press. This information can be provided with a
408          * call to p2p_connect().
409          */
410         void (*go_neg_req_rx)(void *ctx, const u8 *src, u16 dev_passwd_id);
411
412         /**
413          * go_neg_completed - Notification of GO Negotiation results
414          * @ctx: Callback context from cb_ctx
415          * @res: GO Negotiation results
416          *
417          * This callback is used to notify that Group Owner Negotiation has
418          * been completed. Non-zero struct p2p_go_neg_results::status indicates
419          * failed negotiation. In case of success, this function is responsible
420          * for creating a new group interface (or using the existing interface
421          * depending on driver features), setting up the group interface in
422          * proper mode based on struct p2p_go_neg_results::role_go and
423          * initializing WPS provisioning either as a Registrar (if GO) or as an
424          * Enrollee. Successful WPS provisioning must be indicated by calling
425          * p2p_wps_success_cb(). The callee is responsible for timing out group
426          * formation if WPS provisioning cannot be completed successfully
427          * within 15 seconds.
428          */
429         void (*go_neg_completed)(void *ctx, struct p2p_go_neg_results *res);
430
431         /**
432          * sd_request - Callback on Service Discovery Request
433          * @ctx: Callback context from cb_ctx
434          * @freq: Frequency (in MHz) of the channel
435          * @sa: Source address of the request
436          * @dialog_token: Dialog token
437          * @update_indic: Service Update Indicator from the source of request
438          * @tlvs: P2P Service Request TLV(s)
439          * @tlvs_len: Length of tlvs buffer in octets
440          *
441          * This callback is used to indicate reception of a service discovery
442          * request. Response to the query must be indicated by calling
443          * p2p_sd_response() with the context information from the arguments to
444          * this callback function.
445          *
446          * This callback handler can be set to %NULL to indicate that service
447          * discovery is not supported.
448          */
449         void (*sd_request)(void *ctx, int freq, const u8 *sa, u8 dialog_token,
450                            u16 update_indic, const u8 *tlvs, size_t tlvs_len);
451
452         /**
453          * sd_response - Callback on Service Discovery Response
454          * @ctx: Callback context from cb_ctx
455          * @sa: Source address of the request
456          * @update_indic: Service Update Indicator from the source of response
457          * @tlvs: P2P Service Response TLV(s)
458          * @tlvs_len: Length of tlvs buffer in octets
459          *
460          * This callback is used to indicate reception of a service discovery
461          * response. This callback handler can be set to %NULL if no service
462          * discovery requests are used. The information provided with this call
463          * is replies to the queries scheduled with p2p_sd_request().
464          */
465         void (*sd_response)(void *ctx, const u8 *sa, u16 update_indic,
466                             const u8 *tlvs, size_t tlvs_len);
467
468         /**
469          * prov_disc_req - Callback on Provisiong Discovery Request
470          * @ctx: Callback context from cb_ctx
471          * @peer: Source address of the request
472          * @config_methods: Requested WPS Config Method
473          * @dev_addr: P2P Device Address of the found P2P Device
474          * @pri_dev_type: Primary Device Type
475          * @dev_name: Device Name
476          * @supp_config_methods: Supported configuration Methods
477          * @dev_capab: Device Capabilities
478          * @group_capab: Group Capabilities
479          *
480          * This callback is used to indicate reception of a Provision Discovery
481          * Request frame that the P2P module accepted.
482          */
483         void (*prov_disc_req)(void *ctx, const u8 *peer, u16 config_methods,
484                               const u8 *dev_addr, const u8 *pri_dev_type,
485                               const char *dev_name, u16 supp_config_methods,
486                               u8 dev_capab, u8 group_capab);
487
488         /**
489          * prov_disc_resp - Callback on Provisiong Discovery Response
490          * @ctx: Callback context from cb_ctx
491          * @peer: Source address of the response
492          * @config_methods: Value from p2p_prov_disc_req() or 0 on failure
493          *
494          * This callback is used to indicate reception of a Provision Discovery
495          * Response frame for a pending request scheduled with
496          * p2p_prov_disc_req(). This callback handler can be set to %NULL if
497          * provision discovery is not used.
498          */
499         void (*prov_disc_resp)(void *ctx, const u8 *peer, u16 config_methods);
500
501         /**
502          * invitation_process - Optional callback for processing Invitations
503          * @ctx: Callback context from cb_ctx
504          * @sa: Source address of the Invitation Request
505          * @bssid: P2P Group BSSID from the request or %NULL if not included
506          * @go_dev_addr: GO Device Address from P2P Group ID
507          * @ssid: SSID from P2P Group ID
508          * @ssid_len: Length of ssid buffer in octets
509          * @go: Variable for returning whether the local end is GO in the group
510          * @group_bssid: Buffer for returning P2P Group BSSID (if local end GO)
511          * @force_freq: Variable for returning forced frequency for the group
512          * @persistent_group: Whether this is an invitation to reinvoke a
513          *      persistent group (instead of invitation to join an active
514          *      group)
515          * Returns: Status code (P2P_SC_*)
516          *
517          * This optional callback can be used to implement persistent reconnect
518          * by allowing automatic restarting of persistent groups without user
519          * interaction. If this callback is not implemented (i.e., is %NULL),
520          * the received Invitation Request frames are replied with
521          * %P2P_SC_REQ_RECEIVED status and indicated to upper layer with the
522          * invitation_result() callback.
523          *
524          * If the requested parameters are acceptable and the group is known,
525          * %P2P_SC_SUCCESS may be returned. If the requested group is unknown,
526          * %P2P_SC_FAIL_UNKNOWN_GROUP should be returned. %P2P_SC_REQ_RECEIVED
527          * can be returned if there is not enough data to provide immediate
528          * response, i.e., if some sort of user interaction is needed. The
529          * invitation_received() callback will be called in that case
530          * immediately after this call.
531          */
532         u8 (*invitation_process)(void *ctx, const u8 *sa, const u8 *bssid,
533                                  const u8 *go_dev_addr, const u8 *ssid,
534                                  size_t ssid_len, int *go, u8 *group_bssid,
535                                  int *force_freq, int persistent_group);
536
537         /**
538          * invitation_received - Callback on Invitation Request RX
539          * @ctx: Callback context from cb_ctx
540          * @sa: Source address of the Invitation Request
541          * @bssid: P2P Group BSSID or %NULL if not received
542          * @ssid: SSID of the group
543          * @ssid_len: Length of ssid in octets
544          * @go_dev_addr: GO Device Address
545          * @status: Response Status
546          * @op_freq: Operational frequency for the group
547          *
548          * This callback is used to indicate sending of an Invitation Response
549          * for a received Invitation Request. If status == 0 (success), the
550          * upper layer code is responsible for starting the group. status == 1
551          * indicates need to get user authorization for the group. Other status
552          * values indicate that the invitation request was rejected.
553          */
554         void (*invitation_received)(void *ctx, const u8 *sa, const u8 *bssid,
555                                     const u8 *ssid, size_t ssid_len,
556                                     const u8 *go_dev_addr, u8 status,
557                                     int op_freq);
558
559         /**
560          * invitation_result - Callback on Invitation result
561          * @ctx: Callback context from cb_ctx
562          * @status: Negotiation result (Status Code)
563          * @bssid: P2P Group BSSID or %NULL if not received
564          *
565          * This callback is used to indicate result of an Invitation procedure
566          * started with a call to p2p_invite(). The indicated status code is
567          * the value received from the peer in Invitation Response with 0
568          * (P2P_SC_SUCCESS) indicating success or -1 to indicate a timeout or a
569          * local failure in transmitting the Invitation Request.
570          */
571         void (*invitation_result)(void *ctx, int status, const u8 *bssid);
572 };
573
574
575 /* P2P module initialization/deinitialization */
576
577 /**
578  * p2p_init - Initialize P2P module
579  * @cfg: P2P module configuration
580  * Returns: Pointer to private data or %NULL on failure
581  *
582  * This function is used to initialize global P2P module context (one per
583  * device). The P2P module will keep a copy of the configuration data, so the
584  * caller does not need to maintain this structure. However, the callback
585  * functions and the context parameters to them must be kept available until
586  * the P2P module is deinitialized with p2p_deinit().
587  */
588 struct p2p_data * p2p_init(const struct p2p_config *cfg);
589
590 /**
591  * p2p_deinit - Deinitialize P2P module
592  * @p2p: P2P module context from p2p_init()
593  */
594 void p2p_deinit(struct p2p_data *p2p);
595
596 /**
597  * p2p_flush - Flush P2P module state
598  * @p2p: P2P module context from p2p_init()
599  *
600  * This command removes the P2P module state like peer device entries.
601  */
602 void p2p_flush(struct p2p_data *p2p);
603
604 /**
605  * p2p_set_dev_name - Set device name
606  * @p2p: P2P module context from p2p_init()
607  * Returns: 0 on success, -1 on failure
608  *
609  * This function can be used to update the P2P module configuration with
610  * information that was not available at the time of the p2p_init() call.
611  */
612 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name);
613
614 /**
615  * p2p_set_pri_dev_type - Set primary device type
616  * @p2p: P2P module context from p2p_init()
617  * Returns: 0 on success, -1 on failure
618  *
619  * This function can be used to update the P2P module configuration with
620  * information that was not available at the time of the p2p_init() call.
621  */
622 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type);
623
624 /**
625  * p2p_set_sec_dev_types - Set secondary device types
626  * @p2p: P2P module context from p2p_init()
627  * Returns: 0 on success, -1 on failure
628  *
629  * This function can be used to update the P2P module configuration with
630  * information that was not available at the time of the p2p_init() call.
631  */
632 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
633                           size_t num_dev_types);
634
635 int p2p_set_country(struct p2p_data *p2p, const char *country);
636
637
638 /* Commands from upper layer management entity */
639
640 enum p2p_discovery_type {
641         P2P_FIND_START_WITH_FULL,
642         P2P_FIND_ONLY_SOCIAL,
643         P2P_FIND_PROGRESSIVE
644 };
645
646 /**
647  * p2p_find - Start P2P Find (Device Discovery)
648  * @p2p: P2P module context from p2p_init()
649  * @timeout: Timeout for find operation in seconds or 0 for no timeout
650  * @type: Device Discovery type
651  * Returns: 0 on success, -1 on failure
652  */
653 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
654              enum p2p_discovery_type type);
655
656 /**
657  * p2p_stop_find - Stop P2P Find (Device Discovery)
658  * @p2p: P2P module context from p2p_init()
659  */
660 void p2p_stop_find(struct p2p_data *p2p);
661
662 /**
663  * p2p_listen - Start P2P Listen state for specified duration
664  * @p2p: P2P module context from p2p_init()
665  * @timeout: Listen state duration in milliseconds
666  * Returns: 0 on success, -1 on failure
667  *
668  * This function can be used to request the P2P module to keep the device
669  * discoverable on the listen channel for an extended set of time. At least in
670  * its current form, this is mainly used for testing purposes and may not be of
671  * much use for normal P2P operations.
672  */
673 int p2p_listen(struct p2p_data *p2p, unsigned int timeout);
674
675 /**
676  * p2p_connect - Start P2P group formation (GO negotiation)
677  * @p2p: P2P module context from p2p_init()
678  * @peer_addr: MAC address of the peer P2P client
679  * @wps_method: WPS method to be used in provisioning
680  * @go_intent: Local GO intent value (1..15)
681  * @own_interface_addr: Intended interface address to use with the group
682  * @force_freq: The only allowed channel frequency in MHz or 0
683  * @persistent_group: Whether to create a persistent group
684  * Returns: 0 on success, -1 on failure
685  */
686 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
687                 enum p2p_wps_method wps_method,
688                 int go_intent, const u8 *own_interface_addr,
689                 unsigned int force_freq, int persistent_group);
690
691 /**
692  * p2p_authorize - Authorize P2P group formation (GO negotiation)
693  * @p2p: P2P module context from p2p_init()
694  * @peer_addr: MAC address of the peer P2P client
695  * @wps_method: WPS method to be used in provisioning
696  * @go_intent: Local GO intent value (1..15)
697  * @own_interface_addr: Intended interface address to use with the group
698  * @force_freq: The only allowed channel frequency in MHz or 0
699  * @persistent_group: Whether to create a persistent group
700  * Returns: 0 on success, -1 on failure
701  *
702  * This is like p2p_connect(), but the actual group negotiation is not
703  * initiated automatically, i.e., the other end is expected to do that.
704  */
705 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
706                   enum p2p_wps_method wps_method,
707                   int go_intent, const u8 *own_interface_addr,
708                   unsigned int force_freq, int persistent_group);
709
710 /**
711  * p2p_reject - Reject peer device (explicitly block connection attempts)
712  * @p2p: P2P module context from p2p_init()
713  * @peer_addr: MAC address of the peer P2P client
714  * Returns: 0 on success, -1 on failure
715  */
716 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr);
717
718 /**
719  * p2p_prov_disc_req - Send Provision Discovery Request
720  * @p2p: P2P module context from p2p_init()
721  * @peer_addr: MAC address of the peer P2P client
722  * @config_methods: WPS Config Methods value (only one bit set)
723  * @join: Whether this is used by a client joining an active group
724  * Returns: 0 on success, -1 on failure
725  *
726  * This function can be used to request a discovered P2P peer to display a PIN
727  * (config_methods = WPS_CONFIG_DISPLAY) or be prepared to enter a PIN from us
728  * (config_methods = WPS_CONFIG_KEYPAD). The Provision Discovery Request frame
729  * is transmitted once immediately and if no response is received, the frame
730  * will be sent again whenever the target device is discovered during device
731  * dsicovery (start with a p2p_find() call). Response from the peer is
732  * indicated with the p2p_config::prov_disc_resp() callback.
733  */
734 int p2p_prov_disc_req(struct p2p_data *p2p, const u8 *peer_addr,
735                       u16 config_methods, int join);
736
737 /**
738  * p2p_sd_request - Schedule a service discovery query
739  * @p2p: P2P module context from p2p_init()
740  * @dst: Destination peer or %NULL to apply for all peers
741  * @tlvs: P2P Service Query TLV(s)
742  * Returns: Reference to the query or %NULL on failure
743  *
744  * Response to the query is indicated with the p2p_config::sd_response()
745  * callback.
746  */
747 void * p2p_sd_request(struct p2p_data *p2p, const u8 *dst,
748                       const struct wpabuf *tlvs);
749
750 /**
751  * p2p_sd_cancel_request - Cancel a pending service discovery query
752  * @p2p: P2P module context from p2p_init()
753  * @req: Query reference from p2p_sd_request()
754  * Returns: 0 if request for cancelled; -1 if not found
755  */
756 int p2p_sd_cancel_request(struct p2p_data *p2p, void *req);
757
758 /**
759  * p2p_sd_response - Send response to a service discovery query
760  * @p2p: P2P module context from p2p_init()
761  * @freq: Frequency from p2p_config::sd_request() callback
762  * @dst: Destination address from p2p_config::sd_request() callback
763  * @dialog_token: Dialog token from p2p_config::sd_request() callback
764  * @resp_tlvs: P2P Service Response TLV(s)
765  *
766  * This function is called as a response to the request indicated with
767  * p2p_config::sd_request() callback.
768  */
769 void p2p_sd_response(struct p2p_data *p2p, int freq, const u8 *dst,
770                      u8 dialog_token, const struct wpabuf *resp_tlvs);
771
772 /**
773  * p2p_sd_service_update - Indicate a change in local services
774  * @p2p: P2P module context from p2p_init()
775  *
776  * This function needs to be called whenever there is a change in availability
777  * of the local services. This will increment the Service Update Indicator
778  * value which will be used in SD Request and Response frames.
779  */
780 void p2p_sd_service_update(struct p2p_data *p2p);
781
782
783 enum p2p_invite_role {
784         P2P_INVITE_ROLE_GO,
785         P2P_INVITE_ROLE_ACTIVE_GO,
786         P2P_INVITE_ROLE_CLIENT
787 };
788
789 /**
790  * p2p_invite - Invite a P2P Device into a group
791  * @p2p: P2P module context from p2p_init()
792  * @peer: Device Address of the peer P2P Device
793  * @role: Local role in the group
794  * @bssid: Group BSSID or %NULL if not known
795  * @ssid: Group SSID
796  * @ssid_len: Length of ssid in octets
797  * @force_freq: The only allowed channel frequency in MHz or 0
798  * @go_dev_addr: Forced GO Device Address or %NULL if none
799  * @persistent_group: Whether this is to reinvoke a persistent group
800  * Returns: 0 on success, -1 on failure
801  */
802 int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
803                const u8 *bssid, const u8 *ssid, size_t ssid_len,
804                unsigned int force_freq, const u8 *go_dev_addr,
805                int persistent_group);
806
807 /**
808  * p2p_presence_req - Request GO presence
809  * @p2p: P2P module context from p2p_init()
810  * @go_interface_addr: GO P2P Interface Address
811  * @own_interface_addr: Own P2P Interface Address for this group
812  * @freq: Group operating frequence (in MHz)
813  * @duration1: Preferred presence duration in microseconds
814  * @interval1: Preferred presence interval in microseconds
815  * @duration2: Acceptable presence duration in microseconds
816  * @interval2: Acceptable presence interval in microseconds
817  * Returns: 0 on success, -1 on failure
818  *
819  * If both duration and interval values are zero, the parameter pair is not
820  * specified (i.e., to remove Presence Request, use duration1 = interval1 = 0).
821  */
822 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
823                      const u8 *own_interface_addr, unsigned int freq,
824                      u32 duration1, u32 interval1, u32 duration2,
825                      u32 interval2);
826
827 /**
828  * p2p_ext_listen - Set Extended Listen Timing
829  * @p2p: P2P module context from p2p_init()
830  * @freq: Group operating frequence (in MHz)
831  * @period: Availability period in milliseconds (1-65535; 0 to disable)
832  * @interval: Availability interval in milliseconds (1-65535; 0 to disable)
833  * Returns: 0 on success, -1 on failure
834  *
835  * This function can be used to enable or disable (period = interval = 0)
836  * Extended Listen Timing. When enabled, the P2P Device will become
837  * discoverable (go into Listen State) every @interval milliseconds for at
838  * least @period milliseconds.
839  */
840 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
841                    unsigned int interval);
842
843 /* Event notifications from upper layer management operations */
844
845 /**
846  * p2p_wps_success_cb - Report successfully completed WPS provisioning
847  * @p2p: P2P module context from p2p_init()
848  * @mac_addr: Peer address
849  *
850  * This function is used to report successfully completed WPS provisioning
851  * during group formation in both GO/Registrar and client/Enrollee roles.
852  */
853 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr);
854
855 /**
856  * p2p_group_formation_failed - Report failed WPS provisioning
857  * @p2p: P2P module context from p2p_init()
858  *
859  * This function is used to report failed group formation. This can happen
860  * either due to failed WPS provisioning or due to 15 second timeout during
861  * the provisioning phase.
862  */
863 void p2p_group_formation_failed(struct p2p_data *p2p);
864
865
866 /* Event notifications from lower layer driver operations */
867
868 /**
869  * p2p_probe_req_rx - Report reception of a Probe Request frame
870  * @p2p: P2P module context from p2p_init()
871  * @addr: Source MAC address
872  * @ie: Information elements from the Probe Request frame body
873  * @ie_len: Length of ie buffer in octets
874  * Returns: 0 to indicate the frame was not processed or 1 if it was
875  */
876 int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *ie,
877                      size_t ie_len);
878
879 /**
880  * p2p_rx_action - Report received Action frame
881  * @p2p: P2P module context from p2p_init()
882  * @da: Destination address of the received Action frame
883  * @sa: Source address of the received Action frame
884  * @bssid: Address 3 of the received Action frame
885  * @category: Category of the received Action frame
886  * @data: Action frame body after the Category field
887  * @len: Length of the data buffer in octets
888  * @freq: Frequency (in MHz) on which the frame was received
889  */
890 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
891                    const u8 *bssid, u8 category,
892                    const u8 *data, size_t len, int freq);
893
894 /**
895  * p2p_scan_res_handler - Indicate a P2P scan results
896  * @p2p: P2P module context from p2p_init()
897  * @bssid: BSSID of the scan result
898  * @freq: Frequency of the channel on which the device was found in MHz
899  * @level: Signal level (signal strength of the received Beacon/Probe Response
900  *      frame)
901  * @ies: Pointer to IEs from the scan result
902  * @ies_len: Length of the ies buffer
903  * Returns: 0 to continue or 1 to stop scan result indication
904  *
905  * This function is called to indicate a scan result entry with P2P IE from a
906  * scan requested with struct p2p_config::p2p_scan(). This can be called during
907  * the actual scan process (i.e., whenever a new device is found) or as a
908  * sequence of calls after the full scan has been completed. The former option
909  * can result in optimized operations, but may not be supported by all
910  * driver/firmware designs. The ies buffer need to include at least the P2P IE,
911  * but it is recommended to include all IEs received from the device. The
912  * caller does not need to check that the IEs contain a P2P IE before calling
913  * this function since frames will be filtered internally if needed.
914  *
915  * This function will return 1 if it wants to stop scan result iteration (and
916  * scan in general if it is still in progress). This is used to allow faster
917  * start of a pending operation, e.g., to start a pending GO negotiation.
918  */
919 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
920                          int level, const u8 *ies, size_t ies_len);
921
922 /**
923  * p2p_scan_res_handled - Indicate end of scan results
924  * @p2p: P2P module context from p2p_init()
925  *
926  * This function is called to indicate that all P2P scan results from a scan
927  * have been reported with zero or more calls to p2p_scan_res_handler(). This
928  * function must be called as a response to successful
929  * struct p2p_config::p2p_scan() call if none of the p2p_scan_res_handler()
930  * calls stopped iteration.
931  */
932 void p2p_scan_res_handled(struct p2p_data *p2p);
933
934 /**
935  * p2p_send_action_cb - Notify TX status of an Action frame
936  * @p2p: P2P module context from p2p_init()
937  * @freq: Channel frequency in MHz
938  * @dst: Destination MAC address (Address 1)
939  * @src: Source MAC address (Address 2)
940  * @bssid: BSSID (Address 3)
941  * @success: Whether the transmission succeeded
942  *
943  * This function is used to indicate the result of an Action frame transmission
944  * that was requested with struct p2p_config::send_action() callback.
945  */
946 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
947                         const u8 *src, const u8 *bssid, int success);
948
949 /**
950  * p2p_listen_cb - Indicate the start of a requested Listen state
951  * @p2p: P2P module context from p2p_init()
952  * @freq: Listen channel frequency in MHz
953  * @duration: Duration for the Listen state in milliseconds
954  *
955  * This function is used to indicate that a Listen state requested with
956  * struct p2p_config::start_listen() callback has started.
957  */
958 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
959                    unsigned int duration);
960
961 /**
962  * p2p_listen_end - Indicate the end of a requested Listen state
963  * @p2p: P2P module context from p2p_init()
964  * @freq: Listen channel frequency in MHz
965  * Returns: 0 if no operations were started, 1 if an operation was started
966  *
967  * This function is used to indicate that a Listen state requested with
968  * struct p2p_config::start_listen() callback has ended.
969  */
970 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq);
971
972 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
973                       const u8 *ie, size_t ie_len);
974
975 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
976                         const u8 *ie, size_t ie_len);
977
978
979 /* Per-group P2P state for GO */
980
981 struct p2p_group;
982
983 /**
984  * struct p2p_group_config - P2P group configuration
985  *
986  * This configuration is provided to the P2P module during initialization of
987  * the per-group information with p2p_group_init().
988  */
989 struct p2p_group_config {
990         /**
991          * persistent_group - Whether the group is persistent
992          */
993         int persistent_group;
994
995         /**
996          * interface_addr - P2P Interface Address of the group
997          */
998         u8 interface_addr[ETH_ALEN];
999
1000         /**
1001          * cb_ctx - Context to use with callback functions
1002          */
1003         void *cb_ctx;
1004
1005         /**
1006          * ie_update - Notification of IE update
1007          * @ctx: Callback context from cb_ctx
1008          * @beacon_ies: P2P IE for Beacon frames or %NULL if no change
1009          * @proberesp_ies: P2P Ie for Probe Response frames
1010          *
1011          * P2P module uses this callback function to notify whenever the P2P IE
1012          * in Beacon or Probe Response frames should be updated based on group
1013          * events.
1014          *
1015          * The callee is responsible for freeing the returned buffer(s) with
1016          * wpabuf_free().
1017          */
1018         void (*ie_update)(void *ctx, struct wpabuf *beacon_ies,
1019                           struct wpabuf *proberesp_ies);
1020 };
1021
1022 /**
1023  * p2p_group_init - Initialize P2P group
1024  * @p2p: P2P module context from p2p_init()
1025  * @config: P2P group configuration (will be freed by p2p_group_deinit())
1026  * Returns: Pointer to private data or %NULL on failure
1027  *
1028  * This function is used to initialize per-group P2P module context. Currently,
1029  * this is only used to manage GO functionality and P2P clients do not need to
1030  * create an instance of this per-group information.
1031  */
1032 struct p2p_group * p2p_group_init(struct p2p_data *p2p,
1033                                   struct p2p_group_config *config);
1034
1035 /**
1036  * p2p_group_deinit - Deinitialize P2P group
1037  * @group: P2P group context from p2p_group_init()
1038  */
1039 void p2p_group_deinit(struct p2p_group *group);
1040
1041 /**
1042  * p2p_group_notif_assoc - Notification of P2P client association with GO
1043  * @group: P2P group context from p2p_group_init()
1044  * @addr: Interface address of the P2P client
1045  * @ie: IEs from the (Re)association Request frame
1046  * @len: Length of the ie buffer in octets
1047  * Returns: 0 on success, -1 on failure
1048  */
1049 int p2p_group_notif_assoc(struct p2p_group *group, const u8 *addr,
1050                           const u8 *ie, size_t len);
1051
1052 /**
1053  * p2p_group_assoc_resp_ie - Build P2P IE for (re)association response
1054  * @group: P2P group context from p2p_group_init()
1055  * @status: Status value (P2P_SC_SUCCESS if association succeeded)
1056  * Returns: P2P IE for (Re)association Response or %NULL on failure
1057  *
1058  * The caller is responsible for freeing the returned buffer with
1059  * wpabuf_free().
1060  */
1061 struct wpabuf * p2p_group_assoc_resp_ie(struct p2p_group *group, u8 status);
1062
1063 /**
1064  * p2p_group_notif_disassoc - Notification of P2P client disassociation from GO
1065  * @group: P2P group context from p2p_group_init()
1066  * @addr: Interface address of the P2P client
1067  */
1068 void p2p_group_notif_disassoc(struct p2p_group *group, const u8 *addr);
1069
1070 /**
1071  * p2p_group_notif_formation_done - Notification of completed group formation
1072  * @group: P2P group context from p2p_group_init()
1073  */
1074 void p2p_group_notif_formation_done(struct p2p_group *group);
1075
1076 /**
1077  * p2p_group_notif_noa - Notification of NoA change
1078  * @group: P2P group context from p2p_group_init()
1079  * @noa: Notice of Absence attribute payload, %NULL if none
1080  * @noa_len: Length of noa buffer in octets
1081  * Returns: 0 on success, -1 on failure
1082  *
1083  * Notify the P2P group management about a new NoA contents. This will be
1084  * inserted into the P2P IEs in Beacon and Probe Response frames with rest of
1085  * the group information.
1086  */
1087 int p2p_group_notif_noa(struct p2p_group *group, const u8 *noa,
1088                         size_t noa_len);
1089
1090 /**
1091  * p2p_group_match_dev_type - Match device types in group with requested type
1092  * @group: P2P group context from p2p_group_init()
1093  * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1094  * Returns: 1 on match, 0 on mismatch
1095  *
1096  * This function can be used to match the Requested Device Type attribute in
1097  * WPS IE with the device types of a group member for deciding whether a GO
1098  * should reply to a Probe Request frame. Match will be reported if the WPS IE
1099  * is not requested any specific device type.
1100  */
1101 int p2p_group_match_dev_type(struct p2p_group *group, struct wpabuf *wps);
1102
1103 /**
1104  * p2p_group_go_discover - Send GO Discoverability Request to a group client
1105  * @group: P2P group context from p2p_group_init()
1106  * Returns: 0 on success (frame scheduled); -1 if client was not found
1107  */
1108 int p2p_group_go_discover(struct p2p_group *group, const u8 *dev_id,
1109                           const u8 *searching_dev, int rx_freq);
1110
1111
1112 /* Generic helper functions */
1113
1114 /**
1115  * p2p_ie_text - Build text format description of P2P IE
1116  * @p2p_ie: P2P IE
1117  * @buf: Buffer for returning text
1118  * @end: Pointer to the end of the buf area
1119  * Returns: Number of octets written to the buffer or -1 on failure
1120  *
1121  * This function can be used to parse P2P IE contents into text format
1122  * field=value lines.
1123  */
1124 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end);
1125
1126 /**
1127  * p2p_scan_result_text - Build text format description of P2P IE
1128  * @ies: Information elements from scan results
1129  * @ies_len: ies buffer length in octets
1130  * @buf: Buffer for returning text
1131  * @end: Pointer to the end of the buf area
1132  * Returns: Number of octets written to the buffer or -1 on failure
1133  *
1134  * This function can be used to parse P2P IE contents into text format
1135  * field=value lines.
1136  */
1137 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end);
1138
1139 /**
1140  * p2p_assoc_req_ie - Build P2P IE for (Re)Association Request frame
1141  * @p2p: P2P module context from p2p_init()
1142  * @bssid: BSSID
1143  * @buf: Buffer for writing the P2P IE
1144  * @len: Maximum buf length in octets
1145  * @p2p_group: Whether this is for association with a P2P GO
1146  * @p2p_ie: Reassembled P2P IE data from scan results or %NULL if none
1147  * Returns: Number of octets written into buf or -1 on failure
1148  */
1149 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
1150                      size_t len, int p2p_group, struct wpabuf *p2p_ie);
1151
1152 /**
1153  * p2p_scan_ie - Build P2P IE for Probe Request
1154  * @p2p: P2P module context from p2p_init()
1155  * @ies: Buffer for writing P2P IE
1156  */
1157 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies);
1158
1159 /**
1160  * p2p_go_params - Generate random P2P group parameters
1161  * @p2p: P2P module context from p2p_init()
1162  * @params: Buffer for parameters
1163  * Returns: 0 on success, -1 on failure
1164  */
1165 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params);
1166
1167 /**
1168  * p2p_get_group_capab - Get Group Capability from P2P IE data
1169  * @p2p_ie: P2P IE(s) contents
1170  * Returns: Group Capability
1171  */
1172 u8 p2p_get_group_capab(const struct wpabuf *p2p_ie);
1173
1174 /**
1175  * p2p_get_cross_connect_disallowed - Does WLAN AP disallows cross connection
1176  * @p2p_ie: P2P IE(s) contents
1177  * Returns: 0 if cross connection is allow, 1 if not
1178  */
1179 int p2p_get_cross_connect_disallowed(const struct wpabuf *p2p_ie);
1180
1181 /**
1182  * p2p_get_go_dev_addr - Get P2P Device Address from P2P IE data
1183  * @p2p_ie: P2P IE(s) contents
1184  * Returns: Pointer to P2P Device Address or %NULL if not included
1185  */
1186 const u8 * p2p_get_go_dev_addr(const struct wpabuf *p2p_ie);
1187
1188 /**
1189  * p2p_get_peer_info - Get P2P peer information in text format
1190  * @p2p: P2P module context from p2p_init()
1191  * @addr: P2P Device Address of the peer or %NULL to indicate the first peer
1192  * @next: Whether to select the peer entry following the one indicated by addr
1193  * @buf: Buffer for returning text
1194  * @buflen: Maximum buffer length
1195  * Returns: Number of octets written to the buffer or -1 on failure
1196  */
1197 int p2p_get_peer_info(struct p2p_data *p2p, const u8 *addr, int next,
1198                       char *buf, size_t buflen);
1199
1200 /**
1201  * p2p_set_client_discoverability - Set client discoverability capability
1202  * @p2p: P2P module context from p2p_init()
1203  * @enabled: Whether client discoverability will be enabled
1204  *
1205  * This function can be used to disable (and re-enable) client discoverability.
1206  * This capability is enabled by default and should not be disabled in normal
1207  * use cases, i.e., this is mainly for testing purposes.
1208  */
1209 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled);
1210
1211 /**
1212  * p2p_set_manageD_oper - Set managed P2P Device operations capability
1213  * @p2p: P2P module context from p2p_init()
1214  * @enabled: Whether managed P2P Device operations will be enabled
1215  */
1216 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled);
1217
1218 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel);
1219
1220 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len);
1221
1222 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
1223                            u8 *iface_addr);
1224 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
1225                            u8 *dev_addr);
1226
1227 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr);
1228
1229 /**
1230  * p2p_set_cross_connect - Set cross connection capability
1231  * @p2p: P2P module context from p2p_init()
1232  * @enabled: Whether cross connection will be enabled
1233  */
1234 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled);
1235
1236 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr);
1237
1238 #endif /* P2P_H */