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
26*ee67461eSJoseph Mingrone #include <config.h>
275b0fe478SBruce M Simpson
28*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
295b0fe478SBruce M Simpson
30*ee67461eSJoseph Mingrone #define ND_LONGJMP_FROM_TCHECK
313340d773SGleb Smirnoff #include "netdissect.h"
3227df3f5dSRui Paulo #include "extract.h"
33*ee67461eSJoseph Mingrone #include "af.h"
345b0fe478SBruce M Simpson
353c602fabSXin LI /* From $OpenBSD: if_enc.h,v 1.8 2001/06/25 05:14:00 angelos Exp $ */
363c602fabSXin LI /*
373c602fabSXin LI * The authors of this code are John Ioannidis (ji@tla.org),
383c602fabSXin LI * Angelos D. Keromytis (kermit@csd.uch.gr) and
393c602fabSXin LI * Niels Provos (provos@physnet.uni-hamburg.de).
403c602fabSXin LI *
413c602fabSXin LI * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
423c602fabSXin LI * in November 1995.
433c602fabSXin LI *
443c602fabSXin LI * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
453c602fabSXin LI * by Angelos D. Keromytis.
463c602fabSXin LI *
473c602fabSXin LI * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
483c602fabSXin LI * and Niels Provos.
493c602fabSXin LI *
503c602fabSXin LI * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
513c602fabSXin LI * and Niels Provos.
523c602fabSXin LI * Copyright (c) 2001, Angelos D. Keromytis.
533c602fabSXin LI *
543c602fabSXin LI * Permission to use, copy, and modify this software with or without fee
553c602fabSXin LI * is hereby granted, provided that this entire notice is included in
563c602fabSXin LI * all copies of any software which is or includes a copy or
573c602fabSXin LI * modification of this software.
583c602fabSXin LI * You may use this code under the GNU public license if you so wish. Please
593c602fabSXin LI * contribute changes back to the authors under this freer than GPL license
603c602fabSXin LI * so that we may further the use of strong encryption without limitations to
613c602fabSXin LI * all.
623c602fabSXin LI *
633c602fabSXin LI * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
643c602fabSXin LI * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
653c602fabSXin LI * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
663c602fabSXin LI * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
673c602fabSXin LI * PURPOSE.
683c602fabSXin LI */
693c602fabSXin LI
703c602fabSXin LI #define ENC_HDRLEN 12
713c602fabSXin LI
723c602fabSXin LI /* From $OpenBSD: mbuf.h,v 1.56 2002/01/25 15:50:23 art Exp $ */
733c602fabSXin LI #define M_CONF 0x0400 /* packet was encrypted (ESP-transport) */
743c602fabSXin LI #define M_AUTH 0x0800 /* packet was authenticated (AH) */
753c602fabSXin LI
763c602fabSXin LI struct enchdr {
77*ee67461eSJoseph Mingrone nd_uint32_t af;
78*ee67461eSJoseph Mingrone nd_uint32_t spi;
79*ee67461eSJoseph Mingrone nd_uint32_t flags;
803c602fabSXin LI };
815b0fe478SBruce M Simpson
82*ee67461eSJoseph Mingrone #define ENC_PRINT_TYPE(wh, xf, name) \
835b0fe478SBruce M Simpson if ((wh) & (xf)) { \
84*ee67461eSJoseph Mingrone ND_PRINT("%s%s", name, (wh) == (xf) ? "): " : ","); \
855b0fe478SBruce M Simpson (wh) &= ~(xf); \
865b0fe478SBruce M Simpson }
875b0fe478SBruce M Simpson
88*ee67461eSJoseph Mingrone /*
89*ee67461eSJoseph Mingrone * Byte-swap a 32-bit number.
90*ee67461eSJoseph Mingrone * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
91*ee67461eSJoseph Mingrone * big-endian platforms.)
92*ee67461eSJoseph Mingrone */
93*ee67461eSJoseph Mingrone #define SWAPLONG(y) \
94*ee67461eSJoseph Mingrone ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
95*ee67461eSJoseph Mingrone
96*ee67461eSJoseph Mingrone void
enc_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)973c602fabSXin LI enc_if_print(netdissect_options *ndo,
98*ee67461eSJoseph Mingrone const struct pcap_pkthdr *h, const u_char *p)
995b0fe478SBruce M Simpson {
100*ee67461eSJoseph Mingrone u_int length = h->len;
101*ee67461eSJoseph Mingrone u_int af, flags;
1025b0fe478SBruce M Simpson const struct enchdr *hdr;
1035b0fe478SBruce M Simpson
104*ee67461eSJoseph Mingrone ndo->ndo_protocol = "enc";
105*ee67461eSJoseph Mingrone ND_TCHECK_LEN(p, ENC_HDRLEN);
106*ee67461eSJoseph Mingrone ndo->ndo_ll_hdr_len += ENC_HDRLEN;
1075b0fe478SBruce M Simpson
1083340d773SGleb Smirnoff hdr = (const struct enchdr *)p;
109*ee67461eSJoseph Mingrone /*
110*ee67461eSJoseph Mingrone * The address family and flags fields are in the byte order
111*ee67461eSJoseph Mingrone * of the host that originally captured the traffic.
112*ee67461eSJoseph Mingrone *
113*ee67461eSJoseph Mingrone * To determine that, look at the address family. It's 32-bit,
114*ee67461eSJoseph Mingrone * it is not likely ever to be > 65535 (I doubt there will
115*ee67461eSJoseph Mingrone * ever be > 65535 address families and, so far, AF_ values have
116*ee67461eSJoseph Mingrone * not been allocated very sparsely) so it should not have the
117*ee67461eSJoseph Mingrone * upper 16 bits set, and it is not likely ever to be AF_UNSPEC,
118*ee67461eSJoseph Mingrone * i.e. it's not likely ever to be 0, so if it's byte-swapped,
119*ee67461eSJoseph Mingrone * it should have at least one of the upper 16 bits set.
120*ee67461eSJoseph Mingrone *
121*ee67461eSJoseph Mingrone * So if any of the upper 16 bits are set, we assume it, and
122*ee67461eSJoseph Mingrone * the flags field, are byte-swapped.
123*ee67461eSJoseph Mingrone *
124*ee67461eSJoseph Mingrone * The SPI field is always in network byte order, i.e. big-
125*ee67461eSJoseph Mingrone * endian.
126*ee67461eSJoseph Mingrone */
127*ee67461eSJoseph Mingrone UNALIGNED_MEMCPY(&af, &hdr->af, sizeof (af));
128*ee67461eSJoseph Mingrone UNALIGNED_MEMCPY(&flags, &hdr->flags, sizeof (flags));
129*ee67461eSJoseph Mingrone if ((af & 0xFFFF0000) != 0) {
130*ee67461eSJoseph Mingrone af = SWAPLONG(af);
131*ee67461eSJoseph Mingrone flags = SWAPLONG(flags);
132*ee67461eSJoseph Mingrone }
133*ee67461eSJoseph Mingrone
1345b0fe478SBruce M Simpson if (flags == 0)
135*ee67461eSJoseph Mingrone ND_PRINT("(unprotected): ");
1365b0fe478SBruce M Simpson else
137*ee67461eSJoseph Mingrone ND_PRINT("(");
1385b0fe478SBruce M Simpson ENC_PRINT_TYPE(flags, M_AUTH, "authentic");
1395b0fe478SBruce M Simpson ENC_PRINT_TYPE(flags, M_CONF, "confidential");
1405b0fe478SBruce M Simpson /* ENC_PRINT_TYPE(flags, M_TUNNEL, "tunnel"); */
141*ee67461eSJoseph Mingrone ND_PRINT("SPI 0x%08x: ", GET_BE_U_4(hdr->spi));
1425b0fe478SBruce M Simpson
1435b0fe478SBruce M Simpson length -= ENC_HDRLEN;
144a5779b6eSRui Paulo p += ENC_HDRLEN;
145a5779b6eSRui Paulo
146*ee67461eSJoseph Mingrone switch (af) {
147*ee67461eSJoseph Mingrone case BSD_AFNUM_INET:
1483c602fabSXin LI ip_print(ndo, p, length);
149a5779b6eSRui Paulo break;
150*ee67461eSJoseph Mingrone case BSD_AFNUM_INET6_BSD:
151*ee67461eSJoseph Mingrone case BSD_AFNUM_INET6_FREEBSD:
152*ee67461eSJoseph Mingrone case BSD_AFNUM_INET6_DARWIN:
1533c602fabSXin LI ip6_print(ndo, p, length);
154a5779b6eSRui Paulo break;
155a5779b6eSRui Paulo }
1565b0fe478SBruce M Simpson }
157