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