- In bridge_enqueue(), dispatch the mbuf to the current cpu's netisr, instead
[dragonfly/vkernel-mp.git] / contrib / hostapd-0.4.9 / eap_identity.c
blob54efc47961aa6c224faa57ff64d7a4b5a1fde5ae
1 /*
2 * hostapd / EAP-Identity
3 * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.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
10 * license.
12 * See README and COPYING for more details.
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <netinet/in.h>
20 #include "hostapd.h"
21 #include "common.h"
22 #include "eap_i.h"
25 struct eap_identity_data {
26 enum { CONTINUE, SUCCESS, FAILURE } state;
27 int pick_up;
31 static void * eap_identity_init(struct eap_sm *sm)
33 struct eap_identity_data *data;
35 data = malloc(sizeof(*data));
36 if (data == NULL)
37 return data;
38 memset(data, 0, sizeof(*data));
39 data->state = CONTINUE;
41 return data;
45 static void * eap_identity_initPickUp(struct eap_sm *sm)
47 struct eap_identity_data *data;
48 data = eap_identity_init(sm);
49 if (data) {
50 data->pick_up = 1;
52 return data;
56 static void eap_identity_reset(struct eap_sm *sm, void *priv)
58 struct eap_identity_data *data = priv;
59 free(data);
63 static u8 * eap_identity_buildReq(struct eap_sm *sm, void *priv, int id,
64 size_t *reqDataLen)
66 struct eap_identity_data *data = priv;
67 struct eap_hdr *req;
68 u8 *pos;
69 const char *req_data;
70 size_t req_data_len;
72 if (sm->eapol_cb->get_eap_req_id_text) {
73 req_data = sm->eapol_cb->get_eap_req_id_text(sm->eapol_ctx,
74 &req_data_len);
75 } else {
76 req_data = NULL;
77 req_data_len = 0;
79 *reqDataLen = sizeof(*req) + 1 + req_data_len;
80 req = malloc(*reqDataLen);
81 if (req == NULL) {
82 wpa_printf(MSG_ERROR, "EAP-Identity: Failed to allocate "
83 "memory for request");
84 data->state = FAILURE;
85 return NULL;
88 req->code = EAP_CODE_REQUEST;
89 req->identifier = id;
90 req->length = htons(*reqDataLen);
91 pos = (u8 *) (req + 1);
92 *pos++ = EAP_TYPE_IDENTITY;
93 if (req_data)
94 memcpy(pos, req_data, req_data_len);
96 return (u8 *) req;
100 static Boolean eap_identity_check(struct eap_sm *sm, void *priv,
101 u8 *respData, size_t respDataLen)
103 struct eap_hdr *resp;
104 u8 *pos;
105 size_t len;
107 resp = (struct eap_hdr *) respData;
108 pos = (u8 *) (resp + 1);
109 if (respDataLen < sizeof(*resp) + 1 || *pos != EAP_TYPE_IDENTITY ||
110 (len = ntohs(resp->length)) > respDataLen) {
111 wpa_printf(MSG_INFO, "EAP-Identity: Invalid frame");
112 return TRUE;
115 return FALSE;
119 static void eap_identity_process(struct eap_sm *sm, void *priv,
120 u8 *respData, size_t respDataLen)
122 struct eap_identity_data *data = priv;
123 struct eap_hdr *resp;
124 u8 *pos;
125 int len;
127 if (data->pick_up) {
128 if (eap_identity_check(sm, data, respData, respDataLen)) {
129 wpa_printf(MSG_DEBUG, "EAP-Identity: failed to pick "
130 "up already started negotiation");
131 data->state = FAILURE;
132 return;
134 data->pick_up = 0;
137 resp = (struct eap_hdr *) respData;
138 len = ntohs(resp->length);
139 pos = (u8 *) (resp + 1);
140 pos++;
141 len -= sizeof(*resp) + 1;
142 if (len < 0) {
143 data->state = FAILURE;
144 return;
146 wpa_hexdump_ascii(MSG_DEBUG, "EAP-Identity: Peer identity", pos, len);
147 free(sm->identity);
148 sm->identity = malloc(len);
149 if (sm->identity == NULL) {
150 data->state = FAILURE;
151 } else {
152 memcpy(sm->identity, pos, len);
153 sm->identity_len = len;
154 data->state = SUCCESS;
159 static Boolean eap_identity_isDone(struct eap_sm *sm, void *priv)
161 struct eap_identity_data *data = priv;
162 return data->state != CONTINUE;
166 static Boolean eap_identity_isSuccess(struct eap_sm *sm, void *priv)
168 struct eap_identity_data *data = priv;
169 return data->state == SUCCESS;
173 const struct eap_method eap_method_identity =
175 .method = EAP_TYPE_IDENTITY,
176 .name = "Identity",
177 .init = eap_identity_init,
178 .initPickUp = eap_identity_initPickUp,
179 .reset = eap_identity_reset,
180 .buildReq = eap_identity_buildReq,
181 .check = eap_identity_check,
182 .process = eap_identity_process,
183 .isDone = eap_identity_isDone,
184 .isSuccess = eap_identity_isSuccess,