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