xref: /netbsd-src/external/bsd/tcpdump/dist/print-enc.c (revision 26ba0b503b498a5194a71ac319838b7f5497f3fe)
10f74e101Schristos /*	$OpenBSD: print-enc.c,v 1.7 2002/02/19 19:39:40 millert Exp $	*/
20f74e101Schristos 
30f74e101Schristos /*
40f74e101Schristos  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
50f74e101Schristos  *	The Regents of the University of California.  All rights reserved.
60f74e101Schristos  *
70f74e101Schristos  * Redistribution and use in source and binary forms, with or without
80f74e101Schristos  * modification, are permitted provided that: (1) source code distributions
90f74e101Schristos  * retain the above copyright notice and this paragraph in its entirety, (2)
100f74e101Schristos  * distributions including binary code include the above copyright notice and
110f74e101Schristos  * this paragraph in its entirety in the documentation or other materials
120f74e101Schristos  * provided with the distribution, and (3) all advertising materials mentioning
130f74e101Schristos  * features or use of this software display the following acknowledgement:
140f74e101Schristos  * ``This product includes software developed by the University of California,
150f74e101Schristos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
160f74e101Schristos  * the University nor the names of its contributors may be used to endorse
170f74e101Schristos  * or promote products derived from this software without specific prior
180f74e101Schristos  * written permission.
190f74e101Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
200f74e101Schristos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
210f74e101Schristos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
220f74e101Schristos  */
230f74e101Schristos 
2411b3aaa1Schristos #include <sys/cdefs.h>
250f74e101Schristos #ifndef lint
26*26ba0b50Schristos __RCSID("$NetBSD: print-enc.c,v 1.9 2024/09/02 16:15:31 christos Exp $");
270f74e101Schristos #endif
280f74e101Schristos 
29dc860a36Sspz /* \summary: OpenBSD IPsec encapsulation BPF layer printer */
30dc860a36Sspz 
31c74ad251Schristos #include <config.h>
320f74e101Schristos 
33c74ad251Schristos #include "netdissect-stdinc.h"
340f74e101Schristos 
35c74ad251Schristos #define ND_LONGJMP_FROM_TCHECK
36fdccd7e4Schristos #include "netdissect.h"
370f74e101Schristos #include "extract.h"
38c74ad251Schristos #include "af.h"
390f74e101Schristos 
40b3a00663Schristos /* From $OpenBSD: if_enc.h,v 1.8 2001/06/25 05:14:00 angelos Exp $ */
41b3a00663Schristos /*
42b3a00663Schristos  * The authors of this code are John Ioannidis (ji@tla.org),
43b3a00663Schristos  * Angelos D. Keromytis (kermit@csd.uch.gr) and
44b3a00663Schristos  * Niels Provos (provos@physnet.uni-hamburg.de).
45b3a00663Schristos  *
46b3a00663Schristos  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
47b3a00663Schristos  * in November 1995.
48b3a00663Schristos  *
49b3a00663Schristos  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
50b3a00663Schristos  * by Angelos D. Keromytis.
51b3a00663Schristos  *
52b3a00663Schristos  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
53b3a00663Schristos  * and Niels Provos.
54b3a00663Schristos  *
55b3a00663Schristos  * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
56b3a00663Schristos  * and Niels Provos.
57b3a00663Schristos  * Copyright (c) 2001, Angelos D. Keromytis.
58b3a00663Schristos  *
59b3a00663Schristos  * Permission to use, copy, and modify this software with or without fee
60b3a00663Schristos  * is hereby granted, provided that this entire notice is included in
61b3a00663Schristos  * all copies of any software which is or includes a copy or
62b3a00663Schristos  * modification of this software.
63b3a00663Schristos  * You may use this code under the GNU public license if you so wish. Please
64b3a00663Schristos  * contribute changes back to the authors under this freer than GPL license
65b3a00663Schristos  * so that we may further the use of strong encryption without limitations to
66b3a00663Schristos  * all.
67b3a00663Schristos  *
68b3a00663Schristos  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
69b3a00663Schristos  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
70b3a00663Schristos  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
71b3a00663Schristos  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
72b3a00663Schristos  * PURPOSE.
73b3a00663Schristos  */
74b3a00663Schristos 
75b3a00663Schristos #define ENC_HDRLEN	12
76b3a00663Schristos 
77b3a00663Schristos /* From $OpenBSD: mbuf.h,v 1.56 2002/01/25 15:50:23 art Exp $	*/
78b3a00663Schristos #define M_CONF		0x0400  /* packet was encrypted (ESP-transport) */
79b3a00663Schristos #define M_AUTH		0x0800  /* packet was authenticated (AH) */
80b3a00663Schristos 
81b3a00663Schristos struct enchdr {
82c74ad251Schristos 	nd_uint32_t af;
83c74ad251Schristos 	nd_uint32_t spi;
84c74ad251Schristos 	nd_uint32_t flags;
85b3a00663Schristos };
860f74e101Schristos 
87c74ad251Schristos #define ENC_PRINT_TYPE(wh, xf, name) \
880f74e101Schristos 	if ((wh) & (xf)) { \
89c74ad251Schristos 		ND_PRINT("%s%s", name, (wh) == (xf) ? "): " : ","); \
900f74e101Schristos 		(wh) &= ~(xf); \
910f74e101Schristos 	}
920f74e101Schristos 
93c74ad251Schristos /*
94c74ad251Schristos  * Byte-swap a 32-bit number.
95c74ad251Schristos  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
96c74ad251Schristos  * big-endian platforms.)
97c74ad251Schristos  */
98c74ad251Schristos #define	SWAPLONG(y) \
99c74ad251Schristos ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
100c74ad251Schristos 
101c74ad251Schristos void
102b3a00663Schristos enc_if_print(netdissect_options *ndo,
103c74ad251Schristos              const struct pcap_pkthdr *h, const u_char *p)
1040f74e101Schristos {
105c74ad251Schristos 	u_int length = h->len;
106c74ad251Schristos 	u_int af, flags;
1070f74e101Schristos 	const struct enchdr *hdr;
1080f74e101Schristos 
109c74ad251Schristos 	ndo->ndo_protocol = "enc";
110c74ad251Schristos 	ND_TCHECK_LEN(p, ENC_HDRLEN);
111c74ad251Schristos 	ndo->ndo_ll_hdr_len += ENC_HDRLEN;
1120f74e101Schristos 
113fdccd7e4Schristos 	hdr = (const struct enchdr *)p;
114c74ad251Schristos 	/*
115c74ad251Schristos 	 * The address family and flags fields are in the byte order
116c74ad251Schristos 	 * of the host that originally captured the traffic.
117c74ad251Schristos 	 *
118c74ad251Schristos 	 * To determine that, look at the address family.  It's 32-bit,
119c74ad251Schristos 	 * it is not likely ever to be > 65535 (I doubt there will
120c74ad251Schristos 	 * ever be > 65535 address families and, so far, AF_ values have
121c74ad251Schristos 	 * not been allocated very sparsely) so it should not have the
122c74ad251Schristos 	 * upper 16 bits set, and it is not likely ever to be AF_UNSPEC,
123c74ad251Schristos 	 * i.e. it's not likely ever to be 0, so if it's byte-swapped,
124c74ad251Schristos 	 * it should have at least one of the upper 16 bits set.
125c74ad251Schristos 	 *
126c74ad251Schristos 	 * So if any of the upper 16 bits are set, we assume it, and
127c74ad251Schristos 	 * the flags field, are byte-swapped.
128c74ad251Schristos 	 *
129c74ad251Schristos 	 * The SPI field is always in network byte order, i.e. big-
130c74ad251Schristos 	 * endian.
131c74ad251Schristos 	 */
132c74ad251Schristos 	UNALIGNED_MEMCPY(&af, &hdr->af, sizeof (af));
133c74ad251Schristos 	UNALIGNED_MEMCPY(&flags, &hdr->flags, sizeof (flags));
134c74ad251Schristos 	if ((af & 0xFFFF0000) != 0) {
135c74ad251Schristos 		af = SWAPLONG(af);
136c74ad251Schristos 		flags = SWAPLONG(flags);
137c74ad251Schristos 	}
138c74ad251Schristos 
1390f74e101Schristos 	if (flags == 0)
140c74ad251Schristos 		ND_PRINT("(unprotected): ");
1410f74e101Schristos 	else
142c74ad251Schristos 		ND_PRINT("(");
1430f74e101Schristos 	ENC_PRINT_TYPE(flags, M_AUTH, "authentic");
1440f74e101Schristos 	ENC_PRINT_TYPE(flags, M_CONF, "confidential");
1450f74e101Schristos 	/* ENC_PRINT_TYPE(flags, M_TUNNEL, "tunnel"); */
146c74ad251Schristos 	ND_PRINT("SPI 0x%08x: ", GET_BE_U_4(hdr->spi));
1470f74e101Schristos 
1480f74e101Schristos 	length -= ENC_HDRLEN;
1490f74e101Schristos 	p += ENC_HDRLEN;
1500f74e101Schristos 
151c74ad251Schristos 	switch (af) {
152c74ad251Schristos 	case BSD_AFNUM_INET:
153b3a00663Schristos 		ip_print(ndo, p, length);
1540f74e101Schristos 		break;
155c74ad251Schristos 	case BSD_AFNUM_INET6_BSD:
156c74ad251Schristos 	case BSD_AFNUM_INET6_FREEBSD:
157c74ad251Schristos 	case BSD_AFNUM_INET6_DARWIN:
158b3a00663Schristos 		ip6_print(ndo, p, length);
1590f74e101Schristos 		break;
1600f74e101Schristos 	}
1610f74e101Schristos }
162