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