xform_tcp.c revision 194357
1/* $FreeBSD: head/sys/netipsec/xform_tcp.c 194357 2009-06-17 12:44:11Z bz $ */ 2 3/*- 4 * Copyright (c) 2003 Bruce M. Simpson <bms@spc.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30/* TCP MD5 Signature Option (RFC2385) */ 31#include "opt_inet.h" 32#include "opt_inet6.h" 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/mbuf.h> 37#include <sys/lock.h> 38#include <sys/socket.h> 39#include <sys/kernel.h> 40#include <sys/protosw.h> 41#include <sys/sysctl.h> 42#include <sys/vimage.h> 43 44#include <netinet/in.h> 45#include <netinet/in_systm.h> 46#include <netinet/ip.h> 47#include <netinet/ip_var.h> 48#include <netinet/tcp.h> 49#include <netinet/tcp_var.h> 50 51#include <net/route.h> 52#include <netipsec/ipsec.h> 53#include <netipsec/xform.h> 54 55#ifdef INET6 56#include <netinet/ip6.h> 57#include <netipsec/ipsec6.h> 58#endif 59 60#include <netipsec/key.h> 61#include <netipsec/key_debug.h> 62 63/* 64 * Initialize a TCP-MD5 SA. Called when the SA is being set up. 65 * 66 * We don't need to set up the tdb prefixed fields, as we don't use the 67 * opencrypto code; we just perform a key length check. 68 * 69 * XXX: Currently we only allow a single 'magic' SPI to be used. 70 * 71 * This allows per-host granularity without affecting the userland 72 * interface, which is a simple socket option toggle switch, 73 * TCP_SIGNATURE_ENABLE. 74 * 75 * To allow per-service granularity requires that we have a means 76 * of mapping port to SPI. The mandated way of doing this is to 77 * use SPD entries to specify packet flows which get the TCP-MD5 78 * treatment, however the code to do this is currently unstable 79 * and unsuitable for production use. 80 * 81 * Therefore we use this compromise in the meantime. 82 */ 83static int 84tcpsignature_init(struct secasvar *sav, struct xformsw *xsp) 85{ 86 INIT_VNET_IPSEC(curvnet); 87 int keylen; 88 89 if (sav->spi != htonl(TCP_SIG_SPI)) { 90 DPRINTF(("%s: SPI must be TCP_SIG_SPI (0x1000)\n", 91 __func__)); 92 return (EINVAL); 93 } 94 if (sav->alg_auth != SADB_X_AALG_TCP_MD5) { 95 DPRINTF(("%s: unsupported authentication algorithm %u\n", 96 __func__, sav->alg_auth)); 97 return (EINVAL); 98 } 99 if (sav->key_auth == NULL) { 100 DPRINTF(("%s: no authentication key present\n", __func__)); 101 return (EINVAL); 102 } 103 keylen = _KEYLEN(sav->key_auth); 104 if ((keylen < TCP_KEYLEN_MIN) || (keylen > TCP_KEYLEN_MAX)) { 105 DPRINTF(("%s: invalid key length %u\n", __func__, keylen)); 106 return (EINVAL); 107 } 108 109 return (0); 110} 111 112/* 113 * Paranoia. 114 * 115 * Called when the SA is deleted. 116 */ 117static int 118tcpsignature_zeroize(struct secasvar *sav) 119{ 120 121 if (sav->key_auth) 122 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth)); 123 124 sav->tdb_cryptoid = 0; 125 sav->tdb_authalgxform = NULL; 126 sav->tdb_xform = NULL; 127 128 return (0); 129} 130 131/* 132 * Verify that an input packet passes authentication. 133 * Called from the ipsec layer. 134 * We do this from within tcp itself, so this routine is just a stub. 135 */ 136static int 137tcpsignature_input(struct mbuf *m, struct secasvar *sav, int skip, 138 int protoff) 139{ 140 141 return (0); 142} 143 144/* 145 * Prepend the authentication header. 146 * Called from the ipsec layer. 147 * We do this from within tcp itself, so this routine is just a stub. 148 */ 149static int 150tcpsignature_output(struct mbuf *m, struct ipsecrequest *isr, 151 struct mbuf **mp, int skip, int protoff) 152{ 153 154 return (EINVAL); 155} 156 157static struct xformsw tcpsignature_xformsw = { 158 XF_TCPSIGNATURE, XFT_AUTH, "TCPMD5", 159 tcpsignature_init, tcpsignature_zeroize, 160 tcpsignature_input, tcpsignature_output 161}; 162 163static void 164tcpsignature_attach(void) 165{ 166 167 xform_register(&tcpsignature_xformsw); 168} 169 170SYSINIT(tcpsignature_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, 171 tcpsignature_attach, NULL); 172