2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Paycounter, Inc.
5 * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #define ACCEPT_FILTER_MOD
35 #include <sys/param.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/signalvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/socketvar.h>
43 /* check for GET/HEAD */
44 static int sohashttpget(struct socket *so, void *arg, int waitflag);
45 /* check for HTTP/1.0 or HTTP/1.1 */
46 static int soparsehttpvers(struct socket *so, void *arg, int waitflag);
47 /* check for end of HTTP/1.x request */
48 static int soishttpconnected(struct socket *so, void *arg, int waitflag);
49 /* strcmp on an mbuf chain */
50 static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp);
51 /* strncmp on an mbuf chain */
52 static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
54 /* socketbuffer is full */
55 static int sbfull(struct sockbuf *sb);
57 static struct accept_filter accf_http_filter = {
64 static moduledata_t accf_http_mod = {
66 accept_filt_generic_mod_event,
70 DECLARE_MODULE(accf_http, accf_http_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
72 static int parse_http_version = 1;
74 static SYSCTL_NODE(_net_inet_accf, OID_AUTO, http,
75 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
76 "HTTP accept filter");
77 SYSCTL_INT(_net_inet_accf_http, OID_AUTO, parsehttpversion, CTLFLAG_RW,
78 &parse_http_version, 1,
79 "Parse http version so that non 1.x requests work");
81 #ifdef ACCF_HTTP_DEBUG
82 #define DPRINT(fmt, args...) \
84 printf("%s:%d: " fmt "\n", __func__, __LINE__, ##args); \
87 #define DPRINT(fmt, args...)
91 sbfull(struct sockbuf *sb)
94 DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, "
95 "mbcnt(%ld) >= mbmax(%ld): %d",
96 sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat,
97 sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax);
98 return (sbused(sb) >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
102 * start at mbuf m, (must provide npkt if exists)
103 * starting at offset in m compare characters in mbuf chain for 'cmp'
106 mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
110 for (; m != NULL; m = n) {
113 npkt = npkt->m_nextpkt;
114 for (; m; m = m->m_next) {
115 for (; offset < m->m_len; offset++, cmp++) {
118 else if (*cmp != *(mtod(m, char *) + offset))
130 * start at mbuf m, (must provide npkt if exists)
131 * starting at offset in m compare characters in mbuf chain for 'cmp'
132 * stop at 'max' characters
135 mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
139 for (; m != NULL; m = n) {
142 npkt = npkt->m_nextpkt;
143 for (; m; m = m->m_next) {
144 for (; offset < m->m_len; offset++, cmp++, max--) {
145 if (max == 0 || *cmp == '\0')
147 else if (*cmp != *(mtod(m, char *) + offset))
150 if (max == 0 || *cmp == '\0')
158 #define STRSETUP(sptr, slen, str) \
161 slen = sizeof(str) - 1; \
165 sohashttpget(struct socket *so, void *arg, int waitflag)
168 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 &&
169 !sbfull(&so->so_rcv)) {
174 m = so->so_rcv.sb_mb;
175 cc = sbavail(&so->so_rcv) - 1;
178 switch (*mtod(m, char *)) {
180 STRSETUP(cmp, cmplen, "ET ");
183 STRSETUP(cmp, cmplen, "EAD ");
189 if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
190 DPRINT("short cc (%d) but mbufstrncmp ok", cc);
193 DPRINT("short cc (%d) mbufstrncmp failed", cc);
197 if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
198 DPRINT("mbufstrcmp ok");
199 if (parse_http_version == 0)
200 return (soishttpconnected(so, arg, waitflag));
202 return (soparsehttpvers(so, arg, waitflag));
204 DPRINT("mbufstrcmp bad");
209 return (SU_ISCONNECTED);
213 soparsehttpvers(struct socket *so, void *arg, int waitflag)
216 int i, cc, spaces, inspaces;
218 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
221 m = so->so_rcv.sb_mb;
222 cc = sbavail(&so->so_rcv);
223 inspaces = spaces = 0;
224 for (m = so->so_rcv.sb_mb; m; m = n) {
226 for (; m; m = m->m_next) {
227 for (i = 0; i < m->m_len; i++, cc--) {
228 switch (*(mtod(m, char *) + i)) {
247 * if we don't have enough characters
248 * left (cc < sizeof("HTTP/1.0") - 1)
249 * then see if the remaining ones
250 * are a request we can parse.
252 if (cc < sizeof("HTTP/1.0") - 1) {
253 if (mbufstrncmp(m, n, i, cc,
262 mbufstrcmp(m, n, i, "HTTP/1.0") ||
263 mbufstrcmp(m, n, i, "HTTP/1.1")) {
265 return (soishttpconnected(so,
278 * if we hit here we haven't hit something
279 * we don't understand or a newline, so try again
281 soupcall_set(so, SO_RCV, soparsehttpvers, arg);
286 return (SU_ISCONNECTED);
293 soishttpconnected(struct socket *so, void *arg, int waitflag)
300 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
304 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
305 * copied - how much we've copied so far
306 * ccleft - how many bytes remaining in the socketbuffer
307 * just loop over the mbufs subtracting from 'ccleft' until we only
311 ccleft = sbavail(&so->so_rcv);
315 for (m = so->so_rcv.sb_mb; m; m = n) {
317 for (; m; m = m->m_next) {
319 if (ccleft <= NCHRS) {
323 tocopy = (NCHRS - ccleft) - copied;
324 src = mtod(m, char *) + (m->m_len - tocopy);
342 if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
343 /* we have all request headers */
348 soupcall_set(so, SO_RCV, soishttpconnected, arg);
352 return (SU_ISCONNECTED);