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