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