xref: /freebsd/contrib/tcpdump/print-macsec.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
1*ee67461eSJoseph Mingrone /* Copyright (c) 2017, Sabrina Dubroca <sd@queasysnail.net>
2*ee67461eSJoseph Mingrone  *
3*ee67461eSJoseph Mingrone  * Redistribution and use in source and binary forms, with or without
4*ee67461eSJoseph Mingrone  * modification, are permitted provided that the following conditions
5*ee67461eSJoseph Mingrone  * are met:
6*ee67461eSJoseph Mingrone  *
7*ee67461eSJoseph Mingrone  *   1. Redistributions of source code must retain the above copyright
8*ee67461eSJoseph Mingrone  *      notice, this list of conditions and the following disclaimer.
9*ee67461eSJoseph Mingrone  *   2. Redistributions in binary form must reproduce the above copyright
10*ee67461eSJoseph Mingrone  *      notice, this list of conditions and the following disclaimer in
11*ee67461eSJoseph Mingrone  *      the documentation and/or other materials provided with the
12*ee67461eSJoseph Mingrone  *      distribution.
13*ee67461eSJoseph Mingrone  *   3. The names of the authors may not be used to endorse or promote
14*ee67461eSJoseph Mingrone  *      products derived from this software without specific prior
15*ee67461eSJoseph Mingrone  *      written permission.
16*ee67461eSJoseph Mingrone  *
17*ee67461eSJoseph Mingrone  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
18*ee67461eSJoseph Mingrone  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
19*ee67461eSJoseph Mingrone  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*ee67461eSJoseph Mingrone  */
21*ee67461eSJoseph Mingrone 
22*ee67461eSJoseph Mingrone /* \summary: MACsec printer */
23*ee67461eSJoseph Mingrone 
24*ee67461eSJoseph Mingrone #include <config.h>
25*ee67461eSJoseph Mingrone 
26*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
27*ee67461eSJoseph Mingrone 
28*ee67461eSJoseph Mingrone #include "netdissect.h"
29*ee67461eSJoseph Mingrone #include "addrtoname.h"
30*ee67461eSJoseph Mingrone #include "extract.h"
31*ee67461eSJoseph Mingrone 
32*ee67461eSJoseph Mingrone #define MACSEC_DEFAULT_ICV_LEN 16
33*ee67461eSJoseph Mingrone 
34*ee67461eSJoseph Mingrone /* Header format (SecTAG), following an Ethernet header
35*ee67461eSJoseph Mingrone  * IEEE 802.1AE-2006 9.3
36*ee67461eSJoseph Mingrone  *
37*ee67461eSJoseph Mingrone  * +---------------------------------+----------------+----------------+
38*ee67461eSJoseph Mingrone  * |        (MACsec ethertype)       |     TCI_AN     |       SL       |
39*ee67461eSJoseph Mingrone  * +---------------------------------+----------------+----------------+
40*ee67461eSJoseph Mingrone  * |                           Packet Number                           |
41*ee67461eSJoseph Mingrone  * +-------------------------------------------------------------------+
42*ee67461eSJoseph Mingrone  * |                     Secure Channel Identifier                     |
43*ee67461eSJoseph Mingrone  * |                            (optional)                             |
44*ee67461eSJoseph Mingrone  * +-------------------------------------------------------------------+
45*ee67461eSJoseph Mingrone  *
46*ee67461eSJoseph Mingrone  * MACsec ethertype = 0x88e5
47*ee67461eSJoseph Mingrone  * TCI: Tag Control Information, set of flags
48*ee67461eSJoseph Mingrone  * AN: association number, 2 bits
49*ee67461eSJoseph Mingrone  * SL (short length): 6-bit length of the protected payload, if < 48
50*ee67461eSJoseph Mingrone  * Packet Number: 32-bits packet identifier
51*ee67461eSJoseph Mingrone  * Secure Channel Identifier: 64-bit unique identifier, usually
52*ee67461eSJoseph Mingrone  *     composed of a MAC address + 16-bit port number
53*ee67461eSJoseph Mingrone  */
54*ee67461eSJoseph Mingrone struct macsec_sectag {
55*ee67461eSJoseph Mingrone 	nd_uint8_t  tci_an;
56*ee67461eSJoseph Mingrone 	nd_uint8_t  short_length;
57*ee67461eSJoseph Mingrone 	nd_uint32_t packet_number;
58*ee67461eSJoseph Mingrone 	nd_uint8_t  secure_channel_id[8]; /* optional */
59*ee67461eSJoseph Mingrone };
60*ee67461eSJoseph Mingrone 
61*ee67461eSJoseph Mingrone /* IEEE 802.1AE-2006 9.5 */
62*ee67461eSJoseph Mingrone #define MACSEC_TCI_VERSION 0x80
63*ee67461eSJoseph Mingrone #define MACSEC_TCI_ES      0x40 /* end station */
64*ee67461eSJoseph Mingrone #define MACSEC_TCI_SC      0x20 /* SCI present */
65*ee67461eSJoseph Mingrone #define MACSEC_TCI_SCB     0x10 /* epon */
66*ee67461eSJoseph Mingrone #define MACSEC_TCI_E       0x08 /* encryption */
67*ee67461eSJoseph Mingrone #define MACSEC_TCI_C       0x04 /* changed text */
68*ee67461eSJoseph Mingrone #define MACSEC_AN_MASK     0x03 /* association number */
69*ee67461eSJoseph Mingrone #define MACSEC_TCI_FLAGS   (MACSEC_TCI_ES | MACSEC_TCI_SC | MACSEC_TCI_SCB | MACSEC_TCI_E | MACSEC_TCI_C)
70*ee67461eSJoseph Mingrone #define MACSEC_TCI_CONFID  (MACSEC_TCI_E | MACSEC_TCI_C)
71*ee67461eSJoseph Mingrone #define MACSEC_SL_MASK     0x3F /* short length */
72*ee67461eSJoseph Mingrone 
73*ee67461eSJoseph Mingrone #define MACSEC_SECTAG_LEN_NOSCI 6  /* length of MACsec header without SCI */
74*ee67461eSJoseph Mingrone #define MACSEC_SECTAG_LEN_SCI   14 /* length of MACsec header with SCI */
75*ee67461eSJoseph Mingrone 
76*ee67461eSJoseph Mingrone #define SCI_FMT "%016" PRIx64
77*ee67461eSJoseph Mingrone 
78*ee67461eSJoseph Mingrone static const struct tok macsec_flag_values[] = {
79*ee67461eSJoseph Mingrone 	{ MACSEC_TCI_E,   "E" },
80*ee67461eSJoseph Mingrone 	{ MACSEC_TCI_C,   "C" },
81*ee67461eSJoseph Mingrone 	{ MACSEC_TCI_ES,  "S" },
82*ee67461eSJoseph Mingrone 	{ MACSEC_TCI_SCB, "B" },
83*ee67461eSJoseph Mingrone 	{ MACSEC_TCI_SC,  "I" },
84*ee67461eSJoseph Mingrone 	{ 0, NULL }
85*ee67461eSJoseph Mingrone };
86*ee67461eSJoseph Mingrone 
macsec_print_header(netdissect_options * ndo,const struct macsec_sectag * sectag,u_int short_length)87*ee67461eSJoseph Mingrone static void macsec_print_header(netdissect_options *ndo,
88*ee67461eSJoseph Mingrone 				const struct macsec_sectag *sectag,
89*ee67461eSJoseph Mingrone 				u_int short_length)
90*ee67461eSJoseph Mingrone {
91*ee67461eSJoseph Mingrone 	ND_PRINT("an %u, pn %u, flags %s",
92*ee67461eSJoseph Mingrone 		 GET_U_1(sectag->tci_an) & MACSEC_AN_MASK,
93*ee67461eSJoseph Mingrone 		 GET_BE_U_4(sectag->packet_number),
94*ee67461eSJoseph Mingrone 		 bittok2str_nosep(macsec_flag_values, "none",
95*ee67461eSJoseph Mingrone 				  GET_U_1(sectag->tci_an) & MACSEC_TCI_FLAGS));
96*ee67461eSJoseph Mingrone 
97*ee67461eSJoseph Mingrone 	if (short_length != 0)
98*ee67461eSJoseph Mingrone 		ND_PRINT(", sl %u", short_length);
99*ee67461eSJoseph Mingrone 
100*ee67461eSJoseph Mingrone 	if (GET_U_1(sectag->tci_an) & MACSEC_TCI_SC)
101*ee67461eSJoseph Mingrone 		ND_PRINT(", sci " SCI_FMT, GET_BE_U_8(sectag->secure_channel_id));
102*ee67461eSJoseph Mingrone 
103*ee67461eSJoseph Mingrone 	ND_PRINT(", ");
104*ee67461eSJoseph Mingrone }
105*ee67461eSJoseph Mingrone 
106*ee67461eSJoseph Mingrone /* returns < 0 iff the packet can be decoded completely */
macsec_print(netdissect_options * ndo,const u_char ** bp,u_int * lengthp,u_int * caplenp,u_int * hdrlenp,const struct lladdr_info * src,const struct lladdr_info * dst)107*ee67461eSJoseph Mingrone int macsec_print(netdissect_options *ndo, const u_char **bp,
108*ee67461eSJoseph Mingrone 		 u_int *lengthp, u_int *caplenp, u_int *hdrlenp,
109*ee67461eSJoseph Mingrone 		 const struct lladdr_info *src, const struct lladdr_info *dst)
110*ee67461eSJoseph Mingrone {
111*ee67461eSJoseph Mingrone 	const char *save_protocol;
112*ee67461eSJoseph Mingrone 	const u_char *p = *bp;
113*ee67461eSJoseph Mingrone 	u_int length = *lengthp;
114*ee67461eSJoseph Mingrone 	u_int caplen = *caplenp;
115*ee67461eSJoseph Mingrone 	u_int hdrlen = *hdrlenp;
116*ee67461eSJoseph Mingrone 	const struct macsec_sectag *sectag = (const struct macsec_sectag *)p;
117*ee67461eSJoseph Mingrone 	u_int sectag_len;
118*ee67461eSJoseph Mingrone 	u_int short_length;
119*ee67461eSJoseph Mingrone 
120*ee67461eSJoseph Mingrone 	save_protocol = ndo->ndo_protocol;
121*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "macsec";
122*ee67461eSJoseph Mingrone 
123*ee67461eSJoseph Mingrone 	/* we need the full MACsec header in the capture */
124*ee67461eSJoseph Mingrone 	if (caplen < MACSEC_SECTAG_LEN_NOSCI) {
125*ee67461eSJoseph Mingrone 		nd_print_trunc(ndo);
126*ee67461eSJoseph Mingrone 		ndo->ndo_protocol = save_protocol;
127*ee67461eSJoseph Mingrone 		return hdrlen + caplen;
128*ee67461eSJoseph Mingrone 	}
129*ee67461eSJoseph Mingrone 	if (length < MACSEC_SECTAG_LEN_NOSCI) {
130*ee67461eSJoseph Mingrone 		nd_print_trunc(ndo);
131*ee67461eSJoseph Mingrone 		ndo->ndo_protocol = save_protocol;
132*ee67461eSJoseph Mingrone 		return hdrlen + caplen;
133*ee67461eSJoseph Mingrone 	}
134*ee67461eSJoseph Mingrone 
135*ee67461eSJoseph Mingrone 	if (GET_U_1(sectag->tci_an) & MACSEC_TCI_SC) {
136*ee67461eSJoseph Mingrone 		sectag_len = MACSEC_SECTAG_LEN_SCI;
137*ee67461eSJoseph Mingrone 		if (caplen < MACSEC_SECTAG_LEN_SCI) {
138*ee67461eSJoseph Mingrone 			nd_print_trunc(ndo);
139*ee67461eSJoseph Mingrone 			ndo->ndo_protocol = save_protocol;
140*ee67461eSJoseph Mingrone 			return hdrlen + caplen;
141*ee67461eSJoseph Mingrone 		}
142*ee67461eSJoseph Mingrone 		if (length < MACSEC_SECTAG_LEN_SCI) {
143*ee67461eSJoseph Mingrone 			nd_print_trunc(ndo);
144*ee67461eSJoseph Mingrone 			ndo->ndo_protocol = save_protocol;
145*ee67461eSJoseph Mingrone 			return hdrlen + caplen;
146*ee67461eSJoseph Mingrone 		}
147*ee67461eSJoseph Mingrone 	} else
148*ee67461eSJoseph Mingrone 		sectag_len = MACSEC_SECTAG_LEN_NOSCI;
149*ee67461eSJoseph Mingrone 
150*ee67461eSJoseph Mingrone 	if ((GET_U_1(sectag->short_length) & ~MACSEC_SL_MASK) != 0 ||
151*ee67461eSJoseph Mingrone 	    GET_U_1(sectag->tci_an) & MACSEC_TCI_VERSION) {
152*ee67461eSJoseph Mingrone 		nd_print_invalid(ndo);
153*ee67461eSJoseph Mingrone 		ndo->ndo_protocol = save_protocol;
154*ee67461eSJoseph Mingrone 		return hdrlen + caplen;
155*ee67461eSJoseph Mingrone 	}
156*ee67461eSJoseph Mingrone 
157*ee67461eSJoseph Mingrone 	short_length = GET_U_1(sectag->short_length) & MACSEC_SL_MASK;
158*ee67461eSJoseph Mingrone 	if (ndo->ndo_eflag)
159*ee67461eSJoseph Mingrone 		macsec_print_header(ndo, sectag, short_length);
160*ee67461eSJoseph Mingrone 
161*ee67461eSJoseph Mingrone 	/* Skip the MACsec header. */
162*ee67461eSJoseph Mingrone 	*bp += sectag_len;
163*ee67461eSJoseph Mingrone 	*hdrlenp += sectag_len;
164*ee67461eSJoseph Mingrone 
165*ee67461eSJoseph Mingrone 	/* Remove it from the lengths, as it's been processed. */
166*ee67461eSJoseph Mingrone 	*lengthp -= sectag_len;
167*ee67461eSJoseph Mingrone 	*caplenp -= sectag_len;
168*ee67461eSJoseph Mingrone 
169*ee67461eSJoseph Mingrone 	if ((GET_U_1(sectag->tci_an) & MACSEC_TCI_CONFID)) {
170*ee67461eSJoseph Mingrone 		/*
171*ee67461eSJoseph Mingrone 		 * The payload is encrypted.  Print link-layer
172*ee67461eSJoseph Mingrone 		 * information, if it hasn't already been printed.
173*ee67461eSJoseph Mingrone 		 */
174*ee67461eSJoseph Mingrone 		if (!ndo->ndo_eflag) {
175*ee67461eSJoseph Mingrone 			/*
176*ee67461eSJoseph Mingrone 			 * Nobody printed the link-layer addresses,
177*ee67461eSJoseph Mingrone 			 * so print them, if we have any.
178*ee67461eSJoseph Mingrone 			 */
179*ee67461eSJoseph Mingrone 			if (src != NULL && dst != NULL) {
180*ee67461eSJoseph Mingrone 				ND_PRINT("%s > %s ",
181*ee67461eSJoseph Mingrone 					(src->addr_string)(ndo, src->addr),
182*ee67461eSJoseph Mingrone 					(dst->addr_string)(ndo, dst->addr));
183*ee67461eSJoseph Mingrone 			}
184*ee67461eSJoseph Mingrone 
185*ee67461eSJoseph Mingrone 			ND_PRINT("802.1AE MACsec, ");
186*ee67461eSJoseph Mingrone 
187*ee67461eSJoseph Mingrone 			/*
188*ee67461eSJoseph Mingrone 			 * Print the MACsec header.
189*ee67461eSJoseph Mingrone 			 */
190*ee67461eSJoseph Mingrone 			macsec_print_header(ndo, sectag, short_length);
191*ee67461eSJoseph Mingrone 		}
192*ee67461eSJoseph Mingrone 
193*ee67461eSJoseph Mingrone 		/*
194*ee67461eSJoseph Mingrone 		 * Tell our caller it can't be dissected.
195*ee67461eSJoseph Mingrone 		 */
196*ee67461eSJoseph Mingrone 		ndo->ndo_protocol = save_protocol;
197*ee67461eSJoseph Mingrone 		return 0;
198*ee67461eSJoseph Mingrone 	}
199*ee67461eSJoseph Mingrone 
200*ee67461eSJoseph Mingrone 	/*
201*ee67461eSJoseph Mingrone 	 * The payload isn't encrypted; remove the
202*ee67461eSJoseph Mingrone 	 * ICV length from the lengths, so our caller
203*ee67461eSJoseph Mingrone 	 * doesn't treat it as payload.
204*ee67461eSJoseph Mingrone 	 */
205*ee67461eSJoseph Mingrone 	if (*lengthp < MACSEC_DEFAULT_ICV_LEN) {
206*ee67461eSJoseph Mingrone 		nd_print_trunc(ndo);
207*ee67461eSJoseph Mingrone 		ndo->ndo_protocol = save_protocol;
208*ee67461eSJoseph Mingrone 		return hdrlen + caplen;
209*ee67461eSJoseph Mingrone 	}
210*ee67461eSJoseph Mingrone 	if (*caplenp < MACSEC_DEFAULT_ICV_LEN) {
211*ee67461eSJoseph Mingrone 		nd_print_trunc(ndo);
212*ee67461eSJoseph Mingrone 		ndo->ndo_protocol = save_protocol;
213*ee67461eSJoseph Mingrone 		return hdrlen + caplen;
214*ee67461eSJoseph Mingrone 	}
215*ee67461eSJoseph Mingrone 	*lengthp -= MACSEC_DEFAULT_ICV_LEN;
216*ee67461eSJoseph Mingrone 	*caplenp -= MACSEC_DEFAULT_ICV_LEN;
217*ee67461eSJoseph Mingrone 	/*
218*ee67461eSJoseph Mingrone 	 * Update the snapend thus the ICV field is not in the payload for
219*ee67461eSJoseph Mingrone 	 * the caller.
220*ee67461eSJoseph Mingrone 	 * The ICV (Integrity Check Value) is at the end of the frame, after
221*ee67461eSJoseph Mingrone 	 * the secure data.
222*ee67461eSJoseph Mingrone 	 */
223*ee67461eSJoseph Mingrone 	ndo->ndo_snapend -= MACSEC_DEFAULT_ICV_LEN;
224*ee67461eSJoseph Mingrone 
225*ee67461eSJoseph Mingrone 	/*
226*ee67461eSJoseph Mingrone 	 * If the SL field is non-zero, then it's the length of the
227*ee67461eSJoseph Mingrone 	 * Secure Data; otherwise, the Secure Data is what's left
228*ee67461eSJoseph Mingrone 	 * ver after the MACsec header and ICV are removed.
229*ee67461eSJoseph Mingrone 	 */
230*ee67461eSJoseph Mingrone 	if (short_length != 0) {
231*ee67461eSJoseph Mingrone 		/*
232*ee67461eSJoseph Mingrone 		 * If the short length is more than we *have*,
233*ee67461eSJoseph Mingrone 		 * that's an error.
234*ee67461eSJoseph Mingrone 		 */
235*ee67461eSJoseph Mingrone 		if (short_length > *lengthp) {
236*ee67461eSJoseph Mingrone 			nd_print_trunc(ndo);
237*ee67461eSJoseph Mingrone 			ndo->ndo_protocol = save_protocol;
238*ee67461eSJoseph Mingrone 			return hdrlen + caplen;
239*ee67461eSJoseph Mingrone 		}
240*ee67461eSJoseph Mingrone 		if (short_length > *caplenp) {
241*ee67461eSJoseph Mingrone 			nd_print_trunc(ndo);
242*ee67461eSJoseph Mingrone 			ndo->ndo_protocol = save_protocol;
243*ee67461eSJoseph Mingrone 			return hdrlen + caplen;
244*ee67461eSJoseph Mingrone 		}
245*ee67461eSJoseph Mingrone 		if (*lengthp > short_length)
246*ee67461eSJoseph Mingrone 			*lengthp = short_length;
247*ee67461eSJoseph Mingrone 		if (*caplenp > short_length)
248*ee67461eSJoseph Mingrone 			*caplenp = short_length;
249*ee67461eSJoseph Mingrone 	}
250*ee67461eSJoseph Mingrone 
251*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = save_protocol;
252*ee67461eSJoseph Mingrone 	return -1;
253*ee67461eSJoseph Mingrone }
254