xref: /freebsd/sys/netinet/accf_http.c (revision d6b92ffa)
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 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #define ACCEPT_FILTER_MOD
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/mbuf.h>
36 #include <sys/module.h>
37 #include <sys/signalvar.h>
38 #include <sys/sysctl.h>
39 #include <sys/socketvar.h>
40 
41 /* check for GET/HEAD */
42 static int sohashttpget(struct socket *so, void *arg, int waitflag);
43 /* check for HTTP/1.0 or HTTP/1.1 */
44 static int soparsehttpvers(struct socket *so, void *arg, int waitflag);
45 /* check for end of HTTP/1.x request */
46 static int soishttpconnected(struct socket *so, void *arg, int waitflag);
47 /* strcmp on an mbuf chain */
48 static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp);
49 /* strncmp on an mbuf chain */
50 static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
51 	int max, char *cmp);
52 /* socketbuffer is full */
53 static int sbfull(struct sockbuf *sb);
54 
55 static struct accept_filter accf_http_filter = {
56 	"httpready",
57 	sohashttpget,
58 	NULL,
59 	NULL
60 };
61 
62 static moduledata_t accf_http_mod = {
63 	"accf_http",
64 	accept_filt_generic_mod_event,
65 	&accf_http_filter
66 };
67 
68 DECLARE_MODULE(accf_http, accf_http_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
69 
70 static int parse_http_version = 1;
71 
72 static SYSCTL_NODE(_net_inet_accf, OID_AUTO, http, CTLFLAG_RW, 0,
73 "HTTP accept filter");
74 SYSCTL_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 
87 static int
88 sbfull(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 (sbused(sb) >= 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  */
102 static int
103 mbufstrcmp(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  */
131 static int
132 mbufstrncmp(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 
161 static int
162 sohashttpget(struct socket *so, void *arg, int waitflag)
163 {
164 
165 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 &&
166 	    !sbfull(&so->so_rcv)) {
167 		struct mbuf *m;
168 		char *cmp;
169 		int	cmplen, cc;
170 
171 		m = so->so_rcv.sb_mb;
172 		cc = sbavail(&so->so_rcv) - 1;
173 		if (cc < 1)
174 			return (SU_OK);
175 		switch (*mtod(m, char *)) {
176 		case 'G':
177 			STRSETUP(cmp, cmplen, "ET ");
178 			break;
179 		case 'H':
180 			STRSETUP(cmp, cmplen, "EAD ");
181 			break;
182 		default:
183 			goto fallout;
184 		}
185 		if (cc < cmplen) {
186 			if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
187 				DPRINT("short cc (%d) but mbufstrncmp ok", cc);
188 				return (SU_OK);
189 			} else {
190 				DPRINT("short cc (%d) mbufstrncmp failed", cc);
191 				goto fallout;
192 			}
193 		}
194 		if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
195 			DPRINT("mbufstrcmp ok");
196 			if (parse_http_version == 0)
197 				return (soishttpconnected(so, arg, waitflag));
198 			else
199 				return (soparsehttpvers(so, arg, waitflag));
200 		}
201 		DPRINT("mbufstrcmp bad");
202 	}
203 
204 fallout:
205 	DPRINT("fallout");
206 	return (SU_ISCONNECTED);
207 }
208 
209 static int
210 soparsehttpvers(struct socket *so, void *arg, int waitflag)
211 {
212 	struct mbuf *m, *n;
213 	int	i, cc, spaces, inspaces;
214 
215 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
216 		goto fallout;
217 
218 	m = so->so_rcv.sb_mb;
219 	cc = sbavail(&so->so_rcv);
220 	inspaces = spaces = 0;
221 	for (m = so->so_rcv.sb_mb; m; m = n) {
222 		n = m->m_nextpkt;
223 		for (; m; m = m->m_next) {
224 			for (i = 0; i < m->m_len; i++, cc--) {
225 				switch (*(mtod(m, char *) + i)) {
226 				case ' ':
227 					/* tabs? '\t' */
228 					if (!inspaces) {
229 						spaces++;
230 						inspaces = 1;
231 					}
232 					break;
233 				case '\r':
234 				case '\n':
235 					DPRINT("newline");
236 					goto fallout;
237 				default:
238 					if (spaces != 2) {
239 						inspaces = 0;
240 						break;
241 					}
242 
243 					/*
244 					 * if we don't have enough characters
245 					 * left (cc < sizeof("HTTP/1.0") - 1)
246 					 * then see if the remaining ones
247 					 * are a request we can parse.
248 					 */
249 					if (cc < sizeof("HTTP/1.0") - 1) {
250 						if (mbufstrncmp(m, n, i, cc,
251 							"HTTP/1.") == 1) {
252 							DPRINT("ok");
253 							goto readmore;
254 						} else {
255 							DPRINT("bad");
256 							goto fallout;
257 						}
258 					} else if (
259 					    mbufstrcmp(m, n, i, "HTTP/1.0") ||
260 					    mbufstrcmp(m, n, i, "HTTP/1.1")) {
261 						DPRINT("ok");
262 						return (soishttpconnected(so,
263 						    arg, waitflag));
264 					} else {
265 						DPRINT("bad");
266 						goto fallout;
267 					}
268 				}
269 			}
270 		}
271 	}
272 readmore:
273 	DPRINT("readmore");
274 	/*
275 	 * if we hit here we haven't hit something
276 	 * we don't understand or a newline, so try again
277 	 */
278 	soupcall_set(so, SO_RCV, soparsehttpvers, arg);
279 	return (SU_OK);
280 
281 fallout:
282 	DPRINT("fallout");
283 	return (SU_ISCONNECTED);
284 }
285 
286 
287 #define NCHRS 3
288 
289 static int
290 soishttpconnected(struct socket *so, void *arg, int waitflag)
291 {
292 	char a, b, c;
293 	struct mbuf *m, *n;
294 	int ccleft, copied;
295 
296 	DPRINT("start");
297 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
298 		goto gotit;
299 
300 	/*
301 	 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
302 	 * copied - how much we've copied so far
303 	 * ccleft - how many bytes remaining in the socketbuffer
304 	 * just loop over the mbufs subtracting from 'ccleft' until we only
305 	 * have NCHRS left
306 	 */
307 	copied = 0;
308 	ccleft = sbavail(&so->so_rcv);
309 	if (ccleft < NCHRS)
310 		goto readmore;
311 	a = b = c = '\0';
312 	for (m = so->so_rcv.sb_mb; m; m = n) {
313 		n = m->m_nextpkt;
314 		for (; m; m = m->m_next) {
315 			ccleft -= m->m_len;
316 			if (ccleft <= NCHRS) {
317 				char *src;
318 				int tocopy;
319 
320 				tocopy = (NCHRS - ccleft) - copied;
321 				src = mtod(m, char *) + (m->m_len - tocopy);
322 
323 				while (tocopy--) {
324 					switch (copied++) {
325 					case 0:
326 						a = *src++;
327 						break;
328 					case 1:
329 						b = *src++;
330 						break;
331 					case 2:
332 						c = *src++;
333 						break;
334 					}
335 				}
336 			}
337 		}
338 	}
339 	if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
340 		/* we have all request headers */
341 		goto gotit;
342 	}
343 
344 readmore:
345 	soupcall_set(so, SO_RCV, soishttpconnected, arg);
346 	return (SU_OK);
347 
348 gotit:
349 	return (SU_ISCONNECTED);
350 }
351