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