]> The Tcpdump Group git mirrors - tcpdump/blob - print-macsec.c
More GET_ macros.
[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 <string.h>
31
32 #include "netdissect.h"
33 #include "addrtoname.h"
34 #include "ether.h"
35 #include "ethertype.h"
36 #include "extract.h"
37
38 static const char tstr[] = "[|MACsec]";
39
40 #define MACSEC_DEFAULT_ICV_LEN 16
41
42 /* Header format (SecTAG), following an Ethernet header
43 * IEEE 802.1AE-2006 9.3
44 *
45 * +---------------------------------+----------------+----------------+
46 * | (MACsec ethertype) | TCI_AN | SL |
47 * +---------------------------------+----------------+----------------+
48 * | Packet Number |
49 * +-------------------------------------------------------------------+
50 * | Secure Channel Identifier |
51 * | (optional) |
52 * +-------------------------------------------------------------------+
53 *
54 * MACsec ethertype = 0x88e5
55 * TCI: Tag Control Information, set of flags
56 * AN: association number, 2 bits
57 * SL (short length): 6-bit length of the protected payload, if < 48
58 * Packet Number: 32-bits packet identifier
59 * Secure Channel Identifier: 64-bit unique identifier, usually
60 * composed of a MAC address + 16-bit port number
61 */
62 struct macsec_sectag {
63 nd_uint8_t tci_an;
64 nd_uint8_t short_length;
65 nd_uint32_t packet_number;
66 nd_uint8_t secure_channel_id[8]; /* optional */
67 };
68
69 /* IEEE 802.1AE-2006 9.5 */
70 #define MACSEC_TCI_VERSION 0x80
71 #define MACSEC_TCI_ES 0x40 /* end station */
72 #define MACSEC_TCI_SC 0x20 /* SCI present */
73 #define MACSEC_TCI_SCB 0x10 /* epon */
74 #define MACSEC_TCI_E 0x08 /* encryption */
75 #define MACSEC_TCI_C 0x04 /* changed text */
76 #define MACSEC_AN_MASK 0x03 /* association number */
77 #define MACSEC_TCI_FLAGS (MACSEC_TCI_ES | MACSEC_TCI_SC | MACSEC_TCI_SCB | MACSEC_TCI_E | MACSEC_TCI_C)
78 #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
79 #define MACSEC_SL_MASK 0x3F /* short length */
80
81 #define MACSEC_SECTAG_LEN_NOSCI 6
82 #define MACSEC_SECTAG_LEN_SCI 14
83 static int
84 ieee8021ae_sectag_len(netdissect_options *ndo, const struct macsec_sectag *sectag)
85 {
86 return (GET_U_1(sectag->tci_an) & MACSEC_TCI_SC) ?
87 MACSEC_SECTAG_LEN_SCI :
88 MACSEC_SECTAG_LEN_NOSCI;
89 }
90
91 static int macsec_check_length(netdissect_options *ndo, cconst struct macsec_sectag *sectag, u_int length, u_int caplen)
92 {
93 u_int len;
94
95 /* we need the full MACsec header in the capture */
96 if (caplen < (MACSEC_SECTAG_LEN_NOSCI + 2))
97 return 0;
98
99 len = ieee8021ae_sectag_len(ndo, sectag);
100 if (caplen < (len + 2))
101 return 0;
102
103 if ((GET_U_1(sectag->short_length) & MACSEC_SL_MASK) != 0) {
104 /* original packet must have exact length */
105 u_int exact = ETHER_HDRLEN + len + 2 + (GET_U_1(sectag->short_length) & MACSEC_SL_MASK);
106 return exact == length;
107 } else {
108 /* original packet must not be short */
109 u_int minlen = ETHER_HDRLEN + len + 2 + 48;
110 return length >= minlen;
111 }
112
113 return 1;
114 }
115
116 #define SCI_FMT "%016" PRIx64
117
118 static const struct tok macsec_flag_values[] = {
119 { MACSEC_TCI_E, "E" },
120 { MACSEC_TCI_C, "C" },
121 { MACSEC_TCI_ES, "S" },
122 { MACSEC_TCI_SCB, "B" },
123 { MACSEC_TCI_SC, "I" },
124 { 0, NULL }
125 };
126
127 /* returns < 0 iff the packet can be decoded completely */
128 int macsec_print(netdissect_options *ndo, const u_char **bp,
129 u_int *lengthp, u_int *caplenp, u_int *hdrlenp,
130 u_short *length_type)
131 {
132 const u_char *p = *bp;
133 u_int length = *lengthp;
134 u_int caplen = *caplenp;
135 u_int hdrlen = *hdrlenp;
136 const struct macsec_sectag *sectag = (const struct macsec_sectag *)p;
137 u_int len;
138
139 if (!macsec_check_length(sectag, length, caplen)) {
140 ND_PRINT((ndo, tstr));
141 return hdrlen + caplen;
142 }
143
144 if (sectag->unused || sectag->tci_an & MACSEC_TCI_VERSION) {
145 ND_PRINT((ndo, "%s", istr));
146 return hdrlen + caplen;
147 }
148
149 if (ndo->ndo_eflag) {
150 char buf[128];
151 int n = snprintf(buf, sizeof(buf), "an %u, pn %u, flags %s",
152 GET_U_1(sectag->tci_an) & MACSEC_AN_MASK,
153 GET_BE_U_4(sectag->packet_number),
154 bittok2str_nosep(macsec_flag_values, "none",
155 GET_U_1(sectag->tci_an) & MACSEC_TCI_FLAGS));
156 if (n < 0)
157 return hdrlen + caplen;
158
159
160 if ((GET_U_1(sectag->short_length) & MACSEC_SL_MASK) != 0 {
161 int r = snprintf(buf + n, sizeof(buf) - n, ", sl %u",
162 GET_U_1(sectag->short_length) & MACSEC_SL_MASK);
163 if (r < 0)
164 return hdrlen + caplen;
165 n += r;
166 }
167
168 if (GET_U_1(sectag->tci_an) & MACSEC_TCI_SC) {
169 uint64_t sci;
170 int r;
171 sci = GET_BE_U_8(sectag->secure_channel_id);
172 r = snprintf(buf + n, sizeof(buf) - n, ", sci " SCI_FMT, sci);
173 if (r < 0)
174 return hdrlen + caplen;
175 n += r;
176 }
177
178 ND_PRINT((ndo, "%s, ", buf));
179 }
180
181 len = ieee8021ae_sectag_len(ndo, sectag);
182 *length_type = GET_BE_U_2(*bp + len);
183 if (ndo->ndo_eflag && *length_type > ETHERMTU && !(GET_U_1(sectag->tci_an) & MACSEC_TCI_E))
184 ND_PRINT((ndo, "ethertype %s, ", tok2str(ethertype_values,"0x%04x", *length_type)));
185
186 if ((GET_U_1(sectag->tci_an) & MACSEC_TCI_CONFID)) {
187 *bp += len;
188 *hdrlenp += len;
189
190 *lengthp -= len;
191 *caplenp -= len;
192 return 0;
193 } else {
194 len += 2;
195 *bp += len;
196 *hdrlenp += len;
197
198 len += MACSEC_DEFAULT_ICV_LEN;
199 *lengthp -= len;
200 *caplenp -= len;
201 return -1;
202 }
203 }