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.2 2003/06/17 04:28:51 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 sockbuf *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 printf("%s:%d: " fmt "\n", __func__, __LINE__ , ##args); \
95 #define DPRINT(fmt, args...)
99 sbfull(struct sockbuf *sb)
102 DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, mbcnt(%ld) >= mbmax(%ld): %d",
103 sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat,
104 sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax);
105 return(sb->sb_cc >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
109 * start at mbuf m, (must provide npkt if exists)
110 * starting at offset in m compare characters in mbuf chain for 'cmp'
113 mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
117 for (;m != NULL; m = n) {
120 npkt = npkt->m_nextpkt;
121 for (; m; m = m->m_next) {
122 for (; offset < m->m_len; offset++, cmp++) {
125 } else if (*cmp != *(mtod(m, char *) + offset)) {
138 * start at mbuf m, (must provide npkt if exists)
139 * starting at offset in m compare characters in mbuf chain for 'cmp'
140 * stop at 'max' characters
143 mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
147 for (;m != NULL; m = n) {
150 npkt = npkt->m_nextpkt;
151 for (; m; m = m->m_next) {
152 for (; offset < m->m_len; offset++, cmp++, max--) {
153 if (max == 0 || *cmp == '\0') {
155 } else if (*cmp != *(mtod(m, char *) + offset)) {
159 if (max == 0 || *cmp == '\0')
167 #define STRSETUP(sptr, slen, str) \
170 slen = sizeof(str) - 1; \
174 sohashttpget(struct socket *so, void *arg, int waitflag)
177 if ((so->so_state & SS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) {
182 m = so->so_rcv.sb_mb;
183 cc = so->so_rcv.sb_cc - 1;
186 switch (*mtod(m, char *)) {
188 STRSETUP(cmp, cmplen, "ET ");
191 STRSETUP(cmp, cmplen, "EAD ");
197 if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
198 DPRINT("short cc (%d) but mbufstrncmp ok", cc);
201 DPRINT("short cc (%d) mbufstrncmp failed", cc);
205 if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
206 DPRINT("mbufstrcmp ok");
207 if (parse_http_version == 0)
208 soishttpconnected(so, arg, waitflag);
210 soparsehttpvers(so, arg, waitflag);
213 DPRINT("mbufstrcmp bad");
218 so->so_upcall = NULL;
219 so->so_rcv.sb_flags &= ~SB_UPCALL;
225 soparsehttpvers(struct socket *so, void *arg, int waitflag)
228 int i, cc, spaces, inspaces;
230 if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
233 m = so->so_rcv.sb_mb;
234 cc = so->so_rcv.sb_cc;
235 inspaces = spaces = 0;
236 for (m = so->so_rcv.sb_mb; m; m = n) {
238 for (; m; m = m->m_next) {
239 for (i = 0; i < m->m_len; i++, cc--) {
240 switch (*(mtod(m, char *) + i)) {
253 /* make sure we have enough data left */
254 if (cc < sizeof("HTTP/1.0") - 1) {
255 if (mbufstrncmp(m, n, i, cc, "HTTP/1.") == 1) {
256 DPRINT("mbufstrncmp ok");
259 DPRINT("mbufstrncmp bad");
262 } else if (mbufstrcmp(m, n, i, "HTTP/1.0") == 1 ||
263 mbufstrcmp(m, n, i, "HTTP/1.1") == 1) {
264 DPRINT("mbufstrcmp ok");
265 soishttpconnected(so, arg, waitflag);
268 DPRINT("mbufstrcmp bad");
281 * if we hit here we haven't hit something
282 * we don't understand or a newline, so try again
284 so->so_upcall = soparsehttpvers;
285 so->so_rcv.sb_flags |= SB_UPCALL;
290 so->so_upcall = NULL;
291 so->so_rcv.sb_flags &= ~SB_UPCALL;
300 soishttpconnected(struct socket *so, void *arg, int waitflag)
307 if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
311 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
312 * copied - how much we've copied so far
313 * ccleft - how many bytes remaining in the socketbuffer
314 * just loop over the mbufs subtracting from 'ccleft' until we only
318 ccleft = so->so_rcv.sb_cc;
322 for (m = so->so_rcv.sb_mb; m; m = n) {
324 for (; m; m = m->m_next) {
326 if (ccleft <= NCHRS) {
330 tocopy = (NCHRS - ccleft) - copied;
331 src = mtod(m, char *) + (m->m_len - tocopy);
349 if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
350 /* we have all request headers */
355 so->so_upcall = soishttpconnected;
356 so->so_rcv.sb_flags |= SB_UPCALL;
360 so->so_upcall = NULL;
361 so->so_rcv.sb_flags &= ~SB_UPCALL;