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