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