eap_wsc.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "common.h"
00019 #include "eloop.h"
00020 #include "eap_i.h"
00021 #include "eap_common/eap_wsc_common.h"
00022 #include "wps/wps.h"
00023 
00024 
00025 struct eap_wsc_data {
00026         enum { START, MSG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
00027         int registrar;
00028         struct wpabuf *in_buf;
00029         struct wpabuf *out_buf;
00030         enum wsc_op_code in_op_code, out_op_code;
00031         size_t out_used;
00032         size_t fragment_size;
00033         struct wps_data *wps;
00034         int ext_reg_timeout;
00035 };
00036 
00037 
00038 static const char * eap_wsc_state_txt(int state)
00039 {
00040         switch (state) {
00041         case START:
00042                 return "START";
00043         case MSG:
00044                 return "MSG";
00045         case FRAG_ACK:
00046                 return "FRAG_ACK";
00047         case WAIT_FRAG_ACK:
00048                 return "WAIT_FRAG_ACK";
00049         case DONE:
00050                 return "DONE";
00051         case FAIL:
00052                 return "FAIL";
00053         default:
00054                 return "?";
00055         }
00056 }
00057 
00058 
00059 static void eap_wsc_state(struct eap_wsc_data *data, int state)
00060 {
00061         wpa_printf(MSG_DEBUG, "EAP-WSC: %s -> %s",
00062                    eap_wsc_state_txt(data->state),
00063                    eap_wsc_state_txt(state));
00064         data->state = state;
00065 }
00066 
00067 
00068 static void eap_wsc_ext_reg_timeout(void *eloop_ctx, void *timeout_ctx)
00069 {
00070         struct eap_sm *sm = eloop_ctx;
00071         struct eap_wsc_data *data = timeout_ctx;
00072 
00073         if (sm->method_pending != METHOD_PENDING_WAIT)
00074                 return;
00075 
00076         wpa_printf(MSG_DEBUG, "EAP-WSC: Timeout while waiting for an External "
00077                    "Registrar");
00078         data->ext_reg_timeout = 1;
00079         eap_sm_pending_cb(sm);
00080 }
00081 
00082 
00083 static void * eap_wsc_init(struct eap_sm *sm)
00084 {
00085         struct eap_wsc_data *data;
00086         int registrar;
00087         struct wps_config cfg;
00088 
00089         if (sm->identity && sm->identity_len == WSC_ID_REGISTRAR_LEN &&
00090             os_memcmp(sm->identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) ==
00091             0)
00092                 registrar = 0; /* Supplicant is Registrar */
00093         else if (sm->identity && sm->identity_len == WSC_ID_ENROLLEE_LEN &&
00094                  os_memcmp(sm->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN)
00095                  == 0)
00096                 registrar = 1; /* Supplicant is Enrollee */
00097         else {
00098                 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
00099                                   sm->identity, sm->identity_len);
00100                 return NULL;
00101         }
00102 
00103         data = os_zalloc(sizeof(*data));
00104         if (data == NULL)
00105                 return NULL;
00106         data->state = registrar ? START : MSG;
00107         data->registrar = registrar;
00108 
00109         os_memset(&cfg, 0, sizeof(cfg));
00110         cfg.wps = sm->wps;
00111         cfg.registrar = registrar;
00112         if (registrar) {
00113                 if (sm->wps == NULL || sm->wps->registrar == NULL) {
00114                         wpa_printf(MSG_INFO, "EAP-WSC: WPS Registrar not "
00115                                    "initialized");
00116                         os_free(data);
00117                         return NULL;
00118                 }
00119         } else {
00120                 if (sm->user == NULL || sm->user->password == NULL) {
00121                         wpa_printf(MSG_INFO, "EAP-WSC: No AP PIN (password) "
00122                                    "configured for Enrollee functionality");
00123                         os_free(data);
00124                         return NULL;
00125                 }
00126                 cfg.pin = sm->user->password;
00127                 cfg.pin_len = sm->user->password_len;
00128         }
00129         cfg.assoc_wps_ie = sm->assoc_wps_ie;
00130         cfg.peer_addr = sm->peer_addr;
00131         data->wps = wps_init(&cfg);
00132         if (data->wps == NULL) {
00133                 os_free(data);
00134                 return NULL;
00135         }
00136         data->fragment_size = WSC_FRAGMENT_SIZE;
00137 
00138         return data;
00139 }
00140 
00141 
00142 static void eap_wsc_reset(struct eap_sm *sm, void *priv)
00143 {
00144         struct eap_wsc_data *data = priv;
00145         eloop_cancel_timeout(eap_wsc_ext_reg_timeout, sm, data);
00146         wpabuf_free(data->in_buf);
00147         wpabuf_free(data->out_buf);
00148         wps_deinit(data->wps);
00149         os_free(data);
00150 }
00151 
00152 
00153 static struct wpabuf * eap_wsc_build_start(struct eap_sm *sm,
00154                                            struct eap_wsc_data *data, u8 id)
00155 {
00156         struct wpabuf *req;
00157 
00158         req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, 2,
00159                             EAP_CODE_REQUEST, id);
00160         if (req == NULL) {
00161                 wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
00162                            "request");
00163                 return NULL;
00164         }
00165 
00166         wpa_printf(MSG_DEBUG, "EAP-WSC: Send WSC/Start");
00167         wpabuf_put_u8(req, WSC_Start); /* Op-Code */
00168         wpabuf_put_u8(req, 0); /* Flags */
00169 
00170         return req;
00171 }
00172 
00173 
00174 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data, u8 id)
00175 {
00176         struct wpabuf *req;
00177         u8 flags;
00178         size_t send_len, plen;
00179 
00180         flags = 0;
00181         send_len = wpabuf_len(data->out_buf) - data->out_used;
00182         if (2 + send_len > data->fragment_size) {
00183                 send_len = data->fragment_size - 2;
00184                 flags |= WSC_FLAGS_MF;
00185                 if (data->out_used == 0) {
00186                         flags |= WSC_FLAGS_LF;
00187                         send_len -= 2;
00188                 }
00189         }
00190         plen = 2 + send_len;
00191         if (flags & WSC_FLAGS_LF)
00192                 plen += 2;
00193         req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
00194                             EAP_CODE_REQUEST, id);
00195         if (req == NULL) {
00196                 wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
00197                            "request");
00198                 return NULL;
00199         }
00200 
00201         wpabuf_put_u8(req, data->out_op_code); /* Op-Code */
00202         wpabuf_put_u8(req, flags); /* Flags */
00203         if (flags & WSC_FLAGS_LF)
00204                 wpabuf_put_be16(req, wpabuf_len(data->out_buf));
00205 
00206         wpabuf_put_data(req, wpabuf_head_u8(data->out_buf) + data->out_used,
00207                         send_len);
00208         data->out_used += send_len;
00209 
00210         if (data->out_used == wpabuf_len(data->out_buf)) {
00211                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
00212                            "(message sent completely)",
00213                            (unsigned long) send_len);
00214                 wpabuf_free(data->out_buf);
00215                 data->out_buf = NULL;
00216                 data->out_used = 0;
00217                 eap_wsc_state(data, MSG);
00218         } else {
00219                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
00220                            "(%lu more to send)", (unsigned long) send_len,
00221                            (unsigned long) wpabuf_len(data->out_buf) -
00222                            data->out_used);
00223                 eap_wsc_state(data, WAIT_FRAG_ACK);
00224         }
00225 
00226         return req;
00227 }
00228 
00229 
00230 static struct wpabuf * eap_wsc_buildReq(struct eap_sm *sm, void *priv, u8 id)
00231 {
00232         struct eap_wsc_data *data = priv;
00233 
00234         switch (data->state) {
00235         case START:
00236                 return eap_wsc_build_start(sm, data, id);
00237         case MSG:
00238                 if (data->out_buf == NULL) {
00239                         data->out_buf = wps_get_msg(data->wps,
00240                                                     &data->out_op_code);
00241                         if (data->out_buf == NULL) {
00242                                 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to "
00243                                            "receive message from WPS");
00244                                 return NULL;
00245                         }
00246                         data->out_used = 0;
00247                 }
00248                 /* pass through */
00249         case WAIT_FRAG_ACK:
00250                 return eap_wsc_build_msg(data, id);
00251         case FRAG_ACK:
00252                 return eap_wsc_build_frag_ack(id, EAP_CODE_REQUEST);
00253         default:
00254                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected state %d in "
00255                            "buildReq", data->state);
00256                 return NULL;
00257         }
00258 }
00259 
00260 
00261 static Boolean eap_wsc_check(struct eap_sm *sm, void *priv,
00262                              struct wpabuf *respData)
00263 {
00264         const u8 *pos;
00265         size_t len;
00266 
00267         pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
00268                                respData, &len);
00269         if (pos == NULL || len < 2) {
00270                 wpa_printf(MSG_INFO, "EAP-WSC: Invalid frame");
00271                 return TRUE;
00272         }
00273 
00274         return FALSE;
00275 }
00276 
00277 
00278 static int eap_wsc_process_cont(struct eap_wsc_data *data,
00279                                 const u8 *buf, size_t len, u8 op_code)
00280 {
00281         /* Process continuation of a pending message */
00282         if (op_code != data->in_op_code) {
00283                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
00284                            "fragment (expected %d)",
00285                            op_code, data->in_op_code);
00286                 eap_wsc_state(data, FAIL);
00287                 return -1;
00288         }
00289 
00290         if (len > wpabuf_tailroom(data->in_buf)) {
00291                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
00292                 eap_wsc_state(data, FAIL);
00293                 return -1;
00294         }
00295 
00296         wpabuf_put_data(data->in_buf, buf, len);
00297         wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting for %lu "
00298                    "bytes more", (unsigned long) len,
00299                    (unsigned long) wpabuf_tailroom(data->in_buf));
00300 
00301         return 0;
00302 }
00303 
00304 
00305 static int eap_wsc_process_fragment(struct eap_wsc_data *data,
00306                                     u8 flags, u8 op_code, u16 message_length,
00307                                     const u8 *buf, size_t len)
00308 {
00309         /* Process a fragment that is not the last one of the message */
00310         if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
00311                 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length "
00312                            "field in a fragmented packet");
00313                 return -1;
00314         }
00315 
00316         if (data->in_buf == NULL) {
00317                 /* First fragment of the message */
00318                 data->in_buf = wpabuf_alloc(message_length);
00319                 if (data->in_buf == NULL) {
00320                         wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
00321                                    "message");
00322                         return -1;
00323                 }
00324                 data->in_op_code = op_code;
00325                 wpabuf_put_data(data->in_buf, buf, len);
00326                 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in "
00327                            "first fragment, waiting for %lu bytes more",
00328                            (unsigned long) len,
00329                            (unsigned long) wpabuf_tailroom(data->in_buf));
00330         }
00331 
00332         return 0;
00333 }
00334 
00335 
00336 static void eap_wsc_process(struct eap_sm *sm, void *priv,
00337                             struct wpabuf *respData)
00338 {
00339         struct eap_wsc_data *data = priv;
00340         const u8 *start, *pos, *end;
00341         size_t len;
00342         u8 op_code, flags;
00343         u16 message_length = 0;
00344         enum wps_process_res res;
00345         struct wpabuf tmpbuf;
00346 
00347         eloop_cancel_timeout(eap_wsc_ext_reg_timeout, sm, data);
00348         if (data->ext_reg_timeout) {
00349                 eap_wsc_state(data, FAIL);
00350                 return;
00351         }
00352 
00353         pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
00354                                respData, &len);
00355         if (pos == NULL || len < 2)
00356                 return; /* Should not happen; message already verified */
00357 
00358         start = pos;
00359         end = start + len;
00360 
00361         op_code = *pos++;
00362         flags = *pos++;
00363         if (flags & WSC_FLAGS_LF) {
00364                 if (end - pos < 2) {
00365                         wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
00366                         return;
00367                 }
00368                 message_length = WPA_GET_BE16(pos);
00369                 pos += 2;
00370 
00371                 if (message_length < end - pos) {
00372                         wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
00373                                    "Length");
00374                         return;
00375                 }
00376         }
00377 
00378         wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
00379                    "Flags 0x%x Message Length %d",
00380                    op_code, flags, message_length);
00381 
00382         if (data->state == WAIT_FRAG_ACK) {
00383                 if (op_code != WSC_FRAG_ACK) {
00384                         wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
00385                                    "in WAIT_FRAG_ACK state", op_code);
00386                         eap_wsc_state(data, FAIL);
00387                         return;
00388                 }
00389                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
00390                 eap_wsc_state(data, MSG);
00391                 return;
00392         }
00393 
00394         if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
00395             op_code != WSC_Done) {
00396                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
00397                            op_code);
00398                 eap_wsc_state(data, FAIL);
00399                 return;
00400         }
00401 
00402         if (data->in_buf &&
00403             eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
00404                 eap_wsc_state(data, FAIL);
00405                 return;
00406         }
00407 
00408         if (flags & WSC_FLAGS_MF) {
00409                 if (eap_wsc_process_fragment(data, flags, op_code,
00410                                              message_length, pos, end - pos) <
00411                     0)
00412                         eap_wsc_state(data, FAIL);
00413                 else
00414                         eap_wsc_state(data, FRAG_ACK);
00415                 return;
00416         }
00417 
00418         if (data->in_buf == NULL) {
00419                 /* Wrap unfragmented messages as wpabuf without extra copy */
00420                 wpabuf_set(&tmpbuf, pos, end - pos);
00421                 data->in_buf = &tmpbuf;
00422         }
00423 
00424         res = wps_process_msg(data->wps, op_code, data->in_buf);
00425         switch (res) {
00426         case WPS_DONE:
00427                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
00428                            "successfully - report EAP failure");
00429                 eap_wsc_state(data, FAIL);
00430                 break;
00431         case WPS_CONTINUE:
00432                 eap_wsc_state(data, MSG);
00433                 break;
00434         case WPS_FAILURE:
00435                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
00436                 eap_wsc_state(data, FAIL);
00437                 break;
00438         case WPS_PENDING:
00439                 eap_wsc_state(data, MSG);
00440                 sm->method_pending = METHOD_PENDING_WAIT;
00441                 eloop_cancel_timeout(eap_wsc_ext_reg_timeout, sm, data);
00442                 eloop_register_timeout(5, 0, eap_wsc_ext_reg_timeout,
00443                                        sm, data);
00444                 break;
00445         }
00446 
00447         if (data->in_buf != &tmpbuf)
00448                 wpabuf_free(data->in_buf);
00449         data->in_buf = NULL;
00450 }
00451 
00452 
00453 static Boolean eap_wsc_isDone(struct eap_sm *sm, void *priv)
00454 {
00455         struct eap_wsc_data *data = priv;
00456         return data->state == FAIL;
00457 }
00458 
00459 
00460 static Boolean eap_wsc_isSuccess(struct eap_sm *sm, void *priv)
00461 {
00462         /* EAP-WSC will always result in EAP-Failure */
00463         return FALSE;
00464 }
00465 
00466 
00467 static int eap_wsc_getTimeout(struct eap_sm *sm, void *priv)
00468 {
00469         /* Recommended retransmit times: retransmit timeout 5 seconds,
00470          * per-message timeout 15 seconds, i.e., 3 tries. */
00471         sm->MaxRetrans = 2; /* total 3 attempts */
00472         return 5;
00473 }
00474 
00475 
00476 int eap_server_wsc_register(void)
00477 {
00478         struct eap_method *eap;
00479         int ret;
00480 
00481         eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
00482                                       EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
00483                                       "WSC");
00484         if (eap == NULL)
00485                 return -1;
00486 
00487         eap->init = eap_wsc_init;
00488         eap->reset = eap_wsc_reset;
00489         eap->buildReq = eap_wsc_buildReq;
00490         eap->check = eap_wsc_check;
00491         eap->process = eap_wsc_process;
00492         eap->isDone = eap_wsc_isDone;
00493         eap->isSuccess = eap_wsc_isSuccess;
00494         eap->getTimeout = eap_wsc_getTimeout;
00495 
00496         ret = eap_server_method_register(eap);
00497         if (ret)
00498                 eap_server_method_free(eap);
00499         return ret;
00500 }
00501 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:52 2009 for hostapd by  doxygen 1.6.1