2005-01-07 01:45:51 +00:00
|
|
|
/*-
|
2000-09-06 18:49:13 +00:00
|
|
|
* Copyright (c) 2000 Paycounter, Inc.
|
|
|
|
* Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
|
2000-06-20 01:09:23 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 20:44:24 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2000-06-20 01:09:23 +00:00
|
|
|
#define ACCEPT_FILTER_MOD
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/mbuf.h>
|
2004-05-30 20:27:19 +00:00
|
|
|
#include <sys/module.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/signalvar.h>
|
2000-09-06 18:49:13 +00:00
|
|
|
#include <sys/sysctl.h>
|
2000-06-20 01:09:23 +00:00
|
|
|
#include <sys/socketvar.h>
|
|
|
|
|
2000-09-06 18:49:13 +00:00
|
|
|
/* check for GET/HEAD */
|
2009-06-01 21:17:03 +00:00
|
|
|
static int sohashttpget(struct socket *so, void *arg, int waitflag);
|
2000-09-06 18:49:13 +00:00
|
|
|
/* check for HTTP/1.0 or HTTP/1.1 */
|
2009-06-01 21:17:03 +00:00
|
|
|
static int soparsehttpvers(struct socket *so, void *arg, int waitflag);
|
2000-09-06 18:49:13 +00:00
|
|
|
/* check for end of HTTP/1.x request */
|
2009-06-01 21:17:03 +00:00
|
|
|
static int soishttpconnected(struct socket *so, void *arg, int waitflag);
|
2000-09-06 18:49:13 +00:00
|
|
|
/* strcmp on an mbuf chain */
|
|
|
|
static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp);
|
|
|
|
/* strncmp on an mbuf chain */
|
|
|
|
static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
|
|
|
|
int max, char *cmp);
|
|
|
|
/* socketbuffer is full */
|
|
|
|
static int sbfull(struct sockbuf *sb);
|
2000-06-20 01:09:23 +00:00
|
|
|
|
|
|
|
static struct accept_filter accf_http_filter = {
|
|
|
|
"httpready",
|
|
|
|
sohashttpget,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static moduledata_t accf_http_mod = {
|
|
|
|
"accf_http",
|
|
|
|
accept_filt_generic_mod_event,
|
|
|
|
&accf_http_filter
|
|
|
|
};
|
|
|
|
|
|
|
|
DECLARE_MODULE(accf_http, accf_http_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
|
|
|
|
|
2000-09-06 18:49:13 +00:00
|
|
|
static int parse_http_version = 1;
|
2000-06-20 01:09:23 +00:00
|
|
|
|
2000-09-06 18:49:13 +00:00
|
|
|
SYSCTL_NODE(_net_inet_accf, OID_AUTO, http, CTLFLAG_RW, 0,
|
|
|
|
"HTTP accept filter");
|
|
|
|
SYSCTL_INT(_net_inet_accf_http, OID_AUTO, parsehttpversion, CTLFLAG_RW,
|
|
|
|
&parse_http_version, 1,
|
|
|
|
"Parse http version so that non 1.x requests work");
|
|
|
|
|
|
|
|
#ifdef ACCF_HTTP_DEBUG
|
2002-05-01 08:08:24 +00:00
|
|
|
#define DPRINT(fmt, args...) \
|
|
|
|
do { \
|
|
|
|
printf("%s:%d: " fmt "\n", __func__, __LINE__, ##args); \
|
2000-09-06 18:49:13 +00:00
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define DPRINT(fmt, args...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
|
|
|
sbfull(struct sockbuf *sb)
|
2000-06-20 01:09:23 +00:00
|
|
|
{
|
2000-09-06 18:49:13 +00:00
|
|
|
|
2002-05-01 08:08:24 +00:00
|
|
|
DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, "
|
|
|
|
"mbcnt(%ld) >= mbmax(%ld): %d",
|
|
|
|
sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat,
|
|
|
|
sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax);
|
|
|
|
return (sb->sb_cc >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
|
2000-09-06 18:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* start at mbuf m, (must provide npkt if exists)
|
|
|
|
* starting at offset in m compare characters in mbuf chain for 'cmp'
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
|
|
|
|
{
|
|
|
|
struct mbuf *n;
|
|
|
|
|
2002-05-01 08:08:24 +00:00
|
|
|
for (; m != NULL; m = n) {
|
2000-09-06 18:49:13 +00:00
|
|
|
n = npkt;
|
|
|
|
if (npkt)
|
|
|
|
npkt = npkt->m_nextpkt;
|
|
|
|
for (; m; m = m->m_next) {
|
|
|
|
for (; offset < m->m_len; offset++, cmp++) {
|
2002-05-01 08:08:24 +00:00
|
|
|
if (*cmp == '\0')
|
2000-09-06 18:49:13 +00:00
|
|
|
return (1);
|
2002-05-01 08:08:24 +00:00
|
|
|
else if (*cmp != *(mtod(m, char *) + offset))
|
2000-09-06 18:49:13 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2002-05-01 08:29:41 +00:00
|
|
|
if (*cmp == '\0')
|
|
|
|
return (1);
|
2000-09-06 18:49:13 +00:00
|
|
|
offset = 0;
|
2000-06-20 01:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
2000-09-06 18:49:13 +00:00
|
|
|
return (0);
|
2000-06-20 01:09:23 +00:00
|
|
|
}
|
|
|
|
|
2000-09-06 18:49:13 +00:00
|
|
|
/*
|
|
|
|
* start at mbuf m, (must provide npkt if exists)
|
|
|
|
* starting at offset in m compare characters in mbuf chain for 'cmp'
|
|
|
|
* stop at 'max' characters
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
|
|
|
|
{
|
|
|
|
struct mbuf *n;
|
|
|
|
|
2002-05-01 08:08:24 +00:00
|
|
|
for (; m != NULL; m = n) {
|
2000-09-06 18:49:13 +00:00
|
|
|
n = npkt;
|
|
|
|
if (npkt)
|
|
|
|
npkt = npkt->m_nextpkt;
|
|
|
|
for (; m; m = m->m_next) {
|
|
|
|
for (; offset < m->m_len; offset++, cmp++, max--) {
|
2002-05-01 08:08:24 +00:00
|
|
|
if (max == 0 || *cmp == '\0')
|
2000-09-06 18:49:13 +00:00
|
|
|
return (1);
|
2002-05-01 08:08:24 +00:00
|
|
|
else if (*cmp != *(mtod(m, char *) + offset))
|
2000-09-06 18:49:13 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2002-05-01 08:29:41 +00:00
|
|
|
if (max == 0 || *cmp == '\0')
|
|
|
|
return (1);
|
2000-09-06 18:49:13 +00:00
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-05-01 08:08:24 +00:00
|
|
|
#define STRSETUP(sptr, slen, str) \
|
|
|
|
do { \
|
|
|
|
sptr = str; \
|
|
|
|
slen = sizeof(str) - 1; \
|
2000-09-06 18:49:13 +00:00
|
|
|
} while(0)
|
|
|
|
|
2009-06-01 21:17:03 +00:00
|
|
|
static int
|
2000-06-20 01:09:23 +00:00
|
|
|
sohashttpget(struct socket *so, void *arg, int waitflag)
|
|
|
|
{
|
|
|
|
|
2004-06-14 18:16:22 +00:00
|
|
|
if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) {
|
2000-06-20 01:09:23 +00:00
|
|
|
struct mbuf *m;
|
2000-09-06 18:49:13 +00:00
|
|
|
char *cmp;
|
|
|
|
int cmplen, cc;
|
2000-06-20 01:09:23 +00:00
|
|
|
|
|
|
|
m = so->so_rcv.sb_mb;
|
2000-09-06 18:49:13 +00:00
|
|
|
cc = so->so_rcv.sb_cc - 1;
|
|
|
|
if (cc < 1)
|
2009-06-01 21:17:03 +00:00
|
|
|
return (SU_OK);
|
2000-09-06 18:49:13 +00:00
|
|
|
switch (*mtod(m, char *)) {
|
|
|
|
case 'G':
|
|
|
|
STRSETUP(cmp, cmplen, "ET ");
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
STRSETUP(cmp, cmplen, "EAD ");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fallout;
|
|
|
|
}
|
|
|
|
if (cc < cmplen) {
|
|
|
|
if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
|
|
|
|
DPRINT("short cc (%d) but mbufstrncmp ok", cc);
|
2009-06-01 21:17:03 +00:00
|
|
|
return (SU_OK);
|
2000-09-06 18:49:13 +00:00
|
|
|
} else {
|
|
|
|
DPRINT("short cc (%d) mbufstrncmp failed", cc);
|
|
|
|
goto fallout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
|
|
|
|
DPRINT("mbufstrcmp ok");
|
|
|
|
if (parse_http_version == 0)
|
2009-06-01 21:17:03 +00:00
|
|
|
return (soishttpconnected(so, arg, waitflag));
|
2000-09-06 18:49:13 +00:00
|
|
|
else
|
2009-06-01 21:17:03 +00:00
|
|
|
return (soparsehttpvers(so, arg, waitflag));
|
2000-06-20 01:09:23 +00:00
|
|
|
}
|
2000-09-06 18:49:13 +00:00
|
|
|
DPRINT("mbufstrcmp bad");
|
2002-05-31 11:52:35 +00:00
|
|
|
}
|
2000-06-20 01:09:23 +00:00
|
|
|
|
2000-09-06 18:49:13 +00:00
|
|
|
fallout:
|
|
|
|
DPRINT("fallout");
|
2009-06-01 21:17:03 +00:00
|
|
|
return (SU_ISCONNECTED);
|
2000-06-20 01:09:23 +00:00
|
|
|
}
|
|
|
|
|
2009-06-01 21:17:03 +00:00
|
|
|
static int
|
2000-09-06 18:49:13 +00:00
|
|
|
soparsehttpvers(struct socket *so, void *arg, int waitflag)
|
2000-06-20 01:09:23 +00:00
|
|
|
{
|
2000-09-06 18:49:13 +00:00
|
|
|
struct mbuf *m, *n;
|
|
|
|
int i, cc, spaces, inspaces;
|
|
|
|
|
2004-06-14 18:16:22 +00:00
|
|
|
if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
|
2000-09-06 18:49:13 +00:00
|
|
|
goto fallout;
|
|
|
|
|
|
|
|
m = so->so_rcv.sb_mb;
|
|
|
|
cc = so->so_rcv.sb_cc;
|
|
|
|
inspaces = spaces = 0;
|
|
|
|
for (m = so->so_rcv.sb_mb; m; m = n) {
|
|
|
|
n = m->m_nextpkt;
|
|
|
|
for (; m; m = m->m_next) {
|
|
|
|
for (i = 0; i < m->m_len; i++, cc--) {
|
|
|
|
switch (*(mtod(m, char *) + i)) {
|
|
|
|
case ' ':
|
2002-05-01 08:08:24 +00:00
|
|
|
/* tabs? '\t' */
|
2000-09-06 18:49:13 +00:00
|
|
|
if (!inspaces) {
|
|
|
|
spaces++;
|
|
|
|
inspaces = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
DPRINT("newline");
|
|
|
|
goto fallout;
|
|
|
|
default:
|
2002-05-01 08:08:24 +00:00
|
|
|
if (spaces != 2) {
|
|
|
|
inspaces = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if we don't have enough characters
|
|
|
|
* left (cc < sizeof("HTTP/1.0") - 1)
|
|
|
|
* then see if the remaining ones
|
|
|
|
* are a request we can parse.
|
|
|
|
*/
|
|
|
|
if (cc < sizeof("HTTP/1.0") - 1) {
|
|
|
|
if (mbufstrncmp(m, n, i, cc,
|
|
|
|
"HTTP/1.") == 1) {
|
|
|
|
DPRINT("ok");
|
|
|
|
goto readmore;
|
2000-09-06 18:49:13 +00:00
|
|
|
} else {
|
2002-05-01 08:08:24 +00:00
|
|
|
DPRINT("bad");
|
2000-09-06 18:49:13 +00:00
|
|
|
goto fallout;
|
|
|
|
}
|
2002-05-01 08:08:24 +00:00
|
|
|
} else if (
|
|
|
|
mbufstrcmp(m, n, i, "HTTP/1.0") ||
|
|
|
|
mbufstrcmp(m, n, i, "HTTP/1.1")) {
|
2009-06-01 21:17:03 +00:00
|
|
|
DPRINT("ok");
|
|
|
|
return (soishttpconnected(so,
|
|
|
|
arg, waitflag));
|
2002-05-01 08:08:24 +00:00
|
|
|
} else {
|
|
|
|
DPRINT("bad");
|
|
|
|
goto fallout;
|
2000-09-06 18:49:13 +00:00
|
|
|
}
|
|
|
|
}
|
2000-06-20 01:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
2000-09-06 18:49:13 +00:00
|
|
|
}
|
|
|
|
readmore:
|
|
|
|
DPRINT("readmore");
|
|
|
|
/*
|
|
|
|
* if we hit here we haven't hit something
|
|
|
|
* we don't understand or a newline, so try again
|
|
|
|
*/
|
2009-06-01 21:17:03 +00:00
|
|
|
soupcall_set(so, SO_RCV, soparsehttpvers, arg);
|
|
|
|
return (SU_OK);
|
2000-06-20 01:09:23 +00:00
|
|
|
|
2000-09-06 18:49:13 +00:00
|
|
|
fallout:
|
|
|
|
DPRINT("fallout");
|
2009-06-01 21:17:03 +00:00
|
|
|
return (SU_ISCONNECTED);
|
2000-09-06 18:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define NCHRS 3
|
|
|
|
|
2009-06-01 21:17:03 +00:00
|
|
|
static int
|
2000-09-06 18:49:13 +00:00
|
|
|
soishttpconnected(struct socket *so, void *arg, int waitflag)
|
|
|
|
{
|
|
|
|
char a, b, c;
|
|
|
|
struct mbuf *m, *n;
|
|
|
|
int ccleft, copied;
|
|
|
|
|
|
|
|
DPRINT("start");
|
2004-06-14 18:16:22 +00:00
|
|
|
if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
|
2000-09-06 18:49:13 +00:00
|
|
|
goto gotit;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
|
|
|
|
* copied - how much we've copied so far
|
|
|
|
* ccleft - how many bytes remaining in the socketbuffer
|
|
|
|
* just loop over the mbufs subtracting from 'ccleft' until we only
|
|
|
|
* have NCHRS left
|
|
|
|
*/
|
|
|
|
copied = 0;
|
|
|
|
ccleft = so->so_rcv.sb_cc;
|
|
|
|
if (ccleft < NCHRS)
|
|
|
|
goto readmore;
|
|
|
|
a = b = c = '\0';
|
|
|
|
for (m = so->so_rcv.sb_mb; m; m = n) {
|
|
|
|
n = m->m_nextpkt;
|
|
|
|
for (; m; m = m->m_next) {
|
|
|
|
ccleft -= m->m_len;
|
|
|
|
if (ccleft <= NCHRS) {
|
|
|
|
char *src;
|
|
|
|
int tocopy;
|
|
|
|
|
|
|
|
tocopy = (NCHRS - ccleft) - copied;
|
|
|
|
src = mtod(m, char *) + (m->m_len - tocopy);
|
|
|
|
|
|
|
|
while (tocopy--) {
|
|
|
|
switch (copied++) {
|
|
|
|
case 0:
|
|
|
|
a = *src++;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
b = *src++;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
c = *src++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-06-20 01:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
2000-09-06 18:49:13 +00:00
|
|
|
if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
|
|
|
|
/* we have all request headers */
|
|
|
|
goto gotit;
|
|
|
|
}
|
|
|
|
|
|
|
|
readmore:
|
2009-06-01 21:17:03 +00:00
|
|
|
soupcall_set(so, SO_RCV, soishttpconnected, arg);
|
|
|
|
return (SU_OK);
|
2000-06-20 01:09:23 +00:00
|
|
|
|
2000-09-06 18:49:13 +00:00
|
|
|
gotit:
|
2009-06-01 21:17:03 +00:00
|
|
|
return (SU_ISCONNECTED);
|
2000-06-20 01:09:23 +00:00
|
|
|
}
|