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
27 * $FreeBSD: src/sys/netinet/accf_http.c,v 1.1.2.4 2002/05/01 08:34:37 alfred Exp $
28 * $DragonFly: src/sys/net/accf_http/accf_http.c,v 1.4 2007/04/22 01:13:13 dillon Exp $
31 #define ACCEPT_FILTER_MOD
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysproto.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/unistd.h>
41 #include <sys/fcntl.h>
42 #include <sys/protosw.h>
43 #include <sys/sysctl.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
48 #include <sys/resource.h>
49 #include <sys/sysent.h>
50 #include <sys/resourcevar.h>
52 /* check for GET/HEAD */
53 static void sohashttpget(struct socket *so, void *arg, int waitflag);
54 /* check for HTTP/1.0 or HTTP/1.1 */
55 static void soparsehttpvers(struct socket *so, void *arg, int waitflag);
56 /* check for end of HTTP/1.x request */
57 static void soishttpconnected(struct socket *so, void *arg, int waitflag);
58 /* strcmp on an mbuf chain */
59 static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp);
60 /* strncmp on an mbuf chain */
61 static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
63 /* socketbuffer is full */
64 static int sbfull(struct signalsockbuf *sb);
66 static struct accept_filter accf_http_filter = {
73 static moduledata_t accf_http_mod = {
75 accept_filt_generic_mod_event,
79 DECLARE_MODULE(accf_http, accf_http_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
81 static int parse_http_version = 1;
83 SYSCTL_NODE(_net_inet_accf, OID_AUTO, http, CTLFLAG_RW, 0,
84 "HTTP accept filter");
85 SYSCTL_INT(_net_inet_accf_http, OID_AUTO, parsehttpversion, CTLFLAG_RW,
86 &parse_http_version, 1,
87 "Parse http version so that non 1.x requests work");
89 #ifdef ACCF_HTTP_DEBUG
90 #define DPRINT(fmt, args...) \
92 kprintf("%s:%d: " fmt "\n", __func__, __LINE__ , ##args); \
95 #define DPRINT(fmt, args...)
99 sbfull(struct signalsockbuf *ssb)
102 DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, mbcnt(%ld) >= mbmax(%ld): %d",
103 ssb->ssb_cc, ssb->ssb_hiwat, ssb->ssb_cc >= ssb->ssb_hiwat,
104 ssb->ssb_mbcnt, ssb->ssb_mbmax, ssb->ssb_mbcnt >= ssb->ssb_mbmax);
105 return(ssb->ssb_cc >= ssb->ssb_hiwat ||
106 ssb->ssb_mbcnt >= ssb->ssb_mbmax);
110 * start at mbuf m, (must provide npkt if exists)
111 * starting at offset in m compare characters in mbuf chain for 'cmp'
114 mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
118 for (;m != NULL; m = n) {
121 npkt = npkt->m_nextpkt;
122 for (; m; m = m->m_next) {
123 for (; offset < m->m_len; offset++, cmp++) {
126 } else if (*cmp != *(mtod(m, char *) + offset)) {
139 * start at mbuf m, (must provide npkt if exists)
140 * starting at offset in m compare characters in mbuf chain for 'cmp'
141 * stop at 'max' characters
144 mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
148 for (;m != NULL; m = n) {
151 npkt = npkt->m_nextpkt;
152 for (; m; m = m->m_next) {
153 for (; offset < m->m_len; offset++, cmp++, max--) {
154 if (max == 0 || *cmp == '\0') {
156 } else if (*cmp != *(mtod(m, char *) + offset)) {
160 if (max == 0 || *cmp == '\0')
168 #define STRSETUP(sptr, slen, str) \
171 slen = sizeof(str) - 1; \
175 sohashttpget(struct socket *so, void *arg, int waitflag)
178 if ((so->so_state & SS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) {
183 m = so->so_rcv.ssb_mb;
184 cc = so->so_rcv.ssb_cc - 1;
187 switch (*mtod(m, char *)) {
189 STRSETUP(cmp, cmplen, "ET ");
192 STRSETUP(cmp, cmplen, "EAD ");
198 if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
199 DPRINT("short cc (%d) but mbufstrncmp ok", cc);
202 DPRINT("short cc (%d) mbufstrncmp failed", cc);
206 if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
207 DPRINT("mbufstrcmp ok");
208 if (parse_http_version == 0)
209 soishttpconnected(so, arg, waitflag);
211 soparsehttpvers(so, arg, waitflag);
214 DPRINT("mbufstrcmp bad");
219 so->so_upcall = NULL;
220 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_UPCALL);
226 soparsehttpvers(struct socket *so, void *arg, int waitflag)
229 int i, cc, spaces, inspaces;
231 if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
234 m = so->so_rcv.ssb_mb;
235 cc = so->so_rcv.ssb_cc;
236 inspaces = spaces = 0;
237 for (m = so->so_rcv.ssb_mb; m; m = n) {
239 for (; m; m = m->m_next) {
240 for (i = 0; i < m->m_len; i++, cc--) {
241 switch (*(mtod(m, char *) + i)) {
254 /* make sure we have enough data left */
255 if (cc < sizeof("HTTP/1.0") - 1) {
256 if (mbufstrncmp(m, n, i, cc, "HTTP/1.") == 1) {
257 DPRINT("mbufstrncmp ok");
260 DPRINT("mbufstrncmp bad");
263 } else if (mbufstrcmp(m, n, i, "HTTP/1.0") == 1 ||
264 mbufstrcmp(m, n, i, "HTTP/1.1") == 1) {
265 DPRINT("mbufstrcmp ok");
266 soishttpconnected(so, arg, waitflag);
269 DPRINT("mbufstrcmp bad");
282 * if we hit here we haven't hit something
283 * we don't understand or a newline, so try again
285 so->so_upcall = soparsehttpvers;
286 atomic_set_int(&so->so_rcv.ssb_flags, SSB_UPCALL);
291 so->so_upcall = NULL;
292 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_UPCALL);
301 soishttpconnected(struct socket *so, void *arg, int waitflag)
308 if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
312 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
313 * copied - how much we've copied so far
314 * ccleft - how many bytes remaining in the socketbuffer
315 * just loop over the mbufs subtracting from 'ccleft' until we only
319 ccleft = so->so_rcv.ssb_cc;
323 for (m = so->so_rcv.ssb_mb; m; m = n) {
325 for (; m; m = m->m_next) {
327 if (ccleft <= NCHRS) {
331 tocopy = (NCHRS - ccleft) - copied;
332 src = mtod(m, char *) + (m->m_len - tocopy);
350 if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
351 /* we have all request headers */
356 so->so_upcall = soishttpconnected;
357 atomic_set_int(&so->so_rcv.ssb_flags, SSB_UPCALL);
361 so->so_upcall = NULL;
362 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_UPCALL);