WPS: Added option to disable AP auto-config on first registration
[libeap.git] / src / wps / wps.h
1 /*
2  * Wi-Fi Protected Setup
3  * Copyright (c) 2007-2008, 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_Start = 0x01,
25         WSC_ACK = 0x02,
26         WSC_NACK = 0x03,
27         WSC_MSG = 0x04,
28         WSC_Done = 0x05,
29         WSC_FRAG_ACK = 0x06
30 };
31
32 struct wps_registrar;
33
34 /**
35  * struct wps_credential - WPS Credential
36  * @ssid: SSID
37  * @ssid_len: Length of SSID
38  * @auth_type: Authentication Type (WPS_AUTH_OPEN, .. flags)
39  * @encr_type: Encryption Type (WPS_ENCR_NONE, .. flags)
40  * @key_idx: Key index
41  * @key: Key
42  * @key_len: Key length in octets
43  * @mac_addr: MAC address of the peer
44  * @cred_attr: Unparsed Credential attribute data (used only in cred_cb());
45  *      this may be %NULL, if not used
46  * @cred_attr_len: Length of cred_attr in octets
47  */
48 struct wps_credential {
49         u8 ssid[32];
50         size_t ssid_len;
51         u16 auth_type;
52         u16 encr_type;
53         u8 key_idx;
54         u8 key[64];
55         size_t key_len;
56         u8 mac_addr[ETH_ALEN];
57         const u8 *cred_attr;
58         size_t cred_attr_len;
59 };
60
61 /**
62  * struct wps_device_data - WPS Device Data
63  * @mac_addr: Device MAC address
64  * @device_name: Device Name (0..32 octets encoded in UTF-8)
65  * @manufacturer: Manufacturer (0..64 octets encoded in UTF-8)
66  * @model_name: Model Name (0..32 octets encoded in UTF-8)
67  * @model_number: Model Number (0..32 octets encoded in UTF-8)
68  * @serial_number: Serial Number (0..32 octets encoded in UTF-8)
69  * @categ: Primary Device Category
70  * @oui: Primary Device OUI
71  * @sub_categ: Primary Device Sub-Category
72  * @os_version: OS Version
73  * @rf_bands: RF bands (WPS_RF_24GHZ, WPS_RF_50GHZ flags)
74  */
75 struct wps_device_data {
76         u8 mac_addr[ETH_ALEN];
77         char *device_name;
78         char *manufacturer;
79         char *model_name;
80         char *model_number;
81         char *serial_number;
82         u16 categ;
83         u32 oui;
84         u16 sub_categ;
85         u32 os_version;
86         u8 rf_bands;
87 };
88
89 /**
90  * struct wps_config - WPS configuration for a single registration protocol run
91  */
92 struct wps_config {
93         /**
94          * wps - Pointer to long term WPS context
95          */
96         struct wps_context *wps;
97
98         /**
99          * registrar - Whether this end is a Registrar
100          */
101         int registrar;
102
103         /**
104          * pin - Enrollee Device Password (%NULL for Registrar or PBC)
105          */
106         const u8 *pin;
107
108         /**
109          * pin_len - Length on pin in octets
110          */
111         size_t pin_len;
112
113         /**
114          * pbc - Whether this is protocol run uses PBC
115          */
116         int pbc;
117
118         /**
119          * assoc_wps_ie: (Re)AssocReq WPS IE (in AP; %NULL if not AP)
120          */
121         const struct wpabuf *assoc_wps_ie;
122 };
123
124 struct wps_data * wps_init(const struct wps_config *cfg);
125
126 void wps_deinit(struct wps_data *data);
127
128 /**
129  * enum wps_process_res - WPS message processing result
130  */
131 enum wps_process_res {
132         /**
133          * WPS_DONE - Processing done
134          */
135         WPS_DONE,
136
137         /**
138          * WPS_CONTINUE - Processing continues
139          */
140         WPS_CONTINUE,
141
142         /**
143          * WPS_FAILURE - Processing failed
144          */
145         WPS_FAILURE
146 };
147 enum wps_process_res wps_process_msg(struct wps_data *wps,
148                                      enum wsc_op_code op_code,
149                                      const struct wpabuf *msg);
150
151 struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code);
152
153 int wps_is_selected_pbc_registrar(const struct wpabuf *msg);
154 int wps_is_selected_pin_registrar(const struct wpabuf *msg);
155 const u8 * wps_get_uuid_e(const struct wpabuf *msg);
156
157 struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type);
158 struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
159                                        const u8 *uuid,
160                                        enum wps_request_type req_type);
161
162
163 /**
164  * struct wps_registrar_config - WPS Registrar configuration
165  */
166 struct wps_registrar_config {
167         /**
168          * new_psk_cb - Callback for new PSK
169          * @ctx: Higher layer context data (cb_ctx)
170          * @mac_addr: MAC address of the Enrollee
171          * @psk: The new PSK
172          * @psk_len: The length of psk in octets
173          * Returns: 0 on success, -1 on failure
174          *
175          * This callback is called when a new per-device PSK is provisioned.
176          */
177         int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
178                           size_t psk_len);
179
180         /**
181          * set_ie_cb - Callback for WPS IE changes
182          * @ctx: Higher layer context data (cb_ctx)
183          * @beacon_ie: WPS IE for Beacon
184          * @beacon_ie_len: WPS IE length for Beacon
185          * @probe_resp_ie: WPS IE for Probe Response
186          * @probe_resp_ie_len: WPS IE length for Probe Response
187          * Returns: 0 on success, -1 on failure
188          *
189          * This callback is called whenever the WPS IE in Beacon or Probe
190          * Response frames needs to be changed (AP only).
191          */
192         int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
193                          const u8 *probe_resp_ie, size_t probe_resp_ie_len);
194
195         /**
196          * pin_needed_cb - Callback for requesting a PIN
197          * @ctx: Higher layer context data (cb_ctx)
198          * @uuid_e: UUID-E of the unknown Enrollee
199          * @dev: Device Data from the unknown Enrollee
200          *
201          * This callback is called whenever an unknown Enrollee requests to use
202          * PIN method and a matching PIN (Device Password) is not found in
203          * Registrar data.
204          */
205         void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
206                               const struct wps_device_data *dev);
207
208         /**
209          * reg_success_cb - Callback for reporting successful registration
210          * @ctx: Higher layer context data (cb_ctx)
211          * @mac_addr: MAC address of the Enrollee
212          * @uuid_e: UUID-E of the Enrollee
213          *
214          * This callback is called whenever an Enrollee completes registration
215          * successfully.
216          */
217         void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
218                                const u8 *uuid_e);
219
220         /**
221          * cb_ctx: Higher layer context data for Registrar callbacks
222          */
223         void *cb_ctx;
224
225         /**
226          * skip_cred_build: Do not build credential
227          *
228          * This option can be used to disable internal code that builds
229          * Credential attribute into M8 based on the current network
230          * configuration and Enrollee capabilities. The extra_cred data will
231          * then be used as the Credential(s).
232          */
233         int skip_cred_build;
234
235         /**
236          * extra_cred: Additional Credential attribute(s)
237          *
238          * This optional data (set to %NULL to disable) can be used to add
239          * Credential attribute(s) for other networks into M8. If
240          * skip_cred_build is set, this will also override the automatically
241          * generated Credential attribute.
242          */
243         const u8 *extra_cred;
244
245         /**
246          * extra_cred_len: Length of extra_cred in octets
247          */
248         size_t extra_cred_len;
249
250         /**
251          * disable_auto_conf - Disable auto-configuration on first registration
252          *
253          * By default, the AP that is started in not configured state will
254          * generate a random PSK and move to configured state when the first
255          * registration protocol run is completed successfully. This option can
256          * be used to disable this functionality and leave it up to an external
257          * program to take care of configuration. This requires the extra_cred
258          * to be set with a suitable Credential and skip_cred_build being used.
259          */
260         int disable_auto_conf;
261 };
262
263
264 /**
265  * enum wps_event - WPS event types
266  */
267 enum wps_event {
268         /**
269          * WPS_EV_M2D - M2D received (Registrar did not know us)
270          */
271         WPS_EV_M2D,
272
273         /**
274          * WPS_EV_FAIL - Registration failed
275          */
276         WPS_EV_FAIL,
277
278         /**
279          * WPS_EV_SUCCESS - Registration succeeded
280          */
281         WPS_EV_SUCCESS
282 };
283
284 /**
285  * union wps_event_data - WPS event data
286  */
287 union wps_event_data {
288         /**
289          * struct wps_event_m2d - M2D event data
290          */
291         struct wps_event_m2d {
292                 u16 config_methods;
293                 const u8 *manufacturer;
294                 size_t manufacturer_len;
295                 const u8 *model_name;
296                 size_t model_name_len;
297                 const u8 *model_number;
298                 size_t model_number_len;
299                 const u8 *serial_number;
300                 size_t serial_number_len;
301                 const u8 *dev_name;
302                 size_t dev_name_len;
303                 const u8 *primary_dev_type; /* 8 octets */
304                 u16 config_error;
305                 u16 dev_password_id;
306         } m2d;
307
308         /**
309          * struct wps_event_fail - Registration failure information
310          * @msg: enum wps_msg_type
311          */
312         struct wps_event_fail {
313                 int msg;
314         } fail;
315 };
316
317 /**
318  * struct wps_context - Long term WPS context data
319  *
320  * This data is stored at the higher layer Authenticator or Supplicant data
321  * structures and it is maintained over multiple registration protocol runs.
322  */
323 struct wps_context {
324         /**
325          * ap - Whether the local end is an access point
326          */
327         int ap;
328
329         /**
330          * registrar - Pointer to WPS registrar data from wps_registrar_init()
331          */
332         struct wps_registrar *registrar;
333
334         /**
335          * wps_state - Current WPS state
336          */
337         enum wps_state wps_state;
338
339         /**
340          * ap_setup_locked - Whether AP setup is locked (only used at AP)
341          */
342         int ap_setup_locked;
343
344         /**
345          * uuid - Own UUID
346          */
347         u8 uuid[16];
348
349         /**
350          * ssid - SSID
351          *
352          * This SSID is used by the Registrar to fill in information for
353          * Credentials. In addition, AP uses it when acting as an Enrollee to
354          * notify Registrar of the current configuration.
355          */
356         u8 ssid[32];
357
358         /**
359          * ssid_len - Length of ssid in octets
360          */
361         size_t ssid_len;
362
363         /**
364          * dev - Own WPS device data
365          */
366         struct wps_device_data dev;
367
368         /**
369          * config_methods - Enabled configuration methods
370          *
371          * Bit field of WPS_CONFIG_*
372          */
373         u16 config_methods;
374
375         /**
376          * encr_types - Enabled encryption types (bit field of WPS_ENCR_*)
377          */
378         u16 encr_types;
379
380         /**
381          * auth_types - Authentication types (bit field of WPS_AUTH_*)
382          */
383         u16 auth_types;
384
385         /**
386          * network_key - The current Network Key (PSK) or %NULL to generate new
387          *
388          * If %NULL, Registrar will generate per-device PSK. In addition, AP
389          * uses this when acting as an Enrollee to notify Registrar of the
390          * current configuration.
391          */
392         u8 *network_key;
393
394         /**
395          * network_key_len - Length of network_key in octets
396          */
397         size_t network_key_len;
398
399         /**
400          * cred_cb - Callback to notify that new Credentials were received
401          * @ctx: Higher layer context data (cb_ctx)
402          * @cred: The received Credential
403          * Return: 0 on success, -1 on failure
404          */
405         int (*cred_cb)(void *ctx, const struct wps_credential *cred);
406
407         /**
408          * event_cb - Event callback (state information about progress)
409          * @ctx: Higher layer context data (cb_ctx)
410          * @event: Event type
411          * @data: Event data
412          */
413         void (*event_cb)(void *ctx, enum wps_event event,
414                          union wps_event_data *data);
415
416         /**
417          * cb_ctx: Higher layer context data for callbacks
418          */
419         void *cb_ctx;
420 };
421
422
423 struct wps_registrar *
424 wps_registrar_init(struct wps_context *wps,
425                    const struct wps_registrar_config *cfg);
426 void wps_registrar_deinit(struct wps_registrar *reg);
427 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
428                           const u8 *pin, size_t pin_len);
429 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid);
430 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid);
431 int wps_registrar_button_pushed(struct wps_registrar *reg);
432 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
433                                 const struct wpabuf *wps_data);
434
435 unsigned int wps_pin_checksum(unsigned int pin);
436 unsigned int wps_pin_valid(unsigned int pin);
437 unsigned int wps_generate_pin(void);
438
439 #endif /* WPS_H */