xref: /freebsd/sys/netinet/accf_http.c (revision 56e53cb8ef000c3ef72337a4095987a932cdedef)
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, CTLFLAG_RW, 0,
75 "HTTP accept filter");
76 SYSCTL_INT(_net_inet_accf_http, OID_AUTO, parsehttpversion, CTLFLAG_RW,
77 &parse_http_version, 1,
78 "Parse http version so that non 1.x requests work");
79 
80 #ifdef ACCF_HTTP_DEBUG
81 #define DPRINT(fmt, args...)						\
82 	do {								\
83 		printf("%s:%d: " fmt "\n", __func__, __LINE__, ##args);	\
84 	} while (0)
85 #else
86 #define DPRINT(fmt, args...)
87 #endif
88 
89 static int
90 sbfull(struct sockbuf *sb)
91 {
92 
93 	DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, "
94 	    "mbcnt(%ld) >= mbmax(%ld): %d",
95 	    sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat,
96 	    sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax);
97 	return (sbused(sb) >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
98 }
99 
100 /*
101  * start at mbuf m, (must provide npkt if exists)
102  * starting at offset in m compare characters in mbuf chain for 'cmp'
103  */
104 static int
105 mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
106 {
107 	struct mbuf *n;
108 
109 	for (; m != NULL; m = n) {
110 		n = npkt;
111 		if (npkt)
112 			npkt = npkt->m_nextpkt;
113 		for (; m; m = m->m_next) {
114 			for (; offset < m->m_len; offset++, cmp++) {
115 				if (*cmp == '\0')
116 					return (1);
117 				else if (*cmp != *(mtod(m, char *) + offset))
118 					return (0);
119 			}
120 			if (*cmp == '\0')
121 				return (1);
122 			offset = 0;
123 		}
124 	}
125 	return (0);
126 }
127 
128 /*
129  * start at mbuf m, (must provide npkt if exists)
130  * starting at offset in m compare characters in mbuf chain for 'cmp'
131  * stop at 'max' characters
132  */
133 static int
134 mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
135 {
136 	struct mbuf *n;
137 
138 	for (; m != NULL; m = n) {
139 		n = npkt;
140 		if (npkt)
141 			npkt = npkt->m_nextpkt;
142 		for (; m; m = m->m_next) {
143 			for (; offset < m->m_len; offset++, cmp++, max--) {
144 				if (max == 0 || *cmp == '\0')
145 					return (1);
146 				else if (*cmp != *(mtod(m, char *) + offset))
147 					return (0);
148 			}
149 			if (max == 0 || *cmp == '\0')
150 				return (1);
151 			offset = 0;
152 		}
153 	}
154 	return (0);
155 }
156 
157 #define STRSETUP(sptr, slen, str)					\
158 	do {								\
159 		sptr = str;						\
160 		slen = sizeof(str) - 1;					\
161 	} while(0)
162 
163 static int
164 sohashttpget(struct socket *so, void *arg, int waitflag)
165 {
166 
167 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 &&
168 	    !sbfull(&so->so_rcv)) {
169 		struct mbuf *m;
170 		char *cmp;
171 		int	cmplen, cc;
172 
173 		m = so->so_rcv.sb_mb;
174 		cc = sbavail(&so->so_rcv) - 1;
175 		if (cc < 1)
176 			return (SU_OK);
177 		switch (*mtod(m, char *)) {
178 		case 'G':
179 			STRSETUP(cmp, cmplen, "ET ");
180 			break;
181 		case 'H':
182 			STRSETUP(cmp, cmplen, "EAD ");
183 			break;
184 		default:
185 			goto fallout;
186 		}
187 		if (cc < cmplen) {
188 			if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
189 				DPRINT("short cc (%d) but mbufstrncmp ok", cc);
190 				return (SU_OK);
191 			} else {
192 				DPRINT("short cc (%d) mbufstrncmp failed", cc);
193 				goto fallout;
194 			}
195 		}
196 		if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
197 			DPRINT("mbufstrcmp ok");
198 			if (parse_http_version == 0)
199 				return (soishttpconnected(so, arg, waitflag));
200 			else
201 				return (soparsehttpvers(so, arg, waitflag));
202 		}
203 		DPRINT("mbufstrcmp bad");
204 	}
205 
206 fallout:
207 	DPRINT("fallout");
208 	return (SU_ISCONNECTED);
209 }
210 
211 static int
212 soparsehttpvers(struct socket *so, void *arg, int waitflag)
213 {
214 	struct mbuf *m, *n;
215 	int	i, cc, spaces, inspaces;
216 
217 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
218 		goto fallout;
219 
220 	m = so->so_rcv.sb_mb;
221 	cc = sbavail(&so->so_rcv);
222 	inspaces = spaces = 0;
223 	for (m = so->so_rcv.sb_mb; m; m = n) {
224 		n = m->m_nextpkt;
225 		for (; m; m = m->m_next) {
226 			for (i = 0; i < m->m_len; i++, cc--) {
227 				switch (*(mtod(m, char *) + i)) {
228 				case ' ':
229 					/* tabs? '\t' */
230 					if (!inspaces) {
231 						spaces++;
232 						inspaces = 1;
233 					}
234 					break;
235 				case '\r':
236 				case '\n':
237 					DPRINT("newline");
238 					goto fallout;
239 				default:
240 					if (spaces != 2) {
241 						inspaces = 0;
242 						break;
243 					}
244 
245 					/*
246 					 * if we don't have enough characters
247 					 * left (cc < sizeof("HTTP/1.0") - 1)
248 					 * then see if the remaining ones
249 					 * are a request we can parse.
250 					 */
251 					if (cc < sizeof("HTTP/1.0") - 1) {
252 						if (mbufstrncmp(m, n, i, cc,
253 							"HTTP/1.") == 1) {
254 							DPRINT("ok");
255 							goto readmore;
256 						} else {
257 							DPRINT("bad");
258 							goto fallout;
259 						}
260 					} else if (
261 					    mbufstrcmp(m, n, i, "HTTP/1.0") ||
262 					    mbufstrcmp(m, n, i, "HTTP/1.1")) {
263 						DPRINT("ok");
264 						return (soishttpconnected(so,
265 						    arg, waitflag));
266 					} else {
267 						DPRINT("bad");
268 						goto fallout;
269 					}
270 				}
271 			}
272 		}
273 	}
274 readmore:
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 	soupcall_set(so, SO_RCV, soparsehttpvers, arg);
281 	return (SU_OK);
282 
283 fallout:
284 	DPRINT("fallout");
285 	return (SU_ISCONNECTED);
286 }
287 
288 
289 #define NCHRS 3
290 
291 static int
292 soishttpconnected(struct socket *so, void *arg, int waitflag)
293 {
294 	char a, b, c;
295 	struct mbuf *m, *n;
296 	int ccleft, copied;
297 
298 	DPRINT("start");
299 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
300 		goto gotit;
301 
302 	/*
303 	 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
304 	 * copied - how much we've copied so far
305 	 * ccleft - how many bytes remaining in the socketbuffer
306 	 * just loop over the mbufs subtracting from 'ccleft' until we only
307 	 * have NCHRS left
308 	 */
309 	copied = 0;
310 	ccleft = sbavail(&so->so_rcv);
311 	if (ccleft < NCHRS)
312 		goto readmore;
313 	a = b = c = '\0';
314 	for (m = so->so_rcv.sb_mb; m; m = n) {
315 		n = m->m_nextpkt;
316 		for (; m; m = m->m_next) {
317 			ccleft -= m->m_len;
318 			if (ccleft <= NCHRS) {
319 				char *src;
320 				int tocopy;
321 
322 				tocopy = (NCHRS - ccleft) - copied;
323 				src = mtod(m, char *) + (m->m_len - tocopy);
324 
325 				while (tocopy--) {
326 					switch (copied++) {
327 					case 0:
328 						a = *src++;
329 						break;
330 					case 1:
331 						b = *src++;
332 						break;
333 					case 2:
334 						c = *src++;
335 						break;
336 					}
337 				}
338 			}
339 		}
340 	}
341 	if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
342 		/* we have all request headers */
343 		goto gotit;
344 	}
345 
346 readmore:
347 	soupcall_set(so, SO_RCV, soishttpconnected, arg);
348 	return (SU_OK);
349 
350 gotit:
351 	return (SU_ISCONNECTED);
352 }
353