2 * EAP-WSC server for Wi-Fi Protected Setup
3 * Copyright (c) 2007, 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 u8 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 data->wps = wps_init(&cfg);
113 if (data->wps == NULL) {
117 data->fragment_size = WSC_FRAGMENT_SIZE;
123 static void eap_wsc_reset(struct eap_sm *sm, void *priv)
125 struct eap_wsc_data *data = priv;
126 wpabuf_free(data->in_buf);
127 wpabuf_free(data->out_buf);
128 wps_deinit(data->wps);
133 static struct wpabuf * eap_wsc_build_start(struct eap_sm *sm,
134 struct eap_wsc_data *data, u8 id)
138 req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, 2,
139 EAP_CODE_REQUEST, id);
141 wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
146 wpa_printf(MSG_DEBUG, "EAP-WSC: Send WSC/Start");
147 wpabuf_put_u8(req, WSC_Start); /* Op-Code */
148 wpabuf_put_u8(req, 0); /* Flags */
154 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data, u8 id)
158 size_t send_len, plen;
161 send_len = wpabuf_len(data->out_buf) - data->out_used;
162 if (2 + send_len > data->fragment_size) {
163 send_len = data->fragment_size - 2;
164 flags |= WSC_FLAGS_MF;
165 if (data->out_used == 0) {
166 flags |= WSC_FLAGS_LF;
171 if (flags & WSC_FLAGS_LF)
173 req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
174 EAP_CODE_REQUEST, id);
176 wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
181 wpabuf_put_u8(req, data->out_op_code); /* Op-Code */
182 wpabuf_put_u8(req, flags); /* Flags */
183 if (flags & WSC_FLAGS_LF)
184 wpabuf_put_be16(req, wpabuf_len(data->out_buf));
186 wpabuf_put_data(req, wpabuf_head_u8(data->out_buf) + data->out_used,
188 data->out_used += send_len;
190 if (data->out_used == wpabuf_len(data->out_buf)) {
191 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
192 "(message sent completely)",
193 (unsigned long) send_len);
194 wpabuf_free(data->out_buf);
195 data->out_buf = NULL;
197 eap_wsc_state(data, MSG);
199 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
200 "(%lu more to send)", (unsigned long) send_len,
201 (unsigned long) wpabuf_len(data->out_buf) -
203 eap_wsc_state(data, WAIT_FRAG_ACK);
210 static struct wpabuf * eap_wsc_buildReq(struct eap_sm *sm, void *priv, u8 id)
212 struct eap_wsc_data *data = priv;
214 switch (data->state) {
216 return eap_wsc_build_start(sm, data, id);
218 if (data->out_buf == NULL) {
219 data->out_buf = wps_get_msg(data->wps,
221 if (data->out_buf == NULL) {
222 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to "
223 "receive message from WPS");
230 return eap_wsc_build_msg(data, id);
232 return eap_wsc_build_frag_ack(id, EAP_CODE_REQUEST);
234 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected state %d in "
235 "buildReq", data->state);
241 static Boolean eap_wsc_check(struct eap_sm *sm, void *priv,
242 struct wpabuf *respData)
247 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
249 if (pos == NULL || len < 2) {
250 wpa_printf(MSG_INFO, "EAP-WSC: Invalid frame");
258 static int eap_wsc_process_cont(struct eap_wsc_data *data,
259 const u8 *buf, size_t len, u8 op_code)
261 /* Process continuation of a pending message */
262 if (op_code != data->in_op_code) {
263 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
264 "fragment (expected %d)",
265 op_code, data->in_op_code);
266 eap_wsc_state(data, FAIL);
270 if (len > wpabuf_tailroom(data->in_buf)) {
271 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
272 eap_wsc_state(data, FAIL);
276 wpabuf_put_data(data->in_buf, buf, len);
277 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting for %lu "
278 "bytes more", (unsigned long) len,
279 (unsigned long) wpabuf_tailroom(data->in_buf));
285 static int eap_wsc_process_fragment(struct eap_wsc_data *data,
286 u8 flags, u8 op_code, u16 message_length,
287 const u8 *buf, size_t len)
289 /* Process a fragment that is not the last one of the message */
290 if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
291 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length "
292 "field in a fragmented packet");
296 if (data->in_buf == NULL) {
297 /* First fragment of the message */
298 data->in_buf = wpabuf_alloc(message_length);
299 if (data->in_buf == NULL) {
300 wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
304 data->in_op_code = op_code;
305 wpabuf_put_data(data->in_buf, buf, len);
306 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in "
307 "first fragment, waiting for %lu bytes more",
309 (unsigned long) wpabuf_tailroom(data->in_buf));
316 static void eap_wsc_process(struct eap_sm *sm, void *priv,
317 struct wpabuf *respData)
319 struct eap_wsc_data *data = priv;
320 const u8 *start, *pos, *end;
323 u16 message_length = 0;
324 enum wps_process_res res;
325 struct wpabuf tmpbuf;
327 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
329 if (pos == NULL || len < 2)
330 return; /* Should not happen; message already verified */
337 if (flags & WSC_FLAGS_LF) {
339 wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
342 message_length = WPA_GET_BE16(pos);
345 if (message_length < end - pos) {
346 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
352 wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
353 "Flags 0x%x Message Length %d",
354 op_code, flags, message_length);
356 if (data->state == WAIT_FRAG_ACK) {
357 if (op_code != WSC_FRAG_ACK) {
358 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
359 "in WAIT_FRAG_ACK state", op_code);
360 eap_wsc_state(data, FAIL);
363 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
364 eap_wsc_state(data, MSG);
368 if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
369 op_code != WSC_Done) {
370 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
372 eap_wsc_state(data, FAIL);
377 eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
378 eap_wsc_state(data, FAIL);
382 if (flags & WSC_FLAGS_MF) {
383 if (eap_wsc_process_fragment(data, flags, op_code,
384 message_length, pos, end - pos) <
386 eap_wsc_state(data, FAIL);
388 eap_wsc_state(data, FRAG_ACK);
392 if (data->in_buf == NULL) {
393 /* Wrap unfragmented messages as wpabuf without extra copy */
394 wpabuf_set(&tmpbuf, pos, end - pos);
395 data->in_buf = &tmpbuf;
398 res = wps_process_msg(data->wps, op_code, data->in_buf);
401 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
402 "successfully - report EAP failure");
403 eap_wsc_state(data, FAIL);
406 eap_wsc_state(data, MSG);
409 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
410 eap_wsc_state(data, FAIL);
413 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing pending");
414 sm->method_pending = METHOD_PENDING_WAIT;
418 if (data->in_buf != &tmpbuf)
419 wpabuf_free(data->in_buf);
424 static Boolean eap_wsc_isDone(struct eap_sm *sm, void *priv)
426 struct eap_wsc_data *data = priv;
427 return data->state == FAIL;
431 static Boolean eap_wsc_isSuccess(struct eap_sm *sm, void *priv)
433 /* EAP-WSC will always result in EAP-Failure */
438 int eap_server_wsc_register(void)
440 struct eap_method *eap;
443 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
444 EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
449 eap->init = eap_wsc_init;
450 eap->reset = eap_wsc_reset;
451 eap->buildReq = eap_wsc_buildReq;
452 eap->check = eap_wsc_check;
453 eap->process = eap_wsc_process;
454 eap->isDone = eap_wsc_isDone;
455 eap->isSuccess = eap_wsc_isSuccess;
457 ret = eap_server_method_register(eap);
459 eap_server_method_free(eap);