WPS: Store device info and make it available through AP ctrl_iface
[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
36 /**
37  * struct wps_credential - WPS Credential
38  * @ssid: SSID
39  * @ssid_len: Length of SSID
40  * @auth_type: Authentication Type (WPS_AUTH_OPEN, .. flags)
41  * @encr_type: Encryption Type (WPS_ENCR_NONE, .. flags)
42  * @key_idx: Key index
43  * @key: Key
44  * @key_len: Key length in octets
45  * @mac_addr: MAC address of the peer
46  * @cred_attr: Unparsed Credential attribute data (used only in cred_cb());
47  *      this may be %NULL, if not used
48  * @cred_attr_len: Length of cred_attr in octets
49  */
50 struct wps_credential {
51         u8 ssid[32];
52         size_t ssid_len;
53         u16 auth_type;
54         u16 encr_type;
55         u8 key_idx;
56         u8 key[64];
57         size_t key_len;
58         u8 mac_addr[ETH_ALEN];
59         const u8 *cred_attr;
60         size_t cred_attr_len;
61 };
62
63 /**
64  * struct wps_device_data - WPS Device Data
65  * @mac_addr: Device MAC address
66  * @device_name: Device Name (0..32 octets encoded in UTF-8)
67  * @manufacturer: Manufacturer (0..64 octets encoded in UTF-8)
68  * @model_name: Model Name (0..32 octets encoded in UTF-8)
69  * @model_number: Model Number (0..32 octets encoded in UTF-8)
70  * @serial_number: Serial Number (0..32 octets encoded in UTF-8)
71  * @categ: Primary Device Category
72  * @oui: Primary Device OUI
73  * @sub_categ: Primary Device Sub-Category
74  * @os_version: OS Version
75  * @rf_bands: RF bands (WPS_RF_24GHZ, WPS_RF_50GHZ flags)
76  */
77 struct wps_device_data {
78         u8 mac_addr[ETH_ALEN];
79         char *device_name;
80         char *manufacturer;
81         char *model_name;
82         char *model_number;
83         char *serial_number;
84         u16 categ;
85         u32 oui;
86         u16 sub_categ;
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 struct wps_data * wps_init(const struct wps_config *cfg);
153
154 void wps_deinit(struct wps_data *data);
155
156 /**
157  * enum wps_process_res - WPS message processing result
158  */
159 enum wps_process_res {
160         /**
161          * WPS_DONE - Processing done
162          */
163         WPS_DONE,
164
165         /**
166          * WPS_CONTINUE - Processing continues
167          */
168         WPS_CONTINUE,
169
170         /**
171          * WPS_FAILURE - Processing failed
172          */
173         WPS_FAILURE,
174
175         /**
176          * WPS_PENDING - Processing continues, but waiting for an external
177          *      event (e.g., UPnP message from an external Registrar)
178          */
179         WPS_PENDING
180 };
181 enum wps_process_res wps_process_msg(struct wps_data *wps,
182                                      enum wsc_op_code op_code,
183                                      const struct wpabuf *msg);
184
185 struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code);
186
187 int wps_is_selected_pbc_registrar(const struct wpabuf *msg);
188 int wps_is_selected_pin_registrar(const struct wpabuf *msg);
189 const u8 * wps_get_uuid_e(const struct wpabuf *msg);
190
191 struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type);
192 struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
193                                        const u8 *uuid,
194                                        enum wps_request_type req_type);
195
196
197 /**
198  * struct wps_registrar_config - WPS Registrar configuration
199  */
200 struct wps_registrar_config {
201         /**
202          * new_psk_cb - Callback for new PSK
203          * @ctx: Higher layer context data (cb_ctx)
204          * @mac_addr: MAC address of the Enrollee
205          * @psk: The new PSK
206          * @psk_len: The length of psk in octets
207          * Returns: 0 on success, -1 on failure
208          *
209          * This callback is called when a new per-device PSK is provisioned.
210          */
211         int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
212                           size_t psk_len);
213
214         /**
215          * set_ie_cb - Callback for WPS IE changes
216          * @ctx: Higher layer context data (cb_ctx)
217          * @beacon_ie: WPS IE for Beacon
218          * @beacon_ie_len: WPS IE length for Beacon
219          * @probe_resp_ie: WPS IE for Probe Response
220          * @probe_resp_ie_len: WPS IE length for Probe Response
221          * Returns: 0 on success, -1 on failure
222          *
223          * This callback is called whenever the WPS IE in Beacon or Probe
224          * Response frames needs to be changed (AP only).
225          */
226         int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
227                          const u8 *probe_resp_ie, size_t probe_resp_ie_len);
228
229         /**
230          * pin_needed_cb - Callback for requesting a PIN
231          * @ctx: Higher layer context data (cb_ctx)
232          * @uuid_e: UUID-E of the unknown Enrollee
233          * @dev: Device Data from the unknown Enrollee
234          *
235          * This callback is called whenever an unknown Enrollee requests to use
236          * PIN method and a matching PIN (Device Password) is not found in
237          * Registrar data.
238          */
239         void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
240                               const struct wps_device_data *dev);
241
242         /**
243          * reg_success_cb - Callback for reporting successful registration
244          * @ctx: Higher layer context data (cb_ctx)
245          * @mac_addr: MAC address of the Enrollee
246          * @uuid_e: UUID-E of the Enrollee
247          *
248          * This callback is called whenever an Enrollee completes registration
249          * successfully.
250          */
251         void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
252                                const u8 *uuid_e);
253
254         /**
255          * cb_ctx: Higher layer context data for Registrar callbacks
256          */
257         void *cb_ctx;
258
259         /**
260          * skip_cred_build: Do not build credential
261          *
262          * This option can be used to disable internal code that builds
263          * Credential attribute into M8 based on the current network
264          * configuration and Enrollee capabilities. The extra_cred data will
265          * then be used as the Credential(s).
266          */
267         int skip_cred_build;
268
269         /**
270          * extra_cred: Additional Credential attribute(s)
271          *
272          * This optional data (set to %NULL to disable) can be used to add
273          * Credential attribute(s) for other networks into M8. If
274          * skip_cred_build is set, this will also override the automatically
275          * generated Credential attribute.
276          */
277         const u8 *extra_cred;
278
279         /**
280          * extra_cred_len: Length of extra_cred in octets
281          */
282         size_t extra_cred_len;
283
284         /**
285          * disable_auto_conf - Disable auto-configuration on first registration
286          *
287          * By default, the AP that is started in not configured state will
288          * generate a random PSK and move to configured state when the first
289          * registration protocol run is completed successfully. This option can
290          * be used to disable this functionality and leave it up to an external
291          * program to take care of configuration. This requires the extra_cred
292          * to be set with a suitable Credential and skip_cred_build being used.
293          */
294         int disable_auto_conf;
295
296         /**
297          * static_wep_only - Whether the BSS supports only static WEP
298          */
299         int static_wep_only;
300 };
301
302
303 /**
304  * enum wps_event - WPS event types
305  */
306 enum wps_event {
307         /**
308          * WPS_EV_M2D - M2D received (Registrar did not know us)
309          */
310         WPS_EV_M2D,
311
312         /**
313          * WPS_EV_FAIL - Registration failed
314          */
315         WPS_EV_FAIL,
316
317         /**
318          * WPS_EV_SUCCESS - Registration succeeded
319          */
320         WPS_EV_SUCCESS,
321
322         /**
323          * WPS_EV_PWD_AUTH_FAIL - Password authentication failed
324          */
325         WPS_EV_PWD_AUTH_FAIL
326 };
327
328 /**
329  * union wps_event_data - WPS event data
330  */
331 union wps_event_data {
332         /**
333          * struct wps_event_m2d - M2D event data
334          */
335         struct wps_event_m2d {
336                 u16 config_methods;
337                 const u8 *manufacturer;
338                 size_t manufacturer_len;
339                 const u8 *model_name;
340                 size_t model_name_len;
341                 const u8 *model_number;
342                 size_t model_number_len;
343                 const u8 *serial_number;
344                 size_t serial_number_len;
345                 const u8 *dev_name;
346                 size_t dev_name_len;
347                 const u8 *primary_dev_type; /* 8 octets */
348                 u16 config_error;
349                 u16 dev_password_id;
350         } m2d;
351
352         /**
353          * struct wps_event_fail - Registration failure information
354          * @msg: enum wps_msg_type
355          */
356         struct wps_event_fail {
357                 int msg;
358         } fail;
359
360         struct wps_event_pwd_auth_fail {
361                 int enrollee;
362                 int part;
363         } pwd_auth_fail;
364 };
365
366 /**
367  * struct upnp_pending_message - Pending PutWLANResponse messages
368  * @next: Pointer to next pending message or %NULL
369  * @addr: NewWLANEventMAC
370  * @msg: NewMessage
371  * @type: Message Type
372  */
373 struct upnp_pending_message {
374         struct upnp_pending_message *next;
375         u8 addr[ETH_ALEN];
376         struct wpabuf *msg;
377         enum wps_msg_type type;
378 };
379
380 /**
381  * struct wps_context - Long term WPS context data
382  *
383  * This data is stored at the higher layer Authenticator or Supplicant data
384  * structures and it is maintained over multiple registration protocol runs.
385  */
386 struct wps_context {
387         /**
388          * ap - Whether the local end is an access point
389          */
390         int ap;
391
392         /**
393          * registrar - Pointer to WPS registrar data from wps_registrar_init()
394          */
395         struct wps_registrar *registrar;
396
397         /**
398          * wps_state - Current WPS state
399          */
400         enum wps_state wps_state;
401
402         /**
403          * ap_setup_locked - Whether AP setup is locked (only used at AP)
404          */
405         int ap_setup_locked;
406
407         /**
408          * uuid - Own UUID
409          */
410         u8 uuid[16];
411
412         /**
413          * ssid - SSID
414          *
415          * This SSID is used by the Registrar to fill in information for
416          * Credentials. In addition, AP uses it when acting as an Enrollee to
417          * notify Registrar of the current configuration.
418          */
419         u8 ssid[32];
420
421         /**
422          * ssid_len - Length of ssid in octets
423          */
424         size_t ssid_len;
425
426         /**
427          * dev - Own WPS device data
428          */
429         struct wps_device_data dev;
430
431         /**
432          * oob_conf - OOB Config data
433          */
434         struct oob_conf_data oob_conf;
435
436         /**
437          * oob_dev_pw_id - OOB Device password id
438          */
439         u16 oob_dev_pw_id;
440
441         /**
442          * dh_privkey - Diffie-Hellman private key
443          */
444         struct wpabuf *dh_privkey;
445
446         /**
447          * dh_pubkey_oob - Diffie-Hellman public key
448          */
449         struct wpabuf *dh_pubkey;
450
451         /**
452          * config_methods - Enabled configuration methods
453          *
454          * Bit field of WPS_CONFIG_*
455          */
456         u16 config_methods;
457
458         /**
459          * encr_types - Enabled encryption types (bit field of WPS_ENCR_*)
460          */
461         u16 encr_types;
462
463         /**
464          * auth_types - Authentication types (bit field of WPS_AUTH_*)
465          */
466         u16 auth_types;
467
468         /**
469          * network_key - The current Network Key (PSK) or %NULL to generate new
470          *
471          * If %NULL, Registrar will generate per-device PSK. In addition, AP
472          * uses this when acting as an Enrollee to notify Registrar of the
473          * current configuration.
474          */
475         u8 *network_key;
476
477         /**
478          * network_key_len - Length of network_key in octets
479          */
480         size_t network_key_len;
481
482         /**
483          * ap_settings - AP Settings override for M7 (only used at AP)
484          *
485          * If %NULL, AP Settings attributes will be generated based on the
486          * current network configuration.
487          */
488         u8 *ap_settings;
489
490         /**
491          * ap_settings_len - Length of ap_settings in octets
492          */
493         size_t ap_settings_len;
494
495         /**
496          * friendly_name - Friendly Name (required for UPnP)
497          */
498         char *friendly_name;
499
500         /**
501          * manufacturer_url - Manufacturer URL (optional for UPnP)
502          */
503         char *manufacturer_url;
504
505         /**
506          * model_description - Model Description (recommended for UPnP)
507          */
508         char *model_description;
509
510         /**
511          * model_url - Model URL (optional for UPnP)
512          */
513         char *model_url;
514
515         /**
516          * upc - Universal Product Code (optional for UPnP)
517          */
518         char *upc;
519
520         /**
521          * cred_cb - Callback to notify that new Credentials were received
522          * @ctx: Higher layer context data (cb_ctx)
523          * @cred: The received Credential
524          * Return: 0 on success, -1 on failure
525          */
526         int (*cred_cb)(void *ctx, const struct wps_credential *cred);
527
528         /**
529          * event_cb - Event callback (state information about progress)
530          * @ctx: Higher layer context data (cb_ctx)
531          * @event: Event type
532          * @data: Event data
533          */
534         void (*event_cb)(void *ctx, enum wps_event event,
535                          union wps_event_data *data);
536
537         /**
538          * cb_ctx: Higher layer context data for callbacks
539          */
540         void *cb_ctx;
541
542         struct upnp_wps_device_sm *wps_upnp;
543
544         /* Pending messages from UPnP PutWLANResponse */
545         struct upnp_pending_message *upnp_msgs;
546 };
547
548 struct oob_device_data {
549         char *device_name;
550         char *device_path;
551         void * (*init_func)(struct wps_context *, struct oob_device_data *,
552                             int);
553         struct wpabuf * (*read_func)(void *);
554         int (*write_func)(void *, struct wpabuf *);
555         void (*deinit_func)(void *);
556 };
557
558 struct oob_nfc_device_data {
559         int (*init_func)(char *);
560         void * (*read_func)(size_t *);
561         int (*write_func)(void *, size_t);
562         void (*deinit_func)(void);
563 };
564
565 struct wps_registrar *
566 wps_registrar_init(struct wps_context *wps,
567                    const struct wps_registrar_config *cfg);
568 void wps_registrar_deinit(struct wps_registrar *reg);
569 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
570                           const u8 *pin, size_t pin_len, int timeout);
571 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid);
572 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid);
573 int wps_registrar_button_pushed(struct wps_registrar *reg);
574 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
575                                 const struct wpabuf *wps_data);
576 int wps_registrar_update_ie(struct wps_registrar *reg);
577 int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
578                                          const struct wpabuf *msg);
579 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
580                            char *buf, size_t buflen);
581
582 unsigned int wps_pin_checksum(unsigned int pin);
583 unsigned int wps_pin_valid(unsigned int pin);
584 unsigned int wps_generate_pin(void);
585 void wps_free_pending_msgs(struct upnp_pending_message *msgs);
586
587 struct oob_device_data * wps_get_oob_device(char *device_type);
588 struct oob_nfc_device_data * wps_get_oob_nfc_device(char *device_name);
589 int wps_get_oob_method(char *method);
590 int wps_process_oob(struct wps_context *wps, struct oob_device_data *oob_dev,
591                     int registrar);
592
593 #endif /* WPS_H */