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