172770Sluigi/* $FreeBSD$ */ 272770Sluigi 372770Sluigi/*- 4155136Sluigi * Copyright (c) 2003 Bruce M. Simpson <bms@spc.org> 572770Sluigi * 6190378Sluigi * Redistribution and use in source and binary forms, with or without 772770Sluigi * modification, are permitted provided that the following conditions 8190378Sluigi * are met: 972770Sluigi * 10239738Sluigi * 1. Redistributions of source code must retain the above copyright 11239738Sluigi * notice, this list of conditions and the following disclaimer. 12190378Sluigi * 2. Redistributions in binary form must reproduce the above copyright 13190378Sluigi * notice, this list of conditions and the following disclaimer in the 1472770Sluigi * documentation and/or other materials provided with the distribution. 1572770Sluigi * 3. The name of the author may not be used to endorse or promote products 1672770Sluigi * derived from this software without specific prior written permission. 1778494Sluigi * 18190378Sluigi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1972770Sluigi * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2072770Sluigi * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2172770Sluigi * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2272770Sluigi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2372770Sluigi * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2472770Sluigi * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2572770Sluigi * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2672770Sluigi * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2772770Sluigi * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2872770Sluigi */ 2972770Sluigi 3072770Sluigi/* TCP MD5 Signature Option (RFC2385) */ 3172770Sluigi#include "opt_inet.h" 3272770Sluigi#include "opt_inet6.h" 33188835Sluigi 3472770Sluigi#include <sys/param.h> 35188835Sluigi#include <sys/systm.h> 3672770Sluigi#include <sys/mbuf.h> 37122229Ssimokawa#include <sys/lock.h> 38190378Sluigi#include <sys/socket.h> 39188835Sluigi#include <sys/kernel.h> 4072770Sluigi#include <sys/protosw.h> 4172770Sluigi#include <sys/sysctl.h> 4272770Sluigi 4372770Sluigi#include <netinet/in.h> 4472770Sluigi#include <netinet/in_systm.h> 45190378Sluigi#include <netinet/ip.h> 4684377Sluigi#include <netinet/ip_var.h> 47190378Sluigi#include <netinet/tcp.h> 48190378Sluigi#include <netinet/tcp_var.h> 49190378Sluigi 5084377Sluigi#include <net/route.h> 51190378Sluigi#include <net/vnet.h> 52190378Sluigi 53190378Sluigi#include <netipsec/ipsec.h> 54190378Sluigi#include <netipsec/xform.h> 5584377Sluigi 5684377Sluigi#ifdef INET6 5784377Sluigi#include <netinet/ip6.h> 58190378Sluigi#include <netipsec/ipsec6.h> 5984377Sluigi#endif 6084377Sluigi 6184377Sluigi#include <netipsec/key.h> 6284377Sluigi#include <netipsec/key_debug.h> 6384377Sluigi 64190378Sluigi/* 65190378Sluigi * Initialize a TCP-MD5 SA. Called when the SA is being set up. 66190378Sluigi * 67190378Sluigi * We don't need to set up the tdb prefixed fields, as we don't use the 6872770Sluigi * opencrypto code; we just perform a key length check. 6984377Sluigi * 7084377Sluigi * XXX: Currently we only allow a single 'magic' SPI to be used. 7184377Sluigi * 72227878Sluigi * This allows per-host granularity without affecting the userland 73190378Sluigi * interface, which is a simple socket option toggle switch, 7484377Sluigi * TCP_SIGNATURE_ENABLE. 7584377Sluigi * 7684377Sluigi * To allow per-service granularity requires that we have a means 7784377Sluigi * of mapping port to SPI. The mandated way of doing this is to 7872770Sluigi * use SPD entries to specify packet flows which get the TCP-MD5 7984377Sluigi * treatment, however the code to do this is currently unstable 8084377Sluigi * and unsuitable for production use. 8172770Sluigi * 8284377Sluigi * Therefore we use this compromise in the meantime. 8384377Sluigi */ 84190378Sluigistatic int 85173602Sluigitcpsignature_init(struct secasvar *sav, struct xformsw *xsp) 86173602Sluigi{ 87173602Sluigi int keylen; 88173602Sluigi 8984377Sluigi if (sav->spi != htonl(TCP_SIG_SPI)) { 9083723Sjoe DPRINTF(("%s: SPI must be TCP_SIG_SPI (0x1000)\n", 91190378Sluigi __func__)); 92190378Sluigi return (EINVAL); 93127266Sluigi } 94259410Sluigi if (sav->alg_auth != SADB_X_AALG_TCP_MD5) { 9591846Sluigi DPRINTF(("%s: unsupported authentication algorithm %u\n", 96173602Sluigi __func__, sav->alg_auth)); 9791846Sluigi return (EINVAL); 9891846Sluigi } 99190378Sluigi if (sav->key_auth == NULL) { 100190378Sluigi DPRINTF(("%s: no authentication key present\n", __func__)); 10172770Sluigi return (EINVAL); 102190378Sluigi } 10372770Sluigi keylen = _KEYLEN(sav->key_auth); 10484377Sluigi if ((keylen < TCP_KEYLEN_MIN) || (keylen > TCP_KEYLEN_MAX)) { 10572770Sluigi DPRINTF(("%s: invalid key length %u\n", __func__, keylen)); 10672770Sluigi return (EINVAL); 10772770Sluigi } 10884377Sluigi 10972770Sluigi return (0); 110190378Sluigi} 111190378Sluigi 112190378Sluigi/* 11384377Sluigi * Paranoia. 11484377Sluigi * 11584377Sluigi * Called when the SA is deleted. 116190378Sluigi */ 11784377Sluigistatic int 118190378Sluigitcpsignature_zeroize(struct secasvar *sav) 119190378Sluigi{ 12085833Sluigi 121227878Sluigi if (sav->key_auth) 12272770Sluigi bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth)); 12384377Sluigi 124155136Sluigi sav->tdb_cryptoid = 0; 125155136Sluigi sav->tdb_authalgxform = NULL; 12684377Sluigi sav->tdb_xform = NULL; 127155136Sluigi 128155136Sluigi return (0); 129155136Sluigi} 130155136Sluigi 131155136Sluigi/* 132127266Sluigi * Verify that an input packet passes authentication. 13384377Sluigi * Called from the ipsec layer. 13484377Sluigi * We do this from within tcp itself, so this routine is just a stub. 13584377Sluigi */ 136155136Sluigistatic int 13784377Sluigitcpsignature_input(struct mbuf *m, struct secasvar *sav, int skip, 138155136Sluigi int protoff) 139155136Sluigi{ 140155136Sluigi 141194635Sluigi return (0); 142188835Sluigi} 14384377Sluigi 144190378Sluigi/* 14584377Sluigi * Prepend the authentication header. 146190378Sluigi * Called from the ipsec layer. 147190378Sluigi * We do this from within tcp itself, so this routine is just a stub. 14884377Sluigi */ 14984377Sluigistatic int 150190378Sluigitcpsignature_output(struct mbuf *m, struct ipsecrequest *isr, 151127266Sluigi struct mbuf **mp, int skip, int protoff) 15284377Sluigi{ 15372770Sluigi 15472770Sluigi return (EINVAL); 15572770Sluigi} 15684377Sluigi 15784377Sluigistatic struct xformsw tcpsignature_xformsw = { 15884377Sluigi XF_TCPSIGNATURE, XFT_AUTH, "TCPMD5", 15984377Sluigi tcpsignature_init, tcpsignature_zeroize, 16072770Sluigi tcpsignature_input, tcpsignature_output 16172770Sluigi}; 162190411Sluigi 163190411Sluigistatic void 164190411Sluigitcpsignature_attach(void) 165155136Sluigi{ 166229532Sluigi 167266580Sluigi xform_register(&tcpsignature_xformsw); 168266580Sluigi} 169266580Sluigi 170266580SluigiSYSINIT(tcpsignature_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, 171266580Sluigi tcpsignature_attach, NULL); 17299946Sluigi