2 * EAP-WSC server for Wi-Fi Protected Setup
3 * Copyright (c) 2007-2008, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 #include "eap_common/eap_wsc_common.h"
20 enum { START, MESG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
22 struct wpabuf *in_buf;
23 struct wpabuf *out_buf;
24 enum wsc_op_code in_op_code, out_op_code;
32 #ifndef CONFIG_NO_STDOUT_DEBUG
33 static const char * eap_wsc_state_txt(int state)
43 return "WAIT_FRAG_ACK";
52 #endif /* CONFIG_NO_STDOUT_DEBUG */
55 static void eap_wsc_state(struct eap_wsc_data *data, int state)
57 wpa_printf(MSG_DEBUG, "EAP-WSC: %s -> %s",
58 eap_wsc_state_txt(data->state),
59 eap_wsc_state_txt(state));
64 static void eap_wsc_ext_reg_timeout(void *eloop_ctx, void *timeout_ctx)
66 struct eap_sm *sm = eloop_ctx;
67 struct eap_wsc_data *data = timeout_ctx;
69 if (sm->method_pending != METHOD_PENDING_WAIT)
72 wpa_printf(MSG_DEBUG, "EAP-WSC: Timeout while waiting for an External "
74 data->ext_reg_timeout = 1;
75 eap_sm_pending_cb(sm);
79 static void * eap_wsc_init(struct eap_sm *sm)
81 struct eap_wsc_data *data;
83 struct wps_config cfg;
85 if (sm->identity && sm->identity_len == WSC_ID_REGISTRAR_LEN &&
86 os_memcmp(sm->identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) ==
88 registrar = 0; /* Supplicant is Registrar */
89 else if (sm->identity && sm->identity_len == WSC_ID_ENROLLEE_LEN &&
90 os_memcmp(sm->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN)
92 registrar = 1; /* Supplicant is Enrollee */
94 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
95 sm->identity, sm->identity_len);
99 data = os_zalloc(sizeof(*data));
102 data->state = registrar ? START : MESG;
103 data->registrar = registrar;
105 os_memset(&cfg, 0, sizeof(cfg));
107 cfg.registrar = registrar;
109 if (sm->wps == NULL || sm->wps->registrar == NULL) {
110 wpa_printf(MSG_INFO, "EAP-WSC: WPS Registrar not "
116 if (sm->user == NULL || sm->user->password == NULL) {
118 * In theory, this should not really be needed, but
119 * Windows 7 uses Registrar mode to probe AP's WPS
120 * capabilities before trying to use Enrollee and fails
121 * if the AP does not allow that probing to happen..
123 wpa_printf(MSG_DEBUG, "EAP-WSC: No AP PIN (password) "
124 "configured for Enrollee functionality - "
125 "allow for probing capabilities (M1)");
127 cfg.pin = sm->user->password;
128 cfg.pin_len = sm->user->password_len;
131 cfg.assoc_wps_ie = sm->assoc_wps_ie;
132 cfg.peer_addr = sm->peer_addr;
134 if (sm->assoc_p2p_ie) {
135 wpa_printf(MSG_DEBUG, "EAP-WSC: Prefer PSK format for P2P "
138 cfg.p2p_dev_addr = p2p_get_go_dev_addr(sm->assoc_p2p_ie);
140 #endif /* CONFIG_P2P */
141 cfg.pbc_in_m1 = sm->pbc_in_m1;
142 data->wps = wps_init(&cfg);
143 if (data->wps == NULL) {
147 data->fragment_size = sm->fragment_size > 0 ? sm->fragment_size :
154 static void eap_wsc_reset(struct eap_sm *sm, void *priv)
156 struct eap_wsc_data *data = priv;
157 eloop_cancel_timeout(eap_wsc_ext_reg_timeout, sm, data);
158 wpabuf_free(data->in_buf);
159 wpabuf_free(data->out_buf);
160 wps_deinit(data->wps);
165 static struct wpabuf * eap_wsc_build_start(struct eap_sm *sm,
166 struct eap_wsc_data *data, u8 id)
170 req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, 2,
171 EAP_CODE_REQUEST, id);
173 wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
178 wpa_printf(MSG_DEBUG, "EAP-WSC: Send WSC/Start");
179 wpabuf_put_u8(req, WSC_Start); /* Op-Code */
180 wpabuf_put_u8(req, 0); /* Flags */
186 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data, u8 id)
190 size_t send_len, plen;
193 send_len = wpabuf_len(data->out_buf) - data->out_used;
194 if (2 + send_len > data->fragment_size) {
195 send_len = data->fragment_size - 2;
196 flags |= WSC_FLAGS_MF;
197 if (data->out_used == 0) {
198 flags |= WSC_FLAGS_LF;
203 if (flags & WSC_FLAGS_LF)
205 req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
206 EAP_CODE_REQUEST, id);
208 wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
213 wpabuf_put_u8(req, data->out_op_code); /* Op-Code */
214 wpabuf_put_u8(req, flags); /* Flags */
215 if (flags & WSC_FLAGS_LF)
216 wpabuf_put_be16(req, wpabuf_len(data->out_buf));
218 wpabuf_put_data(req, wpabuf_head_u8(data->out_buf) + data->out_used,
220 data->out_used += send_len;
222 if (data->out_used == wpabuf_len(data->out_buf)) {
223 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
224 "(message sent completely)",
225 (unsigned long) send_len);
226 wpabuf_free(data->out_buf);
227 data->out_buf = NULL;
229 eap_wsc_state(data, MESG);
231 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
232 "(%lu more to send)", (unsigned long) send_len,
233 (unsigned long) wpabuf_len(data->out_buf) -
235 eap_wsc_state(data, WAIT_FRAG_ACK);
242 static struct wpabuf * eap_wsc_buildReq(struct eap_sm *sm, void *priv, u8 id)
244 struct eap_wsc_data *data = priv;
246 switch (data->state) {
248 return eap_wsc_build_start(sm, data, id);
250 if (data->out_buf == NULL) {
251 data->out_buf = wps_get_msg(data->wps,
253 if (data->out_buf == NULL) {
254 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to "
255 "receive message from WPS");
262 return eap_wsc_build_msg(data, id);
264 return eap_wsc_build_frag_ack(id, EAP_CODE_REQUEST);
266 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected state %d in "
267 "buildReq", data->state);
273 static Boolean eap_wsc_check(struct eap_sm *sm, void *priv,
274 struct wpabuf *respData)
279 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
281 if (pos == NULL || len < 2) {
282 wpa_printf(MSG_INFO, "EAP-WSC: Invalid frame");
290 static int eap_wsc_process_cont(struct eap_wsc_data *data,
291 const u8 *buf, size_t len, u8 op_code)
293 /* Process continuation of a pending message */
294 if (op_code != data->in_op_code) {
295 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
296 "fragment (expected %d)",
297 op_code, data->in_op_code);
298 eap_wsc_state(data, FAIL);
302 if (len > wpabuf_tailroom(data->in_buf)) {
303 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
304 eap_wsc_state(data, FAIL);
308 wpabuf_put_data(data->in_buf, buf, len);
309 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting for %lu "
310 "bytes more", (unsigned long) len,
311 (unsigned long) wpabuf_tailroom(data->in_buf));
317 static int eap_wsc_process_fragment(struct eap_wsc_data *data,
318 u8 flags, u8 op_code, u16 message_length,
319 const u8 *buf, size_t len)
321 /* Process a fragment that is not the last one of the message */
322 if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
323 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length "
324 "field in a fragmented packet");
328 if (data->in_buf == NULL) {
329 /* First fragment of the message */
330 data->in_buf = wpabuf_alloc(message_length);
331 if (data->in_buf == NULL) {
332 wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
336 data->in_op_code = op_code;
337 wpabuf_put_data(data->in_buf, buf, len);
338 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in "
339 "first fragment, waiting for %lu bytes more",
341 (unsigned long) wpabuf_tailroom(data->in_buf));
348 static void eap_wsc_process(struct eap_sm *sm, void *priv,
349 struct wpabuf *respData)
351 struct eap_wsc_data *data = priv;
352 const u8 *start, *pos, *end;
355 u16 message_length = 0;
356 enum wps_process_res res;
357 struct wpabuf tmpbuf;
359 eloop_cancel_timeout(eap_wsc_ext_reg_timeout, sm, data);
360 if (data->ext_reg_timeout) {
361 eap_wsc_state(data, FAIL);
365 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
367 if (pos == NULL || len < 2)
368 return; /* Should not happen; message already verified */
375 if (flags & WSC_FLAGS_LF) {
377 wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
380 message_length = WPA_GET_BE16(pos);
383 if (message_length < end - pos) {
384 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
390 wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
391 "Flags 0x%x Message Length %d",
392 op_code, flags, message_length);
394 if (data->state == WAIT_FRAG_ACK) {
395 if (op_code != WSC_FRAG_ACK) {
396 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
397 "in WAIT_FRAG_ACK state", op_code);
398 eap_wsc_state(data, FAIL);
401 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
402 eap_wsc_state(data, MESG);
406 if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
407 op_code != WSC_Done) {
408 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
410 eap_wsc_state(data, FAIL);
415 eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
416 eap_wsc_state(data, FAIL);
420 if (flags & WSC_FLAGS_MF) {
421 if (eap_wsc_process_fragment(data, flags, op_code,
422 message_length, pos, end - pos) <
424 eap_wsc_state(data, FAIL);
426 eap_wsc_state(data, FRAG_ACK);
430 if (data->in_buf == NULL) {
431 /* Wrap unfragmented messages as wpabuf without extra copy */
432 wpabuf_set(&tmpbuf, pos, end - pos);
433 data->in_buf = &tmpbuf;
436 res = wps_process_msg(data->wps, op_code, data->in_buf);
439 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
440 "successfully - report EAP failure");
441 eap_wsc_state(data, FAIL);
444 eap_wsc_state(data, MESG);
447 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
448 eap_wsc_state(data, FAIL);
451 eap_wsc_state(data, MESG);
452 sm->method_pending = METHOD_PENDING_WAIT;
453 eloop_cancel_timeout(eap_wsc_ext_reg_timeout, sm, data);
454 eloop_register_timeout(5, 0, eap_wsc_ext_reg_timeout,
459 if (data->in_buf != &tmpbuf)
460 wpabuf_free(data->in_buf);
465 static Boolean eap_wsc_isDone(struct eap_sm *sm, void *priv)
467 struct eap_wsc_data *data = priv;
468 return data->state == FAIL;
472 static Boolean eap_wsc_isSuccess(struct eap_sm *sm, void *priv)
474 /* EAP-WSC will always result in EAP-Failure */
479 static int eap_wsc_getTimeout(struct eap_sm *sm, void *priv)
481 /* Recommended retransmit times: retransmit timeout 5 seconds,
482 * per-message timeout 15 seconds, i.e., 3 tries. */
483 sm->MaxRetrans = 2; /* total 3 attempts */
488 int eap_server_wsc_register(void)
490 struct eap_method *eap;
493 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
494 EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
499 eap->init = eap_wsc_init;
500 eap->reset = eap_wsc_reset;
501 eap->buildReq = eap_wsc_buildReq;
502 eap->check = eap_wsc_check;
503 eap->process = eap_wsc_process;
504 eap->isDone = eap_wsc_isDone;
505 eap->isSuccess = eap_wsc_isSuccess;
506 eap->getTimeout = eap_wsc_getTimeout;
508 ret = eap_server_method_register(eap);
510 eap_server_method_free(eap);