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$ 28 */ 29 30 #define ACCEPT_FILTER_MOD 31 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/mbuf.h> 35 #include <sys/module.h> 36 #include <sys/signalvar.h> 37 #include <sys/sysctl.h> 38 #include <sys/socketvar.h> 39 40 /* check for GET/HEAD */ 41 static void sohashttpget(struct socket *so, void *arg, int waitflag); 42 /* check for HTTP/1.0 or HTTP/1.1 */ 43 static void soparsehttpvers(struct socket *so, void *arg, int waitflag); 44 /* check for end of HTTP/1.x request */ 45 static void soishttpconnected(struct socket *so, void *arg, int waitflag); 46 /* strcmp on an mbuf chain */ 47 static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp); 48 /* strncmp on an mbuf chain */ 49 static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, 50 int max, char *cmp); 51 /* socketbuffer is full */ 52 static int sbfull(struct sockbuf *sb); 53 54 static struct accept_filter accf_http_filter = { 55 "httpready", 56 sohashttpget, 57 NULL, 58 NULL 59 }; 60 61 static moduledata_t accf_http_mod = { 62 "accf_http", 63 accept_filt_generic_mod_event, 64 &accf_http_filter 65 }; 66 67 DECLARE_MODULE(accf_http, accf_http_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 68 69 static int parse_http_version = 1; 70 71 SYSCTL_NODE(_net_inet_accf, OID_AUTO, http, CTLFLAG_RW, 0, 72 "HTTP accept filter"); 73 SYSCTL_INT(_net_inet_accf_http, OID_AUTO, parsehttpversion, CTLFLAG_RW, 74 &parse_http_version, 1, 75 "Parse http version so that non 1.x requests work"); 76 77 #ifdef ACCF_HTTP_DEBUG 78 #define DPRINT(fmt, args...) \ 79 do { \ 80 printf("%s:%d: " fmt "\n", __func__, __LINE__, ##args); \ 81 } while (0) 82 #else 83 #define DPRINT(fmt, args...) 84 #endif 85 86 static int 87 sbfull(struct sockbuf *sb) 88 { 89 90 DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, " 91 "mbcnt(%ld) >= mbmax(%ld): %d", 92 sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat, 93 sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax); 94 return (sb->sb_cc >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax); 95 } 96 97 /* 98 * start at mbuf m, (must provide npkt if exists) 99 * starting at offset in m compare characters in mbuf chain for 'cmp' 100 */ 101 static int 102 mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp) 103 { 104 struct mbuf *n; 105 106 for (; m != NULL; m = n) { 107 n = npkt; 108 if (npkt) 109 npkt = npkt->m_nextpkt; 110 for (; m; m = m->m_next) { 111 for (; offset < m->m_len; offset++, cmp++) { 112 if (*cmp == '\0') 113 return (1); 114 else if (*cmp != *(mtod(m, char *) + offset)) 115 return (0); 116 } 117 if (*cmp == '\0') 118 return (1); 119 offset = 0; 120 } 121 } 122 return (0); 123 } 124 125 /* 126 * start at mbuf m, (must provide npkt if exists) 127 * starting at offset in m compare characters in mbuf chain for 'cmp' 128 * stop at 'max' characters 129 */ 130 static int 131 mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp) 132 { 133 struct mbuf *n; 134 135 for (; m != NULL; m = n) { 136 n = npkt; 137 if (npkt) 138 npkt = npkt->m_nextpkt; 139 for (; m; m = m->m_next) { 140 for (; offset < m->m_len; offset++, cmp++, max--) { 141 if (max == 0 || *cmp == '\0') 142 return (1); 143 else if (*cmp != *(mtod(m, char *) + offset)) 144 return (0); 145 } 146 if (max == 0 || *cmp == '\0') 147 return (1); 148 offset = 0; 149 } 150 } 151 return (0); 152 } 153 154 #define STRSETUP(sptr, slen, str) \ 155 do { \ 156 sptr = str; \ 157 slen = sizeof(str) - 1; \ 158 } while(0) 159 160 static void 161 sohashttpget(struct socket *so, void *arg, int waitflag) 162 { 163 164 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 && !sbfull(&so->so_rcv)) { 165 struct mbuf *m; 166 char *cmp; 167 int cmplen, cc; 168 169 m = so->so_rcv.sb_mb; 170 cc = so->so_rcv.sb_cc - 1; 171 if (cc < 1) 172 return; 173 switch (*mtod(m, char *)) { 174 case 'G': 175 STRSETUP(cmp, cmplen, "ET "); 176 break; 177 case 'H': 178 STRSETUP(cmp, cmplen, "EAD "); 179 break; 180 default: 181 goto fallout; 182 } 183 if (cc < cmplen) { 184 if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) { 185 DPRINT("short cc (%d) but mbufstrncmp ok", cc); 186 return; 187 } else { 188 DPRINT("short cc (%d) mbufstrncmp failed", cc); 189 goto fallout; 190 } 191 } 192 if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) { 193 DPRINT("mbufstrcmp ok"); 194 if (parse_http_version == 0) 195 soishttpconnected(so, arg, waitflag); 196 else 197 soparsehttpvers(so, arg, waitflag); 198 return; 199 } 200 DPRINT("mbufstrcmp bad"); 201 } 202 203 fallout: 204 DPRINT("fallout"); 205 so->so_upcall = NULL; 206 so->so_rcv.sb_flags &= ~SB_UPCALL; 207 soisconnected(so); 208 return; 209 } 210 211 static void 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 = so->so_rcv.sb_cc; 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 soishttpconnected(so, 265 arg, waitflag); 266 return; 267 } else { 268 DPRINT("bad"); 269 goto fallout; 270 } 271 } 272 } 273 } 274 } 275 readmore: 276 DPRINT("readmore"); 277 /* 278 * if we hit here we haven't hit something 279 * we don't understand or a newline, so try again 280 */ 281 so->so_upcall = soparsehttpvers; 282 so->so_rcv.sb_flags |= SB_UPCALL; 283 return; 284 285 fallout: 286 DPRINT("fallout"); 287 so->so_upcall = NULL; 288 so->so_rcv.sb_flags &= ~SB_UPCALL; 289 soisconnected(so); 290 return; 291 } 292 293 294 #define NCHRS 3 295 296 static void 297 soishttpconnected(struct socket *so, void *arg, int waitflag) 298 { 299 char a, b, c; 300 struct mbuf *m, *n; 301 int ccleft, copied; 302 303 DPRINT("start"); 304 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv)) 305 goto gotit; 306 307 /* 308 * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c 309 * copied - how much we've copied so far 310 * ccleft - how many bytes remaining in the socketbuffer 311 * just loop over the mbufs subtracting from 'ccleft' until we only 312 * have NCHRS left 313 */ 314 copied = 0; 315 ccleft = so->so_rcv.sb_cc; 316 if (ccleft < NCHRS) 317 goto readmore; 318 a = b = c = '\0'; 319 for (m = so->so_rcv.sb_mb; m; m = n) { 320 n = m->m_nextpkt; 321 for (; m; m = m->m_next) { 322 ccleft -= m->m_len; 323 if (ccleft <= NCHRS) { 324 char *src; 325 int tocopy; 326 327 tocopy = (NCHRS - ccleft) - copied; 328 src = mtod(m, char *) + (m->m_len - tocopy); 329 330 while (tocopy--) { 331 switch (copied++) { 332 case 0: 333 a = *src++; 334 break; 335 case 1: 336 b = *src++; 337 break; 338 case 2: 339 c = *src++; 340 break; 341 } 342 } 343 } 344 } 345 } 346 if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) { 347 /* we have all request headers */ 348 goto gotit; 349 } 350 351 readmore: 352 so->so_upcall = soishttpconnected; 353 so->so_rcv.sb_flags |= SB_UPCALL; 354 return; 355 356 gotit: 357 so->so_upcall = NULL; 358 so->so_rcv.sb_flags &= ~SB_UPCALL; 359 soisconnected(so); 360 return; 361 } 362