xref: /netbsd-src/external/bsd/tcpdump/dist/print-eap.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /*
2  * Copyright (c) 2004 - Michael Richardson <mcr@xelerance.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that: (1) source code distributions
6  * retain the above copyright notice and this paragraph in its entirety, (2)
7  * distributions including binary code include the above copyright notice and
8  * this paragraph in its entirety in the documentation or other materials
9  * provided with the distribution, and (3) all advertising materials mentioning
10  * features or use of this software display the following acknowledgement:
11  * ``This product includes software developed by the University of California,
12  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
13  * the University nor the names of its contributors may be used to endorse
14  * or promote products derived from this software without specific prior
15  * written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20 
21 /* \summary: Extensible Authentication Protocol (EAP) printer */
22 
23 #include <sys/cdefs.h>
24 #ifndef lint
25 __RCSID("$NetBSD: print-eap.c,v 1.8 2023/08/17 20:19:40 christos Exp $");
26 #endif
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 #include "netdissect-stdinc.h"
33 
34 #include "netdissect.h"
35 #include "extract.h"
36 
37 #define	EAP_FRAME_TYPE_PACKET		0
38 #define	EAP_FRAME_TYPE_START		1
39 #define	EAP_FRAME_TYPE_LOGOFF		2
40 #define	EAP_FRAME_TYPE_KEY		3
41 #define	EAP_FRAME_TYPE_ENCAP_ASF_ALERT	4
42 
43 struct eap_frame_t {
44     nd_uint8_t  version;
45     nd_uint8_t  type;
46     nd_uint16_t length;
47 };
48 
49 static const struct tok eap_frame_type_values[] = {
50     { EAP_FRAME_TYPE_PACKET,		"EAP packet" },
51     { EAP_FRAME_TYPE_START,		"EAPOL start" },
52     { EAP_FRAME_TYPE_LOGOFF,		"EAPOL logoff" },
53     { EAP_FRAME_TYPE_KEY,		"EAPOL key" },
54     { EAP_FRAME_TYPE_ENCAP_ASF_ALERT,	"Encapsulated ASF alert" },
55     { 0, NULL}
56 };
57 
58 /* RFC 3748 */
59 struct eap_packet_t {
60     nd_uint8_t  code;
61     nd_uint8_t  id;
62     nd_uint16_t length;
63 };
64 
65 #define		EAP_REQUEST	1
66 #define		EAP_RESPONSE	2
67 #define		EAP_SUCCESS	3
68 #define		EAP_FAILURE	4
69 
70 static const struct tok eap_code_values[] = {
71     { EAP_REQUEST,	"Request" },
72     { EAP_RESPONSE,	"Response" },
73     { EAP_SUCCESS,	"Success" },
74     { EAP_FAILURE,	"Failure" },
75     { 0, NULL}
76 };
77 
78 #define		EAP_TYPE_NO_PROPOSED	0
79 #define		EAP_TYPE_IDENTITY	1
80 #define		EAP_TYPE_NOTIFICATION	2
81 #define		EAP_TYPE_NAK		3
82 #define		EAP_TYPE_MD5_CHALLENGE	4
83 #define		EAP_TYPE_OTP		5
84 #define		EAP_TYPE_GTC		6
85 #define		EAP_TYPE_TLS		13		/* RFC 5216 */
86 #define		EAP_TYPE_SIM		18		/* RFC 4186 */
87 #define		EAP_TYPE_TTLS		21		/* RFC 5281, draft-funk-eap-ttls-v0-01.txt */
88 #define		EAP_TYPE_AKA		23		/* RFC 4187 */
89 #define		EAP_TYPE_FAST		43		/* RFC 4851 */
90 #define		EAP_TYPE_EXPANDED_TYPES	254
91 #define		EAP_TYPE_EXPERIMENTAL	255
92 
93 static const struct tok eap_type_values[] = {
94     { EAP_TYPE_NO_PROPOSED,	"No proposed" },
95     { EAP_TYPE_IDENTITY,	"Identity" },
96     { EAP_TYPE_NOTIFICATION,    "Notification" },
97     { EAP_TYPE_NAK,		"Nak" },
98     { EAP_TYPE_MD5_CHALLENGE,   "MD5-challenge" },
99     { EAP_TYPE_OTP,		"OTP" },
100     { EAP_TYPE_GTC,		"GTC" },
101     { EAP_TYPE_TLS,		"TLS" },
102     { EAP_TYPE_SIM,		"SIM" },
103     { EAP_TYPE_TTLS,		"TTLS" },
104     { EAP_TYPE_AKA,		"AKA" },
105     { EAP_TYPE_FAST,		"FAST" },
106     { EAP_TYPE_EXPANDED_TYPES,  "Expanded types" },
107     { EAP_TYPE_EXPERIMENTAL,    "Experimental" },
108     { 0, NULL}
109 };
110 
111 #define EAP_TLS_EXTRACT_BIT_L(x)	(((x)&0x80)>>7)
112 
113 /* RFC 5216 - EAP TLS bits */
114 #define EAP_TLS_FLAGS_LEN_INCLUDED		(1 << 7)
115 #define EAP_TLS_FLAGS_MORE_FRAGMENTS		(1 << 6)
116 #define EAP_TLS_FLAGS_START			(1 << 5)
117 
118 static const struct tok eap_tls_flags_values[] = {
119 	{ EAP_TLS_FLAGS_LEN_INCLUDED, "L bit" },
120 	{ EAP_TLS_FLAGS_MORE_FRAGMENTS, "More fragments bit"},
121 	{ EAP_TLS_FLAGS_START, "Start bit"},
122 	{ 0, NULL}
123 };
124 
125 #define EAP_TTLS_VERSION(x)		((x)&0x07)
126 
127 /* EAP-AKA and EAP-SIM - RFC 4187 */
128 #define EAP_AKA_CHALLENGE		1
129 #define EAP_AKA_AUTH_REJECT		2
130 #define EAP_AKA_SYNC_FAILURE		4
131 #define EAP_AKA_IDENTITY		5
132 #define EAP_SIM_START			10
133 #define EAP_SIM_CHALLENGE		11
134 #define EAP_AKA_NOTIFICATION		12
135 #define EAP_AKA_REAUTH			13
136 #define EAP_AKA_CLIENT_ERROR		14
137 
138 static const struct tok eap_aka_subtype_values[] = {
139     { EAP_AKA_CHALLENGE,	"Challenge" },
140     { EAP_AKA_AUTH_REJECT,	"Auth reject" },
141     { EAP_AKA_SYNC_FAILURE,	"Sync failure" },
142     { EAP_AKA_IDENTITY,		"Identity" },
143     { EAP_SIM_START,		"Start" },
144     { EAP_SIM_CHALLENGE,	"Challenge" },
145     { EAP_AKA_NOTIFICATION,	"Notification" },
146     { EAP_AKA_REAUTH,		"Reauth" },
147     { EAP_AKA_CLIENT_ERROR,	"Client error" },
148     { 0, NULL}
149 };
150 
151 /*
152  * Print EAP requests / responses
153  */
154 void
155 eap_print(netdissect_options *ndo,
156           const u_char *cp,
157           u_int length)
158 {
159     u_int type, subtype, len;
160     u_int count;
161     const char *sep;
162 
163     type = GET_U_1(cp);
164     len = GET_BE_U_2(cp + 2);
165     if (len != length) {
166         /*
167          * Probably a fragment; in some cases the fragmentation might
168          * not put an EAP header on every packet, if reassembly can
169          * be done without that (e.g., fragmentation to make a message
170          * fit in multiple TLVs in a RADIUS packet).
171          */
172         ND_PRINT("EAP fragment?");
173         return;
174     }
175     ND_PRINT("%s (%u), id %u, len %u",
176             tok2str(eap_code_values, "unknown", type),
177             type,
178             GET_U_1((cp + 1)),
179             len);
180     if (len < 4) {
181         ND_PRINT(" (too short for EAP header)");
182         return;
183     }
184 
185     ND_TCHECK_LEN(cp, len);
186 
187     if (type == EAP_REQUEST || type == EAP_RESPONSE) {
188         /* RFC 3748 Section 4.1 */
189         if (len < 5) {
190             ND_PRINT(" (too short for EAP request/response)");
191             return;
192         }
193         subtype = GET_U_1(cp + 4);
194         ND_PRINT("\n\t\t Type %s (%u)",
195                 tok2str(eap_type_values, "unknown", subtype),
196                 subtype);
197 
198         switch (subtype) {
199             case EAP_TYPE_IDENTITY:
200                 /* According to RFC 3748, the message is optional */
201                 if (len > 5) {
202                     ND_PRINT(", Identity: ");
203                     nd_printjnp(ndo, cp + 5, len - 5);
204                 }
205                 break;
206 
207             case EAP_TYPE_NOTIFICATION:
208                 /* According to RFC 3748, there must be at least one octet of message */
209                 if (len < 6) {
210                     ND_PRINT(" (too short for EAP Notification request/response)");
211                     return;
212                 }
213                 ND_PRINT(", Notification: ");
214                 nd_printjnp(ndo, cp + 5, len - 5);
215                 break;
216 
217             case EAP_TYPE_NAK:
218                 /*
219                  * one or more octets indicating
220                  * the desired authentication
221                  * type one octet per type
222                  */
223                 if (len < 6) {
224                     ND_PRINT(" (too short for EAP Legacy NAK request/response)");
225                     return;
226                 }
227                 sep = "";
228                 for (count = 5; count < len; count++) {
229                     ND_PRINT("%s %s (%u)", sep,
230                            tok2str(eap_type_values, "unknown", GET_U_1((cp + count))),
231                            GET_U_1(cp + count));
232                     sep = ",";
233                 }
234                 break;
235 
236             case EAP_TYPE_TTLS:
237             case EAP_TYPE_TLS:
238                 if (len < 6) {
239                     ND_PRINT(" (too short for EAP TLS/TTLS request/response)");
240                     return;
241                 }
242                 if (subtype == EAP_TYPE_TTLS)
243                     ND_PRINT(" TTLSv%u",
244                            EAP_TTLS_VERSION(GET_U_1((cp + 5))));
245                 ND_PRINT(" flags [%s] 0x%02x",
246                        bittok2str(eap_tls_flags_values, "none", GET_U_1((cp + 5))),
247                        GET_U_1(cp + 5));
248 
249                 if (EAP_TLS_EXTRACT_BIT_L(GET_U_1(cp + 5))) {
250                     if (len < 10) {
251                         ND_PRINT(" (too short for EAP TLS/TTLS request/response with length)");
252                         return;
253                     }
254                     ND_PRINT(", len %u", GET_BE_U_4(cp + 6));
255                 }
256                 break;
257 
258             case EAP_TYPE_FAST:
259                 if (len < 6) {
260                     ND_PRINT(" (too short for EAP FAST request/response)");
261                     return;
262                 }
263                 ND_PRINT(" FASTv%u",
264                        EAP_TTLS_VERSION(GET_U_1((cp + 5))));
265                 ND_PRINT(" flags [%s] 0x%02x",
266                        bittok2str(eap_tls_flags_values, "none", GET_U_1((cp + 5))),
267                        GET_U_1(cp + 5));
268 
269                 if (EAP_TLS_EXTRACT_BIT_L(GET_U_1(cp + 5))) {
270                     if (len < 10) {
271                         ND_PRINT(" (too short for EAP FAST request/response with length)");
272                         return;
273                     }
274                     ND_PRINT(", len %u", GET_BE_U_4(cp + 6));
275                 }
276 
277                 /* FIXME - TLV attributes follow */
278                 break;
279 
280             case EAP_TYPE_AKA:
281             case EAP_TYPE_SIM:
282                 if (len < 6) {
283                     ND_PRINT(" (too short for EAP SIM/AKA request/response)");
284                     return;
285                 }
286                 ND_PRINT(" subtype [%s] 0x%02x",
287                        tok2str(eap_aka_subtype_values, "unknown", GET_U_1((cp + 5))),
288                        GET_U_1(cp + 5));
289 
290                 /* FIXME - TLV attributes follow */
291                 break;
292 
293             case EAP_TYPE_MD5_CHALLENGE:
294             case EAP_TYPE_OTP:
295             case EAP_TYPE_GTC:
296             case EAP_TYPE_EXPANDED_TYPES:
297             case EAP_TYPE_EXPERIMENTAL:
298             default:
299                 break;
300         }
301     }
302     return;
303 trunc:
304     nd_print_trunc(ndo);
305 }
306 
307 void
308 eapol_print(netdissect_options *ndo,
309             const u_char *cp)
310 {
311     const struct eap_frame_t *eap;
312     u_int eap_type, eap_len;
313 
314     ndo->ndo_protocol = "eap";
315     eap = (const struct eap_frame_t *)cp;
316     ND_TCHECK_SIZE(eap);
317     eap_type = GET_U_1(eap->type);
318 
319     ND_PRINT("%s (%u) v%u, len %u",
320            tok2str(eap_frame_type_values, "unknown", eap_type),
321            eap_type,
322            GET_U_1(eap->version),
323            GET_BE_U_2(eap->length));
324     if (ndo->ndo_vflag < 1)
325         return;
326 
327     cp += sizeof(struct eap_frame_t);
328     eap_len = GET_BE_U_2(eap->length);
329 
330     switch (eap_type) {
331     case EAP_FRAME_TYPE_PACKET:
332         if (eap_len == 0)
333             goto trunc;
334         ND_PRINT(", ");
335         eap_print(ndo, cp, eap_len);
336         return;
337     case EAP_FRAME_TYPE_LOGOFF:
338     case EAP_FRAME_TYPE_ENCAP_ASF_ALERT:
339     default:
340         break;
341     }
342     return;
343 
344  trunc:
345     nd_print_trunc(ndo);
346 }
347