Merge driver ops set_wps_beacon_ie and set_wps_probe_resp_ie
[libeap.git] / src / wps / wps.h
1 /*
2  * Wi-Fi Protected Setup
3  * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi>
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 WPS_H
16 #define WPS_H
17
18 #include "wps_defs.h"
19
20 /**
21  * enum wsc_op_code - EAP-WSC OP-Code values
22  */
23 enum wsc_op_code {
24         WSC_UPnP = 0 /* No OP Code in UPnP transport */,
25         WSC_Start = 0x01,
26         WSC_ACK = 0x02,
27         WSC_NACK = 0x03,
28         WSC_MSG = 0x04,
29         WSC_Done = 0x05,
30         WSC_FRAG_ACK = 0x06
31 };
32
33 struct wps_registrar;
34 struct upnp_wps_device_sm;
35 struct wps_er;
36
37 /**
38  * struct wps_credential - WPS Credential
39  * @ssid: SSID
40  * @ssid_len: Length of SSID
41  * @auth_type: Authentication Type (WPS_AUTH_OPEN, .. flags)
42  * @encr_type: Encryption Type (WPS_ENCR_NONE, .. flags)
43  * @key_idx: Key index
44  * @key: Key
45  * @key_len: Key length in octets
46  * @mac_addr: MAC address of the Credential receiver
47  * @cred_attr: Unparsed Credential attribute data (used only in cred_cb());
48  *      this may be %NULL, if not used
49  * @cred_attr_len: Length of cred_attr in octets
50  */
51 struct wps_credential {
52         u8 ssid[32];
53         size_t ssid_len;
54         u16 auth_type;
55         u16 encr_type;
56         u8 key_idx;
57         u8 key[64];
58         size_t key_len;
59         u8 mac_addr[ETH_ALEN];
60         const u8 *cred_attr;
61         size_t cred_attr_len;
62 };
63
64 #define WPS_DEV_TYPE_LEN 8
65 #define WPS_DEV_TYPE_BUFSIZE 21
66
67 /**
68  * struct wps_device_data - WPS Device Data
69  * @mac_addr: Device MAC address
70  * @device_name: Device Name (0..32 octets encoded in UTF-8)
71  * @manufacturer: Manufacturer (0..64 octets encoded in UTF-8)
72  * @model_name: Model Name (0..32 octets encoded in UTF-8)
73  * @model_number: Model Number (0..32 octets encoded in UTF-8)
74  * @serial_number: Serial Number (0..32 octets encoded in UTF-8)
75  * @pri_dev_type: Primary Device Type
76  * @os_version: OS Version
77  * @rf_bands: RF bands (WPS_RF_24GHZ, WPS_RF_50GHZ flags)
78  */
79 struct wps_device_data {
80         u8 mac_addr[ETH_ALEN];
81         char *device_name;
82         char *manufacturer;
83         char *model_name;
84         char *model_number;
85         char *serial_number;
86         u8 pri_dev_type[WPS_DEV_TYPE_LEN];
87         u32 os_version;
88         u8 rf_bands;
89 };
90
91 struct oob_conf_data {
92         enum {
93                 OOB_METHOD_UNKNOWN = 0,
94                 OOB_METHOD_DEV_PWD_E,
95                 OOB_METHOD_DEV_PWD_R,
96                 OOB_METHOD_CRED,
97         } oob_method;
98         struct wpabuf *dev_password;
99         struct wpabuf *pubkey_hash;
100 };
101
102 /**
103  * struct wps_config - WPS configuration for a single registration protocol run
104  */
105 struct wps_config {
106         /**
107          * wps - Pointer to long term WPS context
108          */
109         struct wps_context *wps;
110
111         /**
112          * registrar - Whether this end is a Registrar
113          */
114         int registrar;
115
116         /**
117          * pin - Enrollee Device Password (%NULL for Registrar or PBC)
118          */
119         const u8 *pin;
120
121         /**
122          * pin_len - Length on pin in octets
123          */
124         size_t pin_len;
125
126         /**
127          * pbc - Whether this is protocol run uses PBC
128          */
129         int pbc;
130
131         /**
132          * assoc_wps_ie: (Re)AssocReq WPS IE (in AP; %NULL if not AP)
133          */
134         const struct wpabuf *assoc_wps_ie;
135
136         /**
137          * new_ap_settings - New AP settings (%NULL if not used)
138          *
139          * This parameter provides new AP settings when using a wireless
140          * stations as a Registrar to configure the AP. %NULL means that AP
141          * will not be reconfigured, i.e., the station will only learn the
142          * current AP settings by using AP PIN.
143          */
144         const struct wps_credential *new_ap_settings;
145
146         /**
147          * peer_addr: MAC address of the peer in AP; %NULL if not AP
148          */
149         const u8 *peer_addr;
150
151         /**
152          * use_psk_key - Use PSK format key in Credential
153          *
154          * Force PSK format to be used instead of ASCII passphrase when
155          * building Credential for an Enrollee. The PSK value is set in
156          * struct wpa_context::psk.
157          */
158         int use_psk_key;
159 };
160
161 struct wps_data * wps_init(const struct wps_config *cfg);
162
163 void wps_deinit(struct wps_data *data);
164
165 /**
166  * enum wps_process_res - WPS message processing result
167  */
168 enum wps_process_res {
169         /**
170          * WPS_DONE - Processing done
171          */
172         WPS_DONE,
173
174         /**
175          * WPS_CONTINUE - Processing continues
176          */
177         WPS_CONTINUE,
178
179         /**
180          * WPS_FAILURE - Processing failed
181          */
182         WPS_FAILURE,
183
184         /**
185          * WPS_PENDING - Processing continues, but waiting for an external
186          *      event (e.g., UPnP message from an external Registrar)
187          */
188         WPS_PENDING
189 };
190 enum wps_process_res wps_process_msg(struct wps_data *wps,
191                                      enum wsc_op_code op_code,
192                                      const struct wpabuf *msg);
193
194 struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code);
195
196 int wps_is_selected_pbc_registrar(const struct wpabuf *msg);
197 int wps_is_selected_pin_registrar(const struct wpabuf *msg);
198 const u8 * wps_get_uuid_e(const struct wpabuf *msg);
199
200 struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type);
201 struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
202                                        const u8 *uuid,
203                                        enum wps_request_type req_type);
204
205
206 /**
207  * struct wps_registrar_config - WPS Registrar configuration
208  */
209 struct wps_registrar_config {
210         /**
211          * new_psk_cb - Callback for new PSK
212          * @ctx: Higher layer context data (cb_ctx)
213          * @mac_addr: MAC address of the Enrollee
214          * @psk: The new PSK
215          * @psk_len: The length of psk in octets
216          * Returns: 0 on success, -1 on failure
217          *
218          * This callback is called when a new per-device PSK is provisioned.
219          */
220         int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
221                           size_t psk_len);
222
223         /**
224          * set_ie_cb - Callback for WPS IE changes
225          * @ctx: Higher layer context data (cb_ctx)
226          * @beacon_ie: WPS IE for Beacon
227          * @probe_resp_ie: WPS IE for Probe Response
228          * Returns: 0 on success, -1 on failure
229          *
230          * This callback is called whenever the WPS IE in Beacon or Probe
231          * Response frames needs to be changed (AP only). Callee is responsible
232          * for freeing the buffers.
233          */
234         int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
235                          struct wpabuf *probe_resp_ie);
236
237         /**
238          * pin_needed_cb - Callback for requesting a PIN
239          * @ctx: Higher layer context data (cb_ctx)
240          * @uuid_e: UUID-E of the unknown Enrollee
241          * @dev: Device Data from the unknown Enrollee
242          *
243          * This callback is called whenever an unknown Enrollee requests to use
244          * PIN method and a matching PIN (Device Password) is not found in
245          * Registrar data.
246          */
247         void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
248                               const struct wps_device_data *dev);
249
250         /**
251          * reg_success_cb - Callback for reporting successful registration
252          * @ctx: Higher layer context data (cb_ctx)
253          * @mac_addr: MAC address of the Enrollee
254          * @uuid_e: UUID-E of the Enrollee
255          *
256          * This callback is called whenever an Enrollee completes registration
257          * successfully.
258          */
259         void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
260                                const u8 *uuid_e);
261
262         /**
263          * set_sel_reg_cb - Callback for reporting selected registrar changes
264          * @ctx: Higher layer context data (cb_ctx)
265          * @sel_reg: Whether the Registrar is selected
266          * @dev_passwd_id: Device Password ID to indicate with method or
267          *      specific password the Registrar intends to use
268          * @sel_reg_config_methods: Bit field of active config methods
269          *
270          * This callback is called whenever the Selected Registrar state
271          * changes (e.g., a new PIN becomes available or PBC is invoked). This
272          * callback is only used by External Registrar implementation;
273          * set_ie_cb() is used by AP implementation in similar caes, but it
274          * provides the full WPS IE data instead of just the minimal Registrar
275          * state information.
276          */
277         void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
278                                u16 sel_reg_config_methods);
279
280         /**
281          * cb_ctx: Higher layer context data for Registrar callbacks
282          */
283         void *cb_ctx;
284
285         /**
286          * skip_cred_build: Do not build credential
287          *
288          * This option can be used to disable internal code that builds
289          * Credential attribute into M8 based on the current network
290          * configuration and Enrollee capabilities. The extra_cred data will
291          * then be used as the Credential(s).
292          */
293         int skip_cred_build;
294
295         /**
296          * extra_cred: Additional Credential attribute(s)
297          *
298          * This optional data (set to %NULL to disable) can be used to add
299          * Credential attribute(s) for other networks into M8. If
300          * skip_cred_build is set, this will also override the automatically
301          * generated Credential attribute.
302          */
303         const u8 *extra_cred;
304
305         /**
306          * extra_cred_len: Length of extra_cred in octets
307          */
308         size_t extra_cred_len;
309
310         /**
311          * disable_auto_conf - Disable auto-configuration on first registration
312          *
313          * By default, the AP that is started in not configured state will
314          * generate a random PSK and move to configured state when the first
315          * registration protocol run is completed successfully. This option can
316          * be used to disable this functionality and leave it up to an external
317          * program to take care of configuration. This requires the extra_cred
318          * to be set with a suitable Credential and skip_cred_build being used.
319          */
320         int disable_auto_conf;
321
322         /**
323          * static_wep_only - Whether the BSS supports only static WEP
324          */
325         int static_wep_only;
326 };
327
328
329 /**
330  * enum wps_event - WPS event types
331  */
332 enum wps_event {
333         /**
334          * WPS_EV_M2D - M2D received (Registrar did not know us)
335          */
336         WPS_EV_M2D,
337
338         /**
339          * WPS_EV_FAIL - Registration failed
340          */
341         WPS_EV_FAIL,
342
343         /**
344          * WPS_EV_SUCCESS - Registration succeeded
345          */
346         WPS_EV_SUCCESS,
347
348         /**
349          * WPS_EV_PWD_AUTH_FAIL - Password authentication failed
350          */
351         WPS_EV_PWD_AUTH_FAIL,
352
353         /**
354          * WPS_EV_PBC_OVERLAP - PBC session overlap detected
355          */
356         WPS_EV_PBC_OVERLAP,
357
358         /**
359          * WPS_EV_PBC_TIMEOUT - PBC walktime expired before protocol run start
360          */
361         WPS_EV_PBC_TIMEOUT,
362
363         /**
364          * WPS_EV_ER_AP_ADD - ER: AP added
365          */
366         WPS_EV_ER_AP_ADD,
367
368         /**
369          * WPS_EV_ER_AP_REMOVE - ER: AP removed
370          */
371         WPS_EV_ER_AP_REMOVE,
372
373         /**
374          * WPS_EV_ER_ENROLLEE_ADD - ER: Enrollee added
375          */
376         WPS_EV_ER_ENROLLEE_ADD,
377
378         /**
379          * WPS_EV_ER_ENROLLEE_REMOVE - ER: Enrollee removed
380          */
381         WPS_EV_ER_ENROLLEE_REMOVE
382 };
383
384 /**
385  * union wps_event_data - WPS event data
386  */
387 union wps_event_data {
388         /**
389          * struct wps_event_m2d - M2D event data
390          */
391         struct wps_event_m2d {
392                 u16 config_methods;
393                 const u8 *manufacturer;
394                 size_t manufacturer_len;
395                 const u8 *model_name;
396                 size_t model_name_len;
397                 const u8 *model_number;
398                 size_t model_number_len;
399                 const u8 *serial_number;
400                 size_t serial_number_len;
401                 const u8 *dev_name;
402                 size_t dev_name_len;
403                 const u8 *primary_dev_type; /* 8 octets */
404                 u16 config_error;
405                 u16 dev_password_id;
406         } m2d;
407
408         /**
409          * struct wps_event_fail - Registration failure information
410          * @msg: enum wps_msg_type
411          */
412         struct wps_event_fail {
413                 int msg;
414         } fail;
415
416         struct wps_event_pwd_auth_fail {
417                 int enrollee;
418                 int part;
419         } pwd_auth_fail;
420
421         struct wps_event_er_ap {
422                 const u8 *uuid;
423                 const u8 *mac_addr;
424                 const char *friendly_name;
425                 const char *manufacturer;
426                 const char *manufacturer_url;
427                 const char *model_description;
428                 const char *model_name;
429                 const char *model_number;
430                 const char *model_url;
431                 const char *serial_number;
432                 const char *upc;
433                 const u8 *pri_dev_type;
434                 u8 wps_state;
435         } ap;
436
437         struct wps_event_er_enrollee {
438                 const u8 *uuid;
439                 const u8 *mac_addr;
440                 int m1_received;
441                 u16 config_methods;
442                 u16 dev_passwd_id;
443                 const u8 *pri_dev_type;
444                 const char *dev_name;
445                 const char *manufacturer;
446                 const char *model_name;
447                 const char *model_number;
448                 const char *serial_number;
449         } enrollee;
450 };
451
452 /**
453  * struct upnp_pending_message - Pending PutWLANResponse messages
454  * @next: Pointer to next pending message or %NULL
455  * @addr: NewWLANEventMAC
456  * @msg: NewMessage
457  * @type: Message Type
458  */
459 struct upnp_pending_message {
460         struct upnp_pending_message *next;
461         u8 addr[ETH_ALEN];
462         struct wpabuf *msg;
463         enum wps_msg_type type;
464 };
465
466 /**
467  * struct wps_context - Long term WPS context data
468  *
469  * This data is stored at the higher layer Authenticator or Supplicant data
470  * structures and it is maintained over multiple registration protocol runs.
471  */
472 struct wps_context {
473         /**
474          * ap - Whether the local end is an access point
475          */
476         int ap;
477
478         /**
479          * registrar - Pointer to WPS registrar data from wps_registrar_init()
480          */
481         struct wps_registrar *registrar;
482
483         /**
484          * wps_state - Current WPS state
485          */
486         enum wps_state wps_state;
487
488         /**
489          * ap_setup_locked - Whether AP setup is locked (only used at AP)
490          */
491         int ap_setup_locked;
492
493         /**
494          * uuid - Own UUID
495          */
496         u8 uuid[16];
497
498         /**
499          * ssid - SSID
500          *
501          * This SSID is used by the Registrar to fill in information for
502          * Credentials. In addition, AP uses it when acting as an Enrollee to
503          * notify Registrar of the current configuration.
504          */
505         u8 ssid[32];
506
507         /**
508          * ssid_len - Length of ssid in octets
509          */
510         size_t ssid_len;
511
512         /**
513          * dev - Own WPS device data
514          */
515         struct wps_device_data dev;
516
517         /**
518          * oob_conf - OOB Config data
519          */
520         struct oob_conf_data oob_conf;
521
522         /**
523          * oob_dev_pw_id - OOB Device password id
524          */
525         u16 oob_dev_pw_id;
526
527         /**
528          * dh_ctx - Context data for Diffie-Hellman operation
529          */
530         void *dh_ctx;
531
532         /**
533          * dh_privkey - Diffie-Hellman private key
534          */
535         struct wpabuf *dh_privkey;
536
537         /**
538          * dh_pubkey_oob - Diffie-Hellman public key
539          */
540         struct wpabuf *dh_pubkey;
541
542         /**
543          * config_methods - Enabled configuration methods
544          *
545          * Bit field of WPS_CONFIG_*
546          */
547         u16 config_methods;
548
549         /**
550          * encr_types - Enabled encryption types (bit field of WPS_ENCR_*)
551          */
552         u16 encr_types;
553
554         /**
555          * auth_types - Authentication types (bit field of WPS_AUTH_*)
556          */
557         u16 auth_types;
558
559         /**
560          * network_key - The current Network Key (PSK) or %NULL to generate new
561          *
562          * If %NULL, Registrar will generate per-device PSK. In addition, AP
563          * uses this when acting as an Enrollee to notify Registrar of the
564          * current configuration.
565          *
566          * When using WPA/WPA2-Person, this key can be either the ASCII
567          * passphrase (8..63 characters) or the 32-octet PSK (64 hex
568          * characters). When this is set to the ASCII passphrase, the PSK can
569          * be provided in the psk buffer and used per-Enrollee to control which
570          * key type is included in the Credential (e.g., to reduce calculation
571          * need on low-powered devices by provisioning PSK while still allowing
572          * other devices to get the passphrase).
573          */
574         u8 *network_key;
575
576         /**
577          * network_key_len - Length of network_key in octets
578          */
579         size_t network_key_len;
580
581         /**
582          * psk - The current network PSK
583          *
584          * This optional value can be used to provide the current PSK if
585          * network_key is set to the ASCII passphrase.
586          */
587         u8 psk[32];
588
589         /**
590          * psk_set - Whether psk value is set
591          */
592         int psk_set;
593
594         /**
595          * ap_settings - AP Settings override for M7 (only used at AP)
596          *
597          * If %NULL, AP Settings attributes will be generated based on the
598          * current network configuration.
599          */
600         u8 *ap_settings;
601
602         /**
603          * ap_settings_len - Length of ap_settings in octets
604          */
605         size_t ap_settings_len;
606
607         /**
608          * friendly_name - Friendly Name (required for UPnP)
609          */
610         char *friendly_name;
611
612         /**
613          * manufacturer_url - Manufacturer URL (optional for UPnP)
614          */
615         char *manufacturer_url;
616
617         /**
618          * model_description - Model Description (recommended for UPnP)
619          */
620         char *model_description;
621
622         /**
623          * model_url - Model URL (optional for UPnP)
624          */
625         char *model_url;
626
627         /**
628          * upc - Universal Product Code (optional for UPnP)
629          */
630         char *upc;
631
632         /**
633          * cred_cb - Callback to notify that new Credentials were received
634          * @ctx: Higher layer context data (cb_ctx)
635          * @cred: The received Credential
636          * Return: 0 on success, -1 on failure
637          */
638         int (*cred_cb)(void *ctx, const struct wps_credential *cred);
639
640         /**
641          * event_cb - Event callback (state information about progress)
642          * @ctx: Higher layer context data (cb_ctx)
643          * @event: Event type
644          * @data: Event data
645          */
646         void (*event_cb)(void *ctx, enum wps_event event,
647                          union wps_event_data *data);
648
649         /**
650          * cb_ctx: Higher layer context data for callbacks
651          */
652         void *cb_ctx;
653
654         struct upnp_wps_device_sm *wps_upnp;
655
656         /* Pending messages from UPnP PutWLANResponse */
657         struct upnp_pending_message *upnp_msgs;
658 };
659
660 struct oob_device_data {
661         char *device_name;
662         char *device_path;
663         void * (*init_func)(struct wps_context *, struct oob_device_data *,
664                             int);
665         struct wpabuf * (*read_func)(void *);
666         int (*write_func)(void *, struct wpabuf *);
667         void (*deinit_func)(void *);
668 };
669
670 struct oob_nfc_device_data {
671         int (*init_func)(char *);
672         void * (*read_func)(size_t *);
673         int (*write_func)(void *, size_t);
674         void (*deinit_func)(void);
675 };
676
677 struct wps_registrar *
678 wps_registrar_init(struct wps_context *wps,
679                    const struct wps_registrar_config *cfg);
680 void wps_registrar_deinit(struct wps_registrar *reg);
681 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
682                           const u8 *pin, size_t pin_len, int timeout);
683 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid);
684 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid);
685 int wps_registrar_button_pushed(struct wps_registrar *reg);
686 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
687                                 const struct wpabuf *wps_data);
688 int wps_registrar_update_ie(struct wps_registrar *reg);
689 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
690                            char *buf, size_t buflen);
691
692 unsigned int wps_pin_checksum(unsigned int pin);
693 unsigned int wps_pin_valid(unsigned int pin);
694 unsigned int wps_generate_pin(void);
695 void wps_free_pending_msgs(struct upnp_pending_message *msgs);
696
697 struct oob_device_data * wps_get_oob_device(char *device_type);
698 struct oob_nfc_device_data * wps_get_oob_nfc_device(char *device_name);
699 int wps_get_oob_method(char *method);
700 int wps_process_oob(struct wps_context *wps, struct oob_device_data *oob_dev,
701                     int registrar);
702 int wps_attr_text(struct wpabuf *data, char *buf, char *end);
703
704 struct wps_er * wps_er_init(struct wps_context *wps, const char *ifname);
705 void wps_er_refresh(struct wps_er *er);
706 void wps_er_deinit(struct wps_er *er, void (*cb)(void *ctx), void *ctx);
707 void wps_er_set_sel_reg(struct wps_er *er, int sel_reg, u16 dev_passwd_id,
708                         u16 sel_reg_config_methods);
709 int wps_er_pbc(struct wps_er *er, const u8 *uuid);
710 int wps_er_learn(struct wps_er *er, const u8 *uuid, const u8 *pin,
711                  size_t pin_len);
712
713 int wps_dev_type_str2bin(const char *str, u8 dev_type[WPS_DEV_TYPE_LEN]);
714 char * wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN], char *buf,
715                             size_t buf_len);
716 void uuid_gen_mac_addr(const u8 *mac_addr, u8 *uuid);
717 u16 wps_config_methods_str2bin(const char *str);
718
719 #endif /* WPS_H */