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