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