10f74e101Schristos /* 20f74e101Schristos * Copyright (c) 1991, 1992, 1993, 1994, 1995, 1996, 1997 30f74e101Schristos * The Regents of the University of California. All rights reserved. 40f74e101Schristos * 50f74e101Schristos * Redistribution and use in source and binary forms, with or without 60f74e101Schristos * modification, are permitted provided that: (1) source code distributions 70f74e101Schristos * retain the above copyright notice and this paragraph in its entirety, (2) 80f74e101Schristos * distributions including binary code include the above copyright notice and 90f74e101Schristos * this paragraph in its entirety in the documentation or other materials 100f74e101Schristos * provided with the distribution, and (3) all advertising materials mentioning 110f74e101Schristos * features or use of this software display the following acknowledgement: 120f74e101Schristos * ``This product includes software developed by the University of California, 130f74e101Schristos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 140f74e101Schristos * the University nor the names of its contributors may be used to endorse 150f74e101Schristos * or promote products derived from this software without specific prior 160f74e101Schristos * written permission. 170f74e101Schristos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 180f74e101Schristos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 190f74e101Schristos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 200f74e101Schristos */ 210f74e101Schristos 2211b3aaa1Schristos #include <sys/cdefs.h> 230f74e101Schristos #ifndef lint 24*26ba0b50Schristos __RCSID("$NetBSD: print-fddi.c,v 1.8 2024/09/02 16:15:31 christos Exp $"); 250f74e101Schristos #endif 260f74e101Schristos 27dc860a36Sspz /* \summary: Fiber Distributed Data Interface (FDDI) printer */ 28dc860a36Sspz 29c74ad251Schristos #include <config.h> 300f74e101Schristos 31c74ad251Schristos #include "netdissect-stdinc.h" 320f74e101Schristos 330f74e101Schristos #include <string.h> 340f74e101Schristos 35fdccd7e4Schristos #include "netdissect.h" 36c74ad251Schristos #include "extract.h" 370f74e101Schristos #include "addrtoname.h" 38b3a00663Schristos 39b3a00663Schristos /* 40b3a00663Schristos * Based on Ultrix if_fddi.h 41b3a00663Schristos */ 42b3a00663Schristos 43b3a00663Schristos struct fddi_header { 44c74ad251Schristos nd_uint8_t fddi_fc; /* frame control */ 45c74ad251Schristos nd_mac_addr fddi_dhost; 46c74ad251Schristos nd_mac_addr fddi_shost; 47b3a00663Schristos }; 48b3a00663Schristos 49b3a00663Schristos /* 50b3a00663Schristos * Length of an FDDI header; note that some compilers may pad 51b3a00663Schristos * "struct fddi_header" to a multiple of 4 bytes, for example, so 52b3a00663Schristos * "sizeof (struct fddi_header)" may not give the right 53b3a00663Schristos * answer. 54b3a00663Schristos */ 55b3a00663Schristos #define FDDI_HDRLEN 13 56b3a00663Schristos 57b3a00663Schristos /* Useful values for fddi_fc (frame control) field */ 58b3a00663Schristos 59b3a00663Schristos /* 60b3a00663Schristos * FDDI Frame Control bits 61b3a00663Schristos */ 62b3a00663Schristos #define FDDIFC_C 0x80 /* Class bit */ 63b3a00663Schristos #define FDDIFC_L 0x40 /* Address length bit */ 64b3a00663Schristos #define FDDIFC_F 0x30 /* Frame format bits */ 65b3a00663Schristos #define FDDIFC_Z 0x0f /* Control bits */ 66b3a00663Schristos 67b3a00663Schristos /* 68b3a00663Schristos * FDDI Frame Control values. (48-bit addressing only). 69b3a00663Schristos */ 70b3a00663Schristos #define FDDIFC_VOID 0x40 /* Void frame */ 71b3a00663Schristos #define FDDIFC_NRT 0x80 /* Nonrestricted token */ 72b3a00663Schristos #define FDDIFC_RT 0xc0 /* Restricted token */ 73b3a00663Schristos #define FDDIFC_SMT_INFO 0x41 /* SMT Info */ 74b3a00663Schristos #define FDDIFC_SMT_NSA 0x4F /* SMT Next station adrs */ 75b3a00663Schristos #define FDDIFC_MAC_BEACON 0xc2 /* MAC Beacon frame */ 76b3a00663Schristos #define FDDIFC_MAC_CLAIM 0xc3 /* MAC Claim frame */ 77b3a00663Schristos #define FDDIFC_LLC_ASYNC 0x50 /* Async. LLC frame */ 78b3a00663Schristos #define FDDIFC_LLC_SYNC 0xd0 /* Sync. LLC frame */ 79b3a00663Schristos #define FDDIFC_IMP_ASYNC 0x60 /* Implementor Async. */ 80b3a00663Schristos #define FDDIFC_IMP_SYNC 0xe0 /* Implementor Synch. */ 81b3a00663Schristos #define FDDIFC_SMT 0x40 /* SMT frame */ 82b3a00663Schristos #define FDDIFC_MAC 0xc0 /* MAC frame */ 83b3a00663Schristos 84b3a00663Schristos #define FDDIFC_CLFF 0xF0 /* Class/Length/Format bits */ 85b3a00663Schristos #define FDDIFC_ZZZZ 0x0F /* Control bits */ 860f74e101Schristos 870f74e101Schristos /* 880f74e101Schristos * Some FDDI interfaces use bit-swapped addresses. 890f74e101Schristos */ 900f74e101Schristos #if defined(ultrix) || defined(__alpha) || defined(__bsdi) || defined(__NetBSD__) || defined(__linux__) 91dc860a36Sspz static int fddi_bitswap = 0; 920f74e101Schristos #else 93dc860a36Sspz static int fddi_bitswap = 1; 940f74e101Schristos #endif 950f74e101Schristos 960f74e101Schristos /* 970f74e101Schristos * FDDI support for tcpdump, by Jeffrey Mogul [DECWRL], June 1992 980f74e101Schristos * 990f74e101Schristos * Based in part on code by Van Jacobson, which bears this note: 1000f74e101Schristos * 1010f74e101Schristos * NOTE: This is a very preliminary hack for FDDI support. 1020f74e101Schristos * There are all sorts of wired in constants & nothing (yet) 1030f74e101Schristos * to print SMT packets as anything other than hex dumps. 1040f74e101Schristos * Most of the necessary changes are waiting on my redoing 1050f74e101Schristos * the "header" that a kernel fddi driver supplies to bpf: I 1060f74e101Schristos * want it to look like one byte of 'direction' (0 or 1 1070f74e101Schristos * depending on whether the packet was inbound or outbound), 1080f74e101Schristos * two bytes of system/driver dependent data (anything an 1090f74e101Schristos * implementor thinks would be useful to filter on and/or 1100f74e101Schristos * save per-packet, then the real 21-byte FDDI header. 1110f74e101Schristos * Steve McCanne & I have also talked about adding the 1120f74e101Schristos * 'direction' byte to all bpf headers (e.g., in the two 1130f74e101Schristos * bytes of padding on an ethernet header). It's not clear 1140f74e101Schristos * we could do this in a backwards compatible way & we hate 1150f74e101Schristos * the idea of an incompatible bpf change. Discussions are 1160f74e101Schristos * proceeding. 1170f74e101Schristos * 1180f74e101Schristos * Also, to really support FDDI (and better support 802.2 1190f74e101Schristos * over ethernet) we really need to re-think the rather simple 1200f74e101Schristos * minded assumptions about fixed length & fixed format link 1210f74e101Schristos * level headers made in gencode.c. One day... 1220f74e101Schristos * 1230f74e101Schristos * - vj 1240f74e101Schristos */ 1250f74e101Schristos 126b3a00663Schristos static const u_char fddi_bit_swap[] = { 1270f74e101Schristos 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 1280f74e101Schristos 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, 1290f74e101Schristos 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 1300f74e101Schristos 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 1310f74e101Schristos 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 1320f74e101Schristos 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, 1330f74e101Schristos 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 1340f74e101Schristos 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, 1350f74e101Schristos 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 1360f74e101Schristos 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 1370f74e101Schristos 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 1380f74e101Schristos 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, 1390f74e101Schristos 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 1400f74e101Schristos 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, 1410f74e101Schristos 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 1420f74e101Schristos 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 1430f74e101Schristos 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 1440f74e101Schristos 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, 1450f74e101Schristos 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 1460f74e101Schristos 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, 1470f74e101Schristos 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 1480f74e101Schristos 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 1490f74e101Schristos 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 1500f74e101Schristos 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, 1510f74e101Schristos 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 1520f74e101Schristos 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, 1530f74e101Schristos 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 1540f74e101Schristos 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 1550f74e101Schristos 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 1560f74e101Schristos 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, 1570f74e101Schristos 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 1580f74e101Schristos 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff, 1590f74e101Schristos }; 1600f74e101Schristos 1610f74e101Schristos /* 1620f74e101Schristos * Print FDDI frame-control bits 1630f74e101Schristos */ 164c74ad251Schristos static void 165b3a00663Schristos print_fddi_fc(netdissect_options *ndo, u_char fc) 1660f74e101Schristos { 1670f74e101Schristos switch (fc) { 1680f74e101Schristos 1690f74e101Schristos case FDDIFC_VOID: /* Void frame */ 170c74ad251Schristos ND_PRINT("void "); 1710f74e101Schristos break; 1720f74e101Schristos 1730f74e101Schristos case FDDIFC_NRT: /* Nonrestricted token */ 174c74ad251Schristos ND_PRINT("nrt "); 1750f74e101Schristos break; 1760f74e101Schristos 1770f74e101Schristos case FDDIFC_RT: /* Restricted token */ 178c74ad251Schristos ND_PRINT("rt "); 1790f74e101Schristos break; 1800f74e101Schristos 1810f74e101Schristos case FDDIFC_SMT_INFO: /* SMT Info */ 182c74ad251Schristos ND_PRINT("info "); 1830f74e101Schristos break; 1840f74e101Schristos 1850f74e101Schristos case FDDIFC_SMT_NSA: /* SMT Next station adrs */ 186c74ad251Schristos ND_PRINT("nsa "); 1870f74e101Schristos break; 1880f74e101Schristos 1890f74e101Schristos case FDDIFC_MAC_BEACON: /* MAC Beacon frame */ 190c74ad251Schristos ND_PRINT("beacon "); 1910f74e101Schristos break; 1920f74e101Schristos 1930f74e101Schristos case FDDIFC_MAC_CLAIM: /* MAC Claim frame */ 194c74ad251Schristos ND_PRINT("claim "); 1950f74e101Schristos break; 1960f74e101Schristos 1970f74e101Schristos default: 1980f74e101Schristos switch (fc & FDDIFC_CLFF) { 1990f74e101Schristos 2000f74e101Schristos case FDDIFC_MAC: 201c74ad251Schristos ND_PRINT("mac%1x ", fc & FDDIFC_ZZZZ); 2020f74e101Schristos break; 2030f74e101Schristos 2040f74e101Schristos case FDDIFC_SMT: 205c74ad251Schristos ND_PRINT("smt%1x ", fc & FDDIFC_ZZZZ); 2060f74e101Schristos break; 2070f74e101Schristos 2080f74e101Schristos case FDDIFC_LLC_ASYNC: 209c74ad251Schristos ND_PRINT("async%1x ", fc & FDDIFC_ZZZZ); 2100f74e101Schristos break; 2110f74e101Schristos 2120f74e101Schristos case FDDIFC_LLC_SYNC: 213c74ad251Schristos ND_PRINT("sync%1x ", fc & FDDIFC_ZZZZ); 2140f74e101Schristos break; 2150f74e101Schristos 2160f74e101Schristos case FDDIFC_IMP_ASYNC: 217c74ad251Schristos ND_PRINT("imp_async%1x ", fc & FDDIFC_ZZZZ); 2180f74e101Schristos break; 2190f74e101Schristos 2200f74e101Schristos case FDDIFC_IMP_SYNC: 221c74ad251Schristos ND_PRINT("imp_sync%1x ", fc & FDDIFC_ZZZZ); 2220f74e101Schristos break; 2230f74e101Schristos 2240f74e101Schristos default: 225c74ad251Schristos ND_PRINT("%02x ", fc); 2260f74e101Schristos break; 2270f74e101Schristos } 2280f74e101Schristos } 2290f74e101Schristos } 2300f74e101Schristos 2310f74e101Schristos /* Extract src, dst addresses */ 232c74ad251Schristos static void 2330f74e101Schristos extract_fddi_addrs(const struct fddi_header *fddip, char *fsrc, char *fdst) 2340f74e101Schristos { 235c74ad251Schristos int i; 2360f74e101Schristos 2370f74e101Schristos if (fddi_bitswap) { 2380f74e101Schristos /* 2390f74e101Schristos * bit-swap the fddi addresses (isn't the IEEE standards 2400f74e101Schristos * process wonderful!) then convert them to names. 2410f74e101Schristos */ 2420f74e101Schristos for (i = 0; i < 6; ++i) 2430f74e101Schristos fdst[i] = fddi_bit_swap[fddip->fddi_dhost[i]]; 2440f74e101Schristos for (i = 0; i < 6; ++i) 2450f74e101Schristos fsrc[i] = fddi_bit_swap[fddip->fddi_shost[i]]; 246*26ba0b50Schristos } else { 2470f74e101Schristos memcpy(fdst, (const char *)fddip->fddi_dhost, 6); 2480f74e101Schristos memcpy(fsrc, (const char *)fddip->fddi_shost, 6); 2490f74e101Schristos } 2500f74e101Schristos } 2510f74e101Schristos 2520f74e101Schristos /* 2530f74e101Schristos * Print the FDDI MAC header 2540f74e101Schristos */ 255c74ad251Schristos static void 256b3a00663Schristos fddi_hdr_print(netdissect_options *ndo, 257c74ad251Schristos const struct fddi_header *fddip, u_int length, 258c74ad251Schristos const u_char *fsrc, const u_char *fdst) 2590f74e101Schristos { 2600f74e101Schristos const char *srcname, *dstname; 2610f74e101Schristos 262b3a00663Schristos srcname = etheraddr_string(ndo, fsrc); 263b3a00663Schristos dstname = etheraddr_string(ndo, fdst); 2640f74e101Schristos 265fdccd7e4Schristos if (!ndo->ndo_qflag) 266c74ad251Schristos print_fddi_fc(ndo, GET_U_1(fddip->fddi_fc)); 267c74ad251Schristos ND_PRINT("%s > %s, length %u: ", 2680f74e101Schristos srcname, dstname, 269c74ad251Schristos length); 2700f74e101Schristos } 2710f74e101Schristos 272c74ad251Schristos static void 273b3a00663Schristos fddi_smt_print(netdissect_options *ndo, const u_char *p _U_, u_int length _U_) 2740f74e101Schristos { 275c74ad251Schristos ND_PRINT("<SMT printer not yet implemented>"); 2760f74e101Schristos } 2770f74e101Schristos 278fdccd7e4Schristos u_int 279b3a00663Schristos fddi_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen) 2800f74e101Schristos { 2810f74e101Schristos const struct fddi_header *fddip = (const struct fddi_header *)p; 282c74ad251Schristos uint8_t fc; 283c74ad251Schristos nd_mac_addr srcmac, dstmac; 284dc860a36Sspz struct lladdr_info src, dst; 285fdccd7e4Schristos int llc_hdrlen; 2860f74e101Schristos 287c74ad251Schristos ndo->ndo_protocol = "fddi"; 2880f74e101Schristos if (caplen < FDDI_HDRLEN) { 289c74ad251Schristos nd_print_trunc(ndo); 290fdccd7e4Schristos return (caplen); 2910f74e101Schristos } 2920f74e101Schristos 293c74ad251Schristos fc = GET_U_1(fddip->fddi_fc); 294c74ad251Schristos 2950f74e101Schristos /* 2960f74e101Schristos * Get the FDDI addresses into a canonical form 2970f74e101Schristos */ 298c74ad251Schristos extract_fddi_addrs(fddip, (char *)srcmac, (char *)dstmac); 2990f74e101Schristos 300b3a00663Schristos if (ndo->ndo_eflag) 301c74ad251Schristos fddi_hdr_print(ndo, fddip, length, srcmac, dstmac); 3020f74e101Schristos 303c74ad251Schristos src.addr = srcmac; 304dc860a36Sspz src.addr_string = etheraddr_string; 305c74ad251Schristos dst.addr = dstmac; 306dc860a36Sspz dst.addr_string = etheraddr_string; 307dc860a36Sspz 3080f74e101Schristos /* Skip over FDDI MAC header */ 3090f74e101Schristos length -= FDDI_HDRLEN; 3100f74e101Schristos p += FDDI_HDRLEN; 3110f74e101Schristos caplen -= FDDI_HDRLEN; 3120f74e101Schristos 3130f74e101Schristos /* Frame Control field determines interpretation of packet */ 314c74ad251Schristos if ((fc & FDDIFC_CLFF) == FDDIFC_LLC_ASYNC) { 3150f74e101Schristos /* Try to print the LLC-layer header & higher layers */ 316dc860a36Sspz llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst); 317fdccd7e4Schristos if (llc_hdrlen < 0) { 3180f74e101Schristos /* 3190f74e101Schristos * Some kinds of LLC packet we cannot 3200f74e101Schristos * handle intelligently 3210f74e101Schristos */ 322b3a00663Schristos if (!ndo->ndo_suppress_default_print) 323b3a00663Schristos ND_DEFAULTPRINT(p, caplen); 324fdccd7e4Schristos llc_hdrlen = -llc_hdrlen; 3250f74e101Schristos } 326c74ad251Schristos } else if ((fc & FDDIFC_CLFF) == FDDIFC_SMT) { 327b3a00663Schristos fddi_smt_print(ndo, p, caplen); 328fdccd7e4Schristos llc_hdrlen = 0; 329fdccd7e4Schristos } else { 3300f74e101Schristos /* Some kinds of FDDI packet we cannot handle intelligently */ 331b3a00663Schristos if (!ndo->ndo_eflag) 332c74ad251Schristos fddi_hdr_print(ndo, fddip, length + FDDI_HDRLEN, srcmac, 333c74ad251Schristos dstmac); 334b3a00663Schristos if (!ndo->ndo_suppress_default_print) 335b3a00663Schristos ND_DEFAULTPRINT(p, caplen); 336fdccd7e4Schristos llc_hdrlen = 0; 3370f74e101Schristos } 338fdccd7e4Schristos return (FDDI_HDRLEN + llc_hdrlen); 3390f74e101Schristos } 3400f74e101Schristos 3410f74e101Schristos /* 3420f74e101Schristos * This is the top level routine of the printer. 'p' points 3430f74e101Schristos * to the FDDI header of the packet, 'h->ts' is the timestamp, 3440f74e101Schristos * 'h->len' is the length of the packet off the wire, and 'h->caplen' 3450f74e101Schristos * is the number of bytes actually captured. 3460f74e101Schristos */ 347c74ad251Schristos void 348c74ad251Schristos fddi_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p) 3490f74e101Schristos { 350c74ad251Schristos ndo->ndo_protocol = "fddi"; 351c74ad251Schristos ndo->ndo_ll_hdr_len += fddi_print(ndo, p, h->len, h->caplen); 3520f74e101Schristos } 353