xref: /openbsd-src/usr.sbin/tcpdump/print-null.c (revision f6b75673f6c960a9743bfd16c1e52dd100265c68)
1 /*	$OpenBSD: print-null.c,v 1.23 2018/10/22 16:12:45 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 	/*
123 	 * Some printers want to get back at the link level addresses,
124 	 * and/or check that they're not walking off the end of the packet.
125 	 * Rather than pass them all the way down, we set these globals.
126 	 */
127 	packetp = p;
128 	snapend = p + caplen;
129 
130 	length -= NULL_HDRLEN;
131 
132 	if (eflag)
133 		null_print(p, (struct ip *)(p + NULL_HDRLEN), length);
134 
135 	switch (family) {
136 	case AF_INET:
137 		ip_print(p + NULL_HDRLEN, length);
138 		break;
139 
140 	case AF_INET6:
141 		ip6_print(p + NULL_HDRLEN, length);
142 		break;
143 
144 	case AF_MPLS:
145 		mpls_print(p + NULL_HDRLEN, length);
146 		break;
147 
148 #ifdef __OpenBSD__
149 	case AF_LINK:
150 		if (caplen < sizeof(struct ether_header) + NULL_HDRLEN) {
151 			printf("[|ether]");
152 			goto out;
153 		}
154 
155 		length -= sizeof(struct ether_header);
156 		caplen -= sizeof(struct ether_header);
157 		ep = (struct ether_header *)(p + NULL_HDRLEN);
158 		p += NULL_HDRLEN + sizeof(struct ether_header);
159 		packetp += sizeof(struct ether_header);
160 		ether_type = ntohs(ep->ether_type);
161 
162 		extracted_ethertype = 0;
163 		if (ether_type <= ETHERMTU) {
164 			/* Try to print the LLC-layer header & higher layers */
165 			if (llc_print(p, length, caplen, ESRC(ep),
166 			    EDST(ep)) == 0) {
167 				/* ether_type not known, print raw packet */
168 				if (!eflag)
169 					ether_print((u_char *)ep, length);
170 				if (extracted_ethertype) {
171 					printf("(LLC %s) ",
172 					       etherproto_string(htons(extracted_ethertype)));
173 				}
174 				if (!xflag && !qflag)
175 					default_print(p, caplen - NULL_HDRLEN);
176 			}
177 		} else if (ether_encap_print(ether_type, p, length,
178 		           caplen) == 0) {
179 			/* ether_type not known, print raw packet */
180 			if (!eflag)
181 				ether_print((u_char *)ep, length +
182 				    sizeof(*ep));
183 			if (!xflag && !qflag)
184 				default_print(p, caplen - NULL_HDRLEN);
185 		}
186 		break;
187 #endif /* __OpenBSD__ */
188 	}
189 
190 	if (xflag)
191 		default_print((const u_char *)(packetp + NULL_HDRLEN),
192 		    caplen - NULL_HDRLEN);
193  out:
194 	putchar('\n');
195 }
196 
197