Home | History | Annotate | Line # | Download | only in netipsec
xform_tcp.c revision 1.21.2.1
      1  1.21.2.1  christos /*	$NetBSD: xform_tcp.c,v 1.21.2.1 2019/06/10 22:09:48 christos Exp $ */
      2      1.18      maxv /*	$FreeBSD: 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.20      maxv /*
     32      1.20      maxv  * TCP MD5 Signature Option (RFC2385). Dummy code, everything is handled
     33      1.20      maxv  * in TCP directly.
     34      1.20      maxv  */
     35       1.4     lukem 
     36       1.4     lukem #include <sys/cdefs.h>
     37  1.21.2.1  christos __KERNEL_RCSID(0, "$NetBSD: xform_tcp.c,v 1.21.2.1 2019/06/10 22:09:48 christos Exp $");
     38       1.4     lukem 
     39       1.9     ozaki #if defined(_KERNEL_OPT)
     40       1.1  jonathan #include "opt_inet.h"
     41       1.9     ozaki #endif
     42       1.1  jonathan 
     43       1.1  jonathan #include <sys/param.h>
     44       1.1  jonathan #include <sys/systm.h>
     45       1.1  jonathan #include <sys/mbuf.h>
     46       1.1  jonathan #include <sys/kernel.h>
     47       1.1  jonathan 
     48       1.1  jonathan #include <netinet/in.h>
     49       1.1  jonathan #include <netinet/in_systm.h>
     50       1.1  jonathan #include <netinet/ip.h>
     51       1.1  jonathan #include <netinet/ip_var.h>
     52      1.21     ozaki #ifdef TCP_SIGNATURE
     53      1.21     ozaki #include <netinet/tcp_timer.h>
     54      1.21     ozaki #include <netinet/tcp.h>
     55      1.21     ozaki #include <netinet/tcp_var.h>
     56      1.21     ozaki #endif
     57       1.1  jonathan 
     58       1.1  jonathan #include <netipsec/ipsec.h>
     59       1.1  jonathan #include <netipsec/xform.h>
     60       1.1  jonathan 
     61       1.1  jonathan #include <netipsec/key.h>
     62       1.1  jonathan #include <netipsec/key_debug.h>
     63       1.1  jonathan 
     64       1.1  jonathan /*
     65       1.1  jonathan  * Initialize a TCP-MD5 SA. Called when the SA is being set up.
     66       1.1  jonathan  *
     67       1.1  jonathan  * We don't need to set up the tdb prefixed fields, as we don't use the
     68       1.1  jonathan  * opencrypto code; we just perform a key length check.
     69       1.1  jonathan  *
     70       1.1  jonathan  * XXX: Currently we only allow a single 'magic' SPI to be used.
     71       1.1  jonathan  *
     72       1.1  jonathan  * This allows per-host granularity without affecting the userland
     73       1.1  jonathan  * interface, which is a simple socket option toggle switch,
     74       1.1  jonathan  * TCP_SIGNATURE_ENABLE.
     75       1.1  jonathan  *
     76       1.1  jonathan  * To allow per-service granularity requires that we have a means
     77       1.1  jonathan  * of mapping port to SPI. The mandated way of doing this is to
     78       1.1  jonathan  * use SPD entries to specify packet flows which get the TCP-MD5
     79       1.1  jonathan  * treatment, however the code to do this is currently unstable
     80       1.1  jonathan  * and unsuitable for production use.
     81       1.1  jonathan  *
     82       1.1  jonathan  * Therefore we use this compromise in the meantime.
     83       1.1  jonathan  */
     84       1.1  jonathan static int
     85       1.7  drochner tcpsignature_init(struct secasvar *sav, const struct xformsw *xsp)
     86       1.1  jonathan {
     87       1.1  jonathan 	int keylen;
     88       1.1  jonathan 
     89       1.1  jonathan 	if (sav->spi != htonl(TCP_SIG_SPI)) {
     90       1.1  jonathan 		DPRINTF(("%s: SPI %x must be TCP_SIG_SPI (0x1000)\n",
     91       1.1  jonathan 		    __func__, sav->alg_auth));
     92      1.20      maxv 		return EINVAL;
     93       1.1  jonathan 	}
     94       1.1  jonathan 	if (sav->alg_auth != SADB_X_AALG_TCP_MD5) {
     95       1.1  jonathan 		DPRINTF(("%s: unsupported authentication algorithm %u\n",
     96       1.1  jonathan 		    __func__, sav->alg_auth));
     97      1.20      maxv 		return EINVAL;
     98       1.1  jonathan 	}
     99       1.1  jonathan 	if (sav->key_auth == NULL) {
    100       1.1  jonathan 		DPRINTF(("%s: no authentication key present\n", __func__));
    101      1.20      maxv 		return EINVAL;
    102       1.1  jonathan 	}
    103       1.1  jonathan 	keylen = _KEYLEN(sav->key_auth);
    104       1.1  jonathan 	if ((keylen < TCP_KEYLEN_MIN) || (keylen > TCP_KEYLEN_MAX)) {
    105       1.1  jonathan 		DPRINTF(("%s: invalid key length %u\n", __func__, keylen));
    106      1.20      maxv 		return EINVAL;
    107       1.1  jonathan 	}
    108       1.1  jonathan 
    109      1.20      maxv 	return 0;
    110       1.1  jonathan }
    111       1.1  jonathan 
    112       1.1  jonathan static int
    113       1.1  jonathan tcpsignature_zeroize(struct secasvar *sav)
    114       1.1  jonathan {
    115      1.13     ozaki 	if (sav->key_auth) {
    116      1.13     ozaki 		explicit_memset(_KEYBUF(sav->key_auth), 0,
    117      1.13     ozaki 		    _KEYLEN(sav->key_auth));
    118      1.13     ozaki 	}
    119       1.1  jonathan 
    120       1.1  jonathan 	sav->tdb_cryptoid = 0;
    121       1.1  jonathan 	sav->tdb_authalgxform = NULL;
    122       1.1  jonathan 	sav->tdb_xform = NULL;
    123       1.1  jonathan 
    124      1.20      maxv 	return 0;
    125       1.1  jonathan }
    126       1.1  jonathan 
    127       1.1  jonathan static int
    128      1.14     ozaki tcpsignature_input(struct mbuf *m, struct secasvar *sav, int skip,
    129       1.1  jonathan     int protoff)
    130       1.1  jonathan {
    131      1.20      maxv 	panic("%s: should not have been called", __func__);
    132       1.1  jonathan }
    133       1.1  jonathan 
    134       1.1  jonathan static int
    135      1.16     ozaki tcpsignature_output(struct mbuf *m, const struct ipsecrequest *isr,
    136      1.19      maxv     struct secasvar *sav, int skip, int protoff)
    137       1.1  jonathan {
    138      1.20      maxv 	panic("%s: should not have been called", __func__);
    139       1.1  jonathan }
    140       1.1  jonathan 
    141       1.1  jonathan static struct xformsw tcpsignature_xformsw = {
    142      1.12     ozaki 	.xf_type	= XF_TCPSIGNATURE,
    143      1.12     ozaki 	.xf_flags	= XFT_AUTH,
    144      1.12     ozaki 	.xf_name	= "TCPMD5",
    145      1.12     ozaki 	.xf_init	= tcpsignature_init,
    146      1.12     ozaki 	.xf_zeroize	= tcpsignature_zeroize,
    147      1.12     ozaki 	.xf_input	= tcpsignature_input,
    148      1.12     ozaki 	.xf_output	= tcpsignature_output,
    149      1.12     ozaki 	.xf_next	= NULL,
    150       1.1  jonathan };
    151       1.1  jonathan 
    152      1.11     ozaki void
    153       1.1  jonathan tcpsignature_attach(void)
    154       1.1  jonathan {
    155       1.1  jonathan 	xform_register(&tcpsignature_xformsw);
    156       1.1  jonathan }
    157