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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #define ACCEPT_FILTER_MOD
33 #include <sys/param.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/signalvar.h>
38 #include <sys/sysctl.h>
39 #include <sys/socketvar.h>
41 /* check for GET/HEAD */
42 static int sohashttpget(struct socket *so, void *arg, int waitflag);
43 /* check for HTTP/1.0 or HTTP/1.1 */
44 static int soparsehttpvers(struct socket *so, void *arg, int waitflag);
45 /* check for end of HTTP/1.x request */
46 static int soishttpconnected(struct socket *so, void *arg, int waitflag);
47 /* strcmp on an mbuf chain */
48 static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp);
49 /* strncmp on an mbuf chain */
50 static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
52 /* socketbuffer is full */
53 static int sbfull(struct sockbuf *sb);
55 static struct accept_filter accf_http_filter = {
62 static moduledata_t accf_http_mod = {
64 accept_filt_generic_mod_event,
68 DECLARE_MODULE(accf_http, accf_http_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
70 static int parse_http_version = 1;
72 SYSCTL_NODE(_net_inet_accf, OID_AUTO, http, CTLFLAG_RW, 0,
73 "HTTP accept filter");
74 SYSCTL_INT(_net_inet_accf_http, OID_AUTO, parsehttpversion, CTLFLAG_RW,
75 &parse_http_version, 1,
76 "Parse http version so that non 1.x requests work");
78 #ifdef ACCF_HTTP_DEBUG
79 #define DPRINT(fmt, args...) \
81 printf("%s:%d: " fmt "\n", __func__, __LINE__, ##args); \
84 #define DPRINT(fmt, args...)
88 sbfull(struct sockbuf *sb)
91 DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, "
92 "mbcnt(%ld) >= mbmax(%ld): %d",
93 sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat,
94 sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax);
95 return (sb->sb_cc >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
99 * start at mbuf m, (must provide npkt if exists)
100 * starting at offset in m compare characters in mbuf chain for 'cmp'
103 mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
107 for (; m != NULL; m = n) {
110 npkt = npkt->m_nextpkt;
111 for (; m; m = m->m_next) {
112 for (; offset < m->m_len; offset++, cmp++) {
115 else if (*cmp != *(mtod(m, char *) + offset))
127 * start at mbuf m, (must provide npkt if exists)
128 * starting at offset in m compare characters in mbuf chain for 'cmp'
129 * stop at 'max' characters
132 mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
136 for (; m != NULL; m = n) {
139 npkt = npkt->m_nextpkt;
140 for (; m; m = m->m_next) {
141 for (; offset < m->m_len; offset++, cmp++, max--) {
142 if (max == 0 || *cmp == '\0')
144 else if (*cmp != *(mtod(m, char *) + offset))
147 if (max == 0 || *cmp == '\0')
155 #define STRSETUP(sptr, slen, str) \
158 slen = sizeof(str) - 1; \
162 sohashttpget(struct socket *so, void *arg, int waitflag)
165 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) {
170 m = so->so_rcv.sb_mb;
171 cc = so->so_rcv.sb_cc - 1;
174 switch (*mtod(m, char *)) {
176 STRSETUP(cmp, cmplen, "ET ");
179 STRSETUP(cmp, cmplen, "EAD ");
185 if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
186 DPRINT("short cc (%d) but mbufstrncmp ok", cc);
189 DPRINT("short cc (%d) mbufstrncmp failed", cc);
193 if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
194 DPRINT("mbufstrcmp ok");
195 if (parse_http_version == 0)
196 return (soishttpconnected(so, arg, waitflag));
198 return (soparsehttpvers(so, arg, waitflag));
200 DPRINT("mbufstrcmp bad");
205 return (SU_ISCONNECTED);
209 soparsehttpvers(struct socket *so, void *arg, int waitflag)
212 int i, cc, spaces, inspaces;
214 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
217 m = so->so_rcv.sb_mb;
218 cc = so->so_rcv.sb_cc;
219 inspaces = spaces = 0;
220 for (m = so->so_rcv.sb_mb; m; m = n) {
222 for (; m; m = m->m_next) {
223 for (i = 0; i < m->m_len; i++, cc--) {
224 switch (*(mtod(m, char *) + i)) {
243 * if we don't have enough characters
244 * left (cc < sizeof("HTTP/1.0") - 1)
245 * then see if the remaining ones
246 * are a request we can parse.
248 if (cc < sizeof("HTTP/1.0") - 1) {
249 if (mbufstrncmp(m, n, i, cc,
258 mbufstrcmp(m, n, i, "HTTP/1.0") ||
259 mbufstrcmp(m, n, i, "HTTP/1.1")) {
261 return (soishttpconnected(so,
274 * if we hit here we haven't hit something
275 * we don't understand or a newline, so try again
277 soupcall_set(so, SO_RCV, soparsehttpvers, arg);
282 return (SU_ISCONNECTED);
289 soishttpconnected(struct socket *so, void *arg, int waitflag)
296 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
300 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
301 * copied - how much we've copied so far
302 * ccleft - how many bytes remaining in the socketbuffer
303 * just loop over the mbufs subtracting from 'ccleft' until we only
307 ccleft = so->so_rcv.sb_cc;
311 for (m = so->so_rcv.sb_mb; m; m = n) {
313 for (; m; m = m->m_next) {
315 if (ccleft <= NCHRS) {
319 tocopy = (NCHRS - ccleft) - copied;
320 src = mtod(m, char *) + (m->m_len - tocopy);
338 if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
339 /* we have all request headers */
344 soupcall_set(so, SO_RCV, soishttpconnected, arg);
348 return (SU_ISCONNECTED);