xref: /freebsd/sys/netinet/accf_http.c (revision b0d29bc4)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000 Paycounter, Inc.
5  * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #define ACCEPT_FILTER_MOD
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/mbuf.h>
38 #include <sys/module.h>
39 #include <sys/signalvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/socketvar.h>
42 
43 /* check for GET/HEAD */
44 static int sohashttpget(struct socket *so, void *arg, int waitflag);
45 /* check for HTTP/1.0 or HTTP/1.1 */
46 static int soparsehttpvers(struct socket *so, void *arg, int waitflag);
47 /* check for end of HTTP/1.x request */
48 static int soishttpconnected(struct socket *so, void *arg, int waitflag);
49 /* strcmp on an mbuf chain */
50 static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp);
51 /* strncmp on an mbuf chain */
52 static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
53 	int max, char *cmp);
54 /* socketbuffer is full */
55 static int sbfull(struct sockbuf *sb);
56 
57 static struct accept_filter accf_http_filter = {
58 	"httpready",
59 	sohashttpget,
60 	NULL,
61 	NULL
62 };
63 
64 static moduledata_t accf_http_mod = {
65 	"accf_http",
66 	accept_filt_generic_mod_event,
67 	&accf_http_filter
68 };
69 
70 DECLARE_MODULE(accf_http, accf_http_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
71 
72 static int parse_http_version = 1;
73 
74 static SYSCTL_NODE(_net_inet_accf, OID_AUTO, http,
75     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
76     "HTTP accept filter");
77 SYSCTL_INT(_net_inet_accf_http, OID_AUTO, parsehttpversion, CTLFLAG_RW,
78 &parse_http_version, 1,
79 "Parse http version so that non 1.x requests work");
80 
81 #ifdef ACCF_HTTP_DEBUG
82 #define DPRINT(fmt, args...)						\
83 	do {								\
84 		printf("%s:%d: " fmt "\n", __func__, __LINE__, ##args);	\
85 	} while (0)
86 #else
87 #define DPRINT(fmt, args...)
88 #endif
89 
90 static int
91 sbfull(struct sockbuf *sb)
92 {
93 
94 	DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, "
95 	    "mbcnt(%ld) >= mbmax(%ld): %d",
96 	    sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat,
97 	    sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax);
98 	return (sbused(sb) >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
99 }
100 
101 /*
102  * start at mbuf m, (must provide npkt if exists)
103  * starting at offset in m compare characters in mbuf chain for 'cmp'
104  */
105 static int
106 mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
107 {
108 	struct mbuf *n;
109 
110 	for (; m != NULL; m = n) {
111 		n = npkt;
112 		if (npkt)
113 			npkt = npkt->m_nextpkt;
114 		for (; m; m = m->m_next) {
115 			for (; offset < m->m_len; offset++, cmp++) {
116 				if (*cmp == '\0')
117 					return (1);
118 				else if (*cmp != *(mtod(m, char *) + offset))
119 					return (0);
120 			}
121 			if (*cmp == '\0')
122 				return (1);
123 			offset = 0;
124 		}
125 	}
126 	return (0);
127 }
128 
129 /*
130  * start at mbuf m, (must provide npkt if exists)
131  * starting at offset in m compare characters in mbuf chain for 'cmp'
132  * stop at 'max' characters
133  */
134 static int
135 mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
136 {
137 	struct mbuf *n;
138 
139 	for (; m != NULL; m = n) {
140 		n = npkt;
141 		if (npkt)
142 			npkt = npkt->m_nextpkt;
143 		for (; m; m = m->m_next) {
144 			for (; offset < m->m_len; offset++, cmp++, max--) {
145 				if (max == 0 || *cmp == '\0')
146 					return (1);
147 				else if (*cmp != *(mtod(m, char *) + offset))
148 					return (0);
149 			}
150 			if (max == 0 || *cmp == '\0')
151 				return (1);
152 			offset = 0;
153 		}
154 	}
155 	return (0);
156 }
157 
158 #define STRSETUP(sptr, slen, str)					\
159 	do {								\
160 		sptr = str;						\
161 		slen = sizeof(str) - 1;					\
162 	} while(0)
163 
164 static int
165 sohashttpget(struct socket *so, void *arg, int waitflag)
166 {
167 
168 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 &&
169 	    !sbfull(&so->so_rcv)) {
170 		struct mbuf *m;
171 		char *cmp;
172 		int	cmplen, cc;
173 
174 		m = so->so_rcv.sb_mb;
175 		cc = sbavail(&so->so_rcv) - 1;
176 		if (cc < 1)
177 			return (SU_OK);
178 		switch (*mtod(m, char *)) {
179 		case 'G':
180 			STRSETUP(cmp, cmplen, "ET ");
181 			break;
182 		case 'H':
183 			STRSETUP(cmp, cmplen, "EAD ");
184 			break;
185 		default:
186 			goto fallout;
187 		}
188 		if (cc < cmplen) {
189 			if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
190 				DPRINT("short cc (%d) but mbufstrncmp ok", cc);
191 				return (SU_OK);
192 			} else {
193 				DPRINT("short cc (%d) mbufstrncmp failed", cc);
194 				goto fallout;
195 			}
196 		}
197 		if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
198 			DPRINT("mbufstrcmp ok");
199 			if (parse_http_version == 0)
200 				return (soishttpconnected(so, arg, waitflag));
201 			else
202 				return (soparsehttpvers(so, arg, waitflag));
203 		}
204 		DPRINT("mbufstrcmp bad");
205 	}
206 
207 fallout:
208 	DPRINT("fallout");
209 	return (SU_ISCONNECTED);
210 }
211 
212 static int
213 soparsehttpvers(struct socket *so, void *arg, int waitflag)
214 {
215 	struct mbuf *m, *n;
216 	int	i, cc, spaces, inspaces;
217 
218 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
219 		goto fallout;
220 
221 	m = so->so_rcv.sb_mb;
222 	cc = sbavail(&so->so_rcv);
223 	inspaces = spaces = 0;
224 	for (m = so->so_rcv.sb_mb; m; m = n) {
225 		n = m->m_nextpkt;
226 		for (; m; m = m->m_next) {
227 			for (i = 0; i < m->m_len; i++, cc--) {
228 				switch (*(mtod(m, char *) + i)) {
229 				case ' ':
230 					/* tabs? '\t' */
231 					if (!inspaces) {
232 						spaces++;
233 						inspaces = 1;
234 					}
235 					break;
236 				case '\r':
237 				case '\n':
238 					DPRINT("newline");
239 					goto fallout;
240 				default:
241 					if (spaces != 2) {
242 						inspaces = 0;
243 						break;
244 					}
245 
246 					/*
247 					 * if we don't have enough characters
248 					 * left (cc < sizeof("HTTP/1.0") - 1)
249 					 * then see if the remaining ones
250 					 * are a request we can parse.
251 					 */
252 					if (cc < sizeof("HTTP/1.0") - 1) {
253 						if (mbufstrncmp(m, n, i, cc,
254 							"HTTP/1.") == 1) {
255 							DPRINT("ok");
256 							goto readmore;
257 						} else {
258 							DPRINT("bad");
259 							goto fallout;
260 						}
261 					} else if (
262 					    mbufstrcmp(m, n, i, "HTTP/1.0") ||
263 					    mbufstrcmp(m, n, i, "HTTP/1.1")) {
264 						DPRINT("ok");
265 						return (soishttpconnected(so,
266 						    arg, waitflag));
267 					} else {
268 						DPRINT("bad");
269 						goto fallout;
270 					}
271 				}
272 			}
273 		}
274 	}
275 readmore:
276 	DPRINT("readmore");
277 	/*
278 	 * if we hit here we haven't hit something
279 	 * we don't understand or a newline, so try again
280 	 */
281 	soupcall_set(so, SO_RCV, soparsehttpvers, arg);
282 	return (SU_OK);
283 
284 fallout:
285 	DPRINT("fallout");
286 	return (SU_ISCONNECTED);
287 }
288 
289 
290 #define NCHRS 3
291 
292 static int
293 soishttpconnected(struct socket *so, void *arg, int waitflag)
294 {
295 	char a, b, c;
296 	struct mbuf *m, *n;
297 	int ccleft, copied;
298 
299 	DPRINT("start");
300 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
301 		goto gotit;
302 
303 	/*
304 	 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
305 	 * copied - how much we've copied so far
306 	 * ccleft - how many bytes remaining in the socketbuffer
307 	 * just loop over the mbufs subtracting from 'ccleft' until we only
308 	 * have NCHRS left
309 	 */
310 	copied = 0;
311 	ccleft = sbavail(&so->so_rcv);
312 	if (ccleft < NCHRS)
313 		goto readmore;
314 	a = b = c = '\0';
315 	for (m = so->so_rcv.sb_mb; m; m = n) {
316 		n = m->m_nextpkt;
317 		for (; m; m = m->m_next) {
318 			ccleft -= m->m_len;
319 			if (ccleft <= NCHRS) {
320 				char *src;
321 				int tocopy;
322 
323 				tocopy = (NCHRS - ccleft) - copied;
324 				src = mtod(m, char *) + (m->m_len - tocopy);
325 
326 				while (tocopy--) {
327 					switch (copied++) {
328 					case 0:
329 						a = *src++;
330 						break;
331 					case 1:
332 						b = *src++;
333 						break;
334 					case 2:
335 						c = *src++;
336 						break;
337 					}
338 				}
339 			}
340 		}
341 	}
342 	if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
343 		/* we have all request headers */
344 		goto gotit;
345 	}
346 
347 readmore:
348 	soupcall_set(so, SO_RCV, soishttpconnected, arg);
349 	return (SU_OK);
350 
351 gotit:
352 	return (SU_ISCONNECTED);
353 }
354