accf_http.c revision 96972
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 96972 2002-05-20 05:41:09Z tanimura $
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/mutex.h>
37#include <sys/signalvar.h>
38#include <sys/sysctl.h>
39#include <sys/socketvar.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	SOCK_LOCK(so);
166	if ((so->so_state & SS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) {
167		struct mbuf *m;
168		char *cmp;
169		int	cmplen, cc;
170
171		SOCK_UNLOCK(so);
172		m = so->so_rcv.sb_mb;
173		cc = so->so_rcv.sb_cc - 1;
174		if (cc < 1)
175			return;
176		switch (*mtod(m, char *)) {
177		case 'G':
178			STRSETUP(cmp, cmplen, "ET ");
179			break;
180		case 'H':
181			STRSETUP(cmp, cmplen, "EAD ");
182			break;
183		default:
184			goto fallout;
185		}
186		if (cc < cmplen) {
187			if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
188				DPRINT("short cc (%d) but mbufstrncmp ok", cc);
189				return;
190			} else {
191				DPRINT("short cc (%d) mbufstrncmp failed", cc);
192				goto fallout;
193			}
194		}
195		if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
196			DPRINT("mbufstrcmp ok");
197			if (parse_http_version == 0)
198				soishttpconnected(so, arg, waitflag);
199			else
200				soparsehttpvers(so, arg, waitflag);
201			return;
202		}
203		DPRINT("mbufstrcmp bad");
204	} else
205		SOCK_UNLOCK(so);
206
207fallout:
208	DPRINT("fallout");
209	SOCK_LOCK(so);
210	so->so_upcall = NULL;
211	so->so_rcv.sb_flags &= ~SB_UPCALL;
212	soisconnected(so);
213	SOCK_UNLOCK(so);
214	return;
215}
216
217static void
218soparsehttpvers(struct socket *so, void *arg, int waitflag)
219{
220	struct mbuf *m, *n;
221	int	i, cc, spaces, inspaces;
222
223	SOCK_LOCK(so);
224	if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv)) {
225		SOCK_UNLOCK(so);
226		goto fallout;
227	}
228	SOCK_UNLOCK(so);
229
230	m = so->so_rcv.sb_mb;
231	cc = so->so_rcv.sb_cc;
232	inspaces = spaces = 0;
233	for (m = so->so_rcv.sb_mb; m; m = n) {
234		n = m->m_nextpkt;
235		for (; m; m = m->m_next) {
236			for (i = 0; i < m->m_len; i++, cc--) {
237				switch (*(mtod(m, char *) + i)) {
238				case ' ':
239					/* tabs? '\t' */
240					if (!inspaces) {
241						spaces++;
242						inspaces = 1;
243					}
244					break;
245				case '\r':
246				case '\n':
247					DPRINT("newline");
248					goto fallout;
249				default:
250					if (spaces != 2) {
251						inspaces = 0;
252						break;
253					}
254
255					/*
256					 * if we don't have enough characters
257					 * left (cc < sizeof("HTTP/1.0") - 1)
258					 * then see if the remaining ones
259					 * are a request we can parse.
260					 */
261					if (cc < sizeof("HTTP/1.0") - 1) {
262						if (mbufstrncmp(m, n, i, cc,
263							"HTTP/1.") == 1) {
264							DPRINT("ok");
265							goto readmore;
266						} else {
267							DPRINT("bad");
268							goto fallout;
269						}
270					} else if (
271					    mbufstrcmp(m, n, i, "HTTP/1.0") ||
272					    mbufstrcmp(m, n, i, "HTTP/1.1")) {
273							DPRINT("ok");
274							soishttpconnected(so,
275							    arg, waitflag);
276							return;
277					} else {
278						DPRINT("bad");
279						goto fallout;
280					}
281				}
282			}
283		}
284	}
285readmore:
286	DPRINT("readmore");
287	/*
288	 * if we hit here we haven't hit something
289	 * we don't understand or a newline, so try again
290	 */
291	so->so_upcall = soparsehttpvers;
292	so->so_rcv.sb_flags |= SB_UPCALL;
293	return;
294
295fallout:
296	DPRINT("fallout");
297	SOCK_LOCK(so);
298	so->so_upcall = NULL;
299	so->so_rcv.sb_flags &= ~SB_UPCALL;
300	soisconnected(so);
301	SOCK_UNLOCK(so);
302	return;
303}
304
305
306#define NCHRS 3
307
308static void
309soishttpconnected(struct socket *so, void *arg, int waitflag)
310{
311	char a, b, c;
312	struct mbuf *m, *n;
313	int ccleft, copied;
314
315	DPRINT("start");
316	SOCK_LOCK(so);
317	if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv)) {
318		SOCK_UNLOCK(so);
319		goto gotit;
320	}
321	SOCK_UNLOCK(so);
322
323	/*
324	 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
325	 * copied - how much we've copied so far
326	 * ccleft - how many bytes remaining in the socketbuffer
327	 * just loop over the mbufs subtracting from 'ccleft' until we only
328	 * have NCHRS left
329	 */
330	copied = 0;
331	ccleft = so->so_rcv.sb_cc;
332	if (ccleft < NCHRS)
333		goto readmore;
334	a = b = c = '\0';
335	for (m = so->so_rcv.sb_mb; m; m = n) {
336		n = m->m_nextpkt;
337		for (; m; m = m->m_next) {
338			ccleft -= m->m_len;
339			if (ccleft <= NCHRS) {
340				char *src;
341				int tocopy;
342
343				tocopy = (NCHRS - ccleft) - copied;
344				src = mtod(m, char *) + (m->m_len - tocopy);
345
346				while (tocopy--) {
347					switch (copied++) {
348					case 0:
349						a = *src++;
350						break;
351					case 1:
352						b = *src++;
353						break;
354					case 2:
355						c = *src++;
356						break;
357					}
358				}
359			}
360		}
361	}
362	if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
363		/* we have all request headers */
364		goto gotit;
365	}
366
367readmore:
368	so->so_upcall = soishttpconnected;
369	so->so_rcv.sb_flags |= SB_UPCALL;
370	return;
371
372gotit:
373	SOCK_LOCK(so);
374	so->so_upcall = NULL;
375	so->so_rcv.sb_flags &= ~SB_UPCALL;
376	soisconnected(so);
377	SOCK_UNLOCK(so);
378	return;
379}
380