1 /* $OpenBSD: print-null.c,v 1.15 2002/01/23 23:32:20 mickey Exp $ */ 2 3 /* 4 * Copyright (c) 1991, 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 #ifndef lint 25 static const char rcsid[] = 26 "@(#) $Header: /home/cvs/src/usr.sbin/tcpdump/print-null.c,v 1.15 2002/01/23 23:32:20 mickey Exp $ (LBL)"; 27 #endif 28 29 #include <sys/param.h> 30 #include <sys/time.h> 31 #include <sys/socket.h> 32 #include <sys/file.h> 33 #include <sys/ioctl.h> 34 35 #ifdef __STDC__ 36 struct mbuf; 37 struct rtentry; 38 #endif 39 #include <net/if.h> 40 41 #include <netinet/in.h> 42 #include <netinet/in_systm.h> 43 #include <netinet/ip.h> 44 #include <netinet/if_ether.h> 45 #include <netinet/ip_var.h> 46 #include <netinet/udp.h> 47 #include <netinet/udp_var.h> 48 #include <netinet/tcp.h> 49 50 #include <pcap.h> 51 #include <stdio.h> 52 #include <string.h> 53 54 #ifdef INET6 55 #include <netinet/ip6.h> 56 #endif 57 58 #include "interface.h" 59 #include "addrtoname.h" 60 61 #ifndef AF_NS 62 #define AF_NS 6 /* XEROX NS protocols */ 63 #endif 64 65 /* 66 * The DLT_NULL packet header is 4 bytes long. It contains a host 67 * order 32 bit integer that specifies the family, e.g. AF_INET 68 */ 69 #define NULL_HDRLEN 4 70 71 static void 72 null_print(const u_char *p, const struct ip *ip, u_int length) 73 { 74 u_int family; 75 76 memcpy((char *)&family, (char *)p, sizeof(family)); 77 78 if (nflag && family != AF_LINK) { 79 /* XXX just dump the header */ 80 return; 81 } 82 switch (family) { 83 84 case AF_INET: 85 printf("ip: "); 86 break; 87 88 #ifdef INET6 89 case AF_INET6: 90 printf("ip6: "); 91 break; 92 #endif 93 94 case AF_NS: 95 printf("ns: "); 96 break; 97 98 #ifdef __OpenBSD__ 99 case AF_LINK: 100 ether_print(p + NULL_HDRLEN, length); 101 break; 102 #endif 103 104 default: 105 printf("AF %d: ", family); 106 break; 107 } 108 } 109 110 void 111 loop_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 112 { 113 *(u_int *)p = ntohl(*(u_int *)p); 114 115 null_if_print(user, h, p); 116 } 117 118 void 119 null_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 120 { 121 u_int length = h->len; 122 u_int caplen = h->caplen; 123 u_int family = *(u_int *)p; 124 125 #ifdef __OpenBSD__ 126 struct ether_header *ep; 127 u_short ether_type; 128 extern u_short extracted_ethertype; 129 #endif 130 131 ts_print(&h->ts); 132 133 /* 134 * Some printers want to get back at the link level addresses, 135 * and/or check that they're not walking off the end of the packet. 136 * Rather than pass them all the way down, we set these globals. 137 */ 138 packetp = p; 139 snapend = p + caplen; 140 141 length -= NULL_HDRLEN; 142 143 if (eflag) 144 null_print(p, (struct ip *)(p + NULL_HDRLEN), length); 145 146 switch (family) { 147 case AF_INET: 148 ip_print(p + NULL_HDRLEN, length); 149 break; 150 151 #ifdef INET6 152 case AF_INET6: 153 ip6_print(p + NULL_HDRLEN, length); 154 break; 155 #endif /*INET6*/ 156 157 #ifdef __OpenBSD__ 158 case AF_LINK: 159 if (caplen < sizeof(struct ether_header) + NULL_HDRLEN) { 160 printf("[|ether]"); 161 goto out; 162 } 163 164 length -= sizeof(struct ether_header); 165 caplen -= sizeof(struct ether_header); 166 ep = (struct ether_header *)(p + NULL_HDRLEN); 167 p += NULL_HDRLEN + sizeof(struct ether_header); 168 packetp += sizeof(struct ether_header); 169 ether_type = ntohs(ep->ether_type); 170 171 extracted_ethertype = 0; 172 if (ether_type <= ETHERMTU) { 173 /* Try to print the LLC-layer header & higher layers */ 174 if (llc_print(p, length, caplen, ESRC(ep), 175 EDST(ep)) == 0) { 176 /* ether_type not known, print raw packet */ 177 if (!eflag) 178 ether_print((u_char *)ep, length); 179 if (extracted_ethertype) { 180 printf("(LLC %s) ", 181 etherproto_string(htons(extracted_ethertype))); 182 } 183 if (!xflag && !qflag) 184 default_print(p, caplen); 185 } 186 } else if (ether_encap_print(ether_type, p, length, 187 caplen) == 0) { 188 /* ether_type not known, print raw packet */ 189 if (!eflag) 190 ether_print((u_char *)ep, length + 191 sizeof(*ep)); 192 if (!xflag && !qflag) 193 default_print(p, caplen); 194 } 195 break; 196 #endif /* __OpenBSD__ */ 197 } 198 199 if (xflag) 200 default_print((const u_char *)(packetp + NULL_HDRLEN), 201 caplen - NULL_HDRLEN); 202 out: 203 putchar('\n'); 204 } 205 206