xref: /dflybsd-src/contrib/tcpdump/print-enc.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
141c99275SPeter Avalos /*	$OpenBSD: print-enc.c,v 1.7 2002/02/19 19:39:40 millert Exp $	*/
241c99275SPeter Avalos 
341c99275SPeter Avalos /*
441c99275SPeter Avalos  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
541c99275SPeter Avalos  *	The Regents of the University of California.  All rights reserved.
641c99275SPeter Avalos  *
741c99275SPeter Avalos  * Redistribution and use in source and binary forms, with or without
841c99275SPeter Avalos  * modification, are permitted provided that: (1) source code distributions
941c99275SPeter Avalos  * retain the above copyright notice and this paragraph in its entirety, (2)
1041c99275SPeter Avalos  * distributions including binary code include the above copyright notice and
1141c99275SPeter Avalos  * this paragraph in its entirety in the documentation or other materials
1241c99275SPeter Avalos  * provided with the distribution, and (3) all advertising materials mentioning
1341c99275SPeter Avalos  * features or use of this software display the following acknowledgement:
1441c99275SPeter Avalos  * ``This product includes software developed by the University of California,
1541c99275SPeter Avalos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1641c99275SPeter Avalos  * the University nor the names of its contributors may be used to endorse
1741c99275SPeter Avalos  * or promote products derived from this software without specific prior
1841c99275SPeter Avalos  * written permission.
1941c99275SPeter Avalos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
2041c99275SPeter Avalos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
2141c99275SPeter Avalos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2241c99275SPeter Avalos  */
2341c99275SPeter Avalos 
24411677aeSAaron LI /* \summary: OpenBSD IPsec encapsulation BPF layer printer */
2541c99275SPeter Avalos 
2641c99275SPeter Avalos #ifdef HAVE_CONFIG_H
27*ed775ee7SAntonio Huete Jimenez #include <config.h>
2841c99275SPeter Avalos #endif
2941c99275SPeter Avalos 
30*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
3141c99275SPeter Avalos 
32*ed775ee7SAntonio Huete Jimenez #define ND_LONGJMP_FROM_TCHECK
33411677aeSAaron LI #include "netdissect.h"
3427bfbee1SPeter Avalos #include "extract.h"
35*ed775ee7SAntonio Huete Jimenez #include "af.h"
3641c99275SPeter Avalos 
37411677aeSAaron LI /* From $OpenBSD: if_enc.h,v 1.8 2001/06/25 05:14:00 angelos Exp $ */
38411677aeSAaron LI /*
39411677aeSAaron LI  * The authors of this code are John Ioannidis (ji@tla.org),
40411677aeSAaron LI  * Angelos D. Keromytis (kermit@csd.uch.gr) and
41411677aeSAaron LI  * Niels Provos (provos@physnet.uni-hamburg.de).
42411677aeSAaron LI  *
43411677aeSAaron LI  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
44411677aeSAaron LI  * in November 1995.
45411677aeSAaron LI  *
46411677aeSAaron LI  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
47411677aeSAaron LI  * by Angelos D. Keromytis.
48411677aeSAaron LI  *
49411677aeSAaron LI  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
50411677aeSAaron LI  * and Niels Provos.
51411677aeSAaron LI  *
52411677aeSAaron LI  * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
53411677aeSAaron LI  * and Niels Provos.
54411677aeSAaron LI  * Copyright (c) 2001, Angelos D. Keromytis.
55411677aeSAaron LI  *
56411677aeSAaron LI  * Permission to use, copy, and modify this software with or without fee
57411677aeSAaron LI  * is hereby granted, provided that this entire notice is included in
58411677aeSAaron LI  * all copies of any software which is or includes a copy or
59411677aeSAaron LI  * modification of this software.
60411677aeSAaron LI  * You may use this code under the GNU public license if you so wish. Please
61411677aeSAaron LI  * contribute changes back to the authors under this freer than GPL license
62411677aeSAaron LI  * so that we may further the use of strong encryption without limitations to
63411677aeSAaron LI  * all.
64411677aeSAaron LI  *
65411677aeSAaron LI  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
66411677aeSAaron LI  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
67411677aeSAaron LI  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
68411677aeSAaron LI  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
69411677aeSAaron LI  * PURPOSE.
70411677aeSAaron LI  */
71411677aeSAaron LI 
72411677aeSAaron LI #define ENC_HDRLEN	12
73411677aeSAaron LI 
74411677aeSAaron LI /* From $OpenBSD: mbuf.h,v 1.56 2002/01/25 15:50:23 art Exp $	*/
75411677aeSAaron LI #define M_CONF		0x0400  /* packet was encrypted (ESP-transport) */
76411677aeSAaron LI #define M_AUTH		0x0800  /* packet was authenticated (AH) */
77411677aeSAaron LI 
78411677aeSAaron LI struct enchdr {
79*ed775ee7SAntonio Huete Jimenez 	nd_uint32_t af;
80*ed775ee7SAntonio Huete Jimenez 	nd_uint32_t spi;
81*ed775ee7SAntonio Huete Jimenez 	nd_uint32_t flags;
82411677aeSAaron LI };
8341c99275SPeter Avalos 
84*ed775ee7SAntonio Huete Jimenez #define ENC_PRINT_TYPE(wh, xf, name) \
8541c99275SPeter Avalos 	if ((wh) & (xf)) { \
86*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s%s", name, (wh) == (xf) ? "): " : ","); \
8741c99275SPeter Avalos 		(wh) &= ~(xf); \
8841c99275SPeter Avalos 	}
8941c99275SPeter Avalos 
90*ed775ee7SAntonio Huete Jimenez /*
91*ed775ee7SAntonio Huete Jimenez  * Byte-swap a 32-bit number.
92*ed775ee7SAntonio Huete Jimenez  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
93*ed775ee7SAntonio Huete Jimenez  * big-endian platforms.)
94*ed775ee7SAntonio Huete Jimenez  */
95*ed775ee7SAntonio Huete Jimenez #define	SWAPLONG(y) \
96*ed775ee7SAntonio Huete Jimenez ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
97*ed775ee7SAntonio Huete Jimenez 
98*ed775ee7SAntonio Huete Jimenez void
enc_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)99411677aeSAaron LI enc_if_print(netdissect_options *ndo,
100*ed775ee7SAntonio Huete Jimenez              const struct pcap_pkthdr *h, const u_char *p)
10141c99275SPeter Avalos {
102*ed775ee7SAntonio Huete Jimenez 	u_int length = h->len;
103*ed775ee7SAntonio Huete Jimenez 	u_int caplen = h->caplen;
104*ed775ee7SAntonio Huete Jimenez 	u_int af, flags;
10541c99275SPeter Avalos 	const struct enchdr *hdr;
10641c99275SPeter Avalos 
107*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "enc";
108*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(p, ENC_HDRLEN);
109*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_ll_hdr_len += ENC_HDRLEN;
11041c99275SPeter Avalos 
111411677aeSAaron LI 	hdr = (const struct enchdr *)p;
112*ed775ee7SAntonio Huete Jimenez 	/*
113*ed775ee7SAntonio Huete Jimenez 	 * The address family and flags fields are in the byte order
114*ed775ee7SAntonio Huete Jimenez 	 * of the host that originally captured the traffic.
115*ed775ee7SAntonio Huete Jimenez 	 *
116*ed775ee7SAntonio Huete Jimenez 	 * To determine that, look at the address family.  It's 32-bit,
117*ed775ee7SAntonio Huete Jimenez 	 * it is not likely ever to be > 65535 (I doubt there will
118*ed775ee7SAntonio Huete Jimenez 	 * ever be > 65535 address families and, so far, AF_ values have
119*ed775ee7SAntonio Huete Jimenez 	 * not been allocated very sparsely) so it should not have the
120*ed775ee7SAntonio Huete Jimenez 	 * upper 16 bits set, and it is not likely ever to be AF_UNSPEC,
121*ed775ee7SAntonio Huete Jimenez 	 * i.e. it's not likely ever to be 0, so if it's byte-swapped,
122*ed775ee7SAntonio Huete Jimenez 	 * it should have at least one of the upper 16 bits set.
123*ed775ee7SAntonio Huete Jimenez 	 *
124*ed775ee7SAntonio Huete Jimenez 	 * So if any of the upper 16 bits are set, we assume it, and
125*ed775ee7SAntonio Huete Jimenez 	 * the flags field, are byte-swapped.
126*ed775ee7SAntonio Huete Jimenez 	 *
127*ed775ee7SAntonio Huete Jimenez 	 * The SPI field is always in network byte order, i.e. big-
128*ed775ee7SAntonio Huete Jimenez 	 * endian.
129*ed775ee7SAntonio Huete Jimenez 	 */
130*ed775ee7SAntonio Huete Jimenez 	UNALIGNED_MEMCPY(&af, &hdr->af, sizeof (af));
131*ed775ee7SAntonio Huete Jimenez 	UNALIGNED_MEMCPY(&flags, &hdr->flags, sizeof (flags));
132*ed775ee7SAntonio Huete Jimenez 	if ((af & 0xFFFF0000) != 0) {
133*ed775ee7SAntonio Huete Jimenez 		af = SWAPLONG(af);
134*ed775ee7SAntonio Huete Jimenez 		flags = SWAPLONG(flags);
135*ed775ee7SAntonio Huete Jimenez 	}
136*ed775ee7SAntonio Huete Jimenez 
13741c99275SPeter Avalos 	if (flags == 0)
138*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("(unprotected): ");
13941c99275SPeter Avalos 	else
140*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("(");
14141c99275SPeter Avalos 	ENC_PRINT_TYPE(flags, M_AUTH, "authentic");
14241c99275SPeter Avalos 	ENC_PRINT_TYPE(flags, M_CONF, "confidential");
14341c99275SPeter Avalos 	/* ENC_PRINT_TYPE(flags, M_TUNNEL, "tunnel"); */
144*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("SPI 0x%08x: ", GET_BE_U_4(hdr->spi));
14541c99275SPeter Avalos 
14641c99275SPeter Avalos 	length -= ENC_HDRLEN;
147ea7b4bf5SPeter Avalos 	caplen -= ENC_HDRLEN;
148ea7b4bf5SPeter Avalos 	p += ENC_HDRLEN;
149ea7b4bf5SPeter Avalos 
150*ed775ee7SAntonio Huete Jimenez 	switch (af) {
151*ed775ee7SAntonio Huete Jimenez 	case BSD_AFNUM_INET:
152411677aeSAaron LI 		ip_print(ndo, p, length);
153ea7b4bf5SPeter Avalos 		break;
154*ed775ee7SAntonio Huete Jimenez 	case BSD_AFNUM_INET6_BSD:
155*ed775ee7SAntonio Huete Jimenez 	case BSD_AFNUM_INET6_FREEBSD:
156*ed775ee7SAntonio Huete Jimenez 	case BSD_AFNUM_INET6_DARWIN:
157411677aeSAaron LI 		ip6_print(ndo, p, length);
158ea7b4bf5SPeter Avalos 		break;
159ea7b4bf5SPeter Avalos 	}
16041c99275SPeter Avalos }
161