1*56192e56Sriastradh /* $NetBSD: xform_tcp.c,v 1.25 2022/05/22 11:39:08 riastradh Exp $ */
2e2c8a664Smaxv /* $FreeBSD: xform_tcp.c,v 1.1.2.1 2004/02/14 22:24:09 bms Exp $ */
3887b782bSjonathan
4887b782bSjonathan /*
5887b782bSjonathan * Copyright (c) 2003 Bruce M. Simpson <bms@spc.org>
6887b782bSjonathan *
7887b782bSjonathan * Redistribution and use in source and binary forms, with or without
8887b782bSjonathan * modification, are permitted provided that the following conditions
9887b782bSjonathan * are met:
10887b782bSjonathan *
11887b782bSjonathan * 1. Redistributions of source code must retain the above copyright
12887b782bSjonathan * notice, this list of conditions and the following disclaimer.
13887b782bSjonathan * 2. Redistributions in binary form must reproduce the above copyright
14887b782bSjonathan * notice, this list of conditions and the following disclaimer in the
15887b782bSjonathan * documentation and/or other materials provided with the distribution.
16887b782bSjonathan * 3. The name of the author may not be used to endorse or promote products
17887b782bSjonathan * derived from this software without specific prior written permission.
18887b782bSjonathan *
19887b782bSjonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20887b782bSjonathan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21887b782bSjonathan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22887b782bSjonathan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23887b782bSjonathan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24887b782bSjonathan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25887b782bSjonathan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26887b782bSjonathan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27887b782bSjonathan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28887b782bSjonathan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29887b782bSjonathan */
30887b782bSjonathan
312bf15a96Smaxv /*
322bf15a96Smaxv * TCP MD5 Signature Option (RFC2385). Dummy code, everything is handled
332bf15a96Smaxv * in TCP directly.
342bf15a96Smaxv */
359d8f4932Slukem
369d8f4932Slukem #include <sys/cdefs.h>
37*56192e56Sriastradh __KERNEL_RCSID(0, "$NetBSD: xform_tcp.c,v 1.25 2022/05/22 11:39:08 riastradh Exp $");
389d8f4932Slukem
3980d40a78Sozaki-r #if defined(_KERNEL_OPT)
40887b782bSjonathan #include "opt_inet.h"
4180d40a78Sozaki-r #endif
42887b782bSjonathan
43887b782bSjonathan #include <sys/param.h>
44887b782bSjonathan #include <sys/systm.h>
45887b782bSjonathan #include <sys/mbuf.h>
46887b782bSjonathan #include <sys/kernel.h>
47887b782bSjonathan
48887b782bSjonathan #include <netinet/in.h>
49887b782bSjonathan #include <netinet/in_systm.h>
50887b782bSjonathan #include <netinet/ip.h>
51887b782bSjonathan #include <netinet/ip_var.h>
52b5160548Sozaki-r #ifdef TCP_SIGNATURE
53b5160548Sozaki-r #include <netinet/tcp_timer.h>
54b5160548Sozaki-r #include <netinet/tcp.h>
55b5160548Sozaki-r #include <netinet/tcp_var.h>
56b5160548Sozaki-r #endif
57887b782bSjonathan
58887b782bSjonathan #include <netipsec/ipsec.h>
59887b782bSjonathan #include <netipsec/xform.h>
60887b782bSjonathan
61887b782bSjonathan #include <netipsec/key.h>
62887b782bSjonathan #include <netipsec/key_debug.h>
63887b782bSjonathan
64887b782bSjonathan /*
65887b782bSjonathan * Initialize a TCP-MD5 SA. Called when the SA is being set up.
66887b782bSjonathan *
67887b782bSjonathan * We don't need to set up the tdb prefixed fields, as we don't use the
68887b782bSjonathan * opencrypto code; we just perform a key length check.
69887b782bSjonathan *
70887b782bSjonathan * XXX: Currently we only allow a single 'magic' SPI to be used.
71887b782bSjonathan *
72887b782bSjonathan * This allows per-host granularity without affecting the userland
73887b782bSjonathan * interface, which is a simple socket option toggle switch,
74887b782bSjonathan * TCP_SIGNATURE_ENABLE.
75887b782bSjonathan *
76887b782bSjonathan * To allow per-service granularity requires that we have a means
77887b782bSjonathan * of mapping port to SPI. The mandated way of doing this is to
78887b782bSjonathan * use SPD entries to specify packet flows which get the TCP-MD5
79887b782bSjonathan * treatment, however the code to do this is currently unstable
80887b782bSjonathan * and unsuitable for production use.
81887b782bSjonathan *
82887b782bSjonathan * Therefore we use this compromise in the meantime.
83887b782bSjonathan */
84887b782bSjonathan static int
tcpsignature_init(struct secasvar * sav,const struct xformsw * xsp)85909a8e83Sdrochner tcpsignature_init(struct secasvar *sav, const struct xformsw *xsp)
86887b782bSjonathan {
87887b782bSjonathan int keylen;
88887b782bSjonathan
89887b782bSjonathan if (sav->spi != htonl(TCP_SIG_SPI)) {
904a07f437Schristos DPRINTF("SPI %x must be TCP_SIG_SPI (0x1000)\n", sav->alg_auth);
912bf15a96Smaxv return EINVAL;
92887b782bSjonathan }
93887b782bSjonathan if (sav->alg_auth != SADB_X_AALG_TCP_MD5) {
944a07f437Schristos DPRINTF("unsupported authentication algorithm %u\n",
954a07f437Schristos sav->alg_auth);
962bf15a96Smaxv return EINVAL;
97887b782bSjonathan }
98887b782bSjonathan if (sav->key_auth == NULL) {
994a07f437Schristos DPRINTF("no authentication key present\n");
1002bf15a96Smaxv return EINVAL;
101887b782bSjonathan }
102887b782bSjonathan keylen = _KEYLEN(sav->key_auth);
103887b782bSjonathan if ((keylen < TCP_KEYLEN_MIN) || (keylen > TCP_KEYLEN_MAX)) {
1044a07f437Schristos DPRINTF("invalid key length %u\n", keylen);
1052bf15a96Smaxv return EINVAL;
106887b782bSjonathan }
107887b782bSjonathan
1082bf15a96Smaxv return 0;
109887b782bSjonathan }
110887b782bSjonathan
111*56192e56Sriastradh static void
tcpsignature_zeroize(struct secasvar * sav)112887b782bSjonathan tcpsignature_zeroize(struct secasvar *sav)
113887b782bSjonathan {
11427c8ae80Sozaki-r if (sav->key_auth) {
11527c8ae80Sozaki-r explicit_memset(_KEYBUF(sav->key_auth), 0,
11627c8ae80Sozaki-r _KEYLEN(sav->key_auth));
11727c8ae80Sozaki-r }
118887b782bSjonathan
119887b782bSjonathan sav->tdb_cryptoid = 0;
120887b782bSjonathan sav->tdb_authalgxform = NULL;
121887b782bSjonathan sav->tdb_xform = NULL;
122887b782bSjonathan }
123887b782bSjonathan
124887b782bSjonathan static int
tcpsignature_input(struct mbuf * m,struct secasvar * sav,int skip,int protoff)125be5a3d6fSozaki-r tcpsignature_input(struct mbuf *m, struct secasvar *sav, int skip,
126887b782bSjonathan int protoff)
127887b782bSjonathan {
1282bf15a96Smaxv panic("%s: should not have been called", __func__);
129887b782bSjonathan }
130887b782bSjonathan
131887b782bSjonathan static int
tcpsignature_output(struct mbuf * m,const struct ipsecrequest * isr,struct secasvar * sav,int skip,int protoff,int flags)13283c2b87aSozaki-r tcpsignature_output(struct mbuf *m, const struct ipsecrequest *isr,
133c535599fSknakahara struct secasvar *sav, int skip, int protoff, int flags)
134887b782bSjonathan {
1352bf15a96Smaxv panic("%s: should not have been called", __func__);
136887b782bSjonathan }
137887b782bSjonathan
138887b782bSjonathan static struct xformsw tcpsignature_xformsw = {
1398f43f955Sozaki-r .xf_type = XF_TCPSIGNATURE,
1408f43f955Sozaki-r .xf_flags = XFT_AUTH,
1418f43f955Sozaki-r .xf_name = "TCPMD5",
1428f43f955Sozaki-r .xf_init = tcpsignature_init,
1438f43f955Sozaki-r .xf_zeroize = tcpsignature_zeroize,
1448f43f955Sozaki-r .xf_input = tcpsignature_input,
1458f43f955Sozaki-r .xf_output = tcpsignature_output,
1468f43f955Sozaki-r .xf_next = NULL,
147887b782bSjonathan };
148887b782bSjonathan
149ef67739aSozaki-r void
tcpsignature_attach(void)150887b782bSjonathan tcpsignature_attach(void)
151887b782bSjonathan {
152887b782bSjonathan xform_register(&tcpsignature_xformsw);
153887b782bSjonathan }
154