accf_http.c revision 95865
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 95865 2002-05-01 08:08:24Z 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			offset = 0;
119		}
120	}
121	return (0);
122}
123
124/*
125 * start at mbuf m, (must provide npkt if exists)
126 * starting at offset in m compare characters in mbuf chain for 'cmp'
127 * stop at 'max' characters
128 */
129static int
130mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
131{
132	struct mbuf *n;
133
134	for (; m != NULL; m = n) {
135		n = npkt;
136		if (npkt)
137			npkt = npkt->m_nextpkt;
138		for (; m; m = m->m_next) {
139			for (; offset < m->m_len; offset++, cmp++, max--) {
140				if (max == 0 || *cmp == '\0')
141					return (1);
142				else if (*cmp != *(mtod(m, char *) + offset))
143					return (0);
144			}
145			offset = 0;
146		}
147	}
148	return (0);
149}
150
151#define STRSETUP(sptr, slen, str)					\
152	do {								\
153		sptr = str;						\
154		slen = sizeof(str) - 1;					\
155	} while(0)
156
157static void
158sohashttpget(struct socket *so, void *arg, int waitflag)
159{
160
161	if ((so->so_state & SS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) {
162		struct mbuf *m;
163		char *cmp;
164		int	cmplen, cc;
165
166		m = so->so_rcv.sb_mb;
167		cc = so->so_rcv.sb_cc - 1;
168		if (cc < 1)
169			return;
170		switch (*mtod(m, char *)) {
171		case 'G':
172			STRSETUP(cmp, cmplen, "ET ");
173			break;
174		case 'H':
175			STRSETUP(cmp, cmplen, "EAD ");
176			break;
177		default:
178			goto fallout;
179		}
180		if (cc < cmplen) {
181			if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
182				DPRINT("short cc (%d) but mbufstrncmp ok", cc);
183				return;
184			} else {
185				DPRINT("short cc (%d) mbufstrncmp failed", cc);
186				goto fallout;
187			}
188		}
189		if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
190			DPRINT("mbufstrcmp ok");
191			if (parse_http_version == 0)
192				soishttpconnected(so, arg, waitflag);
193			else
194				soparsehttpvers(so, arg, waitflag);
195			return;
196		}
197		DPRINT("mbufstrcmp bad");
198	}
199
200fallout:
201	DPRINT("fallout");
202	SIGIO_SLOCK();
203	so->so_upcall = NULL;
204	so->so_rcv.sb_flags &= ~SB_UPCALL;
205	soisconnected_locked(so);
206	SIGIO_SUNLOCK();
207	return;
208}
209
210static void
211soparsehttpvers(struct socket *so, void *arg, int waitflag)
212{
213	struct mbuf *m, *n;
214	int	i, cc, spaces, inspaces;
215
216	if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
217		goto fallout;
218
219	m = so->so_rcv.sb_mb;
220	cc = so->so_rcv.sb_cc;
221	inspaces = spaces = 0;
222	for (m = so->so_rcv.sb_mb; m; m = n) {
223		n = m->m_nextpkt;
224		for (; m; m = m->m_next) {
225			for (i = 0; i < m->m_len; i++, cc--) {
226				switch (*(mtod(m, char *) + i)) {
227				case ' ':
228					/* tabs? '\t' */
229					if (!inspaces) {
230						spaces++;
231						inspaces = 1;
232					}
233					break;
234				case '\r':
235				case '\n':
236					DPRINT("newline");
237					goto fallout;
238				default:
239					if (spaces != 2) {
240						inspaces = 0;
241						break;
242					}
243
244					/*
245					 * if we don't have enough characters
246					 * left (cc < sizeof("HTTP/1.0") - 1)
247					 * then see if the remaining ones
248					 * are a request we can parse.
249					 */
250					if (cc < sizeof("HTTP/1.0") - 1) {
251						if (mbufstrncmp(m, n, i, cc,
252							"HTTP/1.") == 1) {
253							DPRINT("ok");
254							goto readmore;
255						} else {
256							DPRINT("bad");
257							goto fallout;
258						}
259					} else if (
260					    mbufstrcmp(m, n, i, "HTTP/1.0") ||
261					    mbufstrcmp(m, n, i, "HTTP/1.1")) {
262							DPRINT("ok");
263							soishttpconnected(so,
264							    arg, waitflag);
265							return;
266					} else {
267						DPRINT("bad");
268						goto fallout;
269					}
270				}
271			}
272		}
273	}
274readmore:
275	DPRINT("readmore");
276	/*
277	 * if we hit here we haven't hit something
278	 * we don't understand or a newline, so try again
279	 */
280	so->so_upcall = soparsehttpvers;
281	so->so_rcv.sb_flags |= SB_UPCALL;
282	return;
283
284fallout:
285	DPRINT("fallout");
286	SIGIO_SLOCK();
287	so->so_upcall = NULL;
288	so->so_rcv.sb_flags &= ~SB_UPCALL;
289	soisconnected_locked(so);
290	SIGIO_SUNLOCK();
291	return;
292}
293
294
295#define NCHRS 3
296
297static void
298soishttpconnected(struct socket *so, void *arg, int waitflag)
299{
300	char a, b, c;
301	struct mbuf *m, *n;
302	int ccleft, copied;
303
304	DPRINT("start");
305	if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
306		goto gotit;
307
308	/*
309	 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
310	 * copied - how much we've copied so far
311	 * ccleft - how many bytes remaining in the socketbuffer
312	 * just loop over the mbufs subtracting from 'ccleft' until we only
313	 * have NCHRS left
314	 */
315	copied = 0;
316	ccleft = so->so_rcv.sb_cc;
317	if (ccleft < NCHRS)
318		goto readmore;
319	a = b = c = '\0';
320	for (m = so->so_rcv.sb_mb; m; m = n) {
321		n = m->m_nextpkt;
322		for (; m; m = m->m_next) {
323			ccleft -= m->m_len;
324			if (ccleft <= NCHRS) {
325				char *src;
326				int tocopy;
327
328				tocopy = (NCHRS - ccleft) - copied;
329				src = mtod(m, char *) + (m->m_len - tocopy);
330
331				while (tocopy--) {
332					switch (copied++) {
333					case 0:
334						a = *src++;
335						break;
336					case 1:
337						b = *src++;
338						break;
339					case 2:
340						c = *src++;
341						break;
342					}
343				}
344			}
345		}
346	}
347	if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
348		/* we have all request headers */
349		goto gotit;
350	}
351
352readmore:
353	so->so_upcall = soishttpconnected;
354	so->so_rcv.sb_flags |= SB_UPCALL;
355	return;
356
357gotit:
358	SIGIO_SLOCK();
359	so->so_upcall = NULL;
360	so->so_rcv.sb_flags &= ~SB_UPCALL;
361	soisconnected_locked(so);
362	SIGIO_SUNLOCK();
363	return;
364}
365