1 /* 2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that: (1) source code distributions 7 * retain the above copyright notice and this paragraph in its entirety, (2) 8 * distributions including binary code include the above copyright notice and 9 * this paragraph in its entirety in the documentation or other materials 10 * provided with the distribution, and (3) all advertising materials mentioning 11 * features or use of this software display the following acknowledgement: 12 * ``This product includes software developed by the University of California, 13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14 * the University nor the names of its contributors may be used to endorse 15 * or promote products derived from this software without specific prior 16 * written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 */ 21 #ifndef lint 22 static const char rcsid[] = 23 "@(#) $Header: /tcpdump/master/tcpdump/print-ether.c,v 1.65.4.1 2002/06/01 23:51:12 guy Exp $ (LBL)"; 24 #endif 25 26 #ifdef HAVE_CONFIG_H 27 #include "config.h" 28 #endif 29 30 #include <sys/param.h> 31 #include <sys/time.h> 32 #include <sys/socket.h> 33 34 #include <netinet/in.h> 35 36 #include <stdio.h> 37 #include <pcap.h> 38 39 #include "interface.h" 40 #include "addrtoname.h" 41 #include "ethertype.h" 42 43 #include "ether.h" 44 45 const u_char *packetp; 46 const u_char *snapend; 47 48 static inline void 49 ether_print(register const u_char *bp, u_int length) 50 { 51 register const struct ether_header *ep; 52 53 ep = (const struct ether_header *)bp; 54 if (qflag) 55 (void)printf("%s %s %d: ", 56 etheraddr_string(ESRC(ep)), 57 etheraddr_string(EDST(ep)), 58 length); 59 else 60 (void)printf("%s %s %s %d: ", 61 etheraddr_string(ESRC(ep)), 62 etheraddr_string(EDST(ep)), 63 etherproto_string(ep->ether_type), 64 length); 65 } 66 67 /* 68 * This is the top level routine of the printer. 'p' is the points 69 * to the ether header of the packet, 'h->tv' is the timestamp, 70 * 'h->length' is the length of the packet off the wire, and 'h->caplen' 71 * is the number of bytes actually captured. 72 */ 73 void 74 ether_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 75 { 76 u_int caplen = h->caplen; 77 u_int length = h->len; 78 struct ether_header *ep; 79 u_short ether_type; 80 u_short extracted_ethertype; 81 82 ++infodelay; 83 ts_print(&h->ts); 84 85 if (caplen < ETHER_HDRLEN) { 86 printf("[|ether]"); 87 goto out; 88 } 89 90 if (eflag) 91 ether_print(p, length); 92 93 /* 94 * Some printers want to get back at the ethernet addresses, 95 * and/or check that they're not walking off the end of the packet. 96 * Rather than pass them all the way down, we set these globals. 97 */ 98 packetp = p; 99 snapend = p + caplen; 100 101 length -= ETHER_HDRLEN; 102 caplen -= ETHER_HDRLEN; 103 ep = (struct ether_header *)p; 104 p += ETHER_HDRLEN; 105 106 ether_type = ntohs(ep->ether_type); 107 108 /* 109 * Is it (gag) an 802.3 encapsulation? 110 */ 111 extracted_ethertype = 0; 112 if (ether_type <= ETHERMTU) { 113 /* Try to print the LLC-layer header & higher layers */ 114 if (llc_print(p, length, caplen, ESRC(ep), EDST(ep), 115 &extracted_ethertype) == 0) { 116 /* ether_type not known, print raw packet */ 117 if (!eflag) 118 ether_print((u_char *)ep, length + ETHER_HDRLEN); 119 if (extracted_ethertype) { 120 printf("(LLC %s) ", 121 etherproto_string(htons(extracted_ethertype))); 122 } 123 if (!xflag && !qflag) 124 default_print(p, caplen); 125 } 126 } else if (ether_encap_print(ether_type, p, length, caplen, 127 &extracted_ethertype) == 0) { 128 /* ether_type not known, print raw packet */ 129 if (!eflag) 130 ether_print((u_char *)ep, length + ETHER_HDRLEN); 131 if (!xflag && !qflag) 132 default_print(p, caplen); 133 } 134 if (xflag) 135 default_print(p, caplen); 136 out: 137 putchar('\n'); 138 --infodelay; 139 if (infoprint) 140 info(0); 141 } 142 143 /* 144 * Prints the packet encapsulated in an Ethernet data segment 145 * (or an equivalent encapsulation), given the Ethernet type code. 146 * 147 * Returns non-zero if it can do so, zero if the ethertype is unknown. 148 * 149 * The Ethernet type code is passed through a pointer; if it was 150 * ETHERTYPE_8021Q, it gets updated to be the Ethernet type of 151 * the 802.1Q payload, for the benefit of lower layers that might 152 * want to know what it is. 153 */ 154 155 int 156 ether_encap_print(u_short ethertype, const u_char *p, 157 u_int length, u_int caplen, u_short *extracted_ethertype) 158 { 159 recurse: 160 *extracted_ethertype = ethertype; 161 162 switch (ethertype) { 163 164 case ETHERTYPE_IP: 165 ip_print(p, length); 166 return (1); 167 168 #ifdef INET6 169 case ETHERTYPE_IPV6: 170 ip6_print(p, length); 171 return (1); 172 #endif /*INET6*/ 173 174 case ETHERTYPE_ARP: 175 case ETHERTYPE_REVARP: 176 arp_print(p, length, caplen); 177 return (1); 178 179 case ETHERTYPE_DN: 180 decnet_print(p, length, caplen); 181 return (1); 182 183 case ETHERTYPE_ATALK: 184 if (vflag) 185 fputs("et1 ", stdout); 186 atalk_print(p, length); 187 return (1); 188 189 case ETHERTYPE_AARP: 190 aarp_print(p, length); 191 return (1); 192 193 case ETHERTYPE_IPX: 194 ipx_print(p, length); 195 return (1); 196 197 case ETHERTYPE_8021Q: 198 printf("802.1Q vlan#%d P%d%s ", 199 ntohs(*(u_int16_t *)p) & 0xfff, 200 ntohs(*(u_int16_t *)p) >> 13, 201 (ntohs(*(u_int16_t *)p) & 0x1000) ? " CFI" : ""); 202 ethertype = ntohs(*(u_int16_t *)(p + 2)); 203 p += 4; 204 length -= 4; 205 caplen -= 4; 206 if (ethertype > ETHERMTU) 207 goto recurse; 208 209 *extracted_ethertype = 0; 210 211 if (llc_print(p, length, caplen, p - 18, p - 12, 212 extracted_ethertype) == 0) { 213 /* ether_type not known, print raw packet */ 214 if (!eflag) 215 ether_print(p - 18, length + 4); 216 if (*extracted_ethertype) { 217 printf("(LLC %s) ", 218 etherproto_string(htons(*extracted_ethertype))); 219 } 220 if (!xflag && !qflag) 221 default_print(p - 18, caplen + 4); 222 } 223 return (1); 224 225 case ETHERTYPE_PPPOED: 226 case ETHERTYPE_PPPOES: 227 pppoe_print(p, length); 228 return (1); 229 230 case ETHERTYPE_PPP: 231 printf("ppp"); 232 if (length) { 233 printf(": "); 234 ppp_print(p, length); 235 } 236 return (1); 237 238 case ETHERTYPE_MPLS: 239 case ETHERTYPE_MPLS_MULTI: 240 mpls_print(p, length); 241 return (1); 242 243 case ETHERTYPE_LAT: 244 case ETHERTYPE_SCA: 245 case ETHERTYPE_MOPRC: 246 case ETHERTYPE_MOPDL: 247 /* default_print for now */ 248 default: 249 return (0); 250 } 251 } 252