2 * Copyright (c) 2000 Paycounter, Inc.
3 * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #define ACCEPT_FILTER_MOD
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/signalvar.h>
36 #include <sys/sysctl.h>
37 #include <sys/socketvar.h>
39 /* check for GET/HEAD */
40 static void sohashttpget(struct socket *so, void *arg, int waitflag);
41 /* check for HTTP/1.0 or HTTP/1.1 */
42 static void soparsehttpvers(struct socket *so, void *arg, int waitflag);
43 /* check for end of HTTP/1.x request */
44 static void soishttpconnected(struct socket *so, void *arg, int waitflag);
45 /* strcmp on an mbuf chain */
46 static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp);
47 /* strncmp on an mbuf chain */
48 static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
50 /* socketbuffer is full */
51 static int sbfull(struct sockbuf *sb);
53 static struct accept_filter accf_http_filter = {
60 static moduledata_t accf_http_mod = {
62 accept_filt_generic_mod_event,
66 DECLARE_MODULE(accf_http, accf_http_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
68 static int parse_http_version = 1;
70 SYSCTL_NODE(_net_inet_accf, OID_AUTO, http, CTLFLAG_RW, 0,
71 "HTTP accept filter");
72 SYSCTL_INT(_net_inet_accf_http, OID_AUTO, parsehttpversion, CTLFLAG_RW,
73 &parse_http_version, 1,
74 "Parse http version so that non 1.x requests work");
76 #ifdef ACCF_HTTP_DEBUG
77 #define DPRINT(fmt, args...) \
79 printf("%s:%d: " fmt "\n", __func__, __LINE__, ##args); \
82 #define DPRINT(fmt, args...)
86 sbfull(struct sockbuf *sb)
89 DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, "
90 "mbcnt(%ld) >= mbmax(%ld): %d",
91 sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat,
92 sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax);
93 return (sb->sb_cc >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
97 * start at mbuf m, (must provide npkt if exists)
98 * starting at offset in m compare characters in mbuf chain for 'cmp'
101 mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
105 for (; m != NULL; m = n) {
108 npkt = npkt->m_nextpkt;
109 for (; m; m = m->m_next) {
110 for (; offset < m->m_len; offset++, cmp++) {
113 else if (*cmp != *(mtod(m, char *) + offset))
125 * start at mbuf m, (must provide npkt if exists)
126 * starting at offset in m compare characters in mbuf chain for 'cmp'
127 * stop at 'max' characters
130 mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
134 for (; m != NULL; m = n) {
137 npkt = npkt->m_nextpkt;
138 for (; m; m = m->m_next) {
139 for (; offset < m->m_len; offset++, cmp++, max--) {
140 if (max == 0 || *cmp == '\0')
142 else if (*cmp != *(mtod(m, char *) + offset))
145 if (max == 0 || *cmp == '\0')
153 #define STRSETUP(sptr, slen, str) \
156 slen = sizeof(str) - 1; \
160 sohashttpget(struct socket *so, void *arg, int waitflag)
163 if ((so->so_state & SS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) {
168 m = so->so_rcv.sb_mb;
169 cc = so->so_rcv.sb_cc - 1;
172 switch (*mtod(m, char *)) {
174 STRSETUP(cmp, cmplen, "ET ");
177 STRSETUP(cmp, cmplen, "EAD ");
183 if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
184 DPRINT("short cc (%d) but mbufstrncmp ok", cc);
187 DPRINT("short cc (%d) mbufstrncmp failed", cc);
191 if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
192 DPRINT("mbufstrcmp ok");
193 if (parse_http_version == 0)
194 soishttpconnected(so, arg, waitflag);
196 soparsehttpvers(so, arg, waitflag);
199 DPRINT("mbufstrcmp bad");
204 so->so_upcall = NULL;
205 so->so_rcv.sb_flags &= ~SB_UPCALL;
211 soparsehttpvers(struct socket *so, void *arg, int waitflag)
214 int i, cc, spaces, inspaces;
216 if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
219 m = so->so_rcv.sb_mb;
220 cc = so->so_rcv.sb_cc;
221 inspaces = spaces = 0;
222 for (m = so->so_rcv.sb_mb; m; m = n) {
224 for (; m; m = m->m_next) {
225 for (i = 0; i < m->m_len; i++, cc--) {
226 switch (*(mtod(m, char *) + i)) {
245 * if we don't have enough characters
246 * left (cc < sizeof("HTTP/1.0") - 1)
247 * then see if the remaining ones
248 * are a request we can parse.
250 if (cc < sizeof("HTTP/1.0") - 1) {
251 if (mbufstrncmp(m, n, i, cc,
260 mbufstrcmp(m, n, i, "HTTP/1.0") ||
261 mbufstrcmp(m, n, i, "HTTP/1.1")) {
263 soishttpconnected(so,
277 * if we hit here we haven't hit something
278 * we don't understand or a newline, so try again
280 so->so_upcall = soparsehttpvers;
281 so->so_rcv.sb_flags |= SB_UPCALL;
286 so->so_upcall = NULL;
287 so->so_rcv.sb_flags &= ~SB_UPCALL;
296 soishttpconnected(struct socket *so, void *arg, int waitflag)
303 if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
307 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
308 * copied - how much we've copied so far
309 * ccleft - how many bytes remaining in the socketbuffer
310 * just loop over the mbufs subtracting from 'ccleft' until we only
314 ccleft = so->so_rcv.sb_cc;
318 for (m = so->so_rcv.sb_mb; m; m = n) {
320 for (; m; m = m->m_next) {
322 if (ccleft <= NCHRS) {
326 tocopy = (NCHRS - ccleft) - copied;
327 src = mtod(m, char *) + (m->m_len - tocopy);
345 if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
346 /* we have all request headers */
351 so->so_upcall = soishttpconnected;
352 so->so_rcv.sb_flags |= SB_UPCALL;
356 so->so_upcall = NULL;
357 so->so_rcv.sb_flags &= ~SB_UPCALL;