1 /* $OpenBSD: print-ether.c,v 1.26 2009/10/27 23:59:55 deraadt 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 24 #include <sys/param.h> 25 #include <sys/time.h> 26 #include <sys/socket.h> 27 28 struct mbuf; 29 struct rtentry; 30 #include <net/if.h> 31 32 #include <netinet/in.h> 33 #include <netinet/if_ether.h> 34 #include <netinet/in_systm.h> 35 #include <netinet/ip.h> 36 #include <netinet/ip_var.h> 37 #include <netinet/udp.h> 38 #include <netinet/udp_var.h> 39 #include <netinet/tcp.h> 40 41 #include <stdio.h> 42 #include <pcap.h> 43 44 #ifdef INET6 45 #include <netinet/ip6.h> 46 #endif 47 48 #include "interface.h" 49 #include "addrtoname.h" 50 #include "ethertype.h" 51 #include "extract.h" 52 53 const u_char *packetp; 54 const u_char *snapend; 55 56 void ether_macctl(const u_char *, u_int); 57 58 void 59 ether_print(register const u_char *bp, u_int length) 60 { 61 register const struct ether_header *ep; 62 63 ep = (const struct ether_header *)bp; 64 if (qflag) { 65 TCHECK2(*ep, 12); 66 (void)printf("%s %s %d: ", 67 etheraddr_string(ESRC(ep)), 68 etheraddr_string(EDST(ep)), 69 length); 70 } else { 71 TCHECK2(*ep, 14); 72 (void)printf("%s %s %s %d: ", 73 etheraddr_string(ESRC(ep)), 74 etheraddr_string(EDST(ep)), 75 etherproto_string(ep->ether_type), 76 length); 77 } 78 return; 79 trunc: 80 printf("[|ether] "); 81 } 82 83 u_short extracted_ethertype; 84 85 /* 86 * This is the top level routine of the printer. 'p' is the points 87 * to the ether header of the packet, 'tvp' is the timestamp, 88 * 'length' is the length of the packet off the wire, and 'caplen' 89 * is the number of bytes actually captured. 90 */ 91 void 92 ether_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 93 { 94 u_int caplen = h->caplen; 95 u_int length = h->len; 96 struct ether_header *ep; 97 u_short ether_type; 98 99 ts_print(&h->ts); 100 101 if (caplen < sizeof(struct ether_header)) { 102 printf("[|ether]"); 103 goto out; 104 } 105 106 /* 107 * Some printers want to get back at the ethernet addresses, 108 * and/or check that they're not walking off the end of the packet. 109 * Rather than pass them all the way down, we set these globals. 110 */ 111 packetp = p; 112 snapend = p + caplen; 113 114 if (eflag) 115 ether_print(p, length); 116 117 length -= sizeof(struct ether_header); 118 caplen -= sizeof(struct ether_header); 119 ep = (struct ether_header *)p; 120 p += sizeof(struct ether_header); 121 122 ether_type = ntohs(ep->ether_type); 123 124 /* 125 * Is it (gag) an 802.3 encapsulation? 126 */ 127 extracted_ethertype = 0; 128 if (ether_type <= ETHERMTU) { 129 /* Try to print the LLC-layer header & higher layers */ 130 if (llc_print(p, length, caplen, ESRC(ep), EDST(ep)) == 0) { 131 /* ether_type not known, print raw packet */ 132 if (!eflag) 133 ether_print((u_char *)ep, length); 134 if (extracted_ethertype) { 135 printf("(LLC %s) ", 136 etherproto_string(htons(extracted_ethertype))); 137 } 138 if (!xflag && !qflag) 139 default_print(p, caplen); 140 } 141 } else if (ether_encap_print(ether_type, p, length, caplen) == 0) { 142 /* ether_type not known, print raw packet */ 143 if (!eflag) 144 ether_print((u_char *)ep, length + sizeof(*ep)); 145 if (!xflag && !qflag) 146 default_print(p, caplen); 147 } 148 if (xflag) 149 default_print(p, caplen); 150 out: 151 putchar('\n'); 152 } 153 154 /* 155 * Prints the packet encapsulated in an Ethernet data segment 156 * (or an equivalent encapsulation), given the Ethernet type code. 157 * 158 * Returns non-zero if it can do so, zero if the ethertype is unknown. 159 * 160 * Stuffs the ether type into a global for the benefit of lower layers 161 * that might want to know what it is. 162 */ 163 164 int 165 ether_encap_print(u_short ethertype, const u_char *p, 166 u_int length, u_int caplen) 167 { 168 recurse: 169 extracted_ethertype = ethertype; 170 171 switch (ethertype) { 172 173 case ETHERTYPE_IP: 174 ip_print(p, length); 175 return (1); 176 177 #ifdef INET6 178 case ETHERTYPE_IPV6: 179 ip6_print(p, length); 180 return (1); 181 #endif /*INET6*/ 182 183 case ETHERTYPE_ARP: 184 case ETHERTYPE_REVARP: 185 arp_print(p, length, caplen); 186 return (1); 187 188 case ETHERTYPE_DN: 189 decnet_print(p, length, caplen); 190 return (1); 191 192 case ETHERTYPE_ATALK: 193 if (vflag) 194 fputs("et1 ", stdout); 195 atalk_print_llap(p, length); 196 return (1); 197 198 case ETHERTYPE_AARP: 199 aarp_print(p, length); 200 return (1); 201 202 case ETHERTYPE_8021Q: 203 printf("802.1Q "); 204 case ETHERTYPE_QINQ: 205 if (ethertype == ETHERTYPE_QINQ) 206 printf("QinQ s"); 207 printf("vid %d pri %d%s", 208 ntohs(*(unsigned short*)p)&0xFFF, 209 ntohs(*(unsigned short*)p)>>13, 210 (ntohs(*(unsigned short*)p)&0x1000) ? " cfi " : " "); 211 ethertype = ntohs(*(unsigned short*)(p+2)); 212 p += 4; 213 length -= 4; 214 caplen -= 4; 215 if (ethertype > ETHERMTU) 216 goto recurse; 217 218 extracted_ethertype = 0; 219 220 if (llc_print(p, length, caplen, p-18, p-12) == 0) { 221 /* ether_type not known, print raw packet */ 222 if (!eflag) 223 ether_print(p-18, length+4); 224 if (extracted_ethertype) { 225 printf("(LLC %s) ", 226 etherproto_string(htons(extracted_ethertype))); 227 } 228 if (!xflag && !qflag) 229 default_print(p-18, caplen+4); 230 } 231 return (1); 232 233 #ifdef PPP 234 case ETHERTYPE_PPPOEDISC: 235 case ETHERTYPE_PPPOE: 236 pppoe_if_print(ethertype, p, length, caplen); 237 return (1); 238 #endif 239 240 case ETHERTYPE_FLOWCONTROL: 241 ether_macctl(p, length); 242 return (1); 243 244 case ETHERTYPE_MPLS: 245 case ETHERTYPE_MPLS_MCAST: 246 mpls_print(p, length); 247 return (1); 248 249 case ETHERTYPE_LLDP: 250 lldp_print(p, length); 251 return (1); 252 253 case ETHERTYPE_SLOW: 254 slow_print(p, length); 255 return (1); 256 257 case ETHERTYPE_LAT: 258 case ETHERTYPE_SCA: 259 case ETHERTYPE_MOPRC: 260 case ETHERTYPE_MOPDL: 261 /* default_print for now */ 262 default: 263 return (0); 264 } 265 } 266 267 void 268 ether_macctl(const u_char *p, u_int length) 269 { 270 printf("MACCTL"); 271 272 if (length < 2) 273 goto trunc; 274 if (EXTRACT_16BITS(p) == 0x0001) { 275 u_int plen; 276 277 printf(" PAUSE"); 278 279 length -= 2; 280 p += 2; 281 if (length < 2) 282 goto trunc; 283 plen = 512 * EXTRACT_16BITS(p); 284 printf(" quanta %u", plen); 285 } else { 286 printf(" unknown-opcode(0x%04x)", EXTRACT_16BITS(p)); 287 } 288 return; 289 290 trunc: 291 printf("[|MACCTL]"); 292 } 293