2 * EAP peer method: EAP-TLV (draft-josefsson-pppext-eap-tls-eap-07.txt)
3 * Copyright (c) 2004-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.
23 * eap_tlv_build_nak - Build EAP-TLV NAK message
24 * @id: EAP identifier for the header
25 * @nak_type: TLV type (EAP_TLV_*)
26 * @resp_len: Buffer for returning the response length
27 * Returns: Buffer to the allocated EAP-TLV NAK message or %NULL on failure
29 * This funtion builds an EAP-TLV NAK message. The caller is responsible for
30 * freeing the returned buffer.
32 u8 * eap_tlv_build_nak(int id, u16 nak_type, size_t *resp_len)
37 hdr = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, resp_len,
38 10, EAP_CODE_RESPONSE, id, &pos);
42 *pos++ = 0x80; /* Mandatory */
43 *pos++ = EAP_TLV_NAK_TLV;
53 WPA_PUT_BE16(pos, nak_type);
60 * eap_tlv_build_result - Build EAP-TLV Result message
61 * @id: EAP identifier for the header
62 * @status: Status (EAP_TLV_RESULT_SUCCESS or EAP_TLV_RESULT_FAILURE)
63 * @resp_len: Buffer for returning the response length
64 * Returns: Buffer to the allocated EAP-TLV Result message or %NULL on failure
66 * This funtion builds an EAP-TLV Result message. The caller is responsible for
67 * freeing the returned buffer.
69 u8 * eap_tlv_build_result(int id, u16 status, size_t *resp_len)
74 hdr = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, resp_len,
75 6, EAP_CODE_RESPONSE, id, &pos);
79 *pos++ = 0x80; /* Mandatory */
80 *pos++ = EAP_TLV_RESULT_TLV;
85 WPA_PUT_BE16(pos, status);
92 * eap_tlv_process - Process a received EAP-TLV message and generate a response
93 * @sm: Pointer to EAP state machine allocated with eap_sm_init()
94 * @ret: Return values from EAP request validation and processing
95 * @hdr: EAP-TLV request to be processed. The caller must have validated that
96 * the buffer is large enough to contain full request (hdr->length bytes) and
97 * that the EAP type is EAP_TYPE_TLV.
98 * @resp: Buffer to return a pointer to the allocated response message. This
99 * field should be initialized to %NULL before the call. The value will be
100 * updated if a response message is generated. The caller is responsible for
101 * freeing the allocated message.
102 * @resp_len: Buffer for returning the response length
103 * Returns: 0 on success, -1 on failure
105 int eap_tlv_process(struct eap_sm *sm, struct eap_method_ret *ret,
106 const struct eap_hdr *hdr, u8 **resp, size_t *resp_len,
109 size_t left, tlv_len;
111 const u8 *result_tlv = NULL;
112 size_t result_tlv_len = 0;
113 int tlv_type, mandatory;
116 left = be_to_host16(hdr->length) - sizeof(struct eap_hdr) - 1;
117 pos = (const u8 *) (hdr + 1);
119 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Received TLVs", pos, left);
121 mandatory = !!(pos[0] & 0x80);
122 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
124 tlv_len = WPA_GET_BE16(pos);
127 if (tlv_len > left) {
128 wpa_printf(MSG_DEBUG, "EAP-TLV: TLV underrun "
129 "(tlv_len=%lu left=%lu)",
130 (unsigned long) tlv_len,
131 (unsigned long) left);
135 case EAP_TLV_RESULT_TLV:
137 result_tlv_len = tlv_len;
140 wpa_printf(MSG_DEBUG, "EAP-TLV: Unsupported TLV Type "
142 mandatory ? " (mandatory)" : "");
144 /* NAK TLV and ignore all TLVs in this packet.
146 *resp = eap_tlv_build_nak(hdr->identifier,
148 return *resp == NULL ? -1 : 0;
150 /* Ignore this TLV, but process other TLVs */
158 wpa_printf(MSG_DEBUG, "EAP-TLV: Last TLV too short in "
159 "Request (left=%lu)", (unsigned long) left);
163 /* Process supported TLVs */
165 int status, resp_status;
166 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Result TLV",
167 result_tlv, result_tlv_len);
168 if (result_tlv_len < 2) {
169 wpa_printf(MSG_INFO, "EAP-TLV: Too short Result TLV "
171 (unsigned long) result_tlv_len);
174 status = WPA_GET_BE16(result_tlv);
175 if (status == EAP_TLV_RESULT_SUCCESS) {
176 wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Success "
177 "- EAP-TLV/Phase2 Completed");
179 wpa_printf(MSG_INFO, "EAP-TLV: Earlier failure"
180 " - force failed Phase 2");
181 resp_status = EAP_TLV_RESULT_FAILURE;
182 ret->decision = DECISION_FAIL;
184 resp_status = EAP_TLV_RESULT_SUCCESS;
185 ret->decision = DECISION_UNCOND_SUCC;
187 } else if (status == EAP_TLV_RESULT_FAILURE) {
188 wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Failure");
189 resp_status = EAP_TLV_RESULT_FAILURE;
190 ret->decision = DECISION_FAIL;
192 wpa_printf(MSG_INFO, "EAP-TLV: Unknown TLV Result "
193 "Status %d", status);
194 resp_status = EAP_TLV_RESULT_FAILURE;
195 ret->decision = DECISION_FAIL;
197 ret->methodState = METHOD_DONE;
199 *resp = eap_tlv_build_result(hdr->identifier, resp_status,