Home | History | Annotate | Line # | Download | only in netipsec
xform_tcp.c revision 1.4
      1  1.4     lukem /*	$NetBSD: xform_tcp.c,v 1.4 2007/12/11 12:40:10 lukem Exp $ */
      2  1.1  jonathan /*	$FreeBSD: sys/netipsec/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.1  jonathan /* TCP MD5 Signature Option (RFC2385) */
     32  1.4     lukem 
     33  1.4     lukem #include <sys/cdefs.h>
     34  1.4     lukem __KERNEL_RCSID(0, "$NetBSD: xform_tcp.c,v 1.4 2007/12/11 12:40:10 lukem Exp $");
     35  1.4     lukem 
     36  1.1  jonathan #include "opt_inet.h"
     37  1.1  jonathan 
     38  1.1  jonathan #include <sys/param.h>
     39  1.1  jonathan #include <sys/systm.h>
     40  1.1  jonathan #include <sys/mbuf.h>
     41  1.1  jonathan #include <sys/lock.h>
     42  1.1  jonathan #include <sys/socket.h>
     43  1.1  jonathan #include <sys/kernel.h>
     44  1.1  jonathan #include <sys/protosw.h>
     45  1.1  jonathan #include <sys/sysctl.h>
     46  1.1  jonathan 
     47  1.1  jonathan #include <netinet/in.h>
     48  1.1  jonathan #include <netinet/in_systm.h>
     49  1.1  jonathan #include <netinet/ip.h>
     50  1.1  jonathan #include <netinet/ip_var.h>
     51  1.1  jonathan #include <netinet/tcp_timer.h>
     52  1.1  jonathan #include <netinet/tcp.h>
     53  1.1  jonathan #include <netinet/tcp_var.h>
     54  1.1  jonathan 
     55  1.1  jonathan #include <net/route.h>
     56  1.1  jonathan #include <netipsec/ipsec.h>
     57  1.1  jonathan #include <netipsec/xform.h>
     58  1.1  jonathan 
     59  1.1  jonathan #ifdef INET6
     60  1.1  jonathan #include <netinet/ip6.h>
     61  1.1  jonathan #include <netipsec/ipsec6.h>
     62  1.1  jonathan #endif
     63  1.1  jonathan 
     64  1.1  jonathan #include <netipsec/key.h>
     65  1.1  jonathan #include <netipsec/key_debug.h>
     66  1.1  jonathan 
     67  1.1  jonathan /*
     68  1.1  jonathan  * Initialize a TCP-MD5 SA. Called when the SA is being set up.
     69  1.1  jonathan  *
     70  1.1  jonathan  * We don't need to set up the tdb prefixed fields, as we don't use the
     71  1.1  jonathan  * opencrypto code; we just perform a key length check.
     72  1.1  jonathan  *
     73  1.1  jonathan  * XXX: Currently we only allow a single 'magic' SPI to be used.
     74  1.1  jonathan  *
     75  1.1  jonathan  * This allows per-host granularity without affecting the userland
     76  1.1  jonathan  * interface, which is a simple socket option toggle switch,
     77  1.1  jonathan  * TCP_SIGNATURE_ENABLE.
     78  1.1  jonathan  *
     79  1.1  jonathan  * To allow per-service granularity requires that we have a means
     80  1.1  jonathan  * of mapping port to SPI. The mandated way of doing this is to
     81  1.1  jonathan  * use SPD entries to specify packet flows which get the TCP-MD5
     82  1.1  jonathan  * treatment, however the code to do this is currently unstable
     83  1.1  jonathan  * and unsuitable for production use.
     84  1.1  jonathan  *
     85  1.1  jonathan  * Therefore we use this compromise in the meantime.
     86  1.1  jonathan  */
     87  1.1  jonathan static int
     88  1.1  jonathan tcpsignature_init(struct secasvar *sav, struct xformsw *xsp)
     89  1.1  jonathan {
     90  1.1  jonathan 	int keylen;
     91  1.1  jonathan 
     92  1.1  jonathan 	if (sav->spi != htonl(TCP_SIG_SPI)) {
     93  1.1  jonathan 		DPRINTF(("%s: SPI %x must be TCP_SIG_SPI (0x1000)\n",
     94  1.1  jonathan 		    __func__, sav->alg_auth));
     95  1.1  jonathan 		return (EINVAL);
     96  1.1  jonathan 	}
     97  1.1  jonathan 	if (sav->alg_auth != SADB_X_AALG_TCP_MD5) {
     98  1.1  jonathan 		DPRINTF(("%s: unsupported authentication algorithm %u\n",
     99  1.1  jonathan 		    __func__, sav->alg_auth));
    100  1.1  jonathan 		return (EINVAL);
    101  1.1  jonathan 	}
    102  1.1  jonathan 	if (sav->key_auth == NULL) {
    103  1.1  jonathan 		DPRINTF(("%s: no authentication key present\n", __func__));
    104  1.1  jonathan 		return (EINVAL);
    105  1.1  jonathan 	}
    106  1.1  jonathan 	keylen = _KEYLEN(sav->key_auth);
    107  1.1  jonathan 	if ((keylen < TCP_KEYLEN_MIN) || (keylen > TCP_KEYLEN_MAX)) {
    108  1.1  jonathan 		DPRINTF(("%s: invalid key length %u\n", __func__, keylen));
    109  1.1  jonathan 		return (EINVAL);
    110  1.1  jonathan 	}
    111  1.1  jonathan 
    112  1.1  jonathan 	return (0);
    113  1.1  jonathan }
    114  1.1  jonathan 
    115  1.1  jonathan /*
    116  1.1  jonathan  * Paranoia.
    117  1.1  jonathan  *
    118  1.1  jonathan  * Called when the SA is deleted.
    119  1.1  jonathan  */
    120  1.1  jonathan static int
    121  1.1  jonathan tcpsignature_zeroize(struct secasvar *sav)
    122  1.1  jonathan {
    123  1.1  jonathan 
    124  1.1  jonathan 	if (sav->key_auth)
    125  1.1  jonathan 		bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
    126  1.1  jonathan 
    127  1.1  jonathan 	sav->tdb_cryptoid = 0;
    128  1.1  jonathan 	sav->tdb_authalgxform = NULL;
    129  1.1  jonathan 	sav->tdb_xform = NULL;
    130  1.1  jonathan 
    131  1.1  jonathan 	return (0);
    132  1.1  jonathan }
    133  1.1  jonathan 
    134  1.1  jonathan /*
    135  1.1  jonathan  * Verify that an input packet passes authentication.
    136  1.1  jonathan  * Called from the ipsec layer.
    137  1.1  jonathan  * We do this from within tcp itself, so this routine is just a stub.
    138  1.1  jonathan  */
    139  1.1  jonathan static int
    140  1.1  jonathan tcpsignature_input(struct mbuf *m, struct secasvar *sav, int skip,
    141  1.1  jonathan     int protoff)
    142  1.1  jonathan {
    143  1.1  jonathan 
    144  1.1  jonathan 	return (0);
    145  1.1  jonathan }
    146  1.1  jonathan 
    147  1.1  jonathan /*
    148  1.1  jonathan  * Prepend the authentication header.
    149  1.1  jonathan  * Called from the ipsec layer.
    150  1.1  jonathan  * We do this from within tcp itself, so this routine is just a stub.
    151  1.1  jonathan  */
    152  1.1  jonathan static int
    153  1.1  jonathan tcpsignature_output(struct mbuf *m, struct ipsecrequest *isr,
    154  1.1  jonathan     struct mbuf **mp, int skip, int protoff)
    155  1.1  jonathan {
    156  1.1  jonathan 
    157  1.1  jonathan 	return (EINVAL);
    158  1.1  jonathan }
    159  1.1  jonathan 
    160  1.1  jonathan static struct xformsw tcpsignature_xformsw = {
    161  1.1  jonathan 	XF_TCPSIGNATURE,	XFT_AUTH,		"TCPMD5",
    162  1.1  jonathan 	tcpsignature_init,	tcpsignature_zeroize,
    163  1.1  jonathan 	tcpsignature_input,	tcpsignature_output
    164  1.1  jonathan };
    165  1.1  jonathan 
    166  1.1  jonathan INITFN void
    167  1.1  jonathan tcpsignature_attach(void)
    168  1.1  jonathan {
    169  1.1  jonathan 
    170  1.1  jonathan 	xform_register(&tcpsignature_xformsw);
    171  1.1  jonathan }
    172  1.1  jonathan 
    173  1.1  jonathan #ifdef __FreeBSD__
    174  1.1  jonathan SYSINIT(tcpsignature_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST,
    175  1.1  jonathan     tcpsignature_attach, NULL)
    176  1.1  jonathan 
    177  1.1  jonathan #endif
    178  1.2     perry 
    179