1 /* Copyright (c) 2017, Sabrina Dubroca <sd@queasysnail.net>
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
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
13 * 3. The names of the authors may not be used to endorse or promote
14 * products derived from this software without specific prior
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.
22 /* \summary: MACsec printer */
28 #include "netdissect-stdinc.h"
30 #include "netdissect.h"
31 #include "addrtoname.h"
34 #define MACSEC_DEFAULT_ICV_LEN 16
36 /* Header format (SecTAG), following an Ethernet header
37 * IEEE 802.1AE-2006 9.3
39 * +---------------------------------+----------------+----------------+
40 * | (MACsec ethertype) | TCI_AN | SL |
41 * +---------------------------------+----------------+----------------+
43 * +-------------------------------------------------------------------+
44 * | Secure Channel Identifier |
46 * +-------------------------------------------------------------------+
48 * MACsec ethertype = 0x88e5
49 * TCI: Tag Control Information, set of flags
50 * AN: association number, 2 bits
51 * SL (short length): 6-bit length of the protected payload, if < 48
52 * Packet Number: 32-bits packet identifier
53 * Secure Channel Identifier: 64-bit unique identifier, usually
54 * composed of a MAC address + 16-bit port number
56 struct macsec_sectag
{
58 nd_uint8_t short_length
;
59 nd_uint32_t packet_number
;
60 nd_uint8_t secure_channel_id
[8]; /* optional */
63 /* IEEE 802.1AE-2006 9.5 */
64 #define MACSEC_TCI_VERSION 0x80
65 #define MACSEC_TCI_ES 0x40 /* end station */
66 #define MACSEC_TCI_SC 0x20 /* SCI present */
67 #define MACSEC_TCI_SCB 0x10 /* epon */
68 #define MACSEC_TCI_E 0x08 /* encryption */
69 #define MACSEC_TCI_C 0x04 /* changed text */
70 #define MACSEC_AN_MASK 0x03 /* association number */
71 #define MACSEC_TCI_FLAGS (MACSEC_TCI_ES | MACSEC_TCI_SC | MACSEC_TCI_SCB | MACSEC_TCI_E | MACSEC_TCI_C)
72 #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
73 #define MACSEC_SL_MASK 0x3F /* short length */
75 #define MACSEC_SECTAG_LEN_NOSCI 6 /* length of MACsec header without SCI */
76 #define MACSEC_SECTAG_LEN_SCI 14 /* length of MACsec header with SCI */
78 #define SCI_FMT "%016" PRIx64
80 static const struct tok macsec_flag_values
[] = {
81 { MACSEC_TCI_E
, "E" },
82 { MACSEC_TCI_C
, "C" },
83 { MACSEC_TCI_ES
, "S" },
84 { MACSEC_TCI_SCB
, "B" },
85 { MACSEC_TCI_SC
, "I" },
90 macsec_print_header(netdissect_options
*ndo
,
91 const struct macsec_sectag
*sectag
,
94 ND_PRINT("an %u, pn %u, flags %s",
95 GET_U_1(sectag
->tci_an
) & MACSEC_AN_MASK
,
96 GET_BE_U_4(sectag
->packet_number
),
97 bittok2str_nosep(macsec_flag_values
, "none",
98 GET_U_1(sectag
->tci_an
) & MACSEC_TCI_FLAGS
));
100 if (short_length
!= 0)
101 ND_PRINT(", sl %u", short_length
);
103 if (GET_U_1(sectag
->tci_an
) & MACSEC_TCI_SC
)
104 ND_PRINT(", sci " SCI_FMT
, GET_BE_U_8(sectag
->secure_channel_id
));
109 /* returns < 0 iff the packet can be decoded completely */
111 macsec_print(netdissect_options
*ndo
, const u_char
**bp
,
112 u_int
*lengthp
, u_int
*caplenp
, u_int
*hdrlenp
,
113 const struct lladdr_info
*src
, const struct lladdr_info
*dst
)
115 const char *save_protocol
;
116 const u_char
*p
= *bp
;
117 u_int length
= *lengthp
;
118 u_int caplen
= *caplenp
;
119 u_int hdrlen
= *hdrlenp
;
120 const struct macsec_sectag
*sectag
= (const struct macsec_sectag
*)p
;
124 save_protocol
= ndo
->ndo_protocol
;
125 ndo
->ndo_protocol
= "macsec";
127 /* we need the full MACsec header in the capture */
128 if (caplen
< MACSEC_SECTAG_LEN_NOSCI
) {
130 ndo
->ndo_protocol
= save_protocol
;
131 return hdrlen
+ caplen
;
133 if (length
< MACSEC_SECTAG_LEN_NOSCI
) {
135 ndo
->ndo_protocol
= save_protocol
;
136 return hdrlen
+ caplen
;
139 if (GET_U_1(sectag
->tci_an
) & MACSEC_TCI_SC
) {
140 sectag_len
= MACSEC_SECTAG_LEN_SCI
;
141 if (caplen
< MACSEC_SECTAG_LEN_SCI
) {
143 ndo
->ndo_protocol
= save_protocol
;
144 return hdrlen
+ caplen
;
146 if (length
< MACSEC_SECTAG_LEN_SCI
) {
148 ndo
->ndo_protocol
= save_protocol
;
149 return hdrlen
+ caplen
;
152 sectag_len
= MACSEC_SECTAG_LEN_NOSCI
;
154 if ((GET_U_1(sectag
->short_length
) & ~MACSEC_SL_MASK
) != 0 ||
155 GET_U_1(sectag
->tci_an
) & MACSEC_TCI_VERSION
) {
156 nd_print_invalid(ndo
);
157 ndo
->ndo_protocol
= save_protocol
;
158 return hdrlen
+ caplen
;
161 short_length
= GET_U_1(sectag
->short_length
) & MACSEC_SL_MASK
;
163 macsec_print_header(ndo
, sectag
, short_length
);
165 /* Skip the MACsec header. */
167 *hdrlenp
+= sectag_len
;
169 /* Remove it from the lengths, as it's been processed. */
170 *lengthp
-= sectag_len
;
171 *caplenp
-= sectag_len
;
173 if ((GET_U_1(sectag
->tci_an
) & MACSEC_TCI_CONFID
)) {
175 * The payload is encrypted. Print link-layer
176 * information, if it hasn't already been printed.
178 if (!ndo
->ndo_eflag
) {
180 * Nobody printed the link-layer addresses,
181 * so print them, if we have any.
183 if (src
!= NULL
&& dst
!= NULL
) {
185 (src
->addr_string
)(ndo
, src
->addr
),
186 (dst
->addr_string
)(ndo
, dst
->addr
));
189 ND_PRINT("802.1AE MACsec, ");
192 * Print the MACsec header.
194 macsec_print_header(ndo
, sectag
, short_length
);
198 * Tell our caller it can't be dissected.
200 ndo
->ndo_protocol
= save_protocol
;
205 * The payload isn't encrypted; remove the
206 * ICV length from the lengths, so our caller
207 * doesn't treat it as payload.
209 if (*lengthp
< MACSEC_DEFAULT_ICV_LEN
) {
211 ndo
->ndo_protocol
= save_protocol
;
212 return hdrlen
+ caplen
;
214 if (*caplenp
< MACSEC_DEFAULT_ICV_LEN
) {
216 ndo
->ndo_protocol
= save_protocol
;
217 return hdrlen
+ caplen
;
219 *lengthp
-= MACSEC_DEFAULT_ICV_LEN
;
220 *caplenp
-= MACSEC_DEFAULT_ICV_LEN
;
222 * Update the snapend thus the ICV field is not in the payload for
224 * The ICV (Integrity Check Value) is at the end of the frame, after
227 ndo
->ndo_snapend
-= MACSEC_DEFAULT_ICV_LEN
;
230 * If the SL field is non-zero, then it's the length of the
231 * Secure Data; otherwise, the Secure Data is what's left
232 * ver after the MACsec header and ICV are removed.
234 if (short_length
!= 0) {
236 * If the short length is more than we *have*,
239 if (short_length
> *lengthp
) {
241 ndo
->ndo_protocol
= save_protocol
;
242 return hdrlen
+ caplen
;
244 if (short_length
> *caplenp
) {
246 ndo
->ndo_protocol
= save_protocol
;
247 return hdrlen
+ caplen
;
249 if (*lengthp
> short_length
)
250 *lengthp
= short_length
;
251 if (*caplenp
> short_length
)
252 *caplenp
= short_length
;
255 ndo
->ndo_protocol
= save_protocol
;