1 /* $NetBSD: accf_http.c,v 1.6 2008/11/21 16:08:57 joerg Exp $ */
4 * Copyright (c) 2000 Paycounter, Inc.
5 * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: accf_http.c,v 1.6 2008/11/21 16:08:57 joerg Exp $");
33 #define ACCEPT_FILTER_MOD
35 #include <sys/param.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/signalvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
44 #include <netinet/accept_filter.h>
46 MODULE(MODULE_CLASS_MISC
, accf_httpready
, NULL
);
48 /* check for GET/HEAD */
49 static void sohashttpget(struct socket
*so
, void *arg
, int events
, int waitflag
);
50 /* check for HTTP/1.0 or HTTP/1.1 */
51 static void soparsehttpvers(struct socket
*so
, void *arg
, int events
, int waitflag
);
52 /* check for end of HTTP/1.x request */
53 static void soishttpconnected(struct socket
*so
, void *arg
, int events
, int waitflag
);
54 /* strcmp on an mbuf chain */
55 static int mbufstrcmp(struct mbuf
*m
, struct mbuf
*npkt
, int offset
, const char *cmp
);
56 /* strncmp on an mbuf chain */
57 static int mbufstrncmp(struct mbuf
*m
, struct mbuf
*npkt
, int offset
,
58 int len
, const char *cmp
);
59 /* socketbuffer is full */
60 static int sbfull(struct sockbuf
*sb
);
62 static struct accept_filter accf_http_filter
= {
63 .accf_name
= "httpready",
64 .accf_callback
= sohashttpget
,
68 * Names of HTTP Accept filter sysctl objects
71 #define ACCFCTL_PARSEVER 1 /* Parse HTTP version */
73 static int parse_http_version
= 1;
75 /* XXX pseudo-device */
76 void accf_httpattach(int);
79 accf_httpattach(int junk
)
85 accf_httpready_modcmd(modcmd_t cmd
, void *arg
)
87 static struct sysctllog
*clog
;
92 error
= accept_filt_add(&accf_http_filter
);
96 sysctl_createv(&clog
, 0, NULL
, NULL
,
98 CTLTYPE_NODE
, "net", NULL
,
101 sysctl_createv(&clog
, 0, NULL
, NULL
,
103 CTLTYPE_NODE
, "inet", NULL
,
105 CTL_NET
, PF_INET
, CTL_EOL
);
106 sysctl_createv(&clog
, 0, NULL
, NULL
,
108 CTLTYPE_NODE
, "accf", NULL
,
110 CTL_NET
, PF_INET
, SO_ACCEPTFILTER
, CTL_EOL
);
111 sysctl_createv(&clog
, 0, NULL
, NULL
,
113 CTLTYPE_NODE
, "http",
114 SYSCTL_DESCR("HTTP accept filter"),
116 CTL_NET
, PF_INET
, SO_ACCEPTFILTER
, ACCF_HTTP
, CTL_EOL
);
117 sysctl_createv(&clog
, 0, NULL
, NULL
,
118 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
119 CTLTYPE_INT
, "parsehttpversion",
120 SYSCTL_DESCR("Parse http version so that non "
121 "1.x requests work"),
122 NULL
, 0, &parse_http_version
, 0,
123 CTL_NET
, PF_INET
, SO_ACCEPTFILTER
, ACCF_HTTP
,
124 ACCFCTL_PARSEVER
, CTL_EOL
);
127 case MODULE_CMD_FINI
:
128 error
= accept_filt_del(&accf_http_filter
);
132 sysctl_teardown(&clog
);
140 #ifdef ACCF_HTTP_DEBUG
141 #define DPRINT(fmt, args...) \
143 printf("%s:%d: " fmt "\n", __func__, __LINE__, ##args); \
146 #define DPRINT(fmt, args...)
150 sbfull(struct sockbuf
*sb
)
153 DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, "
154 "mbcnt(%ld) >= mbmax(%ld): %d",
155 sb
->sb_cc
, sb
->sb_hiwat
, sb
->sb_cc
>= sb
->sb_hiwat
,
156 sb
->sb_mbcnt
, sb
->sb_mbmax
, sb
->sb_mbcnt
>= sb
->sb_mbmax
);
157 return (sb
->sb_cc
>= sb
->sb_hiwat
|| sb
->sb_mbcnt
>= sb
->sb_mbmax
);
161 * start at mbuf m, (must provide npkt if exists)
162 * starting at offset in m compare characters in mbuf chain for 'cmp'
165 mbufstrcmp(struct mbuf
*m
, struct mbuf
*npkt
, int offset
, const char *cmp
)
169 for (; m
!= NULL
; m
= n
) {
172 npkt
= npkt
->m_nextpkt
;
173 for (; m
; m
= m
->m_next
) {
174 for (; offset
< m
->m_len
; offset
++, cmp
++) {
177 else if (*cmp
!= *(mtod(m
, char *) + offset
))
189 * start at mbuf m, (must provide npkt if exists)
190 * starting at offset in m compare characters in mbuf chain for 'cmp'
191 * stop at 'max' characters
194 mbufstrncmp(struct mbuf
*m
, struct mbuf
*npkt
, int offset
, int len
, const char *cmp
)
198 for (; m
!= NULL
; m
= n
) {
201 npkt
= npkt
->m_nextpkt
;
202 for (; m
; m
= m
->m_next
) {
203 for (; offset
< m
->m_len
; offset
++, cmp
++, len
--) {
204 if (len
== 0 || *cmp
== '\0')
206 else if (*cmp
!= *(mtod(m
, char *) + offset
))
209 if (len
== 0 || *cmp
== '\0')
217 #define STRSETUP(sptr, slen, str) \
220 slen = sizeof(str) - 1; \
224 sohashttpget(struct socket
*so
, void *arg
, int events
, int waitflag
)
227 if ((so
->so_state
& SS_CANTRCVMORE
) == 0 && !sbfull(&so
->so_rcv
)) {
232 m
= so
->so_rcv
.sb_mb
;
233 cc
= so
->so_rcv
.sb_cc
- 1;
236 switch (*mtod(m
, char *)) {
238 STRSETUP(cmp
, cmplen
, "ET ");
241 STRSETUP(cmp
, cmplen
, "EAD ");
247 if (mbufstrncmp(m
, m
->m_nextpkt
, 1, cc
, cmp
) == 1) {
248 DPRINT("short cc (%d) but mbufstrncmp ok", cc
);
251 DPRINT("short cc (%d) mbufstrncmp failed", cc
);
255 if (mbufstrcmp(m
, m
->m_nextpkt
, 1, cmp
) == 1) {
256 DPRINT("mbufstrcmp ok");
257 if (parse_http_version
== 0)
258 soishttpconnected(so
, arg
, events
, waitflag
);
260 soparsehttpvers(so
, arg
, events
, waitflag
);
263 DPRINT("mbufstrcmp bad");
268 so
->so_upcall
= NULL
;
269 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
275 soparsehttpvers(struct socket
*so
, void *arg
, int events
, int waitflag
)
278 int i
, cc
, spaces
, inspaces
;
280 if ((so
->so_state
& SS_CANTRCVMORE
) != 0 || sbfull(&so
->so_rcv
))
283 m
= so
->so_rcv
.sb_mb
;
284 cc
= so
->so_rcv
.sb_cc
;
285 inspaces
= spaces
= 0;
286 for (m
= so
->so_rcv
.sb_mb
; m
; m
= n
) {
288 for (; m
; m
= m
->m_next
) {
289 for (i
= 0; i
< m
->m_len
; i
++, cc
--) {
290 switch (*(mtod(m
, char *) + i
)) {
309 * if we don't have enough characters
310 * left (cc < sizeof("HTTP/1.0") - 1)
311 * then see if the remaining ones
312 * are a request we can parse.
314 if (cc
< sizeof("HTTP/1.0") - 1) {
315 if (mbufstrncmp(m
, n
, i
, cc
,
324 mbufstrcmp(m
, n
, i
, "HTTP/1.0") ||
325 mbufstrcmp(m
, n
, i
, "HTTP/1.1")) {
327 soishttpconnected(so
,
328 arg
, events
, waitflag
);
341 * if we hit here we haven't hit something
342 * we don't understand or a newline, so try again
344 so
->so_upcall
= soparsehttpvers
;
345 so
->so_rcv
.sb_flags
|= SB_UPCALL
;
350 so
->so_upcall
= NULL
;
351 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
360 soishttpconnected(struct socket
*so
, void *arg
, int events
, int waitflag
)
367 if ((so
->so_state
& SS_CANTRCVMORE
) != 0 || sbfull(&so
->so_rcv
))
371 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
372 * copied - how much we've copied so far
373 * ccleft - how many bytes remaining in the socketbuffer
374 * just loop over the mbufs subtracting from 'ccleft' until we only
378 ccleft
= so
->so_rcv
.sb_cc
;
382 for (m
= so
->so_rcv
.sb_mb
; m
; m
= n
) {
384 for (; m
; m
= m
->m_next
) {
386 if (ccleft
<= NCHRS
) {
390 tocopy
= (NCHRS
- ccleft
) - copied
;
391 src
= mtod(m
, char *) + (m
->m_len
- tocopy
);
409 if (c
== '\n' && (b
== '\n' || (b
== '\r' && a
== '\n'))) {
410 /* we have all request headers */
415 so
->so_upcall
= soishttpconnected
;
416 so
->so_rcv
.sb_flags
|= SB_UPCALL
;
420 so
->so_upcall
= NULL
;
421 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;