2 * EAP-WSC server 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.
19 #include "eap_common/eap_wsc_common.h"
24 enum { START, MSG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
26 struct wpabuf *in_buf;
27 struct wpabuf *out_buf;
28 enum wsc_op_code in_op_code, out_op_code;
35 static const char * eap_wsc_state_txt(int state)
45 return "WAIT_FRAG_ACK";
56 static void eap_wsc_state(struct eap_wsc_data *data, int state)
58 wpa_printf(MSG_DEBUG, "EAP-WSC: %s -> %s",
59 eap_wsc_state_txt(data->state),
60 eap_wsc_state_txt(state));
65 static void * eap_wsc_init(struct eap_sm *sm)
67 struct eap_wsc_data *data;
69 struct wps_config cfg;
71 if (sm->identity && sm->identity_len == WSC_ID_REGISTRAR_LEN &&
72 os_memcmp(sm->identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) ==
74 registrar = 0; /* Supplicant is Registrar */
75 else if (sm->identity && sm->identity_len == WSC_ID_ENROLLEE_LEN &&
76 os_memcmp(sm->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN)
78 registrar = 1; /* Supplicant is Enrollee */
80 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
81 sm->identity, sm->identity_len);
85 data = os_zalloc(sizeof(*data));
88 data->state = registrar ? START : MSG;
89 data->registrar = registrar;
91 os_memset(&cfg, 0, sizeof(cfg));
92 cfg.authenticator = 1;
95 if (sm->wps == NULL || sm->wps->registrar == NULL) {
96 wpa_printf(MSG_INFO, "EAP-WSC: WPS Registrar not "
101 cfg.registrar = sm->wps->registrar;
103 if (sm->user == NULL || sm->user->password == NULL) {
104 wpa_printf(MSG_INFO, "EAP-WSC: No AP PIN (password) "
105 "configured for Enrollee functionality");
109 cfg.pin = sm->user->password;
110 cfg.pin_len = sm->user->password_len;
112 cfg.assoc_wps_ie = sm->assoc_wps_ie;
113 data->wps = wps_init(&cfg);
114 if (data->wps == NULL) {
118 data->fragment_size = WSC_FRAGMENT_SIZE;
124 static void eap_wsc_reset(struct eap_sm *sm, void *priv)
126 struct eap_wsc_data *data = priv;
127 wpabuf_free(data->in_buf);
128 wpabuf_free(data->out_buf);
129 wps_deinit(data->wps);
134 static struct wpabuf * eap_wsc_build_start(struct eap_sm *sm,
135 struct eap_wsc_data *data, u8 id)
139 req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, 2,
140 EAP_CODE_REQUEST, id);
142 wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
147 wpa_printf(MSG_DEBUG, "EAP-WSC: Send WSC/Start");
148 wpabuf_put_u8(req, WSC_Start); /* Op-Code */
149 wpabuf_put_u8(req, 0); /* Flags */
155 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data, u8 id)
159 size_t send_len, plen;
162 send_len = wpabuf_len(data->out_buf) - data->out_used;
163 if (2 + send_len > data->fragment_size) {
164 send_len = data->fragment_size - 2;
165 flags |= WSC_FLAGS_MF;
166 if (data->out_used == 0) {
167 flags |= WSC_FLAGS_LF;
172 if (flags & WSC_FLAGS_LF)
174 req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
175 EAP_CODE_REQUEST, id);
177 wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
182 wpabuf_put_u8(req, data->out_op_code); /* Op-Code */
183 wpabuf_put_u8(req, flags); /* Flags */
184 if (flags & WSC_FLAGS_LF)
185 wpabuf_put_be16(req, wpabuf_len(data->out_buf));
187 wpabuf_put_data(req, wpabuf_head_u8(data->out_buf) + data->out_used,
189 data->out_used += send_len;
191 if (data->out_used == wpabuf_len(data->out_buf)) {
192 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
193 "(message sent completely)",
194 (unsigned long) send_len);
195 wpabuf_free(data->out_buf);
196 data->out_buf = NULL;
198 eap_wsc_state(data, MSG);
200 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
201 "(%lu more to send)", (unsigned long) send_len,
202 (unsigned long) wpabuf_len(data->out_buf) -
204 eap_wsc_state(data, WAIT_FRAG_ACK);
211 static struct wpabuf * eap_wsc_buildReq(struct eap_sm *sm, void *priv, u8 id)
213 struct eap_wsc_data *data = priv;
215 switch (data->state) {
217 return eap_wsc_build_start(sm, data, id);
219 if (data->out_buf == NULL) {
220 data->out_buf = wps_get_msg(data->wps,
222 if (data->out_buf == NULL) {
223 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to "
224 "receive message from WPS");
231 return eap_wsc_build_msg(data, id);
233 return eap_wsc_build_frag_ack(id, EAP_CODE_REQUEST);
235 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected state %d in "
236 "buildReq", data->state);
242 static Boolean eap_wsc_check(struct eap_sm *sm, void *priv,
243 struct wpabuf *respData)
248 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
250 if (pos == NULL || len < 2) {
251 wpa_printf(MSG_INFO, "EAP-WSC: Invalid frame");
259 static int eap_wsc_process_cont(struct eap_wsc_data *data,
260 const u8 *buf, size_t len, u8 op_code)
262 /* Process continuation of a pending message */
263 if (op_code != data->in_op_code) {
264 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
265 "fragment (expected %d)",
266 op_code, data->in_op_code);
267 eap_wsc_state(data, FAIL);
271 if (len > wpabuf_tailroom(data->in_buf)) {
272 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
273 eap_wsc_state(data, FAIL);
277 wpabuf_put_data(data->in_buf, buf, len);
278 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting for %lu "
279 "bytes more", (unsigned long) len,
280 (unsigned long) wpabuf_tailroom(data->in_buf));
286 static int eap_wsc_process_fragment(struct eap_wsc_data *data,
287 u8 flags, u8 op_code, u16 message_length,
288 const u8 *buf, size_t len)
290 /* Process a fragment that is not the last one of the message */
291 if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
292 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length "
293 "field in a fragmented packet");
297 if (data->in_buf == NULL) {
298 /* First fragment of the message */
299 data->in_buf = wpabuf_alloc(message_length);
300 if (data->in_buf == NULL) {
301 wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
305 data->in_op_code = op_code;
306 wpabuf_put_data(data->in_buf, buf, len);
307 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in "
308 "first fragment, waiting for %lu bytes more",
310 (unsigned long) wpabuf_tailroom(data->in_buf));
317 static void eap_wsc_process(struct eap_sm *sm, void *priv,
318 struct wpabuf *respData)
320 struct eap_wsc_data *data = priv;
321 const u8 *start, *pos, *end;
324 u16 message_length = 0;
325 enum wps_process_res res;
326 struct wpabuf tmpbuf;
328 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
330 if (pos == NULL || len < 2)
331 return; /* Should not happen; message already verified */
338 if (flags & WSC_FLAGS_LF) {
340 wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
343 message_length = WPA_GET_BE16(pos);
346 if (message_length < end - pos) {
347 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
353 wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
354 "Flags 0x%x Message Length %d",
355 op_code, flags, message_length);
357 if (data->state == WAIT_FRAG_ACK) {
358 if (op_code != WSC_FRAG_ACK) {
359 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
360 "in WAIT_FRAG_ACK state", op_code);
361 eap_wsc_state(data, FAIL);
364 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
365 eap_wsc_state(data, MSG);
369 if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
370 op_code != WSC_Done) {
371 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
373 eap_wsc_state(data, FAIL);
378 eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
379 eap_wsc_state(data, FAIL);
383 if (flags & WSC_FLAGS_MF) {
384 if (eap_wsc_process_fragment(data, flags, op_code,
385 message_length, pos, end - pos) <
387 eap_wsc_state(data, FAIL);
389 eap_wsc_state(data, FRAG_ACK);
393 if (data->in_buf == NULL) {
394 /* Wrap unfragmented messages as wpabuf without extra copy */
395 wpabuf_set(&tmpbuf, pos, end - pos);
396 data->in_buf = &tmpbuf;
399 res = wps_process_msg(data->wps, op_code, data->in_buf);
402 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
403 "successfully - report EAP failure");
404 eap_wsc_state(data, FAIL);
407 eap_wsc_state(data, MSG);
410 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
411 eap_wsc_state(data, FAIL);
414 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing pending");
415 sm->method_pending = METHOD_PENDING_WAIT;
419 if (data->in_buf != &tmpbuf)
420 wpabuf_free(data->in_buf);
425 static Boolean eap_wsc_isDone(struct eap_sm *sm, void *priv)
427 struct eap_wsc_data *data = priv;
428 return data->state == FAIL;
432 static Boolean eap_wsc_isSuccess(struct eap_sm *sm, void *priv)
434 /* EAP-WSC will always result in EAP-Failure */
439 static int eap_wsc_getTimeout(struct eap_sm *sm, void *priv)
441 /* Recommended retransmit times: retransmit timeout 5 seconds,
442 * per-message timeout 15 seconds, i.e., 3 tries. */
443 sm->MaxRetrans = 2; /* total 3 attempts */
448 int eap_server_wsc_register(void)
450 struct eap_method *eap;
453 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
454 EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
459 eap->init = eap_wsc_init;
460 eap->reset = eap_wsc_reset;
461 eap->buildReq = eap_wsc_buildReq;
462 eap->check = eap_wsc_check;
463 eap->process = eap_wsc_process;
464 eap->isDone = eap_wsc_isDone;
465 eap->isSuccess = eap_wsc_isSuccess;
466 eap->getTimeout = eap_wsc_getTimeout;
468 ret = eap_server_method_register(eap);
470 eap_server_method_free(eap);