accf_http.c revision 95867
1/*
2 * Copyright (c) 2000 Paycounter, Inc.
3 * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 *
27 *	$FreeBSD: head/sys/netinet/accf_http.c 95867 2002-05-01 08:29:41Z alfred $
28 */
29
30#define ACCEPT_FILTER_MOD
31
32#include <sys/param.h>
33#include <sys/kernel.h>
34#include <sys/lock.h>
35#include <sys/mbuf.h>
36#include <sys/signalvar.h>
37#include <sys/sysctl.h>
38#include <sys/socketvar.h>
39#include <sys/sx.h>
40
41/* check for GET/HEAD */
42static void sohashttpget(struct socket *so, void *arg, int waitflag);
43/* check for HTTP/1.0 or HTTP/1.1 */
44static void soparsehttpvers(struct socket *so, void *arg, int waitflag);
45/* check for end of HTTP/1.x request */
46static void soishttpconnected(struct socket *so, void *arg, int waitflag);
47/* strcmp on an mbuf chain */
48static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp);
49/* strncmp on an mbuf chain */
50static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
51	int max, char *cmp);
52/* socketbuffer is full */
53static int sbfull(struct sockbuf *sb);
54
55static struct accept_filter accf_http_filter = {
56	"httpready",
57	sohashttpget,
58	NULL,
59	NULL
60};
61
62static moduledata_t accf_http_mod = {
63	"accf_http",
64	accept_filt_generic_mod_event,
65	&accf_http_filter
66};
67
68DECLARE_MODULE(accf_http, accf_http_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
69
70static int parse_http_version = 1;
71
72SYSCTL_NODE(_net_inet_accf, OID_AUTO, http, CTLFLAG_RW, 0,
73"HTTP accept filter");
74SYSCTL_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");
77
78#ifdef ACCF_HTTP_DEBUG
79#define DPRINT(fmt, args...)						\
80	do {								\
81		printf("%s:%d: " fmt "\n", __func__, __LINE__, ##args);	\
82	} while (0)
83#else
84#define DPRINT(fmt, args...)
85#endif
86
87static int
88sbfull(struct sockbuf *sb)
89{
90
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);
96}
97
98/*
99 * start at mbuf m, (must provide npkt if exists)
100 * starting at offset in m compare characters in mbuf chain for 'cmp'
101 */
102static int
103mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
104{
105	struct mbuf *n;
106
107	for (; m != NULL; m = n) {
108		n = npkt;
109		if (npkt)
110			npkt = npkt->m_nextpkt;
111		for (; m; m = m->m_next) {
112			for (; offset < m->m_len; offset++, cmp++) {
113				if (*cmp == '\0')
114					return (1);
115				else if (*cmp != *(mtod(m, char *) + offset))
116					return (0);
117			}
118			if (*cmp == '\0')
119				return (1);
120			offset = 0;
121		}
122	}
123	return (0);
124}
125
126/*
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
130 */
131static int
132mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
133{
134	struct mbuf *n;
135
136	for (; m != NULL; m = n) {
137		n = npkt;
138		if (npkt)
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')
143					return (1);
144				else if (*cmp != *(mtod(m, char *) + offset))
145					return (0);
146			}
147			if (max == 0 || *cmp == '\0')
148				return (1);
149			offset = 0;
150		}
151	}
152	return (0);
153}
154
155#define STRSETUP(sptr, slen, str)					\
156	do {								\
157		sptr = str;						\
158		slen = sizeof(str) - 1;					\
159	} while(0)
160
161static void
162sohashttpget(struct socket *so, void *arg, int waitflag)
163{
164
165	if ((so->so_state & SS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) {
166		struct mbuf *m;
167		char *cmp;
168		int	cmplen, cc;
169
170		m = so->so_rcv.sb_mb;
171		cc = so->so_rcv.sb_cc - 1;
172		if (cc < 1)
173			return;
174		switch (*mtod(m, char *)) {
175		case 'G':
176			STRSETUP(cmp, cmplen, "ET ");
177			break;
178		case 'H':
179			STRSETUP(cmp, cmplen, "EAD ");
180			break;
181		default:
182			goto fallout;
183		}
184		if (cc < cmplen) {
185			if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
186				DPRINT("short cc (%d) but mbufstrncmp ok", cc);
187				return;
188			} else {
189				DPRINT("short cc (%d) mbufstrncmp failed", cc);
190				goto fallout;
191			}
192		}
193		if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
194			DPRINT("mbufstrcmp ok");
195			if (parse_http_version == 0)
196				soishttpconnected(so, arg, waitflag);
197			else
198				soparsehttpvers(so, arg, waitflag);
199			return;
200		}
201		DPRINT("mbufstrcmp bad");
202	}
203
204fallout:
205	DPRINT("fallout");
206	SIGIO_SLOCK();
207	so->so_upcall = NULL;
208	so->so_rcv.sb_flags &= ~SB_UPCALL;
209	soisconnected_locked(so);
210	SIGIO_SUNLOCK();
211	return;
212}
213
214static void
215soparsehttpvers(struct socket *so, void *arg, int waitflag)
216{
217	struct mbuf *m, *n;
218	int	i, cc, spaces, inspaces;
219
220	if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
221		goto fallout;
222
223	m = so->so_rcv.sb_mb;
224	cc = so->so_rcv.sb_cc;
225	inspaces = spaces = 0;
226	for (m = so->so_rcv.sb_mb; m; m = n) {
227		n = m->m_nextpkt;
228		for (; m; m = m->m_next) {
229			for (i = 0; i < m->m_len; i++, cc--) {
230				switch (*(mtod(m, char *) + i)) {
231				case ' ':
232					/* tabs? '\t' */
233					if (!inspaces) {
234						spaces++;
235						inspaces = 1;
236					}
237					break;
238				case '\r':
239				case '\n':
240					DPRINT("newline");
241					goto fallout;
242				default:
243					if (spaces != 2) {
244						inspaces = 0;
245						break;
246					}
247
248					/*
249					 * if we don't have enough characters
250					 * left (cc < sizeof("HTTP/1.0") - 1)
251					 * then see if the remaining ones
252					 * are a request we can parse.
253					 */
254					if (cc < sizeof("HTTP/1.0") - 1) {
255						if (mbufstrncmp(m, n, i, cc,
256							"HTTP/1.") == 1) {
257							DPRINT("ok");
258							goto readmore;
259						} else {
260							DPRINT("bad");
261							goto fallout;
262						}
263					} else if (
264					    mbufstrcmp(m, n, i, "HTTP/1.0") ||
265					    mbufstrcmp(m, n, i, "HTTP/1.1")) {
266							DPRINT("ok");
267							soishttpconnected(so,
268							    arg, waitflag);
269							return;
270					} else {
271						DPRINT("bad");
272						goto fallout;
273					}
274				}
275			}
276		}
277	}
278readmore:
279	DPRINT("readmore");
280	/*
281	 * if we hit here we haven't hit something
282	 * we don't understand or a newline, so try again
283	 */
284	so->so_upcall = soparsehttpvers;
285	so->so_rcv.sb_flags |= SB_UPCALL;
286	return;
287
288fallout:
289	DPRINT("fallout");
290	SIGIO_SLOCK();
291	so->so_upcall = NULL;
292	so->so_rcv.sb_flags &= ~SB_UPCALL;
293	soisconnected_locked(so);
294	SIGIO_SUNLOCK();
295	return;
296}
297
298
299#define NCHRS 3
300
301static void
302soishttpconnected(struct socket *so, void *arg, int waitflag)
303{
304	char a, b, c;
305	struct mbuf *m, *n;
306	int ccleft, copied;
307
308	DPRINT("start");
309	if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
310		goto gotit;
311
312	/*
313	 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
314	 * copied - how much we've copied so far
315	 * ccleft - how many bytes remaining in the socketbuffer
316	 * just loop over the mbufs subtracting from 'ccleft' until we only
317	 * have NCHRS left
318	 */
319	copied = 0;
320	ccleft = so->so_rcv.sb_cc;
321	if (ccleft < NCHRS)
322		goto readmore;
323	a = b = c = '\0';
324	for (m = so->so_rcv.sb_mb; m; m = n) {
325		n = m->m_nextpkt;
326		for (; m; m = m->m_next) {
327			ccleft -= m->m_len;
328			if (ccleft <= NCHRS) {
329				char *src;
330				int tocopy;
331
332				tocopy = (NCHRS - ccleft) - copied;
333				src = mtod(m, char *) + (m->m_len - tocopy);
334
335				while (tocopy--) {
336					switch (copied++) {
337					case 0:
338						a = *src++;
339						break;
340					case 1:
341						b = *src++;
342						break;
343					case 2:
344						c = *src++;
345						break;
346					}
347				}
348			}
349		}
350	}
351	if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
352		/* we have all request headers */
353		goto gotit;
354	}
355
356readmore:
357	so->so_upcall = soishttpconnected;
358	so->so_rcv.sb_flags |= SB_UPCALL;
359	return;
360
361gotit:
362	SIGIO_SLOCK();
363	so->so_upcall = NULL;
364	so->so_rcv.sb_flags &= ~SB_UPCALL;
365	soisconnected_locked(so);
366	SIGIO_SUNLOCK();
367	return;
368}
369