P2P: Add support for automatic channel selection at GO
[mech_eap.git] / src / p2p / p2p_i.h
1 /*
2  * P2P - Internal definitions for 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_I_H
16 #define P2P_I_H
17
18 #include "utils/list.h"
19 #include "p2p.h"
20
21 /* TODO: add removal of expired P2P device entries */
22
23 enum p2p_go_state {
24         UNKNOWN_GO,
25         LOCAL_GO,
26         REMOTE_GO
27 };
28
29 /**
30  * struct p2p_device - P2P Device data (internal to P2P module)
31  */
32 struct p2p_device {
33         struct dl_list list;
34         struct os_time last_seen;
35         int listen_freq;
36         int level;
37         enum p2p_wps_method wps_method;
38
39         u8 p2p_device_addr[ETH_ALEN]; /* P2P Device Address of the peer */
40         u8 pri_dev_type[8];
41         char device_name[33];
42         u16 config_methods;
43         u8 dev_capab;
44         u8 group_capab;
45
46         /*
47          * If the peer was discovered based on an interface address (e.g., GO
48          * from Beacon/Probe Response), the interface address is stored here.
49          * p2p_device_addr must still be set in such a case to the unique
50          * identifier for the P2P Device.
51          */
52         u8 interface_addr[ETH_ALEN];
53
54         /*
55          * P2P Device Address of the GO in whose group this P2P Device is a
56          * client.
57          */
58         u8 member_in_go_dev[ETH_ALEN];
59
60         /*
61          * P2P Interface Address of the GO in whose group this P2P Device is a
62          * client.
63          */
64         u8 member_in_go_iface[ETH_ALEN];
65
66         int go_neg_req_sent;
67         enum p2p_go_state go_state;
68         u8 dialog_token;
69         u8 intended_addr[ETH_ALEN];
70
71         char country[3];
72         struct p2p_channels channels;
73         int oper_freq;
74         u8 oper_ssid[32];
75         size_t oper_ssid_len;
76
77         /**
78          * req_config_methods - Pending provisioning discovery methods
79          */
80         u16 req_config_methods;
81
82 #define P2P_DEV_PROBE_REQ_ONLY BIT(0)
83 #define P2P_DEV_REPORTED BIT(1)
84 #define P2P_DEV_NOT_YET_READY BIT(2)
85 #define P2P_DEV_SD_INFO BIT(3)
86 #define P2P_DEV_SD_SCHEDULE BIT(4)
87 #define P2P_DEV_PD_PEER_DISPLAY BIT(5)
88 #define P2P_DEV_PD_PEER_KEYPAD BIT(6)
89 #define P2P_DEV_USER_REJECTED BIT(7)
90 #define P2P_DEV_PEER_WAITING_RESPONSE BIT(8)
91 #define P2P_DEV_PREFER_PERSISTENT_GROUP BIT(9)
92 #define P2P_DEV_WAIT_GO_NEG_RESPONSE BIT(10)
93 #define P2P_DEV_WAIT_GO_NEG_CONFIRM BIT(11)
94 #define P2P_DEV_GROUP_CLIENT_ONLY BIT(12)
95 #define P2P_DEV_FORCE_FREQ BIT(13)
96 #define P2P_DEV_PD_FOR_JOIN BIT(14)
97         unsigned int flags;
98
99         int status; /* enum p2p_status_code */
100         unsigned int wait_count;
101         unsigned int invitation_reqs;
102
103         u16 ext_listen_period;
104         u16 ext_listen_interval;
105
106         u8 go_timeout;
107         u8 client_timeout;
108 };
109
110 struct p2p_sd_query {
111         struct p2p_sd_query *next;
112         u8 peer[ETH_ALEN];
113         int for_all_peers;
114         struct wpabuf *tlvs;
115 };
116
117 struct p2p_pending_action_tx {
118         unsigned int freq;
119         u8 dst[ETH_ALEN];
120         u8 src[ETH_ALEN];
121         u8 bssid[ETH_ALEN];
122         size_t len;
123         unsigned int wait_time;
124         /* Followed by len octets of the frame */
125 };
126
127 /**
128  * struct p2p_data - P2P module data (internal to P2P module)
129  */
130 struct p2p_data {
131         /**
132          * cfg - P2P module configuration
133          *
134          * This is included in the same memory allocation with the
135          * struct p2p_data and as such, must not be freed separately.
136          */
137         struct p2p_config *cfg;
138
139         /**
140          * state - The current P2P state
141          */
142         enum p2p_state {
143                 /**
144                  * P2P_IDLE - Idle
145                  */
146                 P2P_IDLE,
147
148                 /**
149                  * P2P_SEARCH - Search (Device Discovery)
150                  */
151                 P2P_SEARCH,
152
153                 /**
154                  * P2P_CONNECT - Trying to start GO Negotiation
155                  */
156                 P2P_CONNECT,
157
158                 /**
159                  * P2P_CONNECT_LISTEN - Listen during GO Negotiation start
160                  */
161                 P2P_CONNECT_LISTEN,
162
163                 /**
164                  * P2P_GO_NEG - In GO Negotiation
165                  */
166                 P2P_GO_NEG,
167
168                 /**
169                  * P2P_LISTEN_ONLY - Listen only
170                  */
171                 P2P_LISTEN_ONLY,
172
173                 /**
174                  * P2P_WAIT_PEER_CONNECT - Waiting peer in List for GO Neg
175                  */
176                 P2P_WAIT_PEER_CONNECT,
177
178                 /**
179                  * P2P_WAIT_PEER_IDLE - Waiting peer idle for GO Neg
180                  */
181                 P2P_WAIT_PEER_IDLE,
182
183                 /**
184                  * P2P_SD_DURING_FIND - Service Discovery during find
185                  */
186                 P2P_SD_DURING_FIND,
187
188                 /**
189                  * P2P_PROVISIONING - Provisioning (during group formation)
190                  */
191                 P2P_PROVISIONING,
192
193                 /**
194                  * P2P_PD_DURING_FIND - Provision Discovery during find
195                  */
196                 P2P_PD_DURING_FIND,
197
198                 /**
199                  * P2P_INVITE - Trying to start Invite
200                  */
201                 P2P_INVITE,
202
203                 /**
204                  * P2P_INVITE_LISTEN - Listen during Invite
205                  */
206                 P2P_INVITE_LISTEN,
207         } state;
208
209         /**
210          * min_disc_int - minDiscoverableInterval
211          */
212         int min_disc_int;
213
214         /**
215          * max_disc_int - maxDiscoverableInterval
216          */
217         int max_disc_int;
218
219         /**
220          * devices - List of known P2P Device peers
221          */
222         struct dl_list devices;
223
224         /**
225          * go_neg_peer - Pointer to GO Negotiation peer
226          */
227         struct p2p_device *go_neg_peer;
228
229         /**
230          * invite_peer - Pointer to Invite peer
231          */
232         struct p2p_device *invite_peer;
233
234         const u8 *invite_go_dev_addr;
235         u8 invite_go_dev_addr_buf[ETH_ALEN];
236
237         /**
238          * sd_peer - Pointer to Service Discovery peer
239          */
240         struct p2p_device *sd_peer;
241
242         /**
243          * sd_query - Pointer to Service Discovery query
244          */
245         struct p2p_sd_query *sd_query;
246
247         /* GO Negotiation data */
248
249         /**
250          * intended_addr - Local Intended P2P Interface Address
251          *
252          * This address is used during group owner negotiation as the Intended
253          * P2P Interface Address and the group interface will be created with
254          * address as the local address in case of successfully completed
255          * negotiation.
256          */
257         u8 intended_addr[ETH_ALEN];
258
259         /**
260          * go_intent - Local GO Intent to be used during GO Negotiation
261          */
262         u8 go_intent;
263
264         /**
265          * next_tie_breaker - Next tie-breaker value to use in GO Negotiation
266          */
267         u8 next_tie_breaker;
268
269         /**
270          * ssid - Selected SSID for GO Negotiation (if local end will be GO)
271          */
272         u8 ssid[32];
273
274         /**
275          * ssid_len - ssid length in octets
276          */
277         size_t ssid_len;
278
279         /**
280          * Regulatory class for own operational channel
281          */
282         u8 op_reg_class;
283
284         /**
285          * op_channel - Own operational channel
286          */
287         u8 op_channel;
288
289         /**
290          * channels - Own supported regulatory classes and channels
291          *
292          * List of supposerted channels per regulatory class. The regulatory
293          * classes are defined in IEEE Std 802.11-2007 Annex J and the
294          * numbering of the clases depends on the configured country code.
295          */
296         struct p2p_channels channels;
297
298         enum p2p_pending_action_state {
299                 P2P_NO_PENDING_ACTION,
300                 P2P_PENDING_GO_NEG_REQUEST,
301                 P2P_PENDING_GO_NEG_RESPONSE,
302                 P2P_PENDING_GO_NEG_RESPONSE_FAILURE,
303                 P2P_PENDING_GO_NEG_CONFIRM,
304                 P2P_PENDING_SD,
305                 P2P_PENDING_PD,
306                 P2P_PENDING_INVITATION_REQUEST,
307                 P2P_PENDING_INVITATION_RESPONSE,
308                 P2P_PENDING_DEV_DISC_REQUEST,
309                 P2P_PENDING_DEV_DISC_RESPONSE,
310                 P2P_PENDING_GO_DISC_REQ
311         } pending_action_state;
312
313         unsigned int pending_listen_freq;
314         unsigned int pending_listen_sec;
315         unsigned int pending_listen_usec;
316
317         u8 dev_capab;
318
319         int in_listen;
320         int drv_in_listen;
321
322         /**
323          * sd_queries - Pending service discovery queries
324          */
325         struct p2p_sd_query *sd_queries;
326
327         /**
328          * srv_update_indic - Service Update Indicator for local services
329          */
330         u16 srv_update_indic;
331
332         struct wpabuf *sd_resp; /* Fragmented SD response */
333         u8 sd_resp_addr[ETH_ALEN];
334         u8 sd_resp_dialog_token;
335         size_t sd_resp_pos; /* Offset in sd_resp */
336         u8 sd_frag_id;
337
338         struct wpabuf *sd_rx_resp; /* Reassembled SD response */
339         u16 sd_rx_update_indic;
340
341         /* P2P Invitation data */
342         enum p2p_invite_role inv_role;
343         u8 inv_bssid[ETH_ALEN];
344         int inv_bssid_set;
345         u8 inv_ssid[32];
346         size_t inv_ssid_len;
347         u8 inv_sa[ETH_ALEN];
348         u8 inv_group_bssid[ETH_ALEN];
349         u8 *inv_group_bssid_ptr;
350         u8 inv_go_dev_addr[ETH_ALEN];
351         u8 inv_status;
352         int inv_op_freq;
353         int inv_persistent;
354
355         enum p2p_discovery_type find_type;
356         u8 last_prog_scan_class;
357         u8 last_prog_scan_chan;
358         int p2p_scan_running;
359         enum p2p_after_scan {
360                 P2P_AFTER_SCAN_NOTHING,
361                 P2P_AFTER_SCAN_LISTEN,
362                 P2P_AFTER_SCAN_CONNECT
363         } start_after_scan;
364         u8 after_scan_peer[ETH_ALEN];
365         struct p2p_pending_action_tx *after_scan_tx;
366
367         struct p2p_group **groups;
368         size_t num_groups;
369
370         struct p2p_device *pending_client_disc_go;
371         u8 pending_client_disc_addr[ETH_ALEN];
372         u8 pending_dev_disc_dialog_token;
373         u8 pending_dev_disc_addr[ETH_ALEN];
374         int pending_dev_disc_freq;
375         unsigned int pending_client_disc_freq;
376
377         int ext_listen_only;
378         unsigned int ext_listen_period;
379         unsigned int ext_listen_interval;
380         unsigned int ext_listen_interval_sec;
381         unsigned int ext_listen_interval_usec;
382
383         u8 peer_filter[ETH_ALEN];
384
385         int cross_connect;
386
387         int best_freq_24;
388         int best_freq_5;
389         int best_freq_overall;
390 };
391
392 /**
393  * struct p2p_message - Parsed P2P message (or P2P IE)
394  */
395 struct p2p_message {
396         struct wpabuf *p2p_attributes;
397         struct wpabuf *wps_attributes;
398
399         u8 dialog_token;
400
401         const u8 *capability;
402         const u8 *go_intent;
403         const u8 *status;
404         const u8 *listen_channel;
405         const u8 *operating_channel;
406         const u8 *channel_list;
407         u8 channel_list_len;
408         const u8 *config_timeout;
409         const u8 *intended_addr;
410         const u8 *group_bssid;
411         const u8 *invitation_flags;
412
413         const u8 *group_info;
414         size_t group_info_len;
415
416         const u8 *group_id;
417         size_t group_id_len;
418
419         const u8 *device_id;
420
421         const u8 *manageability;
422
423         const u8 *noa;
424         size_t noa_len;
425
426         const u8 *ext_listen_timing;
427
428         const u8 *minor_reason_code;
429
430         /* P2P Device Info */
431         const u8 *p2p_device_info;
432         size_t p2p_device_info_len;
433         const u8 *p2p_device_addr;
434         const u8 *pri_dev_type;
435         u8 num_sec_dev_types;
436         char device_name[33];
437         u16 config_methods;
438
439         /* WPS IE */
440         u16 dev_password_id;
441         u16 wps_config_methods;
442         const u8 *wps_pri_dev_type;
443
444         /* DS Parameter Set IE */
445         const u8 *ds_params;
446
447         /* SSID IE */
448         const u8 *ssid;
449 };
450
451
452 #define P2P_MAX_GROUP_ENTRIES 50
453
454 struct p2p_group_info {
455         unsigned int num_clients;
456         struct p2p_client_info {
457                 const u8 *p2p_device_addr;
458                 const u8 *p2p_interface_addr;
459                 u8 dev_capab;
460                 u16 config_methods;
461                 const u8 *pri_dev_type;
462                 u8 num_sec_dev_types;
463                 const u8 *sec_dev_types;
464                 const char *dev_name;
465                 size_t dev_name_len;
466         } client[P2P_MAX_GROUP_ENTRIES];
467 };
468
469
470 /* p2p_utils.c */
471 int p2p_random(char *buf, size_t len);
472 int p2p_channel_to_freq(const char *country, int reg_class, int channel);
473 int p2p_freq_to_channel(const char *country, unsigned int freq, u8 *reg_class,
474                         u8 *channel);
475 void p2p_channels_intersect(const struct p2p_channels *a,
476                             const struct p2p_channels *b,
477                             struct p2p_channels *res);
478 int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
479                           u8 channel);
480
481 /* p2p_parse.c */
482 int p2p_parse_p2p_ie(const struct wpabuf *buf, struct p2p_message *msg);
483 int p2p_parse_ies(const u8 *data, size_t len, struct p2p_message *msg);
484 int p2p_parse(const u8 *data, size_t len, struct p2p_message *msg);
485 void p2p_parse_free(struct p2p_message *msg);
486 int p2p_attr_text(struct wpabuf *data, char *buf, char *end);
487 int p2p_group_info_parse(const u8 *gi, size_t gi_len,
488                          struct p2p_group_info *info);
489
490 /* p2p_build.c */
491
492 struct p2p_noa_desc {
493         u8 count_type;
494         u32 duration;
495         u32 interval;
496         u32 start_time;
497 };
498
499 /* p2p_group.c */
500 const u8 * p2p_group_get_interface_addr(struct p2p_group *group);
501 u8 p2p_group_presence_req(struct p2p_group *group,
502                           const u8 *client_interface_addr,
503                           const u8 *noa, size_t noa_len);
504
505
506 void p2p_buf_add_action_hdr(struct wpabuf *buf, u8 subtype, u8 dialog_token);
507 void p2p_buf_add_public_action_hdr(struct wpabuf *buf, u8 subtype,
508                                    u8 dialog_token);
509 u8 * p2p_buf_add_ie_hdr(struct wpabuf *buf);
510 void p2p_buf_add_status(struct wpabuf *buf, u8 status);
511 void p2p_buf_add_device_info(struct wpabuf *buf, struct p2p_data *p2p,
512                              struct p2p_device *peer);
513 void p2p_buf_add_device_id(struct wpabuf *buf, const u8 *dev_addr);
514 void p2p_buf_update_ie_hdr(struct wpabuf *buf, u8 *len);
515 void p2p_buf_add_capability(struct wpabuf *buf, u8 dev_capab, u8 group_capab);
516 void p2p_buf_add_go_intent(struct wpabuf *buf, u8 go_intent);
517 void p2p_buf_add_listen_channel(struct wpabuf *buf, const char *country,
518                                 u8 reg_class, u8 channel);
519 void p2p_buf_add_operating_channel(struct wpabuf *buf, const char *country,
520                                    u8 reg_class, u8 channel);
521 void p2p_buf_add_channel_list(struct wpabuf *buf, const char *country,
522                               struct p2p_channels *chan);
523 void p2p_buf_add_config_timeout(struct wpabuf *buf, u8 go_timeout,
524                                 u8 client_timeout);
525 void p2p_buf_add_intended_addr(struct wpabuf *buf, const u8 *interface_addr);
526 void p2p_buf_add_group_bssid(struct wpabuf *buf, const u8 *bssid);
527 void p2p_buf_add_group_id(struct wpabuf *buf, const u8 *dev_addr,
528                           const u8 *ssid, size_t ssid_len);
529 void p2p_buf_add_invitation_flags(struct wpabuf *buf, u8 flags);
530 void p2p_buf_add_noa(struct wpabuf *buf, u8 noa_index, u8 opp_ps, u8 ctwindow,
531                      struct p2p_noa_desc *desc1, struct p2p_noa_desc *desc2);
532 void p2p_buf_add_ext_listen_timing(struct wpabuf *buf, u16 period,
533                                    u16 interval);
534 void p2p_buf_add_p2p_interface(struct wpabuf *buf, struct p2p_data *p2p);
535 void p2p_build_wps_ie(struct p2p_data *p2p, struct wpabuf *buf, u16 pw_id,
536                       int all_attr);
537
538 /* p2p_sd.c */
539 struct p2p_sd_query * p2p_pending_sd_req(struct p2p_data *p2p,
540                                          struct p2p_device *dev);
541 void p2p_free_sd_queries(struct p2p_data *p2p);
542 void p2p_rx_gas_initial_req(struct p2p_data *p2p, const u8 *sa,
543                             const u8 *data, size_t len, int rx_freq);
544 void p2p_rx_gas_initial_resp(struct p2p_data *p2p, const u8 *sa,
545                              const u8 *data, size_t len, int rx_freq);
546 void p2p_rx_gas_comeback_req(struct p2p_data *p2p, const u8 *sa,
547                              const u8 *data, size_t len, int rx_freq);
548 void p2p_rx_gas_comeback_resp(struct p2p_data *p2p, const u8 *sa,
549                               const u8 *data, size_t len, int rx_freq);
550 int p2p_start_sd(struct p2p_data *p2p, struct p2p_device *dev);
551
552 /* p2p_go_neg.c */
553 int p2p_peer_channels_check(struct p2p_data *p2p, struct p2p_channels *own,
554                             struct p2p_device *dev,
555                             const u8 *channel_list, size_t channel_list_len);
556 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
557                             const u8 *data, size_t len, int rx_freq);
558 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
559                              const u8 *data, size_t len, int rx_freq);
560 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
561                              const u8 *data, size_t len);
562 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev);
563
564 /* p2p_pd.c */
565 void p2p_process_prov_disc_req(struct p2p_data *p2p, const u8 *sa,
566                                const u8 *data, size_t len, int rx_freq);
567 void p2p_process_prov_disc_resp(struct p2p_data *p2p, const u8 *sa,
568                                 const u8 *data, size_t len);
569 int p2p_send_prov_disc_req(struct p2p_data *p2p, struct p2p_device *dev,
570                            int join);
571
572 /* p2p_invitation.c */
573 void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
574                                 const u8 *data, size_t len, int rx_freq);
575 void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
576                                  const u8 *data, size_t len);
577 int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
578                     const u8 *go_dev_addr);
579 void p2p_invitation_req_cb(struct p2p_data *p2p, int success);
580 void p2p_invitation_resp_cb(struct p2p_data *p2p, int success);
581
582 /* p2p_dev_disc.c */
583 void p2p_process_dev_disc_req(struct p2p_data *p2p, const u8 *sa,
584                               const u8 *data, size_t len, int rx_freq);
585 void p2p_dev_disc_req_cb(struct p2p_data *p2p, int success);
586 int p2p_send_dev_disc_req(struct p2p_data *p2p, struct p2p_device *dev);
587 void p2p_dev_disc_resp_cb(struct p2p_data *p2p, int success);
588 void p2p_process_dev_disc_resp(struct p2p_data *p2p, const u8 *sa,
589                                const u8 *data, size_t len);
590 void p2p_go_disc_req_cb(struct p2p_data *p2p, int success);
591 void p2p_process_go_disc_req(struct p2p_data *p2p, const u8 *da, const u8 *sa,
592                              const u8 *data, size_t len, int rx_freq);
593
594 /* p2p.c */
595 void p2p_set_state(struct p2p_data *p2p, int new_state);
596 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec,
597                      unsigned int usec);
598 void p2p_clear_timeout(struct p2p_data *p2p);
599 void p2p_continue_find(struct p2p_data *p2p);
600 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
601                                                 const u8 *addr,
602                                                 struct p2p_message *msg);
603 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
604                       struct p2p_device *dev, struct p2p_message *msg);
605 struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr);
606 struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
607                                              const u8 *addr);
608 void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
609                        int status);
610 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer);
611 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps);
612 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
613                         size_t num_req_dev_type);
614 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p);
615 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len);
616 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
617                     const u8 *src, const u8 *bssid, const u8 *buf,
618                     size_t len, unsigned int wait_time);
619
620 #endif /* P2P_I_H */