Add -O and -T to the SYNOPSIS.
[dragonfly.git] / contrib / wpa_supplicant-0.4.9 / eap_tlv.c
1 /*
2  * WPA Supplicant / EAP-TLV (draft-josefsson-pppext-eap-tls-eap-07.txt)
3  * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "common.h"
20 #include "wpa_supplicant.h"
21 #include "eap_i.h"
22 #include "eap_tlv.h"
23
24
25 /**
26  * eap_tlv_build_nak - Build EAP-TLV NAK message
27  * @id: EAP identifier for the header
28  * @nak_type: TLV type (EAP_TLV_*)
29  * @resp_len: Buffer for returning the response length
30  * Returns: Buffer to the allocated EAP-TLV NAK message or %NULL on failure
31  *
32  * This funtion builds an EAP-TLV NAK message. The caller is responsible for
33  * freeing the returned buffer.
34  */
35 u8 * eap_tlv_build_nak(int id, u16 nak_type, size_t *resp_len)
36 {
37         struct eap_hdr *hdr;
38         u8 *pos;
39
40         *resp_len = sizeof(struct eap_hdr) + 1 + 10;
41         hdr = malloc(*resp_len);
42         if (hdr == NULL)
43                 return NULL;
44
45         hdr->code = EAP_CODE_RESPONSE;
46         hdr->identifier = id;
47         hdr->length = host_to_be16(*resp_len);
48         pos = (u8 *) (hdr + 1);
49         *pos++ = EAP_TYPE_TLV;
50         *pos++ = 0x80; /* Mandatory */
51         *pos++ = EAP_TLV_NAK_TLV;
52         /* Length */
53         *pos++ = 0;
54         *pos++ = 6;
55         /* Vendor-Id */
56         *pos++ = 0;
57         *pos++ = 0;
58         *pos++ = 0;
59         *pos++ = 0;
60         /* NAK-Type */
61         WPA_PUT_BE16(pos, nak_type);
62
63         return (u8 *) hdr;
64 }
65
66
67 /**
68  * eap_tlv_build_result - Build EAP-TLV Result message
69  * @id: EAP identifier for the header
70  * @status: Status (EAP_TLV_RESULT_SUCCESS or EAP_TLV_RESULT_FAILURE)
71  * @resp_len: Buffer for returning the response length
72  * Returns: Buffer to the allocated EAP-TLV Result message or %NULL on failure
73  *
74  * This funtion builds an EAP-TLV Result message. The caller is responsible for
75  * freeing the returned buffer.
76  */
77 u8 * eap_tlv_build_result(int id, u16 status, size_t *resp_len)
78 {
79         struct eap_hdr *hdr;
80         u8 *pos;
81
82         *resp_len = sizeof(struct eap_hdr) + 1 + 6;
83         hdr = malloc(*resp_len);
84         if (hdr == NULL)
85                 return NULL;
86
87         hdr->code = EAP_CODE_RESPONSE;
88         hdr->identifier = id;
89         hdr->length = host_to_be16(*resp_len);
90         pos = (u8 *) (hdr + 1);
91         *pos++ = EAP_TYPE_TLV;
92         *pos++ = 0x80; /* Mandatory */
93         *pos++ = EAP_TLV_RESULT_TLV;
94         /* Length */
95         *pos++ = 0;
96         *pos++ = 2;
97         /* Status */
98         WPA_PUT_BE16(pos, status);
99
100         return (u8 *) hdr;
101 }
102
103
104 /**
105  * eap_tlv_process - Process a received EAP-TLV message and generate a response
106  * @sm: Pointer to EAP state machine allocated with eap_sm_init()
107  * @ret: Return values from EAP request validation and processing
108  * @hdr: EAP-TLV request to be processed. The caller must have validated that
109  * the buffer is large enough to contain full request (hdr->length bytes) and
110  * that the EAP type is EAP_TYPE_TLV.
111  * @resp: Buffer to return a pointer to the allocated response message. This
112  * field should be initialized to %NULL before the call. The value will be
113  * updated if a response message is generated. The caller is responsible for
114  * freeing the allocated message.
115  * @resp_len: Buffer for returning the response length
116  * Returns: 0 on success, -1 on failure
117  */
118 int eap_tlv_process(struct eap_sm *sm, struct eap_method_ret *ret,
119                     const struct eap_hdr *hdr, u8 **resp, size_t *resp_len)
120 {
121         size_t left;
122         const u8 *pos;
123         const u8 *result_tlv = NULL;
124         size_t result_tlv_len = 0;
125         int tlv_type, mandatory, tlv_len;
126
127         /* Parse TLVs */
128         left = be_to_host16(hdr->length) - sizeof(struct eap_hdr) - 1;
129         pos = (const u8 *) (hdr + 1);
130         pos++;
131         wpa_hexdump(MSG_DEBUG, "EAP-TLV: Received TLVs", pos, left);
132         while (left >= 4) {
133                 mandatory = !!(pos[0] & 0x80);
134                 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
135                 pos += 2;
136                 tlv_len = WPA_GET_BE16(pos);
137                 pos += 2;
138                 left -= 4;
139                 if (tlv_len > left) {
140                         wpa_printf(MSG_DEBUG, "EAP-TLV: TLV underrun "
141                                    "(tlv_len=%d left=%lu)", tlv_len,
142                                    (unsigned long) left);
143                         return -1;
144                 }
145                 switch (tlv_type) {
146                 case EAP_TLV_RESULT_TLV:
147                         result_tlv = pos;
148                         result_tlv_len = tlv_len;
149                         break;
150                 default:
151                         wpa_printf(MSG_DEBUG, "EAP-TLV: Unsupported TLV Type "
152                                    "%d%s", tlv_type,
153                                    mandatory ? " (mandatory)" : "");
154                         if (mandatory) {
155                                 /* NAK TLV and ignore all TLVs in this packet.
156                                  */
157                                 *resp = eap_tlv_build_nak(hdr->identifier,
158                                                           tlv_type, resp_len);
159                                 return *resp == NULL ? -1 : 0;
160                         }
161                         /* Ignore this TLV, but process other TLVs */
162                         break;
163                 }
164
165                 pos += tlv_len;
166                 left -= tlv_len;
167         }
168         if (left) {
169                 wpa_printf(MSG_DEBUG, "EAP-TLV: Last TLV too short in "
170                            "Request (left=%lu)", (unsigned long) left);
171                 return -1;
172         }
173
174         /* Process supported TLVs */
175         if (result_tlv) {
176                 int status, resp_status;
177                 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Result TLV",
178                             result_tlv, result_tlv_len);
179                 if (result_tlv_len < 2) {
180                         wpa_printf(MSG_INFO, "EAP-TLV: Too short Result TLV "
181                                    "(len=%lu)",
182                                    (unsigned long) result_tlv_len);
183                         return -1;
184                 }
185                 status = WPA_GET_BE16(result_tlv);
186                 if (status == EAP_TLV_RESULT_SUCCESS) {
187                         wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Success "
188                                    "- EAP-TLV/Phase2 Completed");
189                         resp_status = EAP_TLV_RESULT_SUCCESS;
190                         ret->decision = DECISION_UNCOND_SUCC;
191                 } else if (status == EAP_TLV_RESULT_FAILURE) {
192                         wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Failure");
193                         resp_status = EAP_TLV_RESULT_FAILURE;
194                         ret->decision = DECISION_FAIL;
195                 } else {
196                         wpa_printf(MSG_INFO, "EAP-TLV: Unknown TLV Result "
197                                    "Status %d", status);
198                         resp_status = EAP_TLV_RESULT_FAILURE;
199                         ret->decision = DECISION_FAIL;
200                 }
201                 ret->methodState = METHOD_DONE;
202
203                 *resp = eap_tlv_build_result(hdr->identifier, resp_status,
204                                              resp_len);
205         }
206
207         return 0;
208 }