1 1.25 riastrad /* $NetBSD: xform_tcp.c,v 1.25 2022/05/22 11:39:08 riastradh Exp $ */ 2 1.18 maxv /* $FreeBSD: xform_tcp.c,v 1.1.2.1 2004/02/14 22:24:09 bms Exp $ */ 3 1.1 jonathan 4 1.1 jonathan /* 5 1.1 jonathan * Copyright (c) 2003 Bruce M. Simpson <bms (at) spc.org> 6 1.1 jonathan * 7 1.1 jonathan * Redistribution and use in source and binary forms, with or without 8 1.1 jonathan * modification, are permitted provided that the following conditions 9 1.1 jonathan * are met: 10 1.1 jonathan * 11 1.1 jonathan * 1. Redistributions of source code must retain the above copyright 12 1.1 jonathan * notice, this list of conditions and the following disclaimer. 13 1.1 jonathan * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 jonathan * notice, this list of conditions and the following disclaimer in the 15 1.1 jonathan * documentation and/or other materials provided with the distribution. 16 1.1 jonathan * 3. The name of the author may not be used to endorse or promote products 17 1.1 jonathan * derived from this software without specific prior written permission. 18 1.1 jonathan * 19 1.1 jonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 1.1 jonathan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 1.1 jonathan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 1.1 jonathan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 1.1 jonathan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 1.1 jonathan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 1.1 jonathan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 1.1 jonathan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 1.1 jonathan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 1.1 jonathan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 1.1 jonathan */ 30 1.1 jonathan 31 1.20 maxv /* 32 1.20 maxv * TCP MD5 Signature Option (RFC2385). Dummy code, everything is handled 33 1.20 maxv * in TCP directly. 34 1.20 maxv */ 35 1.4 lukem 36 1.4 lukem #include <sys/cdefs.h> 37 1.25 riastrad __KERNEL_RCSID(0, "$NetBSD: xform_tcp.c,v 1.25 2022/05/22 11:39:08 riastradh Exp $"); 38 1.4 lukem 39 1.9 ozaki #if defined(_KERNEL_OPT) 40 1.1 jonathan #include "opt_inet.h" 41 1.9 ozaki #endif 42 1.1 jonathan 43 1.1 jonathan #include <sys/param.h> 44 1.1 jonathan #include <sys/systm.h> 45 1.1 jonathan #include <sys/mbuf.h> 46 1.1 jonathan #include <sys/kernel.h> 47 1.1 jonathan 48 1.1 jonathan #include <netinet/in.h> 49 1.1 jonathan #include <netinet/in_systm.h> 50 1.1 jonathan #include <netinet/ip.h> 51 1.1 jonathan #include <netinet/ip_var.h> 52 1.21 ozaki #ifdef TCP_SIGNATURE 53 1.21 ozaki #include <netinet/tcp_timer.h> 54 1.21 ozaki #include <netinet/tcp.h> 55 1.21 ozaki #include <netinet/tcp_var.h> 56 1.21 ozaki #endif 57 1.1 jonathan 58 1.1 jonathan #include <netipsec/ipsec.h> 59 1.1 jonathan #include <netipsec/xform.h> 60 1.1 jonathan 61 1.1 jonathan #include <netipsec/key.h> 62 1.1 jonathan #include <netipsec/key_debug.h> 63 1.1 jonathan 64 1.1 jonathan /* 65 1.1 jonathan * Initialize a TCP-MD5 SA. Called when the SA is being set up. 66 1.1 jonathan * 67 1.1 jonathan * We don't need to set up the tdb prefixed fields, as we don't use the 68 1.1 jonathan * opencrypto code; we just perform a key length check. 69 1.1 jonathan * 70 1.1 jonathan * XXX: Currently we only allow a single 'magic' SPI to be used. 71 1.1 jonathan * 72 1.1 jonathan * This allows per-host granularity without affecting the userland 73 1.1 jonathan * interface, which is a simple socket option toggle switch, 74 1.1 jonathan * TCP_SIGNATURE_ENABLE. 75 1.1 jonathan * 76 1.1 jonathan * To allow per-service granularity requires that we have a means 77 1.1 jonathan * of mapping port to SPI. The mandated way of doing this is to 78 1.1 jonathan * use SPD entries to specify packet flows which get the TCP-MD5 79 1.1 jonathan * treatment, however the code to do this is currently unstable 80 1.1 jonathan * and unsuitable for production use. 81 1.1 jonathan * 82 1.1 jonathan * Therefore we use this compromise in the meantime. 83 1.1 jonathan */ 84 1.1 jonathan static int 85 1.7 drochner tcpsignature_init(struct secasvar *sav, const struct xformsw *xsp) 86 1.1 jonathan { 87 1.1 jonathan int keylen; 88 1.1 jonathan 89 1.1 jonathan if (sav->spi != htonl(TCP_SIG_SPI)) { 90 1.23 christos DPRINTF("SPI %x must be TCP_SIG_SPI (0x1000)\n", sav->alg_auth); 91 1.20 maxv return EINVAL; 92 1.1 jonathan } 93 1.1 jonathan if (sav->alg_auth != SADB_X_AALG_TCP_MD5) { 94 1.23 christos DPRINTF("unsupported authentication algorithm %u\n", 95 1.23 christos sav->alg_auth); 96 1.20 maxv return EINVAL; 97 1.1 jonathan } 98 1.1 jonathan if (sav->key_auth == NULL) { 99 1.23 christos DPRINTF("no authentication key present\n"); 100 1.20 maxv return EINVAL; 101 1.1 jonathan } 102 1.1 jonathan keylen = _KEYLEN(sav->key_auth); 103 1.1 jonathan if ((keylen < TCP_KEYLEN_MIN) || (keylen > TCP_KEYLEN_MAX)) { 104 1.23 christos DPRINTF("invalid key length %u\n", keylen); 105 1.20 maxv return EINVAL; 106 1.1 jonathan } 107 1.1 jonathan 108 1.20 maxv return 0; 109 1.1 jonathan } 110 1.1 jonathan 111 1.25 riastrad static void 112 1.1 jonathan tcpsignature_zeroize(struct secasvar *sav) 113 1.1 jonathan { 114 1.13 ozaki if (sav->key_auth) { 115 1.13 ozaki explicit_memset(_KEYBUF(sav->key_auth), 0, 116 1.13 ozaki _KEYLEN(sav->key_auth)); 117 1.13 ozaki } 118 1.1 jonathan 119 1.1 jonathan sav->tdb_cryptoid = 0; 120 1.1 jonathan sav->tdb_authalgxform = NULL; 121 1.1 jonathan sav->tdb_xform = NULL; 122 1.1 jonathan } 123 1.1 jonathan 124 1.1 jonathan static int 125 1.14 ozaki tcpsignature_input(struct mbuf *m, struct secasvar *sav, int skip, 126 1.1 jonathan int protoff) 127 1.1 jonathan { 128 1.20 maxv panic("%s: should not have been called", __func__); 129 1.1 jonathan } 130 1.1 jonathan 131 1.1 jonathan static int 132 1.16 ozaki tcpsignature_output(struct mbuf *m, const struct ipsecrequest *isr, 133 1.24 knakahar struct secasvar *sav, int skip, int protoff, int flags) 134 1.1 jonathan { 135 1.20 maxv panic("%s: should not have been called", __func__); 136 1.1 jonathan } 137 1.1 jonathan 138 1.1 jonathan static struct xformsw tcpsignature_xformsw = { 139 1.12 ozaki .xf_type = XF_TCPSIGNATURE, 140 1.12 ozaki .xf_flags = XFT_AUTH, 141 1.12 ozaki .xf_name = "TCPMD5", 142 1.12 ozaki .xf_init = tcpsignature_init, 143 1.12 ozaki .xf_zeroize = tcpsignature_zeroize, 144 1.12 ozaki .xf_input = tcpsignature_input, 145 1.12 ozaki .xf_output = tcpsignature_output, 146 1.12 ozaki .xf_next = NULL, 147 1.1 jonathan }; 148 1.1 jonathan 149 1.11 ozaki void 150 1.1 jonathan tcpsignature_attach(void) 151 1.1 jonathan { 152 1.1 jonathan xform_register(&tcpsignature_xformsw); 153 1.1 jonathan } 154