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