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