Home | History | Annotate | Line # | Download | only in netipsec
xform_tcp.c revision 1.1.2.5
      1 /*	$NetBSD: xform_tcp.c,v 1.1.2.5 2005/03/04 16:53:44 skrll Exp $ */
      2 /*	$FreeBSD: sys/netipsec/xform_tcp.c,v 1.1.2.1 2004/02/14 22:24:09 bms Exp $ */
      3 
      4 /*
      5  * Copyright (c) 2003 Bruce M. Simpson <bms (at) spc.org>
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  *
     11  * 1. Redistributions of source code must retain the above copyright
     12  *   notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *   notice, this list of conditions and the following disclaimer in the
     15  *   documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *   derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /* TCP MD5 Signature Option (RFC2385) */
     32 #include "opt_inet.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 
     43 #include <netinet/in.h>
     44 #include <netinet/in_systm.h>
     45 #include <netinet/ip.h>
     46 #include <netinet/ip_var.h>
     47 #include <netinet/tcp_timer.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  */
     83 static int
     84 tcpsignature_init(struct secasvar *sav, struct xformsw *xsp)
     85 {
     86 	int keylen;
     87 
     88 	if (sav->spi != htonl(TCP_SIG_SPI)) {
     89 		DPRINTF(("%s: SPI %x must be TCP_SIG_SPI (0x1000)\n",
     90 		    __func__, sav->alg_auth));
     91 		return (EINVAL);
     92 	}
     93 	if (sav->alg_auth != SADB_X_AALG_TCP_MD5) {
     94 		DPRINTF(("%s: unsupported authentication algorithm %u\n",
     95 		    __func__, sav->alg_auth));
     96 		return (EINVAL);
     97 	}
     98 	if (sav->key_auth == NULL) {
     99 		DPRINTF(("%s: no authentication key present\n", __func__));
    100 		return (EINVAL);
    101 	}
    102 	keylen = _KEYLEN(sav->key_auth);
    103 	if ((keylen < TCP_KEYLEN_MIN) || (keylen > TCP_KEYLEN_MAX)) {
    104 		DPRINTF(("%s: invalid key length %u\n", __func__, keylen));
    105 		return (EINVAL);
    106 	}
    107 
    108 	return (0);
    109 }
    110 
    111 /*
    112  * Paranoia.
    113  *
    114  * Called when the SA is deleted.
    115  */
    116 static int
    117 tcpsignature_zeroize(struct secasvar *sav)
    118 {
    119 
    120 	if (sav->key_auth)
    121 		bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
    122 
    123 	sav->tdb_cryptoid = 0;
    124 	sav->tdb_authalgxform = NULL;
    125 	sav->tdb_xform = NULL;
    126 
    127 	return (0);
    128 }
    129 
    130 /*
    131  * Verify that an input packet passes authentication.
    132  * Called from the ipsec layer.
    133  * We do this from within tcp itself, so this routine is just a stub.
    134  */
    135 static int
    136 tcpsignature_input(struct mbuf *m, struct secasvar *sav, int skip,
    137     int protoff)
    138 {
    139 
    140 	return (0);
    141 }
    142 
    143 /*
    144  * Prepend the authentication header.
    145  * Called from the ipsec layer.
    146  * We do this from within tcp itself, so this routine is just a stub.
    147  */
    148 static int
    149 tcpsignature_output(struct mbuf *m, struct ipsecrequest *isr,
    150     struct mbuf **mp, int skip, int protoff)
    151 {
    152 
    153 	return (EINVAL);
    154 }
    155 
    156 static struct xformsw tcpsignature_xformsw = {
    157 	XF_TCPSIGNATURE,	XFT_AUTH,		"TCPMD5",
    158 	tcpsignature_init,	tcpsignature_zeroize,
    159 	tcpsignature_input,	tcpsignature_output
    160 };
    161 
    162 INITFN void
    163 tcpsignature_attach(void)
    164 {
    165 
    166 	xform_register(&tcpsignature_xformsw);
    167 }
    168 
    169 #ifdef __FreeBSD__
    170 SYSINIT(tcpsignature_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST,
    171     tcpsignature_attach, NULL)
    172 
    173 #endif
    174 
    175