1 /* 2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 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 * $FreeBSD$ 22 */ 23 #ifndef lint 24 static const char rcsid[] = 25 "@(#) $Header: /tcpdump/master/tcpdump/print-ether.c,v 1.48 1999/11/21 09:36:51 fenner Exp $ (LBL)"; 26 #endif 27 28 #ifdef HAVE_CONFIG_H 29 #include "config.h" 30 #endif 31 32 #include <sys/param.h> 33 #include <sys/time.h> 34 #include <sys/socket.h> 35 36 #if __STDC__ 37 struct mbuf; 38 struct rtentry; 39 #endif 40 #include <net/if.h> 41 42 #include <netinet/in.h> 43 #include <net/ethernet.h> 44 #include <netinet/in_systm.h> 45 #include <netinet/ip.h> 46 #include <netinet/ip_var.h> 47 #include <netinet/udp.h> 48 #include <netinet/udp_var.h> 49 #include <netinet/tcp.h> 50 51 #include <stdio.h> 52 #include <pcap.h> 53 54 #ifdef INET6 55 #include <netinet/ip6.h> 56 #endif 57 58 #include "interface.h" 59 #include "addrtoname.h" 60 #include "ethertype.h" 61 62 const u_char *packetp; 63 const u_char *snapend; 64 65 static inline void 66 ether_print(register const u_char *bp, u_int length) 67 { 68 register const struct ether_header *ep; 69 70 ep = (const struct ether_header *)bp; 71 if (qflag) 72 (void)printf("%s %s %d: ", 73 etheraddr_string(ESRC(ep)), 74 etheraddr_string(EDST(ep)), 75 length); 76 else 77 (void)printf("%s %s %s %d: ", 78 etheraddr_string(ESRC(ep)), 79 etheraddr_string(EDST(ep)), 80 etherproto_string(ep->ether_type), 81 length); 82 } 83 84 static u_short extracted_ethertype; 85 86 /* 87 * This is the top level routine of the printer. 'p' is the points 88 * to the ether header of the packet, 'h->tv' is the timestamp, 89 * 'h->length' is the length of the packet off the wire, and 'h->caplen' 90 * is the number of bytes actually captured. 91 */ 92 void 93 ether_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 94 { 95 u_int caplen = h->caplen; 96 u_int length = h->len; 97 struct ether_header *ep; 98 u_short ether_type; 99 100 ts_print(&h->ts); 101 102 if (caplen < sizeof(struct ether_header)) { 103 printf("[|ether]"); 104 goto out; 105 } 106 107 if (eflag) 108 ether_print(p, length); 109 110 /* 111 * Some printers want to get back at the ethernet addresses, 112 * and/or check that they're not walking off the end of the packet. 113 * Rather than pass them all the way down, we set these globals. 114 */ 115 packetp = p; 116 snapend = p + caplen; 117 118 length -= sizeof(struct ether_header); 119 caplen -= sizeof(struct ether_header); 120 ep = (struct ether_header *)p; 121 p += sizeof(struct ether_header); 122 123 ether_type = ntohs(ep->ether_type); 124 125 /* 126 * Is it (gag) an 802.3 encapsulation? 127 */ 128 extracted_ethertype = 0; 129 if (ether_type <= ETHERMTU) { 130 /* Try to print the LLC-layer header & higher layers */ 131 if (llc_print(p, length, caplen, ESRC(ep), EDST(ep)) == 0) { 132 /* ether_type not known, print raw packet */ 133 if (!eflag) 134 ether_print((u_char *)ep, length); 135 if (extracted_ethertype) { 136 printf("(LLC %s) ", 137 etherproto_string(htons(extracted_ethertype))); 138 } 139 if (!xflag && !qflag) 140 default_print(p, caplen); 141 } 142 } else if (ether_encap_print(ether_type, p, length, caplen) == 0) { 143 /* ether_type not known, print raw packet */ 144 if (!eflag) 145 ether_print((u_char *)ep, length + sizeof(*ep)); 146 if (!xflag && !qflag) 147 default_print(p, caplen); 148 } 149 if (xflag) 150 default_print(p, caplen); 151 out: 152 putchar('\n'); 153 } 154 155 /* 156 * Prints the packet encapsulated in an Ethernet data segment 157 * (or an equivalent encapsulation), given the Ethernet type code. 158 * 159 * Returns non-zero if it can do so, zero if the ethertype is unknown. 160 * 161 * Stuffs the ether type into a global for the benefit of lower layers 162 * that might want to know what it is. 163 */ 164 165 int 166 ether_encap_print(u_short ethertype, const u_char *p, 167 u_int length, u_int caplen) 168 { 169 recurse: 170 extracted_ethertype = ethertype; 171 172 switch (ethertype) { 173 174 case ETHERTYPE_IP: 175 ip_print(p, length); 176 return (1); 177 178 #ifdef INET6 179 case ETHERTYPE_IPV6: 180 ip6_print(p, length); 181 return (1); 182 #endif /*INET6*/ 183 184 case ETHERTYPE_ARP: 185 case ETHERTYPE_REVARP: 186 arp_print(p, length, caplen); 187 return (1); 188 189 case ETHERTYPE_DN: 190 decnet_print(p, length, caplen); 191 return (1); 192 193 case ETHERTYPE_ATALK: 194 if (vflag) 195 fputs("et1 ", stdout); 196 atalk_print(p, length); 197 return (1); 198 199 case ETHERTYPE_AARP: 200 aarp_print(p, length); 201 return (1); 202 203 case ETHERTYPE_IPX: 204 ipx_print(p, length); 205 return (1); 206 207 case ETHERTYPE_8021Q: 208 printf("802.1Q vlan#%d P%d%s", 209 ntohs(*(unsigned short*)p)&0xFFF, 210 ntohs(*(unsigned short*)p)>>13, 211 (ntohs(*(unsigned short*)p)&0x1000) ? " CFI" : ""); 212 ethertype = ntohs(*(unsigned short*)(p+2)); 213 p += 4; 214 length -= 4; 215 caplen -= 4; 216 if (ethertype > ETHERMTU) 217 goto recurse; 218 219 extracted_ethertype = 0; 220 221 if (llc_print(p, length, caplen, p-18, p-12) == 0) { 222 /* ether_type not known, print raw packet */ 223 if (!eflag) 224 ether_print(p-18, length+4); 225 if (extracted_ethertype) { 226 printf("(LLC %s) ", 227 etherproto_string(htons(extracted_ethertype))); 228 } 229 if (!xflag && !qflag) 230 default_print(p-18, caplen+4); 231 } 232 return (1); 233 234 case ETHERTYPE_PPPOED: 235 case ETHERTYPE_PPPOES: 236 pppoe_print(p, length); 237 return (1); 238 239 case ETHERTYPE_LAT: 240 case ETHERTYPE_SCA: 241 case ETHERTYPE_MOPRC: 242 case ETHERTYPE_MOPDL: 243 /* default_print for now */ 244 default: 245 return (0); 246 } 247 } 248