xref: /openbsd-src/usr.sbin/tcpdump/print-null.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: print-null.c,v 1.22 2015/11/05 17:57:37 jca 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/if_ether.h>
36 #include <netinet/ip_var.h>
37 #include <netinet/udp.h>
38 #include <netinet/udp_var.h>
39 #include <netinet/tcp.h>
40 
41 #include <pcap.h>
42 #include <stdio.h>
43 #include <string.h>
44 
45 #ifdef INET6
46 #include <netinet/ip6.h>
47 #endif
48 
49 #include "interface.h"
50 #include "addrtoname.h"
51 
52 #ifndef AF_NS
53 #define AF_NS		6		/* XEROX NS protocols */
54 #endif
55 
56 /*
57  * The DLT_NULL packet header is 4 bytes long. It contains a host
58  * order 32 bit integer that specifies the family, e.g. AF_INET
59  */
60 #define	NULL_HDRLEN 4
61 
62 static void
63 null_print(const u_char *p, const struct ip *ip, u_int length)
64 {
65 	u_int family;
66 
67 	memcpy((char *)&family, (char *)p, sizeof(family));
68 
69 	if (nflag && family != AF_LINK) {
70 		/* XXX just dump the header */
71 		return;
72 	}
73 	switch (family) {
74 
75 	case AF_INET:
76 		printf("ip: ");
77 		break;
78 
79 #ifdef INET6
80 	case AF_INET6:
81 		printf("ip6: ");
82 		break;
83 #endif
84 
85 	case AF_NS:
86 		printf("ns: ");
87 		break;
88 
89 #ifdef __OpenBSD__
90 	case AF_LINK:
91 		ether_print(p + NULL_HDRLEN, length);
92 		break;
93 #endif
94 	case AF_MPLS:
95 		printf("mpls: ");
96 		break;
97 
98 	default:
99 		printf("AF %d: ", family);
100 		break;
101 	}
102 }
103 
104 void
105 loop_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
106 {
107 	*(u_int *)p = ntohl(*(u_int *)p);
108 
109 	null_if_print(user, h, p);
110 }
111 
112 void
113 null_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
114 {
115 	u_int length = h->len;
116 	u_int caplen = h->caplen;
117 	u_int family = *(u_int *)p;
118 
119 #ifdef __OpenBSD__
120 	struct ether_header *ep;
121 	u_short ether_type;
122 	extern u_short extracted_ethertype;
123 #endif
124 
125 	ts_print(&h->ts);
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 #ifdef INET6
146 	case AF_INET6:
147 		ip6_print(p + NULL_HDRLEN, length);
148 		break;
149 #endif /*INET6*/
150 
151 	case AF_MPLS:
152 		mpls_print(p + NULL_HDRLEN, length);
153 		break;
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 - NULL_HDRLEN);
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 - NULL_HDRLEN);
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