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