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