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