xref: /dragonfly/sys/net/accf_http/accf_http.c (revision 333227be)
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: src/sys/netinet/accf_http.c,v 1.1.2.4 2002/05/01 08:34:37 alfred Exp $
28  *	$DragonFly: src/sys/net/accf_http/accf_http.c,v 1.2 2003/06/17 04:28:51 dillon Exp $
29  */
30 
31 #define ACCEPT_FILTER_MOD
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysproto.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/malloc.h>
39 #include <sys/unistd.h>
40 #include <sys/file.h>
41 #include <sys/fcntl.h>
42 #include <sys/protosw.h>
43 #include <sys/sysctl.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/stat.h>
47 #include <sys/mbuf.h>
48 #include <sys/resource.h>
49 #include <sys/sysent.h>
50 #include <sys/resourcevar.h>
51 
52 /* check for GET/HEAD */
53 static void sohashttpget(struct socket *so, void *arg, int waitflag);
54 /* check for HTTP/1.0 or HTTP/1.1 */
55 static void soparsehttpvers(struct socket *so, void *arg, int waitflag);
56 /* check for end of HTTP/1.x request */
57 static void soishttpconnected(struct socket *so, void *arg, int waitflag);
58 /* strcmp on an mbuf chain */
59 static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp);
60 /* strncmp on an mbuf chain */
61 static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
62 	int max, char *cmp);
63 /* socketbuffer is full */
64 static int sbfull(struct sockbuf *sb);
65 
66 static struct accept_filter accf_http_filter = {
67 	"httpready",
68 	sohashttpget,
69 	NULL,
70 	NULL
71 };
72 
73 static moduledata_t accf_http_mod = {
74 	"accf_http",
75 	accept_filt_generic_mod_event,
76 	&accf_http_filter
77 };
78 
79 DECLARE_MODULE(accf_http, accf_http_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
80 
81 static int parse_http_version = 1;
82 
83 SYSCTL_NODE(_net_inet_accf, OID_AUTO, http, CTLFLAG_RW, 0,
84 "HTTP accept filter");
85 SYSCTL_INT(_net_inet_accf_http, OID_AUTO, parsehttpversion, CTLFLAG_RW,
86 &parse_http_version, 1,
87 "Parse http version so that non 1.x requests work");
88 
89 #ifdef ACCF_HTTP_DEBUG
90 #define DPRINT(fmt, args...) \
91 	do {	\
92 		printf("%s:%d: " fmt "\n", __func__, __LINE__ , ##args);	\
93 	} while (0)
94 #else
95 #define DPRINT(fmt, args...)
96 #endif
97 
98 static int
99 sbfull(struct sockbuf *sb)
100 {
101 
102 	DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, mbcnt(%ld) >= mbmax(%ld): %d",
103 		sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat,
104 		sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax);
105 	return(sb->sb_cc >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
106 }
107 
108 /*
109  * start at mbuf m, (must provide npkt if exists)
110  * starting at offset in m compare characters in mbuf chain for 'cmp'
111  */
112 static int
113 mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
114 {
115 	struct mbuf *n;
116 
117 	for (;m != NULL; m = n) {
118 		n = npkt;
119 		if (npkt)
120 			npkt = npkt->m_nextpkt;
121 		for (; m; m = m->m_next) {
122 			for (; offset < m->m_len; offset++, cmp++) {
123 				if (*cmp == '\0') {
124 					return (1);
125 				} else if (*cmp != *(mtod(m, char *) + offset)) {
126 					return (0);
127 				}
128 			}
129 			if (*cmp == '\0')
130 				return (1);
131 			offset = 0;
132 		}
133 	}
134 	return (0);
135 }
136 
137 /*
138  * start at mbuf m, (must provide npkt if exists)
139  * starting at offset in m compare characters in mbuf chain for 'cmp'
140  * stop at 'max' characters
141  */
142 static int
143 mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
144 {
145 	struct mbuf *n;
146 
147 	for (;m != NULL; m = n) {
148 		n = npkt;
149 		if (npkt)
150 			npkt = npkt->m_nextpkt;
151 		for (; m; m = m->m_next) {
152 			for (; offset < m->m_len; offset++, cmp++, max--) {
153 				if (max == 0 || *cmp == '\0') {
154 					return (1);
155 				} else if (*cmp != *(mtod(m, char *) + offset)) {
156 					return (0);
157 				}
158 			}
159 			if (max == 0 || *cmp == '\0')
160 				return (1);
161 			offset = 0;
162 		}
163 	}
164 	return (0);
165 }
166 
167 #define STRSETUP(sptr, slen, str) \
168 	do {	\
169 		sptr = str;	\
170 		slen = sizeof(str) - 1;	\
171 	} while(0)
172 
173 static void
174 sohashttpget(struct socket *so, void *arg, int waitflag)
175 {
176 
177 	if ((so->so_state & SS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) {
178 		struct mbuf *m;
179 		char *cmp;
180 		int	cmplen, cc;
181 
182 		m = so->so_rcv.sb_mb;
183 		cc = so->so_rcv.sb_cc - 1;
184 		if (cc < 1)
185 			return;
186 		switch (*mtod(m, char *)) {
187 		case 'G':
188 			STRSETUP(cmp, cmplen, "ET ");
189 			break;
190 		case 'H':
191 			STRSETUP(cmp, cmplen, "EAD ");
192 			break;
193 		default:
194 			goto fallout;
195 		}
196 		if (cc < cmplen) {
197 			if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
198 				DPRINT("short cc (%d) but mbufstrncmp ok", cc);
199 				return;
200 			} else {
201 				DPRINT("short cc (%d) mbufstrncmp failed", cc);
202 				goto fallout;
203 			}
204 		}
205 		if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
206 			DPRINT("mbufstrcmp ok");
207 			if (parse_http_version == 0)
208 				soishttpconnected(so, arg, waitflag);
209 			else
210 				soparsehttpvers(so, arg, waitflag);
211 			return;
212 		}
213 		DPRINT("mbufstrcmp bad");
214 	}
215 
216 fallout:
217 	DPRINT("fallout");
218 	so->so_upcall = NULL;
219 	so->so_rcv.sb_flags &= ~SB_UPCALL;
220 	soisconnected(so);
221 	return;
222 }
223 
224 static void
225 soparsehttpvers(struct socket *so, void *arg, int waitflag)
226 {
227 	struct mbuf *m, *n;
228 	int	i, cc, spaces, inspaces;
229 
230 	if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
231 		goto fallout;
232 
233 	m = so->so_rcv.sb_mb;
234 	cc = so->so_rcv.sb_cc;
235 	inspaces = spaces = 0;
236 	for (m = so->so_rcv.sb_mb; m; m = n) {
237 		n = m->m_nextpkt;
238 		for (; m; m = m->m_next) {
239 			for (i = 0; i < m->m_len; i++, cc--) {
240 				switch (*(mtod(m, char *) + i)) {
241 				case ' ':
242 					if (!inspaces) {
243 						spaces++;
244 						inspaces = 1;
245 					}
246 					break;
247 				case '\r':
248 				case '\n':
249 					DPRINT("newline");
250 					goto fallout;
251 				default:
252 					if (spaces == 2) {
253 						/* make sure we have enough data left */
254 						if (cc < sizeof("HTTP/1.0") - 1) {
255 							if (mbufstrncmp(m, n, i, cc, "HTTP/1.") == 1) {
256 								DPRINT("mbufstrncmp ok");
257 								goto readmore;
258 							} else {
259 								DPRINT("mbufstrncmp bad");
260 								goto fallout;
261 							}
262 						} else if (mbufstrcmp(m, n, i, "HTTP/1.0") == 1 ||
263 									mbufstrcmp(m, n, i, "HTTP/1.1") == 1) {
264 								DPRINT("mbufstrcmp ok");
265 								soishttpconnected(so, arg, waitflag);
266 								return;
267 						} else {
268 							DPRINT("mbufstrcmp bad");
269 							goto fallout;
270 						}
271 					}
272 					inspaces = 0;
273 					break;
274 				}
275 			}
276 		}
277 	}
278 readmore:
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 
288 fallout:
289 	DPRINT("fallout");
290 	so->so_upcall = NULL;
291 	so->so_rcv.sb_flags &= ~SB_UPCALL;
292 	soisconnected(so);
293 	return;
294 }
295 
296 
297 #define NCHRS 3
298 
299 static void
300 soishttpconnected(struct socket *so, void *arg, int waitflag)
301 {
302 	char a, b, c;
303 	struct mbuf *m, *n;
304 	int ccleft, copied;
305 
306 	DPRINT("start");
307 	if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
308 		goto gotit;
309 
310 	/*
311 	 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
312 	 * copied - how much we've copied so far
313 	 * ccleft - how many bytes remaining in the socketbuffer
314 	 * just loop over the mbufs subtracting from 'ccleft' until we only
315 	 * have NCHRS left
316 	 */
317 	copied = 0;
318 	ccleft = so->so_rcv.sb_cc;
319 	if (ccleft < NCHRS)
320 		goto readmore;
321 	a = b = c = '\0';
322 	for (m = so->so_rcv.sb_mb; m; m = n) {
323 		n = m->m_nextpkt;
324 		for (; m; m = m->m_next) {
325 			ccleft -= m->m_len;
326 			if (ccleft <= NCHRS) {
327 				char *src;
328 				int tocopy;
329 
330 				tocopy = (NCHRS - ccleft) - copied;
331 				src = mtod(m, char *) + (m->m_len - tocopy);
332 
333 				while (tocopy--) {
334 					switch (copied++) {
335 					case 0:
336 						a = *src++;
337 						break;
338 					case 1:
339 						b = *src++;
340 						break;
341 					case 2:
342 						c = *src++;
343 						break;
344 					}
345 				}
346 			}
347 		}
348 	}
349 	if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
350 		/* we have all request headers */
351 		goto gotit;
352 	}
353 
354 readmore:
355 	so->so_upcall = soishttpconnected;
356 	so->so_rcv.sb_flags |= SB_UPCALL;
357 	return;
358 
359 gotit:
360 	so->so_upcall = NULL;
361 	so->so_rcv.sb_flags &= ~SB_UPCALL;
362 	soisconnected(so);
363 	return;
364 }
365