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