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