2
* EAP common peer/server definitions
3
* Copyright (c) 2004-2006, 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.h"
22
* eap_hdr_validate - Validate EAP header
23
* @vendor: Expected EAP Vendor-Id (0 = IETF)
24
* @eap_type: Expected EAP type number
25
* @msg: EAP frame (starting with EAP header)
26
* @msglen: Length of msg
27
* @plen: Pointer to variable to contain the returned payload length
28
* Returns: Pointer to EAP payload (after type field), or %NULL on failure
30
* This is a helper function for EAP method implementations. This is usually
31
* called in the beginning of struct eap_method::process() function to verify
32
* that the received EAP request packet has a valid header. This function is
33
* able to process both legacy and expanded EAP headers and in most cases, the
34
* caller can just use the returned payload pointer (into *plen) for processing
35
* the payload regardless of whether the packet used the expanded EAP header or
38
const u8 * eap_hdr_validate(int vendor, EapType eap_type,
39
const u8 *msg, size_t msglen, size_t *plen)
41
const struct eap_hdr *hdr;
45
hdr = (const struct eap_hdr *) msg;
47
if (msglen < sizeof(*hdr)) {
48
wpa_printf(MSG_INFO, "EAP: Too short EAP frame");
52
len = be_to_host16(hdr->length);
53
if (len < sizeof(*hdr) + 1 || len > msglen) {
54
wpa_printf(MSG_INFO, "EAP: Invalid EAP length");
58
pos = (const u8 *) (hdr + 1);
60
if (*pos == EAP_TYPE_EXPANDED) {
63
if (len < sizeof(*hdr) + 8) {
64
wpa_printf(MSG_INFO, "EAP: Invalid expanded EAP "
69
exp_vendor = WPA_GET_BE24(pos);
71
exp_type = WPA_GET_BE32(pos);
73
if (exp_vendor != vendor || exp_type != (u32) eap_type) {
74
wpa_printf(MSG_INFO, "EAP: Invalid expanded frame "
79
*plen = len - sizeof(*hdr) - 8;
82
if (vendor != EAP_VENDOR_IETF || *pos != eap_type) {
83
wpa_printf(MSG_INFO, "EAP: Invalid frame type");
86
*plen = len - sizeof(*hdr) - 1;
93
* eap_msg_alloc - Allocate a buffer for an EAP message
94
* @vendor: Vendor-Id (0 = IETF)
96
* @len: Buffer for returning message length
97
* @payload_len: Payload length in bytes (data after Type)
98
* @code: Message Code (EAP_CODE_*)
99
* @identifier: Identifier
100
* @payload: Pointer to payload pointer that will be set to point to the
101
* beginning of the payload or %NULL if payload pointer is not needed
102
* Returns: Pointer to the allocated message buffer or %NULL on error
104
* This function can be used to allocate a buffer for an EAP message and fill
105
* in the EAP header. This function is automatically using expanded EAP header
106
* if the selected Vendor-Id is not IETF. In other words, most EAP methods do
107
* not need to separately select which header type to use when using this
108
* function to allocate the message buffers.
110
struct eap_hdr * eap_msg_alloc(int vendor, EapType type, size_t *len,
111
size_t payload_len, u8 code, u8 identifier,
117
*len = sizeof(struct eap_hdr) + (vendor == EAP_VENDOR_IETF ? 1 : 8) +
119
hdr = os_malloc(*len);
122
hdr->identifier = identifier;
123
hdr->length = host_to_be16(*len);
124
pos = (u8 *) (hdr + 1);
125
if (vendor == EAP_VENDOR_IETF) {
128
*pos++ = EAP_TYPE_EXPANDED;
129
WPA_PUT_BE24(pos, vendor);
131
WPA_PUT_BE32(pos, type);