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/module.h>
36 #include <sys/signalvar.h>
37 #include <sys/sysctl.h>
38 #include <sys/socketvar.h>
40 /* check for GET/HEAD */
41 static void sohashttpget(struct socket *so, void *arg, int waitflag);
42 /* check for HTTP/1.0 or HTTP/1.1 */
43 static void soparsehttpvers(struct socket *so, void *arg, int waitflag);
44 /* check for end of HTTP/1.x request */
45 static void soishttpconnected(struct socket *so, void *arg, int waitflag);
46 /* strcmp on an mbuf chain */
47 static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp);
48 /* strncmp on an mbuf chain */
49 static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
51 /* socketbuffer is full */
52 static int sbfull(struct sockbuf *sb);
54 static struct accept_filter accf_http_filter = {
61 static moduledata_t accf_http_mod = {
63 accept_filt_generic_mod_event,
67 DECLARE_MODULE(accf_http, accf_http_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
69 static int parse_http_version = 1;
71 SYSCTL_NODE(_net_inet_accf, OID_AUTO, http, CTLFLAG_RW, 0,
72 "HTTP accept filter");
73 SYSCTL_INT(_net_inet_accf_http, OID_AUTO, parsehttpversion, CTLFLAG_RW,
74 &parse_http_version, 1,
75 "Parse http version so that non 1.x requests work");
77 #ifdef ACCF_HTTP_DEBUG
78 #define DPRINT(fmt, args...) \
80 printf("%s:%d: " fmt "\n", __func__, __LINE__, ##args); \
83 #define DPRINT(fmt, args...)
87 sbfull(struct sockbuf *sb)
90 DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, "
91 "mbcnt(%ld) >= mbmax(%ld): %d",
92 sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat,
93 sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax);
94 return (sb->sb_cc >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
98 * start at mbuf m, (must provide npkt if exists)
99 * starting at offset in m compare characters in mbuf chain for 'cmp'
102 mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
106 for (; m != NULL; m = n) {
109 npkt = npkt->m_nextpkt;
110 for (; m; m = m->m_next) {
111 for (; offset < m->m_len; offset++, cmp++) {
114 else if (*cmp != *(mtod(m, char *) + offset))
126 * start at mbuf m, (must provide npkt if exists)
127 * starting at offset in m compare characters in mbuf chain for 'cmp'
128 * stop at 'max' characters
131 mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
135 for (; m != NULL; m = n) {
138 npkt = npkt->m_nextpkt;
139 for (; m; m = m->m_next) {
140 for (; offset < m->m_len; offset++, cmp++, max--) {
141 if (max == 0 || *cmp == '\0')
143 else if (*cmp != *(mtod(m, char *) + offset))
146 if (max == 0 || *cmp == '\0')
154 #define STRSETUP(sptr, slen, str) \
157 slen = sizeof(str) - 1; \
161 sohashttpget(struct socket *so, void *arg, int waitflag)
164 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) {
169 m = so->so_rcv.sb_mb;
170 cc = so->so_rcv.sb_cc - 1;
173 switch (*mtod(m, char *)) {
175 STRSETUP(cmp, cmplen, "ET ");
178 STRSETUP(cmp, cmplen, "EAD ");
184 if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
185 DPRINT("short cc (%d) but mbufstrncmp ok", cc);
188 DPRINT("short cc (%d) mbufstrncmp failed", cc);
192 if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
193 DPRINT("mbufstrcmp ok");
194 if (parse_http_version == 0)
195 soishttpconnected(so, arg, waitflag);
197 soparsehttpvers(so, arg, waitflag);
200 DPRINT("mbufstrcmp bad");
205 so->so_upcall = NULL;
206 so->so_rcv.sb_flags &= ~SB_UPCALL;
212 soparsehttpvers(struct socket *so, void *arg, int waitflag)
215 int i, cc, spaces, inspaces;
217 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
220 m = so->so_rcv.sb_mb;
221 cc = so->so_rcv.sb_cc;
222 inspaces = spaces = 0;
223 for (m = so->so_rcv.sb_mb; m; m = n) {
225 for (; m; m = m->m_next) {
226 for (i = 0; i < m->m_len; i++, cc--) {
227 switch (*(mtod(m, char *) + i)) {
246 * if we don't have enough characters
247 * left (cc < sizeof("HTTP/1.0") - 1)
248 * then see if the remaining ones
249 * are a request we can parse.
251 if (cc < sizeof("HTTP/1.0") - 1) {
252 if (mbufstrncmp(m, n, i, cc,
261 mbufstrcmp(m, n, i, "HTTP/1.0") ||
262 mbufstrcmp(m, n, i, "HTTP/1.1")) {
264 soishttpconnected(so,
278 * if we hit here we haven't hit something
279 * we don't understand or a newline, so try again
281 so->so_upcall = soparsehttpvers;
282 so->so_rcv.sb_flags |= SB_UPCALL;
287 so->so_upcall = NULL;
288 so->so_rcv.sb_flags &= ~SB_UPCALL;
297 soishttpconnected(struct socket *so, void *arg, int waitflag)
304 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
308 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
309 * copied - how much we've copied so far
310 * ccleft - how many bytes remaining in the socketbuffer
311 * just loop over the mbufs subtracting from 'ccleft' until we only
315 ccleft = so->so_rcv.sb_cc;
319 for (m = so->so_rcv.sb_mb; m; m = n) {
321 for (; m; m = m->m_next) {
323 if (ccleft <= NCHRS) {
327 tocopy = (NCHRS - ccleft) - copied;
328 src = mtod(m, char *) + (m->m_len - tocopy);
346 if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
347 /* we have all request headers */
352 so->so_upcall = soishttpconnected;
353 so->so_rcv.sb_flags |= SB_UPCALL;
357 so->so_upcall = NULL;
358 so->so_rcv.sb_flags &= ~SB_UPCALL;