1127785Sbms/* $FreeBSD$ */ 2127785Sbms 3139823Simp/*- 4127785Sbms * Copyright (c) 2003 Bruce M. Simpson <bms@spc.org> 5127785Sbms * 6127785Sbms * Redistribution and use in source and binary forms, with or without 7127785Sbms * modification, are permitted provided that the following conditions 8127785Sbms * are met: 9127785Sbms * 10127785Sbms * 1. Redistributions of source code must retain the above copyright 11127785Sbms * notice, this list of conditions and the following disclaimer. 12127785Sbms * 2. Redistributions in binary form must reproduce the above copyright 13127785Sbms * notice, this list of conditions and the following disclaimer in the 14127785Sbms * documentation and/or other materials provided with the distribution. 15127785Sbms * 3. The name of the author may not be used to endorse or promote products 16127785Sbms * derived from this software without specific prior written permission. 17127785Sbms * 18127785Sbms * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19127785Sbms * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20127785Sbms * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21127785Sbms * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22127785Sbms * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23127785Sbms * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24127785Sbms * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25127785Sbms * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26127785Sbms * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27127785Sbms * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28127785Sbms */ 29127785Sbms 30127785Sbms/* TCP MD5 Signature Option (RFC2385) */ 31127785Sbms#include "opt_inet.h" 32127785Sbms#include "opt_inet6.h" 33127785Sbms 34127785Sbms#include <sys/param.h> 35127785Sbms#include <sys/systm.h> 36127785Sbms#include <sys/mbuf.h> 37127785Sbms#include <sys/lock.h> 38127785Sbms#include <sys/socket.h> 39127785Sbms#include <sys/kernel.h> 40127785Sbms#include <sys/protosw.h> 41127785Sbms#include <sys/sysctl.h> 42127785Sbms 43127785Sbms#include <netinet/in.h> 44127785Sbms#include <netinet/in_systm.h> 45127785Sbms#include <netinet/ip.h> 46127785Sbms#include <netinet/ip_var.h> 47127785Sbms#include <netinet/tcp.h> 48127785Sbms#include <netinet/tcp_var.h> 49127785Sbms 50195699Srwatson#include <net/vnet.h> 51195699Srwatson 52127785Sbms#include <netipsec/ipsec.h> 53127785Sbms#include <netipsec/xform.h> 54127785Sbms 55127785Sbms#ifdef INET6 56127785Sbms#include <netinet/ip6.h> 57127785Sbms#include <netipsec/ipsec6.h> 58127785Sbms#endif 59127785Sbms 60127785Sbms#include <netipsec/key.h> 61127785Sbms#include <netipsec/key_debug.h> 62127785Sbms 63127785Sbms/* 64127785Sbms * Initialize a TCP-MD5 SA. Called when the SA is being set up. 65127785Sbms * 66127785Sbms * We don't need to set up the tdb prefixed fields, as we don't use the 67127785Sbms * opencrypto code; we just perform a key length check. 68127785Sbms * 69127785Sbms * XXX: Currently we only allow a single 'magic' SPI to be used. 70127785Sbms * 71127785Sbms * This allows per-host granularity without affecting the userland 72127785Sbms * interface, which is a simple socket option toggle switch, 73127785Sbms * TCP_SIGNATURE_ENABLE. 74127785Sbms * 75127785Sbms * To allow per-service granularity requires that we have a means 76127785Sbms * of mapping port to SPI. The mandated way of doing this is to 77127785Sbms * use SPD entries to specify packet flows which get the TCP-MD5 78127785Sbms * treatment, however the code to do this is currently unstable 79127785Sbms * and unsuitable for production use. 80127785Sbms * 81127785Sbms * Therefore we use this compromise in the meantime. 82127785Sbms */ 83127785Sbmsstatic int 84127785Sbmstcpsignature_init(struct secasvar *sav, struct xformsw *xsp) 85127785Sbms{ 86127785Sbms int keylen; 87127785Sbms 88127785Sbms if (sav->spi != htonl(TCP_SIG_SPI)) { 89127785Sbms DPRINTF(("%s: SPI must be TCP_SIG_SPI (0x1000)\n", 90128478Sbms __func__)); 91127785Sbms return (EINVAL); 92127785Sbms } 93127785Sbms if (sav->alg_auth != SADB_X_AALG_TCP_MD5) { 94127785Sbms DPRINTF(("%s: unsupported authentication algorithm %u\n", 95127785Sbms __func__, sav->alg_auth)); 96127785Sbms return (EINVAL); 97127785Sbms } 98127785Sbms if (sav->key_auth == NULL) { 99127785Sbms DPRINTF(("%s: no authentication key present\n", __func__)); 100127785Sbms return (EINVAL); 101127785Sbms } 102127785Sbms keylen = _KEYLEN(sav->key_auth); 103127785Sbms if ((keylen < TCP_KEYLEN_MIN) || (keylen > TCP_KEYLEN_MAX)) { 104127785Sbms DPRINTF(("%s: invalid key length %u\n", __func__, keylen)); 105127785Sbms return (EINVAL); 106127785Sbms } 107127785Sbms 108127785Sbms return (0); 109127785Sbms} 110127785Sbms 111127785Sbms/* 112127785Sbms * Paranoia. 113127785Sbms * 114127785Sbms * Called when the SA is deleted. 115127785Sbms */ 116127785Sbmsstatic int 117127785Sbmstcpsignature_zeroize(struct secasvar *sav) 118127785Sbms{ 119127785Sbms 120127785Sbms if (sav->key_auth) 121157123Sgnn bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth)); 122127785Sbms 123127785Sbms sav->tdb_cryptoid = 0; 124127785Sbms sav->tdb_authalgxform = NULL; 125127785Sbms sav->tdb_xform = NULL; 126127785Sbms 127127785Sbms return (0); 128127785Sbms} 129127785Sbms 130127785Sbms/* 131127785Sbms * Verify that an input packet passes authentication. 132127785Sbms * Called from the ipsec layer. 133127785Sbms * We do this from within tcp itself, so this routine is just a stub. 134127785Sbms */ 135127785Sbmsstatic int 136127785Sbmstcpsignature_input(struct mbuf *m, struct secasvar *sav, int skip, 137127785Sbms int protoff) 138127785Sbms{ 139127785Sbms 140127785Sbms return (0); 141127785Sbms} 142127785Sbms 143127785Sbms/* 144127785Sbms * Prepend the authentication header. 145127785Sbms * Called from the ipsec layer. 146127785Sbms * We do this from within tcp itself, so this routine is just a stub. 147127785Sbms */ 148127785Sbmsstatic int 149127785Sbmstcpsignature_output(struct mbuf *m, struct ipsecrequest *isr, 150127785Sbms struct mbuf **mp, int skip, int protoff) 151127785Sbms{ 152127785Sbms 153127785Sbms return (EINVAL); 154127785Sbms} 155127785Sbms 156127785Sbmsstatic struct xformsw tcpsignature_xformsw = { 157127785Sbms XF_TCPSIGNATURE, XFT_AUTH, "TCPMD5", 158127785Sbms tcpsignature_init, tcpsignature_zeroize, 159127785Sbms tcpsignature_input, tcpsignature_output 160127785Sbms}; 161127785Sbms 162127785Sbmsstatic void 163127785Sbmstcpsignature_attach(void) 164127785Sbms{ 165127785Sbms 166127785Sbms xform_register(&tcpsignature_xformsw); 167127785Sbms} 168127785Sbms 169127785SbmsSYSINIT(tcpsignature_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, 170177481Sbz tcpsignature_attach, NULL); 171