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"
32 #include "ethertype.h"
35 #define MACSEC_DEFAULT_ICV_LEN 16
37 /* Header format (SecTAG), following an Ethernet header
38 * IEEE 802.1AE-2006 9.3
40 * +---------------------------------+----------------+----------------+
41 * | (MACsec ethertype) | TCI_AN | SL |
42 * +---------------------------------+----------------+----------------+
44 * +-------------------------------------------------------------------+
45 * | Secure Channel Identifier |
47 * +-------------------------------------------------------------------+
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
57 struct macsec_sectag
{
59 nd_uint8_t short_length
;
60 nd_uint32_t packet_number
;
61 nd_uint8_t secure_channel_id
[8]; /* optional */
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 */
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 */
79 #define SCI_FMT "%016" PRIx64
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" },
90 static void 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 */
110 int macsec_print(netdissect_options
*ndo
, const u_char
**bp
,
111 u_int
*lengthp
, u_int
*caplenp
, u_int
*hdrlenp
,
112 const struct lladdr_info
*src
, const struct lladdr_info
*dst
)
114 const char *save_protocol
;
115 const u_char
*p
= *bp
;
116 u_int length
= *lengthp
;
117 u_int caplen
= *caplenp
;
118 u_int hdrlen
= *hdrlenp
;
119 const struct macsec_sectag
*sectag
= (const struct macsec_sectag
*)p
;
123 save_protocol
= ndo
->ndo_protocol
;
124 ndo
->ndo_protocol
= "macsec";
126 /* we need the full MACsec header in the capture */
127 if (caplen
< MACSEC_SECTAG_LEN_NOSCI
) {
129 ndo
->ndo_protocol
= save_protocol
;
130 return hdrlen
+ caplen
;
132 if (length
< MACSEC_SECTAG_LEN_NOSCI
) {
134 ndo
->ndo_protocol
= save_protocol
;
135 return hdrlen
+ caplen
;
138 if (GET_U_1(sectag
->tci_an
) & MACSEC_TCI_SC
) {
139 sectag_len
= MACSEC_SECTAG_LEN_SCI
;
140 if (caplen
< MACSEC_SECTAG_LEN_SCI
) {
142 ndo
->ndo_protocol
= save_protocol
;
143 return hdrlen
+ caplen
;
145 if (length
< MACSEC_SECTAG_LEN_SCI
) {
147 ndo
->ndo_protocol
= save_protocol
;
148 return hdrlen
+ caplen
;
151 sectag_len
= MACSEC_SECTAG_LEN_NOSCI
;
153 if ((GET_U_1(sectag
->short_length
) & ~MACSEC_SL_MASK
) != 0 ||
154 GET_U_1(sectag
->tci_an
) & MACSEC_TCI_VERSION
) {
155 nd_print_invalid(ndo
);
156 ndo
->ndo_protocol
= save_protocol
;
157 return hdrlen
+ caplen
;
160 short_length
= GET_U_1(sectag
->short_length
) & MACSEC_SL_MASK
;
162 macsec_print_header(ndo
, sectag
, short_length
);
164 /* Skip the MACsec header. */
166 *hdrlenp
+= sectag_len
;
168 /* Remove it from the lengths, as it's been processed. */
169 *lengthp
-= sectag_len
;
170 *caplenp
-= sectag_len
;
172 if ((GET_U_1(sectag
->tci_an
) & MACSEC_TCI_CONFID
)) {
174 * The payload is encrypted. Print link-layer
175 * information, if it hasn't already been printed.
177 if (!ndo
->ndo_eflag
) {
179 * Nobody printed the link-layer addresses,
180 * so print them, if we have any.
182 if (src
!= NULL
&& dst
!= NULL
) {
184 (src
->addr_string
)(ndo
, src
->addr
),
185 (dst
->addr_string
)(ndo
, dst
->addr
));
188 ND_PRINT("802.1AE MACsec, ");
191 * Print the MACsec header.
193 macsec_print_header(ndo
, sectag
, short_length
);
197 * Tell our caller it can't be dissected.
199 ndo
->ndo_protocol
= save_protocol
;
204 * The payload isn't encrypted; remove the
205 * ICV length from the lengths, so our caller
206 * doesn't treat it as payload.
208 if (*lengthp
< MACSEC_DEFAULT_ICV_LEN
) {
210 ndo
->ndo_protocol
= save_protocol
;
211 return hdrlen
+ caplen
;
213 if (*caplenp
< MACSEC_DEFAULT_ICV_LEN
) {
215 ndo
->ndo_protocol
= save_protocol
;
216 return hdrlen
+ caplen
;
218 *lengthp
-= MACSEC_DEFAULT_ICV_LEN
;
219 *caplenp
-= MACSEC_DEFAULT_ICV_LEN
;
222 * If the SL field is non-zero, then it's the length of the
223 * Secure Data; otherwise, the Secure Data is what's left
224 * ver after the MACsec header and ICV are removed.
226 if (short_length
!= 0) {
228 * If the short length is more than we *have*,
231 if (short_length
> *lengthp
) {
233 ndo
->ndo_protocol
= save_protocol
;
234 return hdrlen
+ caplen
;
236 if (short_length
> *caplenp
) {
238 ndo
->ndo_protocol
= save_protocol
;
239 return hdrlen
+ caplen
;
241 if (*lengthp
> short_length
)
242 *lengthp
= short_length
;
243 if (*caplenp
> short_length
)
244 *caplenp
= short_length
;
247 ndo
->ndo_protocol
= save_protocol
;