2 * EAP common peer/server definitions
3 * Copyright (c) 2004-2007, 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 * @plen: Pointer to variable to contain the returned payload length
27 * Returns: Pointer to EAP payload (after type field), or %NULL on failure
29 * This is a helper function for EAP method implementations. This is usually
30 * called in the beginning of struct eap_method::process() function to verify
31 * that the received EAP request packet has a valid header. This function is
32 * able to process both legacy and expanded EAP headers and in most cases, the
33 * caller can just use the returned payload pointer (into *plen) for processing
34 * the payload regardless of whether the packet used the expanded EAP header or
37 const u8
* eap_hdr_validate(int vendor
, EapType eap_type
,
38 const struct wpabuf
*msg
, size_t *plen
)
40 const struct eap_hdr
*hdr
;
44 hdr
= wpabuf_head(msg
);
46 if (wpabuf_len(msg
) < sizeof(*hdr
)) {
47 wpa_printf(MSG_INFO
, "EAP: Too short EAP frame");
51 len
= be_to_host16(hdr
->length
);
52 if (len
< sizeof(*hdr
) + 1 || len
> wpabuf_len(msg
)) {
53 wpa_printf(MSG_INFO
, "EAP: Invalid EAP length");
57 pos
= (const u8
*) (hdr
+ 1);
59 if (*pos
== EAP_TYPE_EXPANDED
) {
62 if (len
< sizeof(*hdr
) + 8) {
63 wpa_printf(MSG_INFO
, "EAP: Invalid expanded EAP "
68 exp_vendor
= WPA_GET_BE24(pos
);
70 exp_type
= WPA_GET_BE32(pos
);
72 if (exp_vendor
!= vendor
|| exp_type
!= (u32
) eap_type
) {
73 wpa_printf(MSG_INFO
, "EAP: Invalid expanded frame "
78 *plen
= len
- sizeof(*hdr
) - 8;
81 if (vendor
!= EAP_VENDOR_IETF
|| *pos
!= eap_type
) {
82 wpa_printf(MSG_INFO
, "EAP: Invalid frame type");
85 *plen
= len
- sizeof(*hdr
) - 1;
92 * eap_msg_alloc - Allocate a buffer for an EAP message
93 * @vendor: Vendor-Id (0 = IETF)
95 * @payload_len: Payload length in bytes (data after Type)
96 * @code: Message Code (EAP_CODE_*)
97 * @identifier: Identifier
98 * Returns: Pointer to the allocated message buffer or %NULL on error
100 * This function can be used to allocate a buffer for an EAP message and fill
101 * in the EAP header. This function is automatically using expanded EAP header
102 * if the selected Vendor-Id is not IETF. In other words, most EAP methods do
103 * not need to separately select which header type to use when using this
104 * function to allocate the message buffers. The returned buffer has room for
105 * payload_len bytes and has the EAP header and Type field already filled in.
107 struct wpabuf
* eap_msg_alloc(int vendor
, EapType type
, size_t payload_len
,
108 u8 code
, u8 identifier
)
114 len
= sizeof(struct eap_hdr
) + (vendor
== EAP_VENDOR_IETF
? 1 : 8) +
116 buf
= wpabuf_alloc(len
);
120 hdr
= wpabuf_put(buf
, sizeof(*hdr
));
122 hdr
->identifier
= identifier
;
123 hdr
->length
= host_to_be16(len
);
125 if (vendor
== EAP_VENDOR_IETF
) {
126 wpabuf_put_u8(buf
, type
);
128 wpabuf_put_u8(buf
, EAP_TYPE_EXPANDED
);
129 wpabuf_put_be24(buf
, vendor
);
130 wpabuf_put_be32(buf
, type
);
138 * eap_update_len - Update EAP header length
139 * @msg: EAP message from eap_msg_alloc
141 * This function updates the length field in the EAP header to match with the
142 * current length for the buffer. This allows eap_msg_alloc() to be used to
143 * allocate a larger buffer than the exact message length (e.g., if exact
144 * message length is not yet known).
146 void eap_update_len(struct wpabuf
*msg
)
149 hdr
= wpabuf_mhead(msg
);
150 if (wpabuf_len(msg
) < sizeof(*hdr
))
152 hdr
->length
= host_to_be16(wpabuf_len(msg
));
157 * eap_get_id - Get EAP Identifier from wpabuf
158 * @msg: Buffer starting with an EAP header
159 * Returns: The Identifier field from the EAP header
161 u8
eap_get_id(const struct wpabuf
*msg
)
163 const struct eap_hdr
*eap
;
165 if (wpabuf_len(msg
) < sizeof(*eap
))
168 eap
= wpabuf_head(msg
);
169 return eap
->identifier
;
174 * eap_get_id - Get EAP Type from wpabuf
175 * @msg: Buffer starting with an EAP header
176 * Returns: The EAP Type after the EAP header
178 EapType
eap_get_type(const struct wpabuf
*msg
)
180 if (wpabuf_len(msg
) < sizeof(struct eap_hdr
) + 1)
181 return EAP_TYPE_NONE
;
183 return ((const u8
*) wpabuf_head(msg
))[sizeof(struct eap_hdr
)];