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