xref: /netbsd-src/external/bsd/tcpdump/dist/print-null.c (revision 26ba0b503b498a5194a71ac319838b7f5497f3fe)
10f74e101Schristos /*
20f74e101Schristos  * Copyright (c) 1991, 1993, 1994, 1995, 1996, 1997
30f74e101Schristos  *	The Regents of the University of California.  All rights reserved.
40f74e101Schristos  *
50f74e101Schristos  * Redistribution and use in source and binary forms, with or without
60f74e101Schristos  * modification, are permitted provided that: (1) source code distributions
70f74e101Schristos  * retain the above copyright notice and this paragraph in its entirety, (2)
80f74e101Schristos  * distributions including binary code include the above copyright notice and
90f74e101Schristos  * this paragraph in its entirety in the documentation or other materials
100f74e101Schristos  * provided with the distribution, and (3) all advertising materials mentioning
110f74e101Schristos  * features or use of this software display the following acknowledgement:
120f74e101Schristos  * ``This product includes software developed by the University of California,
130f74e101Schristos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
140f74e101Schristos  * the University nor the names of its contributors may be used to endorse
150f74e101Schristos  * or promote products derived from this software without specific prior
160f74e101Schristos  * written permission.
170f74e101Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
180f74e101Schristos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
190f74e101Schristos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
200f74e101Schristos  */
210f74e101Schristos 
2211b3aaa1Schristos #include <sys/cdefs.h>
230f74e101Schristos #ifndef lint
24*26ba0b50Schristos __RCSID("$NetBSD: print-null.c,v 1.10 2024/09/02 16:15:32 christos Exp $");
250f74e101Schristos #endif
260f74e101Schristos 
27dc860a36Sspz /* \summary: BSD loopback device printer */
28dc860a36Sspz 
29c74ad251Schristos #include <config.h>
300f74e101Schristos 
31c74ad251Schristos #include "netdissect-stdinc.h"
320f74e101Schristos 
33c74ad251Schristos #define ND_LONGJMP_FROM_TCHECK
34fdccd7e4Schristos #include "netdissect.h"
35c74ad251Schristos #include "extract.h"
360f74e101Schristos #include "af.h"
370f74e101Schristos 
38c74ad251Schristos 
390f74e101Schristos /*
400f74e101Schristos  * The DLT_NULL packet header is 4 bytes long. It contains a host-byte-order
410f74e101Schristos  * 32-bit integer that specifies the family, e.g. AF_INET.
420f74e101Schristos  *
430f74e101Schristos  * Note here that "host" refers to the host on which the packets were
440f74e101Schristos  * captured; that isn't necessarily *this* host.
450f74e101Schristos  *
460f74e101Schristos  * The OpenBSD DLT_LOOP packet header is the same, except that the integer
470f74e101Schristos  * is in network byte order.
480f74e101Schristos  */
490f74e101Schristos #define	NULL_HDRLEN 4
500f74e101Schristos 
510f74e101Schristos /*
520f74e101Schristos  * Byte-swap a 32-bit number.
530f74e101Schristos  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
540f74e101Schristos  * big-endian platforms.)
550f74e101Schristos  */
560f74e101Schristos #define	SWAPLONG(y) \
570f74e101Schristos ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
580f74e101Schristos 
59c74ad251Schristos static void
60c74ad251Schristos null_hdr_print(netdissect_options *ndo, uint32_t family, u_int length)
610f74e101Schristos {
62b3a00663Schristos 	if (!ndo->ndo_qflag) {
63c74ad251Schristos 		ND_PRINT("AF %s (%u)",
64c74ad251Schristos 			tok2str(bsd_af_values,"Unknown",family),family);
650f74e101Schristos 	} else {
66c74ad251Schristos 		ND_PRINT("%s",
67c74ad251Schristos 			tok2str(bsd_af_values,"Unknown AF %u",family));
680f74e101Schristos 	}
690f74e101Schristos 
70c74ad251Schristos 	ND_PRINT(", length %u: ", length);
710f74e101Schristos }
720f74e101Schristos 
730f74e101Schristos /*
740f74e101Schristos  * This is the top level routine of the printer.  'p' points
750f74e101Schristos  * to the ether header of the packet, 'h->ts' is the timestamp,
760f74e101Schristos  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
770f74e101Schristos  * is the number of bytes actually captured.
780f74e101Schristos  */
79c74ad251Schristos void
80b3a00663Schristos null_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
810f74e101Schristos {
820f74e101Schristos 	u_int length = h->len;
830f74e101Schristos 	u_int caplen = h->caplen;
84c74ad251Schristos 	uint32_t family;
850f74e101Schristos 
86c74ad251Schristos 	ndo->ndo_protocol = "null";
87c74ad251Schristos 	ND_TCHECK_LEN(p, NULL_HDRLEN);
88c74ad251Schristos 	ndo->ndo_ll_hdr_len += NULL_HDRLEN;
890f74e101Schristos 
90c74ad251Schristos 	family = GET_HE_U_4(p);
910f74e101Schristos 
920f74e101Schristos 	/*
930f74e101Schristos 	 * This isn't necessarily in our host byte order; if this is
940f74e101Schristos 	 * a DLT_LOOP capture, it's in network byte order, and if
950f74e101Schristos 	 * this is a DLT_NULL capture from a machine with the opposite
960f74e101Schristos 	 * byte-order, it's in the opposite byte order from ours.
970f74e101Schristos 	 *
980f74e101Schristos 	 * If the upper 16 bits aren't all zero, assume it's byte-swapped.
990f74e101Schristos 	 */
1000f74e101Schristos 	if ((family & 0xFFFF0000) != 0)
1010f74e101Schristos 		family = SWAPLONG(family);
1020f74e101Schristos 
103b3a00663Schristos 	if (ndo->ndo_eflag)
104b3a00663Schristos 		null_hdr_print(ndo, family, length);
1050f74e101Schristos 
1060f74e101Schristos 	length -= NULL_HDRLEN;
1070f74e101Schristos 	caplen -= NULL_HDRLEN;
1080f74e101Schristos 	p += NULL_HDRLEN;
1090f74e101Schristos 
1100f74e101Schristos 	switch (family) {
1110f74e101Schristos 
1120f74e101Schristos 	case BSD_AFNUM_INET:
113b3a00663Schristos 		ip_print(ndo, p, length);
1140f74e101Schristos 		break;
1150f74e101Schristos 
1160f74e101Schristos 	case BSD_AFNUM_INET6_BSD:
1170f74e101Schristos 	case BSD_AFNUM_INET6_FREEBSD:
1180f74e101Schristos 	case BSD_AFNUM_INET6_DARWIN:
119b3a00663Schristos 		ip6_print(ndo, p, length);
1200f74e101Schristos 		break;
1210f74e101Schristos 
1220f74e101Schristos 	case BSD_AFNUM_ISO:
12372c96ff3Schristos 		isoclns_print(ndo, p, length);
1240f74e101Schristos 		break;
1250f74e101Schristos 
1260f74e101Schristos 	case BSD_AFNUM_APPLETALK:
127b3a00663Schristos 		atalk_print(ndo, p, length);
1280f74e101Schristos 		break;
1290f74e101Schristos 
1300f74e101Schristos 	case BSD_AFNUM_IPX:
131b3a00663Schristos 		ipx_print(ndo, p, length);
1320f74e101Schristos 		break;
1330f74e101Schristos 
1340f74e101Schristos 	default:
1350f74e101Schristos 		/* unknown AF_ value */
136b3a00663Schristos 		if (!ndo->ndo_eflag)
137b3a00663Schristos 			null_hdr_print(ndo, family, length + NULL_HDRLEN);
138b3a00663Schristos 		if (!ndo->ndo_suppress_default_print)
139b3a00663Schristos 			ND_DEFAULTPRINT(p, caplen);
1400f74e101Schristos 	}
1410f74e101Schristos }
142