xform_tcp.c revision 195699
1/*	$FreeBSD: head/sys/netipsec/xform_tcp.c 195699 2009-07-14 22:48:30Z rwatson $ */
2
3/*-
4 * Copyright (c) 2003 Bruce M. Simpson <bms@spc.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *   notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *   notice, this list of conditions and the following disclaimer in the
14 *   documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 *   derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/* TCP MD5 Signature Option (RFC2385) */
31#include "opt_inet.h"
32#include "opt_inet6.h"
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/mbuf.h>
37#include <sys/lock.h>
38#include <sys/socket.h>
39#include <sys/kernel.h>
40#include <sys/protosw.h>
41#include <sys/sysctl.h>
42#include <sys/vimage.h>
43
44#include <netinet/in.h>
45#include <netinet/in_systm.h>
46#include <netinet/ip.h>
47#include <netinet/ip_var.h>
48#include <netinet/tcp.h>
49#include <netinet/tcp_var.h>
50
51#include <net/route.h>
52#include <net/vnet.h>
53
54#include <netipsec/ipsec.h>
55#include <netipsec/xform.h>
56
57#ifdef INET6
58#include <netinet/ip6.h>
59#include <netipsec/ipsec6.h>
60#endif
61
62#include <netipsec/key.h>
63#include <netipsec/key_debug.h>
64
65/*
66 * Initialize a TCP-MD5 SA. Called when the SA is being set up.
67 *
68 * We don't need to set up the tdb prefixed fields, as we don't use the
69 * opencrypto code; we just perform a key length check.
70 *
71 * XXX: Currently we only allow a single 'magic' SPI to be used.
72 *
73 * This allows per-host granularity without affecting the userland
74 * interface, which is a simple socket option toggle switch,
75 * TCP_SIGNATURE_ENABLE.
76 *
77 * To allow per-service granularity requires that we have a means
78 * of mapping port to SPI. The mandated way of doing this is to
79 * use SPD entries to specify packet flows which get the TCP-MD5
80 * treatment, however the code to do this is currently unstable
81 * and unsuitable for production use.
82 *
83 * Therefore we use this compromise in the meantime.
84 */
85static int
86tcpsignature_init(struct secasvar *sav, struct xformsw *xsp)
87{
88	int keylen;
89
90	if (sav->spi != htonl(TCP_SIG_SPI)) {
91		DPRINTF(("%s: SPI must be TCP_SIG_SPI (0x1000)\n",
92		    __func__));
93		return (EINVAL);
94	}
95	if (sav->alg_auth != SADB_X_AALG_TCP_MD5) {
96		DPRINTF(("%s: unsupported authentication algorithm %u\n",
97		    __func__, sav->alg_auth));
98		return (EINVAL);
99	}
100	if (sav->key_auth == NULL) {
101		DPRINTF(("%s: no authentication key present\n", __func__));
102		return (EINVAL);
103	}
104	keylen = _KEYLEN(sav->key_auth);
105	if ((keylen < TCP_KEYLEN_MIN) || (keylen > TCP_KEYLEN_MAX)) {
106		DPRINTF(("%s: invalid key length %u\n", __func__, keylen));
107		return (EINVAL);
108	}
109
110	return (0);
111}
112
113/*
114 * Paranoia.
115 *
116 * Called when the SA is deleted.
117 */
118static int
119tcpsignature_zeroize(struct secasvar *sav)
120{
121
122	if (sav->key_auth)
123		bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
124
125	sav->tdb_cryptoid = 0;
126	sav->tdb_authalgxform = NULL;
127	sav->tdb_xform = NULL;
128
129	return (0);
130}
131
132/*
133 * Verify that an input packet passes authentication.
134 * Called from the ipsec layer.
135 * We do this from within tcp itself, so this routine is just a stub.
136 */
137static int
138tcpsignature_input(struct mbuf *m, struct secasvar *sav, int skip,
139    int protoff)
140{
141
142	return (0);
143}
144
145/*
146 * Prepend the authentication header.
147 * Called from the ipsec layer.
148 * We do this from within tcp itself, so this routine is just a stub.
149 */
150static int
151tcpsignature_output(struct mbuf *m, struct ipsecrequest *isr,
152    struct mbuf **mp, int skip, int protoff)
153{
154
155	return (EINVAL);
156}
157
158static struct xformsw tcpsignature_xformsw = {
159	XF_TCPSIGNATURE,	XFT_AUTH,		"TCPMD5",
160	tcpsignature_init,	tcpsignature_zeroize,
161	tcpsignature_input,	tcpsignature_output
162};
163
164static void
165tcpsignature_attach(void)
166{
167
168	xform_register(&tcpsignature_xformsw);
169}
170
171SYSINIT(tcpsignature_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST,
172    tcpsignature_attach, NULL);
173