1c68eed82SGleb Smirnoff /*-
2c68eed82SGleb Smirnoff * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3c68eed82SGleb Smirnoff *
4c68eed82SGleb Smirnoff * Copyright (c) 2018-2024 Netflix
5c68eed82SGleb Smirnoff * Author: Maksim Yevmenkin <maksim.yevmenkin@gmail.com>
6c68eed82SGleb Smirnoff *
7c68eed82SGleb Smirnoff * Redistribution and use in source and binary forms, with or without
8c68eed82SGleb Smirnoff * modification, are permitted provided that the following conditions
9c68eed82SGleb Smirnoff * are met:
10c68eed82SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright
11c68eed82SGleb Smirnoff * notice, this list of conditions and the following disclaimer.
12c68eed82SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright
13c68eed82SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the
14c68eed82SGleb Smirnoff * documentation and/or other materials provided with the distribution.
15c68eed82SGleb Smirnoff *
16c68eed82SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c68eed82SGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c68eed82SGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c68eed82SGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c68eed82SGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c68eed82SGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c68eed82SGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c68eed82SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c68eed82SGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c68eed82SGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c68eed82SGleb Smirnoff * SUCH DAMAGE.
27c68eed82SGleb Smirnoff */
28c68eed82SGleb Smirnoff
29c68eed82SGleb Smirnoff #include <sys/cdefs.h>
30c68eed82SGleb Smirnoff #define ACCEPT_FILTER_MOD
31c68eed82SGleb Smirnoff
32c68eed82SGleb Smirnoff #include <sys/param.h>
33c68eed82SGleb Smirnoff #include <sys/kernel.h>
34c68eed82SGleb Smirnoff #include <sys/mbuf.h>
35c68eed82SGleb Smirnoff #include <sys/module.h>
36c68eed82SGleb Smirnoff #include <sys/signalvar.h>
37c68eed82SGleb Smirnoff #include <sys/sysctl.h>
38c68eed82SGleb Smirnoff #include <sys/socketvar.h>
39c68eed82SGleb Smirnoff
40c68eed82SGleb Smirnoff static int sbfull(struct sockbuf *sb);
41c68eed82SGleb Smirnoff static uint8_t sbmget8(struct mbuf *m, int offset);
42c68eed82SGleb Smirnoff static int so_hastls(struct socket *so, void *arg, int waitflag);
43c68eed82SGleb Smirnoff
44c68eed82SGleb Smirnoff ACCEPT_FILTER_DEFINE(accf_tls, "tlsready", so_hastls, NULL, NULL, 1);
45c68eed82SGleb Smirnoff
46c68eed82SGleb Smirnoff static int
sbfull(struct sockbuf * sb)47c68eed82SGleb Smirnoff sbfull(struct sockbuf *sb)
48c68eed82SGleb Smirnoff {
49c68eed82SGleb Smirnoff
50c68eed82SGleb Smirnoff return (sbused(sb) >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
51c68eed82SGleb Smirnoff }
52c68eed82SGleb Smirnoff
53c68eed82SGleb Smirnoff static uint8_t
sbmget8(struct mbuf * m,int offset)54c68eed82SGleb Smirnoff sbmget8(struct mbuf *m, int offset)
55c68eed82SGleb Smirnoff {
56c68eed82SGleb Smirnoff struct mbuf *n = m->m_nextpkt;
57c68eed82SGleb Smirnoff
58c68eed82SGleb Smirnoff while (m != NULL && offset >= m->m_len) {
59c68eed82SGleb Smirnoff offset -= m->m_len;
60c68eed82SGleb Smirnoff m = m->m_next;
61c68eed82SGleb Smirnoff if (m == NULL) {
62c68eed82SGleb Smirnoff m = n;
63c68eed82SGleb Smirnoff n = m->m_nextpkt;
64c68eed82SGleb Smirnoff }
65c68eed82SGleb Smirnoff }
66c68eed82SGleb Smirnoff
67c68eed82SGleb Smirnoff return *(mtod(m, uint8_t *) + offset);
68c68eed82SGleb Smirnoff }
69c68eed82SGleb Smirnoff
70c68eed82SGleb Smirnoff static int
so_hastls(struct socket * so,void * arg,int waitflag)71c68eed82SGleb Smirnoff so_hastls(struct socket *so, void *arg, int waitflag)
72c68eed82SGleb Smirnoff {
73c68eed82SGleb Smirnoff struct sockbuf *sb = &so->so_rcv;
74c68eed82SGleb Smirnoff int avail;
75c68eed82SGleb Smirnoff uint16_t reclen;
76c68eed82SGleb Smirnoff
77c68eed82SGleb Smirnoff if ((sb->sb_state & SBS_CANTRCVMORE) || sbfull(sb))
78c68eed82SGleb Smirnoff return (SU_ISCONNECTED); /* can't wait any longer */
79c68eed82SGleb Smirnoff
80c68eed82SGleb Smirnoff /*
81c68eed82SGleb Smirnoff * struct {
82c68eed82SGleb Smirnoff * ContentType type; - 1 byte, 0x16 handshake
83c68eed82SGleb Smirnoff * ProtocolVersion version; - 2 bytes (major, minor)
84c68eed82SGleb Smirnoff * uint16 length; - 2 bytes, NBO, 2^14 max
85c68eed82SGleb Smirnoff * opaque fragment[TLSPlaintext.length];
86c68eed82SGleb Smirnoff * } TLSPlaintext;
87c68eed82SGleb Smirnoff */
88c68eed82SGleb Smirnoff
89c68eed82SGleb Smirnoff /* Did we get at least 5 bytes */
90c68eed82SGleb Smirnoff avail = sbavail(sb);
91c68eed82SGleb Smirnoff if (avail < 5)
92c68eed82SGleb Smirnoff return (SU_OK); /* nope */
93c68eed82SGleb Smirnoff
94c68eed82SGleb Smirnoff /* Does this look like TLS handshake? */
95c68eed82SGleb Smirnoff if (sbmget8(sb->sb_mb, 0) != 0x16)
96c68eed82SGleb Smirnoff return (SU_ISCONNECTED); /* nope */
97c68eed82SGleb Smirnoff
98c68eed82SGleb Smirnoff /* Did we get a complete TLS record? */
99c68eed82SGleb Smirnoff reclen = (uint16_t) sbmget8(sb->sb_mb, 3) << 8;
100c68eed82SGleb Smirnoff reclen |= (uint16_t) sbmget8(sb->sb_mb, 4);
101c68eed82SGleb Smirnoff
102c68eed82SGleb Smirnoff if (reclen <= 16384 && avail < (int) 5 + reclen)
103c68eed82SGleb Smirnoff return (SU_OK); /* nope */
104c68eed82SGleb Smirnoff
105c68eed82SGleb Smirnoff return (SU_ISCONNECTED);
106c68eed82SGleb Smirnoff }
107