2 * EAP-WSC peer for Wi-Fi Protected Setup
3 * Copyright (c) 2007-2008, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #include "eap_common/eap_wsc_common.h"
22 #include "wps/wps_defs.h"
26 enum { WAIT_START, MESG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
28 struct wpabuf *in_buf;
29 struct wpabuf *out_buf;
30 enum wsc_op_code in_op_code, out_op_code;
34 struct wps_context *wps_ctx;
38 static const char * eap_wsc_state_txt(int state)
48 return "WAIT_FRAG_ACK";
59 static void eap_wsc_state(struct eap_wsc_data *data, int state)
61 wpa_printf(MSG_DEBUG, "EAP-WSC: %s -> %s",
62 eap_wsc_state_txt(data->state),
63 eap_wsc_state_txt(state));
68 static void * eap_wsc_init(struct eap_sm *sm)
70 struct eap_wsc_data *data;
74 struct wps_config cfg;
77 struct wps_context *wps;
81 wpa_printf(MSG_ERROR, "EAP-WSC: WPS context not available");
85 identity = eap_get_config_identity(sm, &identity_len);
87 if (identity && identity_len == WSC_ID_REGISTRAR_LEN &&
88 os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0)
89 registrar = 1; /* Supplicant is Registrar */
90 else if (identity && identity_len == WSC_ID_ENROLLEE_LEN &&
91 os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0)
92 registrar = 0; /* Supplicant is Enrollee */
94 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
95 identity, identity_len);
99 data = os_zalloc(sizeof(*data));
102 data->state = registrar ? MESG : WAIT_START;
103 data->registrar = registrar;
106 os_memset(&cfg, 0, sizeof(cfg));
108 cfg.registrar = registrar;
110 phase1 = eap_get_config_phase1(sm);
111 if (phase1 == NULL) {
112 wpa_printf(MSG_INFO, "EAP-WSC: phase1 configuration data not "
118 pos = os_strstr(phase1, "pin=");
121 cfg.pin = (const u8 *) pos;
122 while (*pos != '\0' && *pos != ' ')
124 cfg.pin_len = pos - (const char *) cfg.pin;
126 pos = os_strstr(phase1, "pbc=1");
131 if (cfg.pin == NULL && !cfg.pbc) {
132 wpa_printf(MSG_INFO, "EAP-WSC: PIN or PBC not set in phase1 "
133 "configuration data");
138 data->wps = wps_init(&cfg);
139 if (data->wps == NULL) {
143 data->fragment_size = WSC_FRAGMENT_SIZE;
145 if (registrar && cfg.pin) {
146 wps_registrar_add_pin(data->wps_ctx->registrar, NULL,
147 cfg.pin, cfg.pin_len, 0);
154 static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
156 struct eap_wsc_data *data = priv;
157 wpabuf_free(data->in_buf);
158 wpabuf_free(data->out_buf);
159 wps_deinit(data->wps);
160 os_free(data->wps_ctx->network_key);
161 data->wps_ctx->network_key = NULL;
166 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
167 struct eap_method_ret *ret, u8 id)
171 size_t send_len, plen;
174 wpa_printf(MSG_DEBUG, "EAP-WSC: Generating Response");
175 ret->allowNotifications = TRUE;
178 send_len = wpabuf_len(data->out_buf) - data->out_used;
179 if (2 + send_len > data->fragment_size) {
180 send_len = data->fragment_size - 2;
181 flags |= WSC_FLAGS_MF;
182 if (data->out_used == 0) {
183 flags |= WSC_FLAGS_LF;
188 if (flags & WSC_FLAGS_LF)
190 resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
191 EAP_CODE_RESPONSE, id);
195 wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
196 wpabuf_put_u8(resp, flags); /* Flags */
197 if (flags & WSC_FLAGS_LF)
198 wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
200 wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
202 data->out_used += send_len;
204 ret->methodState = METHOD_MAY_CONT;
205 ret->decision = DECISION_FAIL;
207 if (data->out_used == wpabuf_len(data->out_buf)) {
208 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
209 "(message sent completely)",
210 (unsigned long) send_len);
211 wpabuf_free(data->out_buf);
212 data->out_buf = NULL;
214 if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
215 data->out_op_code == WSC_NACK ||
216 data->out_op_code == WSC_Done) {
217 eap_wsc_state(data, FAIL);
218 ret->methodState = METHOD_DONE;
220 eap_wsc_state(data, MESG);
222 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
223 "(%lu more to send)", (unsigned long) send_len,
224 (unsigned long) wpabuf_len(data->out_buf) -
226 eap_wsc_state(data, WAIT_FRAG_ACK);
233 static int eap_wsc_process_cont(struct eap_wsc_data *data,
234 const u8 *buf, size_t len, u8 op_code)
236 /* Process continuation of a pending message */
237 if (op_code != data->in_op_code) {
238 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
239 "fragment (expected %d)",
240 op_code, data->in_op_code);
244 if (len > wpabuf_tailroom(data->in_buf)) {
245 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
246 eap_wsc_state(data, FAIL);
250 wpabuf_put_data(data->in_buf, buf, len);
251 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
252 "for %lu bytes more", (unsigned long) len,
253 (unsigned long) wpabuf_tailroom(data->in_buf));
259 static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
260 struct eap_method_ret *ret,
261 u8 id, u8 flags, u8 op_code,
263 const u8 *buf, size_t len)
265 /* Process a fragment that is not the last one of the message */
266 if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
267 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
268 "fragmented packet");
273 if (data->in_buf == NULL) {
274 /* First fragment of the message */
275 data->in_buf = wpabuf_alloc(message_length);
276 if (data->in_buf == NULL) {
277 wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
282 data->in_op_code = op_code;
283 wpabuf_put_data(data->in_buf, buf, len);
284 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
285 "fragment, waiting for %lu bytes more",
287 (unsigned long) wpabuf_tailroom(data->in_buf));
290 return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
294 static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
295 struct eap_method_ret *ret,
296 const struct wpabuf *reqData)
298 struct eap_wsc_data *data = priv;
299 const u8 *start, *pos, *end;
301 u8 op_code, flags, id;
302 u16 message_length = 0;
303 enum wps_process_res res;
304 struct wpabuf tmpbuf;
306 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
308 if (pos == NULL || len < 2) {
313 id = eap_get_id(reqData);
320 if (flags & WSC_FLAGS_LF) {
322 wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
326 message_length = WPA_GET_BE16(pos);
329 if (message_length < end - pos) {
330 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
337 wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
338 "Flags 0x%x Message Length %d",
339 op_code, flags, message_length);
341 if (data->state == WAIT_FRAG_ACK) {
342 if (op_code != WSC_FRAG_ACK) {
343 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
344 "in WAIT_FRAG_ACK state", op_code);
348 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
349 eap_wsc_state(data, MESG);
350 return eap_wsc_build_msg(data, ret, id);
353 if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
354 op_code != WSC_Done && op_code != WSC_Start) {
355 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
361 if (data->state == WAIT_START) {
362 if (op_code != WSC_Start) {
363 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
364 "in WAIT_START state", op_code);
368 wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
369 eap_wsc_state(data, MESG);
370 /* Start message has empty payload, skip processing */
372 } else if (op_code == WSC_Start) {
373 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
380 eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
385 if (flags & WSC_FLAGS_MF) {
386 return eap_wsc_process_fragment(data, ret, id, flags, op_code,
391 if (data->in_buf == NULL) {
392 /* Wrap unfragmented messages as wpabuf without extra copy */
393 wpabuf_set(&tmpbuf, pos, end - pos);
394 data->in_buf = &tmpbuf;
397 res = wps_process_msg(data->wps, op_code, data->in_buf);
400 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
401 "successfully - wait for EAP failure");
402 eap_wsc_state(data, FAIL);
405 eap_wsc_state(data, MESG);
409 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
410 eap_wsc_state(data, FAIL);
414 if (data->in_buf != &tmpbuf)
415 wpabuf_free(data->in_buf);
419 if (data->out_buf == NULL) {
420 data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
421 if (data->out_buf == NULL) {
422 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
429 eap_wsc_state(data, MESG);
430 return eap_wsc_build_msg(data, ret, id);
434 int eap_peer_wsc_register(void)
436 struct eap_method *eap;
439 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
440 EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
445 eap->init = eap_wsc_init;
446 eap->deinit = eap_wsc_deinit;
447 eap->process = eap_wsc_process;
449 ret = eap_peer_method_register(eap);
451 eap_peer_method_free(eap);