xref: /freebsd/contrib/tcpdump/print-enc.c (revision ee67461e56828dd1f8de165947ba83f6d9148a87)
15b0fe478SBruce M Simpson /*	$OpenBSD: print-enc.c,v 1.7 2002/02/19 19:39:40 millert Exp $	*/
25b0fe478SBruce M Simpson 
35b0fe478SBruce M Simpson /*
45b0fe478SBruce M Simpson  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
55b0fe478SBruce M Simpson  *	The Regents of the University of California.  All rights reserved.
65b0fe478SBruce M Simpson  *
75b0fe478SBruce M Simpson  * Redistribution and use in source and binary forms, with or without
85b0fe478SBruce M Simpson  * modification, are permitted provided that: (1) source code distributions
95b0fe478SBruce M Simpson  * retain the above copyright notice and this paragraph in its entirety, (2)
105b0fe478SBruce M Simpson  * distributions including binary code include the above copyright notice and
115b0fe478SBruce M Simpson  * this paragraph in its entirety in the documentation or other materials
125b0fe478SBruce M Simpson  * provided with the distribution, and (3) all advertising materials mentioning
135b0fe478SBruce M Simpson  * features or use of this software display the following acknowledgement:
145b0fe478SBruce M Simpson  * ``This product includes software developed by the University of California,
155b0fe478SBruce M Simpson  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
165b0fe478SBruce M Simpson  * the University nor the names of its contributors may be used to endorse
175b0fe478SBruce M Simpson  * or promote products derived from this software without specific prior
185b0fe478SBruce M Simpson  * written permission.
195b0fe478SBruce M Simpson  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
205b0fe478SBruce M Simpson  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
215b0fe478SBruce M Simpson  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
225b0fe478SBruce M Simpson  */
235b0fe478SBruce M Simpson 
243340d773SGleb Smirnoff /* \summary: OpenBSD IPsec encapsulation BPF layer printer */
253340d773SGleb Smirnoff 
265b0fe478SBruce M Simpson #ifdef HAVE_CONFIG_H
27*ee67461eSJoseph Mingrone #include <config.h>
285b0fe478SBruce M Simpson #endif
295b0fe478SBruce M Simpson 
30*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
315b0fe478SBruce M Simpson 
32*ee67461eSJoseph Mingrone #define ND_LONGJMP_FROM_TCHECK
333340d773SGleb Smirnoff #include "netdissect.h"
3427df3f5dSRui Paulo #include "extract.h"
35*ee67461eSJoseph Mingrone #include "af.h"
365b0fe478SBruce M Simpson 
373c602fabSXin LI /* From $OpenBSD: if_enc.h,v 1.8 2001/06/25 05:14:00 angelos Exp $ */
383c602fabSXin LI /*
393c602fabSXin LI  * The authors of this code are John Ioannidis (ji@tla.org),
403c602fabSXin LI  * Angelos D. Keromytis (kermit@csd.uch.gr) and
413c602fabSXin LI  * Niels Provos (provos@physnet.uni-hamburg.de).
423c602fabSXin LI  *
433c602fabSXin LI  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
443c602fabSXin LI  * in November 1995.
453c602fabSXin LI  *
463c602fabSXin LI  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
473c602fabSXin LI  * by Angelos D. Keromytis.
483c602fabSXin LI  *
493c602fabSXin LI  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
503c602fabSXin LI  * and Niels Provos.
513c602fabSXin LI  *
523c602fabSXin LI  * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
533c602fabSXin LI  * and Niels Provos.
543c602fabSXin LI  * Copyright (c) 2001, Angelos D. Keromytis.
553c602fabSXin LI  *
563c602fabSXin LI  * Permission to use, copy, and modify this software with or without fee
573c602fabSXin LI  * is hereby granted, provided that this entire notice is included in
583c602fabSXin LI  * all copies of any software which is or includes a copy or
593c602fabSXin LI  * modification of this software.
603c602fabSXin LI  * You may use this code under the GNU public license if you so wish. Please
613c602fabSXin LI  * contribute changes back to the authors under this freer than GPL license
623c602fabSXin LI  * so that we may further the use of strong encryption without limitations to
633c602fabSXin LI  * all.
643c602fabSXin LI  *
653c602fabSXin LI  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
663c602fabSXin LI  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
673c602fabSXin LI  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
683c602fabSXin LI  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
693c602fabSXin LI  * PURPOSE.
703c602fabSXin LI  */
713c602fabSXin LI 
723c602fabSXin LI #define ENC_HDRLEN	12
733c602fabSXin LI 
743c602fabSXin LI /* From $OpenBSD: mbuf.h,v 1.56 2002/01/25 15:50:23 art Exp $	*/
753c602fabSXin LI #define M_CONF		0x0400  /* packet was encrypted (ESP-transport) */
763c602fabSXin LI #define M_AUTH		0x0800  /* packet was authenticated (AH) */
773c602fabSXin LI 
783c602fabSXin LI struct enchdr {
79*ee67461eSJoseph Mingrone 	nd_uint32_t af;
80*ee67461eSJoseph Mingrone 	nd_uint32_t spi;
81*ee67461eSJoseph Mingrone 	nd_uint32_t flags;
823c602fabSXin LI };
835b0fe478SBruce M Simpson 
84*ee67461eSJoseph Mingrone #define ENC_PRINT_TYPE(wh, xf, name) \
855b0fe478SBruce M Simpson 	if ((wh) & (xf)) { \
86*ee67461eSJoseph Mingrone 		ND_PRINT("%s%s", name, (wh) == (xf) ? "): " : ","); \
875b0fe478SBruce M Simpson 		(wh) &= ~(xf); \
885b0fe478SBruce M Simpson 	}
895b0fe478SBruce M Simpson 
90*ee67461eSJoseph Mingrone /*
91*ee67461eSJoseph Mingrone  * Byte-swap a 32-bit number.
92*ee67461eSJoseph Mingrone  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
93*ee67461eSJoseph Mingrone  * big-endian platforms.)
94*ee67461eSJoseph Mingrone  */
95*ee67461eSJoseph Mingrone #define	SWAPLONG(y) \
96*ee67461eSJoseph Mingrone ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
97*ee67461eSJoseph Mingrone 
98*ee67461eSJoseph Mingrone void
993c602fabSXin LI enc_if_print(netdissect_options *ndo,
100*ee67461eSJoseph Mingrone              const struct pcap_pkthdr *h, const u_char *p)
1015b0fe478SBruce M Simpson {
102*ee67461eSJoseph Mingrone 	u_int length = h->len;
103*ee67461eSJoseph Mingrone 	u_int af, flags;
1045b0fe478SBruce M Simpson 	const struct enchdr *hdr;
1055b0fe478SBruce M Simpson 
106*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "enc";
107*ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(p, ENC_HDRLEN);
108*ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len += ENC_HDRLEN;
1095b0fe478SBruce M Simpson 
1103340d773SGleb Smirnoff 	hdr = (const struct enchdr *)p;
111*ee67461eSJoseph Mingrone 	/*
112*ee67461eSJoseph Mingrone 	 * The address family and flags fields are in the byte order
113*ee67461eSJoseph Mingrone 	 * of the host that originally captured the traffic.
114*ee67461eSJoseph Mingrone 	 *
115*ee67461eSJoseph Mingrone 	 * To determine that, look at the address family.  It's 32-bit,
116*ee67461eSJoseph Mingrone 	 * it is not likely ever to be > 65535 (I doubt there will
117*ee67461eSJoseph Mingrone 	 * ever be > 65535 address families and, so far, AF_ values have
118*ee67461eSJoseph Mingrone 	 * not been allocated very sparsely) so it should not have the
119*ee67461eSJoseph Mingrone 	 * upper 16 bits set, and it is not likely ever to be AF_UNSPEC,
120*ee67461eSJoseph Mingrone 	 * i.e. it's not likely ever to be 0, so if it's byte-swapped,
121*ee67461eSJoseph Mingrone 	 * it should have at least one of the upper 16 bits set.
122*ee67461eSJoseph Mingrone 	 *
123*ee67461eSJoseph Mingrone 	 * So if any of the upper 16 bits are set, we assume it, and
124*ee67461eSJoseph Mingrone 	 * the flags field, are byte-swapped.
125*ee67461eSJoseph Mingrone 	 *
126*ee67461eSJoseph Mingrone 	 * The SPI field is always in network byte order, i.e. big-
127*ee67461eSJoseph Mingrone 	 * endian.
128*ee67461eSJoseph Mingrone 	 */
129*ee67461eSJoseph Mingrone 	UNALIGNED_MEMCPY(&af, &hdr->af, sizeof (af));
130*ee67461eSJoseph Mingrone 	UNALIGNED_MEMCPY(&flags, &hdr->flags, sizeof (flags));
131*ee67461eSJoseph Mingrone 	if ((af & 0xFFFF0000) != 0) {
132*ee67461eSJoseph Mingrone 		af = SWAPLONG(af);
133*ee67461eSJoseph Mingrone 		flags = SWAPLONG(flags);
134*ee67461eSJoseph Mingrone 	}
135*ee67461eSJoseph Mingrone 
1365b0fe478SBruce M Simpson 	if (flags == 0)
137*ee67461eSJoseph Mingrone 		ND_PRINT("(unprotected): ");
1385b0fe478SBruce M Simpson 	else
139*ee67461eSJoseph Mingrone 		ND_PRINT("(");
1405b0fe478SBruce M Simpson 	ENC_PRINT_TYPE(flags, M_AUTH, "authentic");
1415b0fe478SBruce M Simpson 	ENC_PRINT_TYPE(flags, M_CONF, "confidential");
1425b0fe478SBruce M Simpson 	/* ENC_PRINT_TYPE(flags, M_TUNNEL, "tunnel"); */
143*ee67461eSJoseph Mingrone 	ND_PRINT("SPI 0x%08x: ", GET_BE_U_4(hdr->spi));
1445b0fe478SBruce M Simpson 
1455b0fe478SBruce M Simpson 	length -= ENC_HDRLEN;
146a5779b6eSRui Paulo 	p += ENC_HDRLEN;
147a5779b6eSRui Paulo 
148*ee67461eSJoseph Mingrone 	switch (af) {
149*ee67461eSJoseph Mingrone 	case BSD_AFNUM_INET:
1503c602fabSXin LI 		ip_print(ndo, p, length);
151a5779b6eSRui Paulo 		break;
152*ee67461eSJoseph Mingrone 	case BSD_AFNUM_INET6_BSD:
153*ee67461eSJoseph Mingrone 	case BSD_AFNUM_INET6_FREEBSD:
154*ee67461eSJoseph Mingrone 	case BSD_AFNUM_INET6_DARWIN:
1553c602fabSXin LI 		ip6_print(ndo, p, length);
156a5779b6eSRui Paulo 		break;
157a5779b6eSRui Paulo 	}
1585b0fe478SBruce M Simpson }
159