xref: /netbsd-src/external/bsd/tcpdump/dist/print.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * Support for splitting captures into multiple files with a maximum
22  * file size:
23  *
24  * Copyright (c) 2001
25  *	Seth Webster <swebster@sst.ll.mit.edu>
26  */
27 
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: print.c,v 1.3 2017/02/05 04:05:05 spz Exp $");
31 #endif
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include <netdissect-stdinc.h>
41 
42 #include "netdissect.h"
43 #include "addrtoname.h"
44 #include "print.h"
45 
46 struct printer {
47 	if_printer f;
48 	int type;
49 };
50 
51 static const struct printer printers[] = {
52 	{ ether_if_print,	DLT_EN10MB },
53 #ifdef DLT_IPNET
54 	{ ipnet_if_print,	DLT_IPNET },
55 #endif
56 #ifdef DLT_IEEE802_15_4
57 	{ ieee802_15_4_if_print, DLT_IEEE802_15_4 },
58 #endif
59 #ifdef DLT_IEEE802_15_4_NOFCS
60 	{ ieee802_15_4_if_print, DLT_IEEE802_15_4_NOFCS },
61 #endif
62 #ifdef DLT_PPI
63 	{ ppi_if_print,		DLT_PPI },
64 #endif
65 #ifdef DLT_NETANALYZER
66 	{ netanalyzer_if_print, DLT_NETANALYZER },
67 #endif
68 #ifdef DLT_NETANALYZER_TRANSPARENT
69 	{ netanalyzer_transparent_if_print, DLT_NETANALYZER_TRANSPARENT },
70 #endif
71 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
72 	{ nflog_if_print,	DLT_NFLOG},
73 #endif
74 #ifdef DLT_CIP
75 	{ cip_if_print,         DLT_CIP },
76 #endif
77 #ifdef DLT_ATM_CLIP
78 	{ cip_if_print,		DLT_ATM_CLIP },
79 #endif
80 #ifdef DLT_IP_OVER_FC
81 	{ ipfc_if_print,	DLT_IP_OVER_FC },
82 #endif
83 	{ null_if_print,	DLT_NULL },
84 #ifdef DLT_LOOP
85 	{ null_if_print,	DLT_LOOP },
86 #endif
87 #ifdef DLT_APPLE_IP_OVER_IEEE1394
88 	{ ap1394_if_print,	DLT_APPLE_IP_OVER_IEEE1394 },
89 #endif
90 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
91 	{ bt_if_print,		DLT_BLUETOOTH_HCI_H4_WITH_PHDR},
92 #endif
93 #ifdef DLT_LANE8023
94 	{ lane_if_print,        DLT_LANE8023 },
95 #endif
96 	{ arcnet_if_print,	DLT_ARCNET },
97 #ifdef DLT_ARCNET_LINUX
98 	{ arcnet_linux_if_print, DLT_ARCNET_LINUX },
99 #endif
100 	{ raw_if_print,		DLT_RAW },
101 #ifdef DLT_IPV4
102 	{ raw_if_print,		DLT_IPV4 },
103 #endif
104 #ifdef DLT_IPV6
105 	{ raw_if_print,		DLT_IPV6 },
106 #endif
107 #ifdef HAVE_PCAP_USB_H
108 #ifdef DLT_USB_LINUX
109 	{ usb_linux_48_byte_print, DLT_USB_LINUX},
110 #endif /* DLT_USB_LINUX */
111 #ifdef DLT_USB_LINUX_MMAPPED
112 	{ usb_linux_64_byte_print, DLT_USB_LINUX_MMAPPED},
113 #endif /* DLT_USB_LINUX_MMAPPED */
114 #endif /* HAVE_PCAP_USB_H */
115 #ifdef DLT_SYMANTEC_FIREWALL
116 	{ symantec_if_print,	DLT_SYMANTEC_FIREWALL },
117 #endif
118 #ifdef DLT_C_HDLC
119 	{ chdlc_if_print,	DLT_C_HDLC },
120 #endif
121 #ifdef DLT_HDLC
122 	{ chdlc_if_print,	DLT_HDLC },
123 #endif
124 #ifdef DLT_PPP_ETHER
125 	{ pppoe_if_print,	DLT_PPP_ETHER },
126 #endif
127 #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H)
128 	{ pflog_if_print,	DLT_PFLOG },
129 #endif
130 	{ token_if_print,	DLT_IEEE802 },
131 	{ fddi_if_print,	DLT_FDDI },
132 #ifdef DLT_LINUX_SLL
133 	{ sll_if_print,		DLT_LINUX_SLL },
134 #endif
135 #ifdef DLT_FR
136 	{ fr_if_print,		DLT_FR },
137 #endif
138 #ifdef DLT_FRELAY
139 	{ fr_if_print,		DLT_FRELAY },
140 #endif
141 #ifdef DLT_MFR
142 	{ mfr_if_print,		DLT_MFR },
143 #endif
144 	{ atm_if_print,		DLT_ATM_RFC1483 },
145 #ifdef DLT_SUNATM
146 	{ sunatm_if_print,	DLT_SUNATM },
147 #endif
148 #ifdef DLT_ENC
149 	{ enc_if_print,		DLT_ENC },
150 #endif
151 	{ sl_if_print,		DLT_SLIP },
152 #ifdef DLT_SLIP_BSDOS
153 	{ sl_bsdos_if_print,	DLT_SLIP_BSDOS },
154 #endif
155 #ifdef DLT_LTALK
156 	{ ltalk_if_print,	DLT_LTALK },
157 #endif
158 #ifdef DLT_JUNIPER_ATM1
159 	{ juniper_atm1_print,	DLT_JUNIPER_ATM1 },
160 #endif
161 #ifdef DLT_JUNIPER_ATM2
162 	{ juniper_atm2_print,	DLT_JUNIPER_ATM2 },
163 #endif
164 #ifdef DLT_JUNIPER_MFR
165 	{ juniper_mfr_print,	DLT_JUNIPER_MFR },
166 #endif
167 #ifdef DLT_JUNIPER_MLFR
168 	{ juniper_mlfr_print,	DLT_JUNIPER_MLFR },
169 #endif
170 #ifdef DLT_JUNIPER_MLPPP
171 	{ juniper_mlppp_print,	DLT_JUNIPER_MLPPP },
172 #endif
173 #ifdef DLT_JUNIPER_PPPOE
174 	{ juniper_pppoe_print,	DLT_JUNIPER_PPPOE },
175 #endif
176 #ifdef DLT_JUNIPER_PPPOE_ATM
177 	{ juniper_pppoe_atm_print, DLT_JUNIPER_PPPOE_ATM },
178 #endif
179 #ifdef DLT_JUNIPER_GGSN
180 	{ juniper_ggsn_print,	DLT_JUNIPER_GGSN },
181 #endif
182 #ifdef DLT_JUNIPER_ES
183 	{ juniper_es_print,	DLT_JUNIPER_ES },
184 #endif
185 #ifdef DLT_JUNIPER_MONITOR
186 	{ juniper_monitor_print, DLT_JUNIPER_MONITOR },
187 #endif
188 #ifdef DLT_JUNIPER_SERVICES
189 	{ juniper_services_print, DLT_JUNIPER_SERVICES },
190 #endif
191 #ifdef DLT_JUNIPER_ETHER
192 	{ juniper_ether_print,	DLT_JUNIPER_ETHER },
193 #endif
194 #ifdef DLT_JUNIPER_PPP
195 	{ juniper_ppp_print,	DLT_JUNIPER_PPP },
196 #endif
197 #ifdef DLT_JUNIPER_FRELAY
198 	{ juniper_frelay_print,	DLT_JUNIPER_FRELAY },
199 #endif
200 #ifdef DLT_JUNIPER_CHDLC
201 	{ juniper_chdlc_print,	DLT_JUNIPER_CHDLC },
202 #endif
203 #ifdef DLT_PKTAP
204 	{ pktap_if_print,	DLT_PKTAP },
205 #endif
206 #ifdef DLT_IEEE802_11_RADIO
207 	{ ieee802_11_radio_if_print,	DLT_IEEE802_11_RADIO },
208 #endif
209 #ifdef DLT_IEEE802_11
210 	{ ieee802_11_if_print,	DLT_IEEE802_11},
211 #endif
212 #ifdef DLT_IEEE802_11_RADIO_AVS
213 	{ ieee802_11_radio_avs_if_print,	DLT_IEEE802_11_RADIO_AVS },
214 #endif
215 #ifdef DLT_PRISM_HEADER
216 	{ prism_if_print,	DLT_PRISM_HEADER },
217 #endif
218 	{ ppp_if_print,		DLT_PPP },
219 #ifdef DLT_PPP_WITHDIRECTION
220 	{ ppp_if_print,		DLT_PPP_WITHDIRECTION },
221 #endif
222 #ifdef DLT_PPP_BSDOS
223 	{ ppp_bsdos_if_print,	DLT_PPP_BSDOS },
224 #endif
225 #ifdef DLT_PPP_SERIAL
226 	{ ppp_hdlc_if_print,	DLT_PPP_SERIAL },
227 #endif
228 #ifdef DLT_PFSYNC
229         { pfsync_if_print,	DLT_PFSYNC },
230 #endif
231 	{ NULL,			0 },
232 };
233 
234 static void	ndo_default_print(netdissect_options *ndo, const u_char *bp,
235 		    u_int length);
236 
237 static void	ndo_error(netdissect_options *ndo, const char *fmt, ...)
238 		    __attribute__((noreturn))
239 #ifdef __ATTRIBUTE___FORMAT_OK
240 		    __attribute__((format (printf, 2, 3)))
241 #endif /* __ATTRIBUTE___FORMAT_OK */
242 		    ;
243 static void	ndo_warning(netdissect_options *ndo, const char *fmt, ...)
244 #ifdef __ATTRIBUTE___FORMAT_OK
245 		    __attribute__((format (printf, 2, 3)))
246 #endif /* __ATTRIBUTE___FORMAT_OK */
247 		    ;
248 
249 static int	ndo_printf(netdissect_options *ndo, const char *fmt, ...)
250 #ifdef __ATTRIBUTE___FORMAT_OK
251 		     __attribute ((format (printf, 2, 3)))
252 #endif /* __ATTRIBUTE___FORMAT_OK */
253 		     ;
254 
255 void
256 init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask,
257     uint32_t timezone_offset)
258 {
259 
260 	thiszone = timezone_offset;
261 	init_addrtoname(ndo, localnet, mask);
262 	init_checksum();
263 }
264 
265 if_printer
266 lookup_printer(int type)
267 {
268 	const struct printer *p;
269 
270 	for (p = printers; p->f; ++p)
271 		if (type == p->type)
272 			return p->f;
273 
274 #if defined(DLT_USER2) && defined(DLT_PKTAP)
275 	/*
276 	 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
277 	 * header.
278 	 *
279 	 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
280 	 * based OSes or the same value as LINKTYPE_PKTAP as it is on
281 	 * other OSes, to LINKTYPE_PKTAP, so files written with
282 	 * this version of libpcap for a DLT_PKTAP capture have a link-
283 	 * layer header type of LINKTYPE_PKTAP.
284 	 *
285 	 * However, files written on OS X Mavericks for a DLT_PKTAP
286 	 * capture have a link-layer header type of LINKTYPE_USER2.
287 	 * If we don't have a printer for DLT_USER2, and type is
288 	 * DLT_USER2, we look up the printer for DLT_PKTAP and use
289 	 * that.
290 	 */
291 	if (type == DLT_USER2) {
292 		for (p = printers; p->f; ++p)
293 			if (DLT_PKTAP == p->type)
294 				return p->f;
295 	}
296 #endif
297 
298 	return NULL;
299 	/* NOTREACHED */
300 }
301 
302 int
303 has_printer(int type)
304 {
305 	return (lookup_printer(type) != NULL);
306 }
307 
308 if_printer
309 get_if_printer(netdissect_options *ndo, int type)
310 {
311 	const char *dltname;
312 	if_printer printer;
313 
314 	printer = lookup_printer(type);
315 	if (printer == NULL) {
316 		dltname = pcap_datalink_val_to_name(type);
317 		if (dltname != NULL)
318 			(*ndo->ndo_error)(ndo,
319 					  "packet printing is not supported for link type %s: use -w",
320 					  dltname);
321 		else
322 			(*ndo->ndo_error)(ndo,
323 					  "packet printing is not supported for link type %d: use -w", type);
324 	}
325 	return printer;
326 }
327 
328 void
329 pretty_print_packet(netdissect_options *ndo, const struct pcap_pkthdr *h,
330     const u_char *sp, u_int packets_captured)
331 {
332 	u_int hdrlen;
333 
334 	if(ndo->ndo_packet_number)
335 		ND_PRINT((ndo, "%5u  ", packets_captured));
336 
337 	ts_print(ndo, &h->ts);
338 
339 	/*
340 	 * Some printers want to check that they're not walking off the
341 	 * end of the packet.
342 	 * Rather than pass it all the way down, we set this member
343 	 * of the netdissect_options structure.
344 	 */
345 	ndo->ndo_snapend = sp + h->caplen;
346 
347         hdrlen = (ndo->ndo_if_printer)(ndo, h, sp);
348 
349 	/*
350 	 * Restore the original snapend, as a printer might have
351 	 * changed it.
352 	 */
353 	ndo->ndo_snapend = sp + h->caplen;
354 	if (ndo->ndo_Xflag) {
355 		/*
356 		 * Print the raw packet data in hex and ASCII.
357 		 */
358 		if (ndo->ndo_Xflag > 1) {
359 			/*
360 			 * Include the link-layer header.
361 			 */
362 			hex_and_ascii_print(ndo, "\n\t", sp, h->caplen);
363 		} else {
364 			/*
365 			 * Don't include the link-layer header - and if
366 			 * we have nothing past the link-layer header,
367 			 * print nothing.
368 			 */
369 			if (h->caplen > hdrlen)
370 				hex_and_ascii_print(ndo, "\n\t", sp + hdrlen,
371 				    h->caplen - hdrlen);
372 		}
373 	} else if (ndo->ndo_xflag) {
374 		/*
375 		 * Print the raw packet data in hex.
376 		 */
377 		if (ndo->ndo_xflag > 1) {
378 			/*
379 			 * Include the link-layer header.
380 			 */
381                         hex_print(ndo, "\n\t", sp, h->caplen);
382 		} else {
383 			/*
384 			 * Don't include the link-layer header - and if
385 			 * we have nothing past the link-layer header,
386 			 * print nothing.
387 			 */
388 			if (h->caplen > hdrlen)
389 				hex_print(ndo, "\n\t", sp + hdrlen,
390                                           h->caplen - hdrlen);
391 		}
392 	} else if (ndo->ndo_Aflag) {
393 		/*
394 		 * Print the raw packet data in ASCII.
395 		 */
396 		if (ndo->ndo_Aflag > 1) {
397 			/*
398 			 * Include the link-layer header.
399 			 */
400 			ascii_print(ndo, sp, h->caplen);
401 		} else {
402 			/*
403 			 * Don't include the link-layer header - and if
404 			 * we have nothing past the link-layer header,
405 			 * print nothing.
406 			 */
407 			if (h->caplen > hdrlen)
408 				ascii_print(ndo, sp + hdrlen, h->caplen - hdrlen);
409 		}
410 	}
411 
412 	ND_PRINT((ndo, "\n"));
413 }
414 
415 /*
416  * By default, print the specified data out in hex and ASCII.
417  */
418 static void
419 ndo_default_print(netdissect_options *ndo, const u_char *bp, u_int length)
420 {
421 	hex_and_ascii_print(ndo, "\n\t", bp, length); /* pass on lf and indentation string */
422 }
423 
424 /* VARARGS */
425 static void
426 ndo_error(netdissect_options *ndo, const char *fmt, ...)
427 {
428 	va_list ap;
429 
430 	if(ndo->program_name)
431 		(void)fprintf(stderr, "%s: ", ndo->program_name);
432 	va_start(ap, fmt);
433 	(void)vfprintf(stderr, fmt, ap);
434 	va_end(ap);
435 	if (*fmt) {
436 		fmt += strlen(fmt);
437 		if (fmt[-1] != '\n')
438 			(void)fputc('\n', stderr);
439 	}
440 	nd_cleanup();
441 	exit(1);
442 	/* NOTREACHED */
443 }
444 
445 /* VARARGS */
446 static void
447 ndo_warning(netdissect_options *ndo, const char *fmt, ...)
448 {
449 	va_list ap;
450 
451 	if(ndo->program_name)
452 		(void)fprintf(stderr, "%s: ", ndo->program_name);
453 	(void)fprintf(stderr, "WARNING: ");
454 	va_start(ap, fmt);
455 	(void)vfprintf(stderr, fmt, ap);
456 	va_end(ap);
457 	if (*fmt) {
458 		fmt += strlen(fmt);
459 		if (fmt[-1] != '\n')
460 			(void)fputc('\n', stderr);
461 	}
462 }
463 
464 static int
465 ndo_printf(netdissect_options *ndo, const char *fmt, ...)
466 {
467 	va_list args;
468 	int ret;
469 
470 	va_start(args, fmt);
471 	ret = vfprintf(stdout, fmt, args);
472 	va_end(args);
473 
474 	if (ret < 0)
475 		ndo_error(ndo, "Unable to write output: %s", pcap_strerror(errno));
476 	return (ret);
477 }
478 
479 void
480 ndo_set_function_pointers(netdissect_options *ndo)
481 {
482 	ndo->ndo_default_print=ndo_default_print;
483 	ndo->ndo_printf=ndo_printf;
484 	ndo->ndo_error=ndo_error;
485 	ndo->ndo_warning=ndo_warning;
486 }
487 /*
488  * Local Variables:
489  * c-style: whitesmith
490  * c-basic-offset: 8
491  * End:
492  */
493