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 $
30 #define ACCEPT_FILTER_MOD
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysproto.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/unistd.h>
40 #include <sys/fcntl.h>
41 #include <sys/protosw.h>
42 #include <sys/sysctl.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
47 #include <sys/resource.h>
48 #include <sys/sysent.h>
49 #include <sys/resourcevar.h>
51 /* check for GET/HEAD */
52 static void sohashttpget(struct socket *so, void *arg, int waitflag);
53 /* check for HTTP/1.0 or HTTP/1.1 */
54 static void soparsehttpvers(struct socket *so, void *arg, int waitflag);
55 /* check for end of HTTP/1.x request */
56 static void soishttpconnected(struct socket *so, void *arg, int waitflag);
57 /* strcmp on an mbuf chain */
58 static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp);
59 /* strncmp on an mbuf chain */
60 static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
62 /* socketbuffer is full */
63 static int sbfull(struct sockbuf *sb);
65 static struct accept_filter accf_http_filter = {
72 static moduledata_t accf_http_mod = {
74 accept_filt_generic_mod_event,
78 DECLARE_MODULE(accf_http, accf_http_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
80 static int parse_http_version = 1;
82 SYSCTL_NODE(_net_inet_accf, OID_AUTO, http, CTLFLAG_RW, 0,
83 "HTTP accept filter");
84 SYSCTL_INT(_net_inet_accf_http, OID_AUTO, parsehttpversion, CTLFLAG_RW,
85 &parse_http_version, 1,
86 "Parse http version so that non 1.x requests work");
88 #ifdef ACCF_HTTP_DEBUG
89 #define DPRINT(fmt, args...) \
91 printf("%s:%d: " fmt "\n", __func__, __LINE__ , ##args); \
94 #define DPRINT(fmt, args...)
98 sbfull(struct sockbuf *sb)
101 DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, mbcnt(%ld) >= mbmax(%ld): %d",
102 sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat,
103 sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax);
104 return(sb->sb_cc >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
108 * start at mbuf m, (must provide npkt if exists)
109 * starting at offset in m compare characters in mbuf chain for 'cmp'
112 mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
116 for (;m != NULL; m = n) {
119 npkt = npkt->m_nextpkt;
120 for (; m; m = m->m_next) {
121 for (; offset < m->m_len; offset++, cmp++) {
124 } else if (*cmp != *(mtod(m, char *) + offset)) {
137 * start at mbuf m, (must provide npkt if exists)
138 * starting at offset in m compare characters in mbuf chain for 'cmp'
139 * stop at 'max' characters
142 mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
146 for (;m != NULL; m = n) {
149 npkt = npkt->m_nextpkt;
150 for (; m; m = m->m_next) {
151 for (; offset < m->m_len; offset++, cmp++, max--) {
152 if (max == 0 || *cmp == '\0') {
154 } else if (*cmp != *(mtod(m, char *) + offset)) {
158 if (max == 0 || *cmp == '\0')
166 #define STRSETUP(sptr, slen, str) \
169 slen = sizeof(str) - 1; \
173 sohashttpget(struct socket *so, void *arg, int waitflag)
176 if ((so->so_state & SS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) {
181 m = so->so_rcv.sb_mb;
182 cc = so->so_rcv.sb_cc - 1;
185 switch (*mtod(m, char *)) {
187 STRSETUP(cmp, cmplen, "ET ");
190 STRSETUP(cmp, cmplen, "EAD ");
196 if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
197 DPRINT("short cc (%d) but mbufstrncmp ok", cc);
200 DPRINT("short cc (%d) mbufstrncmp failed", cc);
204 if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
205 DPRINT("mbufstrcmp ok");
206 if (parse_http_version == 0)
207 soishttpconnected(so, arg, waitflag);
209 soparsehttpvers(so, arg, waitflag);
212 DPRINT("mbufstrcmp bad");
217 so->so_upcall = NULL;
218 so->so_rcv.sb_flags &= ~SB_UPCALL;
224 soparsehttpvers(struct socket *so, void *arg, int waitflag)
227 int i, cc, spaces, inspaces;
229 if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
232 m = so->so_rcv.sb_mb;
233 cc = so->so_rcv.sb_cc;
234 inspaces = spaces = 0;
235 for (m = so->so_rcv.sb_mb; m; m = n) {
237 for (; m; m = m->m_next) {
238 for (i = 0; i < m->m_len; i++, cc--) {
239 switch (*(mtod(m, char *) + i)) {
252 /* make sure we have enough data left */
253 if (cc < sizeof("HTTP/1.0") - 1) {
254 if (mbufstrncmp(m, n, i, cc, "HTTP/1.") == 1) {
255 DPRINT("mbufstrncmp ok");
258 DPRINT("mbufstrncmp bad");
261 } else if (mbufstrcmp(m, n, i, "HTTP/1.0") == 1 ||
262 mbufstrcmp(m, n, i, "HTTP/1.1") == 1) {
263 DPRINT("mbufstrcmp ok");
264 soishttpconnected(so, arg, waitflag);
267 DPRINT("mbufstrcmp bad");
280 * if we hit here we haven't hit something
281 * we don't understand or a newline, so try again
283 so->so_upcall = soparsehttpvers;
284 so->so_rcv.sb_flags |= SB_UPCALL;
289 so->so_upcall = NULL;
290 so->so_rcv.sb_flags &= ~SB_UPCALL;
299 soishttpconnected(struct socket *so, void *arg, int waitflag)
306 if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
310 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
311 * copied - how much we've copied so far
312 * ccleft - how many bytes remaining in the socketbuffer
313 * just loop over the mbufs subtracting from 'ccleft' until we only
317 ccleft = so->so_rcv.sb_cc;
321 for (m = so->so_rcv.sb_mb; m; m = n) {
323 for (; m; m = m->m_next) {
325 if (ccleft <= NCHRS) {
329 tocopy = (NCHRS - ccleft) - copied;
330 src = mtod(m, char *) + (m->m_len - tocopy);
348 if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
349 /* we have all request headers */
354 so->so_upcall = soishttpconnected;
355 so->so_rcv.sb_flags |= SB_UPCALL;
359 so->so_upcall = NULL;
360 so->so_rcv.sb_flags &= ~SB_UPCALL;