accf_http.c revision 1.2 1 1.2 ad /* $NetBSD: accf_http.c,v 1.2 2008/10/14 13:05:44 ad 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.2 ad __KERNEL_RCSID(0, "$NetBSD: accf_http.c,v 1.2 2008/10/14 13:05:44 ad 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.1 tls #include <sys/lkm.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.1 tls /* check for GET/HEAD */
47 1.1 tls static void sohashttpget(struct socket *so, void *arg, int waitflag);
48 1.1 tls /* check for HTTP/1.0 or HTTP/1.1 */
49 1.1 tls static void soparsehttpvers(struct socket *so, void *arg, int waitflag);
50 1.1 tls /* check for end of HTTP/1.x request */
51 1.1 tls static void soishttpconnected(struct socket *so, void *arg, int waitflag);
52 1.1 tls /* strcmp on an mbuf chain */
53 1.1 tls static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, const char *cmp);
54 1.1 tls /* strncmp on an mbuf chain */
55 1.1 tls static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
56 1.1 tls int len, const char *cmp);
57 1.1 tls /* socketbuffer is full */
58 1.1 tls static int sbfull(struct sockbuf *sb);
59 1.1 tls
60 1.1 tls static struct accept_filter accf_http_filter = {
61 1.2 ad .accf_name = "httpready",
62 1.2 ad .accf_callback = sohashttpget,
63 1.1 tls };
64 1.1 tls
65 1.1 tls /*
66 1.1 tls * Names of HTTP Accept filter sysctl objects
67 1.1 tls */
68 1.1 tls
69 1.1 tls #define ACCFCTL_PARSEVER 1 /* Parse HTTP version */
70 1.1 tls
71 1.1 tls static int parse_http_version = 1;
72 1.1 tls
73 1.1 tls SYSCTL_SETUP(sysctl_net_inet_accf__http_setup, "sysctl net.inet.accf.http subtree setup")
74 1.1 tls {
75 1.1 tls sysctl_createv(clog, 0, NULL, NULL,
76 1.1 tls CTLFLAG_PERMANENT,
77 1.1 tls CTLTYPE_NODE, "net", NULL,
78 1.1 tls NULL, 0, NULL, 0,
79 1.1 tls CTL_NET, CTL_EOL);
80 1.1 tls sysctl_createv(clog, 0, NULL, NULL,
81 1.1 tls CTLFLAG_PERMANENT,
82 1.1 tls CTLTYPE_NODE, "inet", NULL,
83 1.1 tls NULL, 0, NULL, 0,
84 1.1 tls CTL_NET, PF_INET, CTL_EOL);
85 1.1 tls sysctl_createv(clog, 0, NULL, NULL,
86 1.1 tls CTLFLAG_PERMANENT,
87 1.1 tls CTLTYPE_NODE, "accf", NULL,
88 1.1 tls NULL, 0, NULL, 0,
89 1.1 tls CTL_NET, PF_INET, SO_ACCEPTFILTER, CTL_EOL);
90 1.1 tls sysctl_createv(clog, 0, NULL, NULL,
91 1.1 tls CTLFLAG_PERMANENT,
92 1.1 tls CTLTYPE_NODE, "http",
93 1.1 tls SYSCTL_DESCR("HTTP accept filter"),
94 1.1 tls NULL, 0, NULL, 0,
95 1.1 tls CTL_NET, PF_INET, SO_ACCEPTFILTER, ACCF_HTTP, CTL_EOL);
96 1.1 tls sysctl_createv(clog, 0, NULL, NULL,
97 1.1 tls CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
98 1.1 tls CTLTYPE_INT, "parsehttpversion",
99 1.1 tls SYSCTL_DESCR("Parse http version so that non "
100 1.1 tls "1.x requests work"),
101 1.1 tls NULL, 0, &parse_http_version, 0,
102 1.1 tls CTL_NET, PF_INET, SO_ACCEPTFILTER, ACCF_HTTP,
103 1.1 tls ACCFCTL_PARSEVER, CTL_EOL);
104 1.1 tls }
105 1.1 tls
106 1.1 tls void accf_httpattach(int);
107 1.1 tls void accf_httpattach(int num)
108 1.1 tls {
109 1.1 tls accept_filt_generic_mod_event(NULL, LKM_E_LOAD, &accf_http_filter);
110 1.1 tls }
111 1.1 tls
112 1.1 tls /*
113 1.1 tls * This code is to make HTTP ready accept filer as LKM.
114 1.1 tls * To compile as LKM we need to move this set of code into
115 1.1 tls * another file and include this file for compilation when
116 1.1 tls * making LKM
117 1.1 tls */
118 1.1 tls
119 1.1 tls #ifdef _LKM
120 1.1 tls static int accf_http_handle(struct lkm_table * lkmtp, int cmd);
121 1.1 tls int accf_http_lkmentry(struct lkm_table * lkmtp, int cmd, int ver);
122 1.1 tls
123 1.1 tls MOD_MISC("accf_http");
124 1.1 tls
125 1.1 tls static int accf_http_handle(struct lkm_table * lkmtp, int cmd)
126 1.1 tls {
127 1.1 tls
128 1.1 tls return accept_filt_generic_mod_event(lkmtp, cmd, &accf_http_filter);
129 1.1 tls }
130 1.1 tls
131 1.1 tls /*
132 1.1 tls * the module entry point.
133 1.1 tls */
134 1.1 tls int
135 1.1 tls accf_http_lkmentry(struct lkm_table *lkmtp, int cmd, int ver)
136 1.1 tls {
137 1.1 tls DISPATCH(lkmtp, cmd, ver, accf_http_handle, accf_http_handle,
138 1.1 tls accf_http_handle)
139 1.1 tls }
140 1.1 tls #endif
141 1.1 tls
142 1.1 tls #ifdef ACCF_HTTP_DEBUG
143 1.1 tls #define DPRINT(fmt, args...) \
144 1.1 tls do { \
145 1.1 tls printf("%s:%d: " fmt "\n", __func__, __LINE__, ##args); \
146 1.1 tls } while (0)
147 1.1 tls #else
148 1.1 tls #define DPRINT(fmt, args...)
149 1.1 tls #endif
150 1.1 tls
151 1.1 tls static int
152 1.1 tls sbfull(struct sockbuf *sb)
153 1.1 tls {
154 1.1 tls
155 1.1 tls DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, "
156 1.1 tls "mbcnt(%ld) >= mbmax(%ld): %d",
157 1.1 tls sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat,
158 1.1 tls sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax);
159 1.1 tls return (sb->sb_cc >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
160 1.1 tls }
161 1.1 tls
162 1.1 tls /*
163 1.1 tls * start at mbuf m, (must provide npkt if exists)
164 1.1 tls * starting at offset in m compare characters in mbuf chain for 'cmp'
165 1.1 tls */
166 1.1 tls static int
167 1.1 tls mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, const char *cmp)
168 1.1 tls {
169 1.1 tls struct mbuf *n;
170 1.1 tls
171 1.1 tls for (; m != NULL; m = n) {
172 1.1 tls n = npkt;
173 1.1 tls if (npkt)
174 1.1 tls npkt = npkt->m_nextpkt;
175 1.1 tls for (; m; m = m->m_next) {
176 1.1 tls for (; offset < m->m_len; offset++, cmp++) {
177 1.1 tls if (*cmp == '\0')
178 1.1 tls return (1);
179 1.1 tls else if (*cmp != *(mtod(m, char *) + offset))
180 1.1 tls return (0);
181 1.1 tls }
182 1.1 tls if (*cmp == '\0')
183 1.1 tls return (1);
184 1.1 tls offset = 0;
185 1.1 tls }
186 1.1 tls }
187 1.1 tls return (0);
188 1.1 tls }
189 1.1 tls
190 1.1 tls /*
191 1.1 tls * start at mbuf m, (must provide npkt if exists)
192 1.1 tls * starting at offset in m compare characters in mbuf chain for 'cmp'
193 1.1 tls * stop at 'max' characters
194 1.1 tls */
195 1.1 tls static int
196 1.1 tls mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int len, const char *cmp)
197 1.1 tls {
198 1.1 tls struct mbuf *n;
199 1.1 tls
200 1.1 tls for (; m != NULL; m = n) {
201 1.1 tls n = npkt;
202 1.1 tls if (npkt)
203 1.1 tls npkt = npkt->m_nextpkt;
204 1.1 tls for (; m; m = m->m_next) {
205 1.1 tls for (; offset < m->m_len; offset++, cmp++, len--) {
206 1.1 tls if (len == 0 || *cmp == '\0')
207 1.1 tls return (1);
208 1.1 tls else if (*cmp != *(mtod(m, char *) + offset))
209 1.1 tls return (0);
210 1.1 tls }
211 1.1 tls if (len == 0 || *cmp == '\0')
212 1.1 tls return (1);
213 1.1 tls offset = 0;
214 1.1 tls }
215 1.1 tls }
216 1.1 tls return (0);
217 1.1 tls }
218 1.1 tls
219 1.1 tls #define STRSETUP(sptr, slen, str) \
220 1.1 tls do { \
221 1.1 tls sptr = str; \
222 1.1 tls slen = sizeof(str) - 1; \
223 1.1 tls } while(0)
224 1.1 tls
225 1.1 tls static void
226 1.1 tls sohashttpget(struct socket *so, void *arg, int waitflag)
227 1.1 tls {
228 1.1 tls
229 1.1 tls if ((so->so_state & SS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) {
230 1.1 tls struct mbuf *m;
231 1.1 tls const char *cmp;
232 1.1 tls int cmplen, cc;
233 1.1 tls
234 1.1 tls m = so->so_rcv.sb_mb;
235 1.1 tls cc = so->so_rcv.sb_cc - 1;
236 1.1 tls if (cc < 1)
237 1.1 tls return;
238 1.1 tls switch (*mtod(m, char *)) {
239 1.1 tls case 'G':
240 1.1 tls STRSETUP(cmp, cmplen, "ET ");
241 1.1 tls break;
242 1.1 tls case 'H':
243 1.1 tls STRSETUP(cmp, cmplen, "EAD ");
244 1.1 tls break;
245 1.1 tls default:
246 1.1 tls goto fallout;
247 1.1 tls }
248 1.1 tls if (cc < cmplen) {
249 1.1 tls if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
250 1.1 tls DPRINT("short cc (%d) but mbufstrncmp ok", cc);
251 1.1 tls return;
252 1.1 tls } else {
253 1.1 tls DPRINT("short cc (%d) mbufstrncmp failed", cc);
254 1.1 tls goto fallout;
255 1.1 tls }
256 1.1 tls }
257 1.1 tls if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
258 1.1 tls DPRINT("mbufstrcmp ok");
259 1.1 tls if (parse_http_version == 0)
260 1.1 tls soishttpconnected(so, arg, waitflag);
261 1.1 tls else
262 1.1 tls soparsehttpvers(so, arg, waitflag);
263 1.1 tls return;
264 1.1 tls }
265 1.1 tls DPRINT("mbufstrcmp bad");
266 1.1 tls }
267 1.1 tls
268 1.1 tls fallout:
269 1.1 tls DPRINT("fallout");
270 1.1 tls so->so_upcall = NULL;
271 1.1 tls so->so_rcv.sb_flags &= ~SB_UPCALL;
272 1.1 tls soisconnected(so);
273 1.1 tls return;
274 1.1 tls }
275 1.1 tls
276 1.1 tls static void
277 1.1 tls soparsehttpvers(struct socket *so, void *arg, int waitflag)
278 1.1 tls {
279 1.1 tls struct mbuf *m, *n;
280 1.1 tls int i, cc, spaces, inspaces;
281 1.1 tls
282 1.1 tls if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
283 1.1 tls goto fallout;
284 1.1 tls
285 1.1 tls m = so->so_rcv.sb_mb;
286 1.1 tls cc = so->so_rcv.sb_cc;
287 1.1 tls inspaces = spaces = 0;
288 1.1 tls for (m = so->so_rcv.sb_mb; m; m = n) {
289 1.1 tls n = m->m_nextpkt;
290 1.1 tls for (; m; m = m->m_next) {
291 1.1 tls for (i = 0; i < m->m_len; i++, cc--) {
292 1.1 tls switch (*(mtod(m, char *) + i)) {
293 1.1 tls case ' ':
294 1.1 tls /* tabs? '\t' */
295 1.1 tls if (!inspaces) {
296 1.1 tls spaces++;
297 1.1 tls inspaces = 1;
298 1.1 tls }
299 1.1 tls break;
300 1.1 tls case '\r':
301 1.1 tls case '\n':
302 1.1 tls DPRINT("newline");
303 1.1 tls goto fallout;
304 1.1 tls default:
305 1.1 tls if (spaces != 2) {
306 1.1 tls inspaces = 0;
307 1.1 tls break;
308 1.1 tls }
309 1.1 tls
310 1.1 tls /*
311 1.1 tls * if we don't have enough characters
312 1.1 tls * left (cc < sizeof("HTTP/1.0") - 1)
313 1.1 tls * then see if the remaining ones
314 1.1 tls * are a request we can parse.
315 1.1 tls */
316 1.1 tls if (cc < sizeof("HTTP/1.0") - 1) {
317 1.1 tls if (mbufstrncmp(m, n, i, cc,
318 1.1 tls "HTTP/1.") == 1) {
319 1.1 tls DPRINT("ok");
320 1.1 tls goto readmore;
321 1.1 tls } else {
322 1.1 tls DPRINT("bad");
323 1.1 tls goto fallout;
324 1.1 tls }
325 1.1 tls } else if (
326 1.1 tls mbufstrcmp(m, n, i, "HTTP/1.0") ||
327 1.1 tls mbufstrcmp(m, n, i, "HTTP/1.1")) {
328 1.1 tls DPRINT("ok");
329 1.1 tls soishttpconnected(so,
330 1.1 tls arg, waitflag);
331 1.1 tls return;
332 1.1 tls } else {
333 1.1 tls DPRINT("bad");
334 1.1 tls goto fallout;
335 1.1 tls }
336 1.1 tls }
337 1.1 tls }
338 1.1 tls }
339 1.1 tls }
340 1.1 tls readmore:
341 1.1 tls DPRINT("readmore");
342 1.1 tls /*
343 1.1 tls * if we hit here we haven't hit something
344 1.1 tls * we don't understand or a newline, so try again
345 1.1 tls */
346 1.1 tls so->so_upcall = soparsehttpvers;
347 1.1 tls so->so_rcv.sb_flags |= SB_UPCALL;
348 1.1 tls return;
349 1.1 tls
350 1.1 tls fallout:
351 1.1 tls DPRINT("fallout");
352 1.1 tls so->so_upcall = NULL;
353 1.1 tls so->so_rcv.sb_flags &= ~SB_UPCALL;
354 1.1 tls soisconnected(so);
355 1.1 tls return;
356 1.1 tls }
357 1.1 tls
358 1.1 tls
359 1.1 tls #define NCHRS 3
360 1.1 tls
361 1.1 tls static void
362 1.1 tls soishttpconnected(struct socket *so, void *arg, int waitflag)
363 1.1 tls {
364 1.1 tls char a, b, c;
365 1.1 tls struct mbuf *m, *n;
366 1.1 tls int ccleft, copied;
367 1.1 tls
368 1.1 tls DPRINT("start");
369 1.1 tls if ((so->so_state & SS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
370 1.1 tls goto gotit;
371 1.1 tls
372 1.1 tls /*
373 1.1 tls * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
374 1.1 tls * copied - how much we've copied so far
375 1.1 tls * ccleft - how many bytes remaining in the socketbuffer
376 1.1 tls * just loop over the mbufs subtracting from 'ccleft' until we only
377 1.1 tls * have NCHRS left
378 1.1 tls */
379 1.1 tls copied = 0;
380 1.1 tls ccleft = so->so_rcv.sb_cc;
381 1.1 tls if (ccleft < NCHRS)
382 1.1 tls goto readmore;
383 1.1 tls a = b = c = '\0';
384 1.1 tls for (m = so->so_rcv.sb_mb; m; m = n) {
385 1.1 tls n = m->m_nextpkt;
386 1.1 tls for (; m; m = m->m_next) {
387 1.1 tls ccleft -= m->m_len;
388 1.1 tls if (ccleft <= NCHRS) {
389 1.1 tls char *src;
390 1.1 tls int tocopy;
391 1.1 tls
392 1.1 tls tocopy = (NCHRS - ccleft) - copied;
393 1.1 tls src = mtod(m, char *) + (m->m_len - tocopy);
394 1.1 tls
395 1.1 tls while (tocopy--) {
396 1.1 tls switch (copied++) {
397 1.1 tls case 0:
398 1.1 tls a = *src++;
399 1.1 tls break;
400 1.1 tls case 1:
401 1.1 tls b = *src++;
402 1.1 tls break;
403 1.1 tls case 2:
404 1.1 tls c = *src++;
405 1.1 tls break;
406 1.1 tls }
407 1.1 tls }
408 1.1 tls }
409 1.1 tls }
410 1.1 tls }
411 1.1 tls if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
412 1.1 tls /* we have all request headers */
413 1.1 tls goto gotit;
414 1.1 tls }
415 1.1 tls
416 1.1 tls readmore:
417 1.1 tls so->so_upcall = soishttpconnected;
418 1.1 tls so->so_rcv.sb_flags |= SB_UPCALL;
419 1.1 tls return;
420 1.1 tls
421 1.1 tls gotit:
422 1.1 tls so->so_upcall = NULL;
423 1.1 tls so->so_rcv.sb_flags &= ~SB_UPCALL;
424 1.1 tls soisconnected(so);
425 1.1 tls return;
426 1.1 tls }
427