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>
32 #include "netdissect.h"
33 #include "addrtoname.h"
34 #include "ethertype.h"
37 #define MACSEC_DEFAULT_ICV_LEN 16
39 /* Header format (SecTAG), following an Ethernet header
40 * IEEE 802.1AE-2006 9.3
42 * +---------------------------------+----------------+----------------+
43 * | (MACsec ethertype) | TCI_AN | SL |
44 * +---------------------------------+----------------+----------------+
46 * +-------------------------------------------------------------------+
47 * | Secure Channel Identifier |
49 * +-------------------------------------------------------------------+
51 * MACsec ethertype = 0x88e5
52 * TCI: Tag Control Information, set of flags
53 * AN: association number, 2 bits
54 * SL (short length): 6-bit length of the protected payload, if < 48
55 * Packet Number: 32-bits packet identifier
56 * Secure Channel Identifier: 64-bit unique identifier, usually
57 * composed of a MAC address + 16-bit port number
59 struct macsec_sectag
{
61 nd_uint8_t short_length
;
62 nd_uint32_t packet_number
;
63 nd_uint8_t secure_channel_id
[8]; /* optional */
66 /* IEEE 802.1AE-2006 9.5 */
67 #define MACSEC_TCI_VERSION 0x80
68 #define MACSEC_TCI_ES 0x40 /* end station */
69 #define MACSEC_TCI_SC 0x20 /* SCI present */
70 #define MACSEC_TCI_SCB 0x10 /* epon */
71 #define MACSEC_TCI_E 0x08 /* encryption */
72 #define MACSEC_TCI_C 0x04 /* changed text */
73 #define MACSEC_AN_MASK 0x03 /* association number */
74 #define MACSEC_TCI_FLAGS (MACSEC_TCI_ES | MACSEC_TCI_SC | MACSEC_TCI_SCB | MACSEC_TCI_E | MACSEC_TCI_C)
75 #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
76 #define MACSEC_SL_MASK 0x3F /* short length */
78 #define MACSEC_SECTAG_LEN_NOSCI 6 /* length of MACsec header without SCI */
79 #define MACSEC_SECTAG_LEN_SCI 14 /* length of MACsec header with SCI */
81 #define SCI_FMT "%016" PRIx64
83 static const struct tok macsec_flag_values
[] = {
84 { MACSEC_TCI_E
, "E" },
85 { MACSEC_TCI_C
, "C" },
86 { MACSEC_TCI_ES
, "S" },
87 { MACSEC_TCI_SCB
, "B" },
88 { MACSEC_TCI_SC
, "I" },
92 /* returns < 0 iff the packet can be decoded completely */
93 int macsec_print(netdissect_options
*ndo
, const u_char
**bp
,
94 u_int
*lengthp
, u_int
*caplenp
, u_int
*hdrlenp
)
96 const char *save_protocol
;
97 const u_char
*p
= *bp
;
98 u_int length
= *lengthp
;
99 u_int caplen
= *caplenp
;
100 u_int hdrlen
= *hdrlenp
;
101 const struct macsec_sectag
*sectag
= (const struct macsec_sectag
*)p
;
105 save_protocol
= ndo
->ndo_protocol
;
106 ndo
->ndo_protocol
= "MACsec";
108 /* we need the full MACsec header in the capture */
109 if (caplen
< MACSEC_SECTAG_LEN_NOSCI
) {
111 ndo
->ndo_protocol
= save_protocol
;
112 return hdrlen
+ caplen
;
114 if (length
< MACSEC_SECTAG_LEN_NOSCI
) {
116 ndo
->ndo_protocol
= save_protocol
;
117 return hdrlen
+ caplen
;
120 if (GET_U_1(sectag
->tci_an
) & MACSEC_TCI_SC
) {
121 sectag_len
= MACSEC_SECTAG_LEN_SCI
;
122 if (caplen
< MACSEC_SECTAG_LEN_SCI
) {
124 ndo
->ndo_protocol
= save_protocol
;
125 return hdrlen
+ caplen
;
127 if (length
< MACSEC_SECTAG_LEN_SCI
) {
129 ndo
->ndo_protocol
= save_protocol
;
130 return hdrlen
+ caplen
;
133 sectag_len
= MACSEC_SECTAG_LEN_NOSCI
;
135 if ((GET_U_1(sectag
->short_length
) & ~MACSEC_SL_MASK
) != 0 ||
136 GET_U_1(sectag
->tci_an
) & MACSEC_TCI_VERSION
) {
137 nd_print_invalid(ndo
);
138 ndo
->ndo_protocol
= save_protocol
;
139 return hdrlen
+ caplen
;
142 short_length
= GET_U_1(sectag
->short_length
) & MACSEC_SL_MASK
;
143 if (ndo
->ndo_eflag
) {
144 ND_PRINT("an %u, pn %u, flags %s",
145 GET_U_1(sectag
->tci_an
) & MACSEC_AN_MASK
,
146 GET_BE_U_4(sectag
->packet_number
),
147 bittok2str_nosep(macsec_flag_values
, "none",
148 GET_U_1(sectag
->tci_an
) & MACSEC_TCI_FLAGS
));
150 if (short_length
!= 0)
151 ND_PRINT(", sl %u", short_length
);
153 if (GET_U_1(sectag
->tci_an
) & MACSEC_TCI_SC
)
154 ND_PRINT(", sci " SCI_FMT
, GET_BE_U_8(sectag
->secure_channel_id
));
159 /* Skip the MACsec header. */
161 *hdrlenp
+= sectag_len
;
163 /* Remove it from the lengths, as it's been processed. */
164 *lengthp
-= sectag_len
;
165 *caplenp
-= sectag_len
;
167 if ((GET_U_1(sectag
->tci_an
) & MACSEC_TCI_CONFID
)) {
169 * The payload is encrypted. Tell our
170 * caller it can't be dissected.
172 ndo
->ndo_protocol
= save_protocol
;
177 * The payload isn't encrypted; remove the
178 * ICV length from the lengths, so our caller
179 * doesn't treat it as payload.
181 if (*lengthp
< MACSEC_DEFAULT_ICV_LEN
) {
183 ndo
->ndo_protocol
= save_protocol
;
184 return hdrlen
+ caplen
;
186 if (*caplenp
< MACSEC_DEFAULT_ICV_LEN
) {
188 ndo
->ndo_protocol
= save_protocol
;
189 return hdrlen
+ caplen
;
191 *lengthp
-= MACSEC_DEFAULT_ICV_LEN
;
192 *caplenp
-= MACSEC_DEFAULT_ICV_LEN
;
195 * If the SL field is non-zero, then it's the length of the
196 * Secure Data; otherwise, the Secure Data is what's left
197 * ver after the MACsec header and ICV are removed.
199 if (short_length
!= 0) {
201 * If the short length is more than we *have*,
204 if (short_length
> *lengthp
) {
206 ndo
->ndo_protocol
= save_protocol
;
207 return hdrlen
+ caplen
;
209 if (short_length
> *caplenp
) {
211 ndo
->ndo_protocol
= save_protocol
;
212 return hdrlen
+ caplen
;
214 if (*lengthp
> short_length
)
215 *lengthp
= short_length
;
216 if (*caplenp
> short_length
)
217 *caplenp
= short_length
;
220 ndo
->ndo_protocol
= save_protocol
;