1127785Sbms/*	$FreeBSD$ */
2127785Sbms
3139823Simp/*-
4127785Sbms * Copyright (c) 2003 Bruce M. Simpson <bms@spc.org>
5127785Sbms *
6127785Sbms * Redistribution and use in source and binary forms, with or without
7127785Sbms * modification, are permitted provided that the following conditions
8127785Sbms * are met:
9127785Sbms *
10127785Sbms * 1. Redistributions of source code must retain the above copyright
11127785Sbms *   notice, this list of conditions and the following disclaimer.
12127785Sbms * 2. Redistributions in binary form must reproduce the above copyright
13127785Sbms *   notice, this list of conditions and the following disclaimer in the
14127785Sbms *   documentation and/or other materials provided with the distribution.
15127785Sbms * 3. The name of the author may not be used to endorse or promote products
16127785Sbms *   derived from this software without specific prior written permission.
17127785Sbms *
18127785Sbms * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19127785Sbms * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20127785Sbms * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21127785Sbms * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22127785Sbms * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23127785Sbms * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24127785Sbms * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25127785Sbms * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26127785Sbms * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27127785Sbms * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28127785Sbms */
29127785Sbms
30127785Sbms/* TCP MD5 Signature Option (RFC2385) */
31127785Sbms#include "opt_inet.h"
32127785Sbms#include "opt_inet6.h"
33127785Sbms
34127785Sbms#include <sys/param.h>
35127785Sbms#include <sys/systm.h>
36127785Sbms#include <sys/mbuf.h>
37127785Sbms#include <sys/lock.h>
38127785Sbms#include <sys/socket.h>
39127785Sbms#include <sys/kernel.h>
40127785Sbms#include <sys/protosw.h>
41127785Sbms#include <sys/sysctl.h>
42127785Sbms
43127785Sbms#include <netinet/in.h>
44127785Sbms#include <netinet/in_systm.h>
45127785Sbms#include <netinet/ip.h>
46127785Sbms#include <netinet/ip_var.h>
47127785Sbms#include <netinet/tcp.h>
48127785Sbms#include <netinet/tcp_var.h>
49127785Sbms
50127785Sbms#include <net/route.h>
51195699Srwatson#include <net/vnet.h>
52195699Srwatson
53127785Sbms#include <netipsec/ipsec.h>
54127785Sbms#include <netipsec/xform.h>
55127785Sbms
56127785Sbms#ifdef INET6
57127785Sbms#include <netinet/ip6.h>
58127785Sbms#include <netipsec/ipsec6.h>
59127785Sbms#endif
60127785Sbms
61127785Sbms#include <netipsec/key.h>
62127785Sbms#include <netipsec/key_debug.h>
63127785Sbms
64127785Sbms/*
65127785Sbms * Initialize a TCP-MD5 SA. Called when the SA is being set up.
66127785Sbms *
67127785Sbms * We don't need to set up the tdb prefixed fields, as we don't use the
68127785Sbms * opencrypto code; we just perform a key length check.
69127785Sbms *
70127785Sbms * XXX: Currently we only allow a single 'magic' SPI to be used.
71127785Sbms *
72127785Sbms * This allows per-host granularity without affecting the userland
73127785Sbms * interface, which is a simple socket option toggle switch,
74127785Sbms * TCP_SIGNATURE_ENABLE.
75127785Sbms *
76127785Sbms * To allow per-service granularity requires that we have a means
77127785Sbms * of mapping port to SPI. The mandated way of doing this is to
78127785Sbms * use SPD entries to specify packet flows which get the TCP-MD5
79127785Sbms * treatment, however the code to do this is currently unstable
80127785Sbms * and unsuitable for production use.
81127785Sbms *
82127785Sbms * Therefore we use this compromise in the meantime.
83127785Sbms */
84127785Sbmsstatic int
85127785Sbmstcpsignature_init(struct secasvar *sav, struct xformsw *xsp)
86127785Sbms{
87127785Sbms	int keylen;
88127785Sbms
89127785Sbms	if (sav->spi != htonl(TCP_SIG_SPI)) {
90127785Sbms		DPRINTF(("%s: SPI must be TCP_SIG_SPI (0x1000)\n",
91128478Sbms		    __func__));
92127785Sbms		return (EINVAL);
93127785Sbms	}
94127785Sbms	if (sav->alg_auth != SADB_X_AALG_TCP_MD5) {
95127785Sbms		DPRINTF(("%s: unsupported authentication algorithm %u\n",
96127785Sbms		    __func__, sav->alg_auth));
97127785Sbms		return (EINVAL);
98127785Sbms	}
99127785Sbms	if (sav->key_auth == NULL) {
100127785Sbms		DPRINTF(("%s: no authentication key present\n", __func__));
101127785Sbms		return (EINVAL);
102127785Sbms	}
103127785Sbms	keylen = _KEYLEN(sav->key_auth);
104127785Sbms	if ((keylen < TCP_KEYLEN_MIN) || (keylen > TCP_KEYLEN_MAX)) {
105127785Sbms		DPRINTF(("%s: invalid key length %u\n", __func__, keylen));
106127785Sbms		return (EINVAL);
107127785Sbms	}
108127785Sbms
109127785Sbms	return (0);
110127785Sbms}
111127785Sbms
112127785Sbms/*
113127785Sbms * Paranoia.
114127785Sbms *
115127785Sbms * Called when the SA is deleted.
116127785Sbms */
117127785Sbmsstatic int
118127785Sbmstcpsignature_zeroize(struct secasvar *sav)
119127785Sbms{
120127785Sbms
121127785Sbms	if (sav->key_auth)
122157123Sgnn		bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
123127785Sbms
124127785Sbms	sav->tdb_cryptoid = 0;
125127785Sbms	sav->tdb_authalgxform = NULL;
126127785Sbms	sav->tdb_xform = NULL;
127127785Sbms
128127785Sbms	return (0);
129127785Sbms}
130127785Sbms
131127785Sbms/*
132127785Sbms * Verify that an input packet passes authentication.
133127785Sbms * Called from the ipsec layer.
134127785Sbms * We do this from within tcp itself, so this routine is just a stub.
135127785Sbms */
136127785Sbmsstatic int
137127785Sbmstcpsignature_input(struct mbuf *m, struct secasvar *sav, int skip,
138127785Sbms    int protoff)
139127785Sbms{
140127785Sbms
141127785Sbms	return (0);
142127785Sbms}
143127785Sbms
144127785Sbms/*
145127785Sbms * Prepend the authentication header.
146127785Sbms * Called from the ipsec layer.
147127785Sbms * We do this from within tcp itself, so this routine is just a stub.
148127785Sbms */
149127785Sbmsstatic int
150127785Sbmstcpsignature_output(struct mbuf *m, struct ipsecrequest *isr,
151127785Sbms    struct mbuf **mp, int skip, int protoff)
152127785Sbms{
153127785Sbms
154127785Sbms	return (EINVAL);
155127785Sbms}
156127785Sbms
157127785Sbmsstatic struct xformsw tcpsignature_xformsw = {
158127785Sbms	XF_TCPSIGNATURE,	XFT_AUTH,		"TCPMD5",
159127785Sbms	tcpsignature_init,	tcpsignature_zeroize,
160127785Sbms	tcpsignature_input,	tcpsignature_output
161127785Sbms};
162127785Sbms
163127785Sbmsstatic void
164127785Sbmstcpsignature_attach(void)
165127785Sbms{
166127785Sbms
167127785Sbms	xform_register(&tcpsignature_xformsw);
168127785Sbms}
169127785Sbms
170127785SbmsSYSINIT(tcpsignature_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST,
171177481Sbz    tcpsignature_attach, NULL);
172