1 /* $OpenBSD: print-ether.c,v 1.25 2008/12/05 01:25:24 sthen Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 */ 23 #ifndef lint 24 static const char rcsid[] = 25 "@(#) $Id: print-ether.c,v 1.25 2008/12/05 01:25:24 sthen Exp $ (LBL)"; 26 #endif 27 28 #include <sys/param.h> 29 #include <sys/time.h> 30 #include <sys/socket.h> 31 32 struct mbuf; 33 struct rtentry; 34 #include <net/if.h> 35 36 #include <netinet/in.h> 37 #include <netinet/if_ether.h> 38 #include <netinet/in_systm.h> 39 #include <netinet/ip.h> 40 #include <netinet/ip_var.h> 41 #include <netinet/udp.h> 42 #include <netinet/udp_var.h> 43 #include <netinet/tcp.h> 44 45 #include <stdio.h> 46 #include <pcap.h> 47 48 #ifdef INET6 49 #include <netinet/ip6.h> 50 #endif 51 52 #include "interface.h" 53 #include "addrtoname.h" 54 #include "ethertype.h" 55 #include "extract.h" 56 57 const u_char *packetp; 58 const u_char *snapend; 59 60 void ether_macctl(const u_char *, u_int); 61 62 void 63 ether_print(register const u_char *bp, u_int length) 64 { 65 register const struct ether_header *ep; 66 67 ep = (const struct ether_header *)bp; 68 if (qflag) { 69 TCHECK2(*ep, 12); 70 (void)printf("%s %s %d: ", 71 etheraddr_string(ESRC(ep)), 72 etheraddr_string(EDST(ep)), 73 length); 74 } else { 75 TCHECK2(*ep, 14); 76 (void)printf("%s %s %s %d: ", 77 etheraddr_string(ESRC(ep)), 78 etheraddr_string(EDST(ep)), 79 etherproto_string(ep->ether_type), 80 length); 81 } 82 return; 83 trunc: 84 printf("[|ether] "); 85 } 86 87 u_short extracted_ethertype; 88 89 /* 90 * This is the top level routine of the printer. 'p' is the points 91 * to the ether header of the packet, 'tvp' is the timestamp, 92 * 'length' is the length of the packet off the wire, and 'caplen' 93 * is the number of bytes actually captured. 94 */ 95 void 96 ether_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 97 { 98 u_int caplen = h->caplen; 99 u_int length = h->len; 100 struct ether_header *ep; 101 u_short ether_type; 102 103 ts_print(&h->ts); 104 105 if (caplen < sizeof(struct ether_header)) { 106 printf("[|ether]"); 107 goto out; 108 } 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 if (eflag) 119 ether_print(p, length); 120 121 length -= sizeof(struct ether_header); 122 caplen -= sizeof(struct ether_header); 123 ep = (struct ether_header *)p; 124 p += sizeof(struct ether_header); 125 126 ether_type = ntohs(ep->ether_type); 127 128 /* 129 * Is it (gag) an 802.3 encapsulation? 130 */ 131 extracted_ethertype = 0; 132 if (ether_type <= ETHERMTU) { 133 /* Try to print the LLC-layer header & higher layers */ 134 if (llc_print(p, length, caplen, ESRC(ep), EDST(ep)) == 0) { 135 /* ether_type not known, print raw packet */ 136 if (!eflag) 137 ether_print((u_char *)ep, length); 138 if (extracted_ethertype) { 139 printf("(LLC %s) ", 140 etherproto_string(htons(extracted_ethertype))); 141 } 142 if (!xflag && !qflag) 143 default_print(p, caplen); 144 } 145 } else if (ether_encap_print(ether_type, p, length, caplen) == 0) { 146 /* ether_type not known, print raw packet */ 147 if (!eflag) 148 ether_print((u_char *)ep, length + sizeof(*ep)); 149 if (!xflag && !qflag) 150 default_print(p, caplen); 151 } 152 if (xflag) 153 default_print(p, caplen); 154 out: 155 putchar('\n'); 156 } 157 158 /* 159 * Prints the packet encapsulated in an Ethernet data segment 160 * (or an equivalent encapsulation), given the Ethernet type code. 161 * 162 * Returns non-zero if it can do so, zero if the ethertype is unknown. 163 * 164 * Stuffs the ether type into a global for the benefit of lower layers 165 * that might want to know what it is. 166 */ 167 168 int 169 ether_encap_print(u_short ethertype, const u_char *p, 170 u_int length, u_int caplen) 171 { 172 recurse: 173 extracted_ethertype = ethertype; 174 175 switch (ethertype) { 176 177 case ETHERTYPE_IP: 178 ip_print(p, length); 179 return (1); 180 181 #ifdef INET6 182 case ETHERTYPE_IPV6: 183 ip6_print(p, length); 184 return (1); 185 #endif /*INET6*/ 186 187 case ETHERTYPE_ARP: 188 case ETHERTYPE_REVARP: 189 arp_print(p, length, caplen); 190 return (1); 191 192 case ETHERTYPE_DN: 193 decnet_print(p, length, caplen); 194 return (1); 195 196 case ETHERTYPE_ATALK: 197 if (vflag) 198 fputs("et1 ", stdout); 199 atalk_print_llap(p, length); 200 return (1); 201 202 case ETHERTYPE_AARP: 203 aarp_print(p, length); 204 return (1); 205 206 case ETHERTYPE_8021Q: 207 printf("802.1Q "); 208 case ETHERTYPE_QINQ: 209 if (ethertype == ETHERTYPE_QINQ) 210 printf("QinQ s"); 211 printf("vid %d pri %d%s", 212 ntohs(*(unsigned short*)p)&0xFFF, 213 ntohs(*(unsigned short*)p)>>13, 214 (ntohs(*(unsigned short*)p)&0x1000) ? " cfi " : " "); 215 ethertype = ntohs(*(unsigned short*)(p+2)); 216 p += 4; 217 length -= 4; 218 caplen -= 4; 219 if (ethertype > ETHERMTU) 220 goto recurse; 221 222 extracted_ethertype = 0; 223 224 if (llc_print(p, length, caplen, p-18, p-12) == 0) { 225 /* ether_type not known, print raw packet */ 226 if (!eflag) 227 ether_print(p-18, length+4); 228 if (extracted_ethertype) { 229 printf("(LLC %s) ", 230 etherproto_string(htons(extracted_ethertype))); 231 } 232 if (!xflag && !qflag) 233 default_print(p-18, caplen+4); 234 } 235 return (1); 236 237 #ifdef PPP 238 case ETHERTYPE_PPPOEDISC: 239 case ETHERTYPE_PPPOE: 240 pppoe_if_print(ethertype, p, length, caplen); 241 return (1); 242 #endif 243 244 case ETHERTYPE_FLOWCONTROL: 245 ether_macctl(p, length); 246 return (1); 247 248 case ETHERTYPE_MPLS: 249 case ETHERTYPE_MPLS_MCAST: 250 mpls_print(p, length); 251 return (1); 252 253 case ETHERTYPE_LLDP: 254 lldp_print(p, length); 255 return (1); 256 257 case ETHERTYPE_SLOW: 258 slow_print(p, length); 259 return (1); 260 261 case ETHERTYPE_LAT: 262 case ETHERTYPE_SCA: 263 case ETHERTYPE_MOPRC: 264 case ETHERTYPE_MOPDL: 265 /* default_print for now */ 266 default: 267 return (0); 268 } 269 } 270 271 void 272 ether_macctl(const u_char *p, u_int length) 273 { 274 printf("MACCTL"); 275 276 if (length < 2) 277 goto trunc; 278 if (EXTRACT_16BITS(p) == 0x0001) { 279 u_int plen; 280 281 printf(" PAUSE"); 282 283 length -= 2; 284 p += 2; 285 if (length < 2) 286 goto trunc; 287 plen = 512 * EXTRACT_16BITS(p); 288 printf(" quanta %u", plen); 289 } else { 290 printf(" unknown-opcode(0x%04x)", EXTRACT_16BITS(p)); 291 } 292 return; 293 294 trunc: 295 printf("[|MACCTL]"); 296 } 297