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