The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/accf_http.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2000 Paycounter, Inc.
    5  * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   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.
   16  *
   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
   27  * SUCH DAMAGE.
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD$");
   32 
   33 #define ACCEPT_FILTER_MOD
   34 
   35 #include <sys/param.h>
   36 #include <sys/kernel.h>
   37 #include <sys/mbuf.h>
   38 #include <sys/module.h>
   39 #include <sys/signalvar.h>
   40 #include <sys/sysctl.h>
   41 #include <sys/socketvar.h>
   42 
   43 /* check for GET/HEAD */
   44 static int sohashttpget(struct socket *so, void *arg, int waitflag);
   45 /* check for HTTP/1.0 or HTTP/1.1 */
   46 static int soparsehttpvers(struct socket *so, void *arg, int waitflag);
   47 /* check for end of HTTP/1.x request */
   48 static int soishttpconnected(struct socket *so, void *arg, int waitflag);
   49 /* strcmp on an mbuf chain */
   50 static int mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp);
   51 /* strncmp on an mbuf chain */
   52 static int mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset,
   53         int max, char *cmp);
   54 /* socketbuffer is full */
   55 static int sbfull(struct sockbuf *sb);
   56 
   57 ACCEPT_FILTER_DEFINE(accf_http, "httpready", sohashttpget, NULL, NULL, 1);
   58 
   59 static int parse_http_version = 1;
   60 
   61 static SYSCTL_NODE(_net_inet_accf, OID_AUTO, http, CTLFLAG_RW, 0,
   62 "HTTP accept filter");
   63 SYSCTL_INT(_net_inet_accf_http, OID_AUTO, parsehttpversion, CTLFLAG_RW,
   64     &parse_http_version, 1,
   65     "Parse http version so that non 1.x requests work");
   66 
   67 #ifdef ACCF_HTTP_DEBUG
   68 #define DPRINT(fmt, args...)                                            \
   69         do {                                                            \
   70                 printf("%s:%d: " fmt "\n", __func__, __LINE__, ##args); \
   71         } while (0)
   72 #else
   73 #define DPRINT(fmt, args...)
   74 #endif
   75 
   76 static int
   77 sbfull(struct sockbuf *sb)
   78 {
   79 
   80         DPRINT("sbfull, cc(%ld) >= hiwat(%ld): %d, "
   81             "mbcnt(%ld) >= mbmax(%ld): %d",
   82             sb->sb_cc, sb->sb_hiwat, sb->sb_cc >= sb->sb_hiwat,
   83             sb->sb_mbcnt, sb->sb_mbmax, sb->sb_mbcnt >= sb->sb_mbmax);
   84         return (sbused(sb) >= sb->sb_hiwat || sb->sb_mbcnt >= sb->sb_mbmax);
   85 }
   86 
   87 /*
   88  * start at mbuf m, (must provide npkt if exists)
   89  * starting at offset in m compare characters in mbuf chain for 'cmp'
   90  */
   91 static int
   92 mbufstrcmp(struct mbuf *m, struct mbuf *npkt, int offset, char *cmp)
   93 {
   94         struct mbuf *n;
   95 
   96         for (; m != NULL; m = n) {
   97                 n = npkt;
   98                 if (npkt)
   99                         npkt = npkt->m_nextpkt;
  100                 for (; m; m = m->m_next) {
  101                         for (; offset < m->m_len; offset++, cmp++) {
  102                                 if (*cmp == '\0')
  103                                         return (1);
  104                                 else if (*cmp != *(mtod(m, char *) + offset))
  105                                         return (0);
  106                         }
  107                         if (*cmp == '\0')
  108                                 return (1);
  109                         offset = 0;
  110                 }
  111         }
  112         return (0);
  113 }
  114 
  115 /*
  116  * start at mbuf m, (must provide npkt if exists)
  117  * starting at offset in m compare characters in mbuf chain for 'cmp'
  118  * stop at 'max' characters
  119  */
  120 static int
  121 mbufstrncmp(struct mbuf *m, struct mbuf *npkt, int offset, int max, char *cmp)
  122 {
  123         struct mbuf *n;
  124 
  125         for (; m != NULL; m = n) {
  126                 n = npkt;
  127                 if (npkt)
  128                         npkt = npkt->m_nextpkt;
  129                 for (; m; m = m->m_next) {
  130                         for (; offset < m->m_len; offset++, cmp++, max--) {
  131                                 if (max == 0 || *cmp == '\0')
  132                                         return (1);
  133                                 else if (*cmp != *(mtod(m, char *) + offset))
  134                                         return (0);
  135                         }
  136                         if (max == 0 || *cmp == '\0')
  137                                 return (1);
  138                         offset = 0;
  139                 }
  140         }
  141         return (0);
  142 }
  143 
  144 #define STRSETUP(sptr, slen, str)                                       \
  145         do {                                                            \
  146                 sptr = str;                                             \
  147                 slen = sizeof(str) - 1;                                 \
  148         } while(0)
  149 
  150 static int
  151 sohashttpget(struct socket *so, void *arg, int waitflag)
  152 {
  153 
  154         if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 &&
  155             !sbfull(&so->so_rcv)) {
  156                 struct mbuf *m;
  157                 char *cmp;
  158                 int     cmplen, cc;
  159 
  160                 m = so->so_rcv.sb_mb;
  161                 cc = sbavail(&so->so_rcv) - 1;
  162                 if (cc < 1)
  163                         return (SU_OK);
  164                 switch (*mtod(m, char *)) {
  165                 case 'G':
  166                         STRSETUP(cmp, cmplen, "ET ");
  167                         break;
  168                 case 'H':
  169                         STRSETUP(cmp, cmplen, "EAD ");
  170                         break;
  171                 default:
  172                         goto fallout;
  173                 }
  174                 if (cc < cmplen) {
  175                         if (mbufstrncmp(m, m->m_nextpkt, 1, cc, cmp) == 1) {
  176                                 DPRINT("short cc (%d) but mbufstrncmp ok", cc);
  177                                 return (SU_OK);
  178                         } else {
  179                                 DPRINT("short cc (%d) mbufstrncmp failed", cc);
  180                                 goto fallout;
  181                         }
  182                 }
  183                 if (mbufstrcmp(m, m->m_nextpkt, 1, cmp) == 1) {
  184                         DPRINT("mbufstrcmp ok");
  185                         if (parse_http_version == 0)
  186                                 return (soishttpconnected(so, arg, waitflag));
  187                         else
  188                                 return (soparsehttpvers(so, arg, waitflag));
  189                 }
  190                 DPRINT("mbufstrcmp bad");
  191         }
  192 
  193 fallout:
  194         DPRINT("fallout");
  195         return (SU_ISCONNECTED);
  196 }
  197 
  198 static int
  199 soparsehttpvers(struct socket *so, void *arg, int waitflag)
  200 {
  201         struct mbuf *m, *n;
  202         int     i, cc, spaces, inspaces;
  203 
  204         if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
  205                 goto fallout;
  206 
  207         m = so->so_rcv.sb_mb;
  208         cc = sbavail(&so->so_rcv);
  209         inspaces = spaces = 0;
  210         for (m = so->so_rcv.sb_mb; m; m = n) {
  211                 n = m->m_nextpkt;
  212                 for (; m; m = m->m_next) {
  213                         for (i = 0; i < m->m_len; i++, cc--) {
  214                                 switch (*(mtod(m, char *) + i)) {
  215                                 case ' ':
  216                                         /* tabs? '\t' */
  217                                         if (!inspaces) {
  218                                                 spaces++;
  219                                                 inspaces = 1;
  220                                         }
  221                                         break;
  222                                 case '\r':
  223                                 case '\n':
  224                                         DPRINT("newline");
  225                                         goto fallout;
  226                                 default:
  227                                         if (spaces != 2) {
  228                                                 inspaces = 0;
  229                                                 break;
  230                                         }
  231 
  232                                         /*
  233                                          * if we don't have enough characters
  234                                          * left (cc < sizeof("HTTP/1.0") - 1)
  235                                          * then see if the remaining ones
  236                                          * are a request we can parse.
  237                                          */
  238                                         if (cc < sizeof("HTTP/1.0") - 1) {
  239                                                 if (mbufstrncmp(m, n, i, cc,
  240                                                         "HTTP/1.") == 1) {
  241                                                         DPRINT("ok");
  242                                                         goto readmore;
  243                                                 } else {
  244                                                         DPRINT("bad");
  245                                                         goto fallout;
  246                                                 }
  247                                         } else if (
  248                                             mbufstrcmp(m, n, i, "HTTP/1.0") ||
  249                                             mbufstrcmp(m, n, i, "HTTP/1.1")) {
  250                                                 DPRINT("ok");
  251                                                 return (soishttpconnected(so,
  252                                                     arg, waitflag));
  253                                         } else {
  254                                                 DPRINT("bad");
  255                                                 goto fallout;
  256                                         }
  257                                 }
  258                         }
  259                 }
  260         }
  261 readmore:
  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         soupcall_set(so, SO_RCV, soparsehttpvers, arg);
  268         return (SU_OK);
  269 
  270 fallout:
  271         DPRINT("fallout");
  272         return (SU_ISCONNECTED);
  273 }
  274 
  275 
  276 #define NCHRS 3
  277 
  278 static int
  279 soishttpconnected(struct socket *so, void *arg, int waitflag)
  280 {
  281         char a, b, c;
  282         struct mbuf *m, *n;
  283         int ccleft, copied;
  284 
  285         DPRINT("start");
  286         if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) != 0 || sbfull(&so->so_rcv))
  287                 goto gotit;
  288 
  289         /*
  290          * Walk the socketbuffer and copy the last NCHRS (3) into a, b, and c
  291          * copied - how much we've copied so far
  292          * ccleft - how many bytes remaining in the socketbuffer
  293          * just loop over the mbufs subtracting from 'ccleft' until we only
  294          * have NCHRS left
  295          */
  296         copied = 0;
  297         ccleft = sbavail(&so->so_rcv);
  298         if (ccleft < NCHRS)
  299                 goto readmore;
  300         a = b = c = '\0';
  301         for (m = so->so_rcv.sb_mb; m; m = n) {
  302                 n = m->m_nextpkt;
  303                 for (; m; m = m->m_next) {
  304                         ccleft -= m->m_len;
  305                         if (ccleft <= NCHRS) {
  306                                 char *src;
  307                                 int tocopy;
  308 
  309                                 tocopy = (NCHRS - ccleft) - copied;
  310                                 src = mtod(m, char *) + (m->m_len - tocopy);
  311 
  312                                 while (tocopy--) {
  313                                         switch (copied++) {
  314                                         case 0:
  315                                                 a = *src++;
  316                                                 break;
  317                                         case 1:
  318                                                 b = *src++;
  319                                                 break;
  320                                         case 2:
  321                                                 c = *src++;
  322                                                 break;
  323                                         }
  324                                 }
  325                         }
  326                 }
  327         }
  328         if (c == '\n' && (b == '\n' || (b == '\r' && a == '\n'))) {
  329                 /* we have all request headers */
  330                 goto gotit;
  331         }
  332 
  333 readmore:
  334         soupcall_set(so, SO_RCV, soishttpconnected, arg);
  335         return (SU_OK);
  336 
  337 gotit:
  338         return (SU_ISCONNECTED);
  339 }

Cache object: a97fcf6213d3fda76b8d71617a87834a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.