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