e1026b6fa44f21a822f8cc7e516a5e6d9a4e919e
[mech_eap.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 enum wsc_op_code {
19         WSC_Start = 0x01,
20         WSC_ACK = 0x02,
21         WSC_NACK = 0x03,
22         WSC_MSG = 0x04,
23         WSC_Done = 0x05,
24         WSC_FRAG_ACK = 0x06
25 };
26
27 struct wps_registrar;
28
29 struct wps_credential {
30         u8 ssid[32];
31         size_t ssid_len;
32         u16 auth_type;
33         u16 encr_type;
34         u8 key_idx;
35         u8 key[64];
36         size_t key_len;
37         u8 mac_addr[ETH_ALEN];
38 };
39
40 struct wps_device_data {
41         u8 mac_addr[ETH_ALEN];
42         char *device_name;
43         char *manufacturer;
44         char *model_name;
45         char *model_number;
46         char *serial_number;
47         u16 categ;
48         u32 oui;
49         u16 sub_categ;
50         u32 os_version;
51         u8 rf_bands; /* WPS_RF_* */
52 };
53
54 struct wps_config {
55         int authenticator;
56         struct wps_context *wps;
57         struct wps_registrar *registrar; /* NULL for Enrollee */
58         const u8 *enrollee_mac_addr; /* NULL for Registrar */
59         const u8 *pin; /* Enrollee Device Password (NULL for Registrar or PBC)
60                         */
61         size_t pin_len;
62         const u8 *uuid; /* 128-bit Enrollee UUID (NULL for Registrar) */
63         int pbc;
64         const struct wpabuf *assoc_wps_ie; /* (Re)AssocReq WPS IE (in AP) */
65 };
66
67 struct wps_data * wps_init(const struct wps_config *cfg);
68
69 void wps_deinit(struct wps_data *data);
70
71 enum wps_process_res {
72         WPS_DONE, WPS_CONTINUE, WPS_FAILURE, WPS_PENDING
73 };
74 enum wps_process_res wps_process_msg(struct wps_data *wps, u8 op_code,
75                                      const struct wpabuf *msg);
76
77 struct wpabuf * wps_get_msg(struct wps_data *wps, u8 *op_code);
78
79 int wps_is_selected_pbc_registrar(const struct wpabuf *msg);
80 int wps_is_selected_pin_registrar(const struct wpabuf *msg);
81 const u8 * wps_get_uuid_e(const struct wpabuf *msg);
82 struct wpabuf * wps_build_assoc_req_ie(u8 req_type);
83 struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
84                                        const u8 *uuid, u8 req_type);
85
86
87 struct wps_registrar_config {
88         int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
89                           size_t psk_len);
90         int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
91                          const u8 *probe_resp_ie, size_t probe_resp_ie_len);
92         void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
93                               const struct wps_device_data *dev);
94         void *cb_ctx;
95 };
96
97
98 enum wps_event {
99         WPS_EV_M2D
100 };
101
102 union wps_event_data {
103         struct wps_event_m2d {
104                 u16 config_methods;
105                 const u8 *manufacturer;
106                 size_t manufacturer_len;
107                 const u8 *model_name;
108                 size_t model_name_len;
109                 const u8 *model_number;
110                 size_t model_number_len;
111                 const u8 *serial_number;
112                 size_t serial_number_len;
113                 const u8 *dev_name;
114                 size_t dev_name_len;
115                 const u8 *primary_dev_type; /* 8 octets */
116                 u16 config_error;
117                 u16 dev_password_id;
118         } m2d;
119 };
120
121 /**
122  * struct wps_context - Long term WPS context data
123  *
124  * This data is stored at the higher layer Authenticator or Supplicant data
125  * structures and it is maintained over multiple registration protocol runs.
126  */
127 struct wps_context {
128         int ap;
129         struct wps_registrar *registrar;
130         int wps_state;
131         int ap_setup_locked;
132         u8 uuid[16];
133         u8 ssid[32];
134         size_t ssid_len;
135         struct wps_device_data dev;
136         u16 config_methods; /* bit field of WPS_CONFIG_* */
137         u16 encr_types; /* bit field of WPS_ENCR_* */
138         u16 auth_types; /* bit field of WPS_AUTH_* */
139         u8 *network_key; /* or NULL to generate per-device PSK */
140         size_t network_key_len;
141
142         int (*cred_cb)(void *ctx, const struct wps_credential *cred);
143         void (*event_cb)(void *ctx, enum wps_event event,
144                          union wps_event_data *data);
145         void *cb_ctx;
146 };
147
148
149 struct wps_registrar *
150 wps_registrar_init(struct wps_context *wps,
151                    const struct wps_registrar_config *cfg);
152 void wps_registrar_deinit(struct wps_registrar *reg);
153 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
154                           const u8 *pin, size_t pin_len);
155 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid);
156 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid);
157 int wps_registrar_button_pushed(struct wps_registrar *reg);
158 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
159                                 const struct wpabuf *wps_data);
160
161 unsigned int wps_pin_checksum(unsigned int pin);
162 unsigned int wps_pin_valid(unsigned int pin);
163 unsigned int wps_generate_pin(void);
164
165 #endif /* WPS_H */