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/net/bpf_filter.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 /*      $NetBSD: bpf_filter.c,v 1.21.2.1 2006/10/23 17:48:04 ghen Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * This code is derived from the Stanford/CMU enet packet filter,
    8  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
    9  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
   10  * Berkeley Laboratory.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. Neither the name of the University nor the names of its contributors
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34  * SUCH DAMAGE.
   35  *
   36  *      @(#)bpf_filter.c        8.1 (Berkeley) 6/10/93
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 __KERNEL_RCSID(0, "$NetBSD: bpf_filter.c,v 1.21.2.1 2006/10/23 17:48:04 ghen Exp $");
   41 
   42 #if 0
   43 #if !(defined(lint) || defined(KERNEL))
   44 static const char rcsid[] =
   45     "@(#) Header: bpf_filter.c,v 1.33 97/04/26 13:37:18 leres Exp  (LBL)";
   46 #endif
   47 #endif
   48 
   49 #include <sys/param.h>
   50 #include <sys/time.h>
   51 
   52 #if !defined(UNALIGNED_ACCESS)
   53 #define BPF_ALIGN
   54 #endif
   55 
   56 #ifndef BPF_ALIGN
   57 #define EXTRACT_SHORT(p)        ((u_int16_t)ntohs(*(u_int16_t *)p))
   58 #define EXTRACT_LONG(p)         (ntohl(*(u_int32_t *)p))
   59 #else
   60 #define EXTRACT_SHORT(p)\
   61         ((u_int16_t)\
   62                 ((u_int16_t)*((u_char *)p+0)<<8|\
   63                  (u_int16_t)*((u_char *)p+1)<<0))
   64 #define EXTRACT_LONG(p)\
   65                 ((u_int32_t)*((u_char *)p+0)<<24|\
   66                  (u_int32_t)*((u_char *)p+1)<<16|\
   67                  (u_int32_t)*((u_char *)p+2)<<8|\
   68                  (u_int32_t)*((u_char *)p+3)<<0)
   69 #endif
   70 
   71 #ifdef _KERNEL
   72 #include <sys/mbuf.h>
   73 #define MINDEX(len, m, k) \
   74 { \
   75         len = m->m_len; \
   76         while (k >= len) { \
   77                 k -= len; \
   78                 m = m->m_next; \
   79                 if (m == 0) \
   80                         return 0; \
   81                 len = m->m_len; \
   82         } \
   83 }
   84 
   85 static int m_xword __P((struct mbuf *, int, int *));
   86 static int m_xhalf __P((struct mbuf *, int, int *));
   87 
   88 static int
   89 m_xword(m, k, err)
   90         struct mbuf *m;
   91         int k, *err;
   92 {
   93         int len;
   94         u_char *cp, *np;
   95         struct mbuf *m0;
   96 
   97         *err = 1;
   98         MINDEX(len, m, k);
   99         cp = mtod(m, u_char *) + k;
  100         if (len - k >= 4) {
  101                 *err = 0;
  102                 return EXTRACT_LONG(cp);
  103         }
  104         m0 = m->m_next;
  105         if (m0 == 0 || m0->m_len + len - k < 4)
  106                 return 0;
  107         *err = 0;
  108         np = mtod(m0, u_char *);
  109         switch (len - k) {
  110 
  111         case 1:
  112                 return (cp[0] << 24) | (np[0] << 16) | (np[1] << 8) | np[2];
  113 
  114         case 2:
  115                 return (cp[0] << 24) | (cp[1] << 16) | (np[0] << 8) | np[1];
  116 
  117         default:
  118                 return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | np[0];
  119         }
  120 }
  121 
  122 static int
  123 m_xhalf(m, k, err)
  124         struct mbuf *m;
  125         int k, *err;
  126 {
  127         int len;
  128         u_char *cp;
  129         struct mbuf *m0;
  130 
  131         *err = 1;
  132         MINDEX(len, m, k);
  133         cp = mtod(m, u_char *) + k;
  134         if (len - k >= 2) {
  135                 *err = 0;
  136                 return EXTRACT_SHORT(cp);
  137         }
  138         m0 = m->m_next;
  139         if (m0 == 0)
  140                 return 0;
  141         *err = 0;
  142         return (cp[0] << 8) | mtod(m0, u_char *)[0];
  143 }
  144 #else /* _KERNEL */
  145 #include <stdlib.h>
  146 #endif /* !_KERNEL */
  147 
  148 #include <net/bpf.h>
  149 
  150 /*
  151  * Execute the filter program starting at pc on the packet p
  152  * wirelen is the length of the original packet
  153  * buflen is the amount of data present
  154  */
  155 u_int
  156 bpf_filter(pc, p, wirelen, buflen)
  157         struct bpf_insn *pc;
  158         u_char *p;
  159         u_int wirelen;
  160         u_int buflen;
  161 {
  162         u_int32_t A, X;
  163         int k;
  164         int32_t mem[BPF_MEMWORDS];
  165 
  166         if (pc == 0)
  167                 /*
  168                  * No filter means accept all.
  169                  */
  170                 return (u_int)-1;
  171         A = 0;
  172         X = 0;
  173         --pc;
  174         while (1) {
  175                 ++pc;
  176                 switch (pc->code) {
  177 
  178                 default:
  179 #ifdef _KERNEL
  180                         return 0;
  181 #else
  182                         abort();
  183 #endif
  184                 case BPF_RET|BPF_K:
  185                         return (u_int)pc->k;
  186 
  187                 case BPF_RET|BPF_A:
  188                         return (u_int)A;
  189 
  190                 case BPF_LD|BPF_W|BPF_ABS:
  191                         k = pc->k;
  192                         if (k + sizeof(int32_t) > buflen) {
  193 #ifdef _KERNEL
  194                                 int merr;
  195 
  196                                 if (buflen != 0)
  197                                         return 0;
  198                                 A = m_xword((struct mbuf *)p, k, &merr);
  199                                 if (merr != 0)
  200                                         return 0;
  201                                 continue;
  202 #else
  203                                 return 0;
  204 #endif
  205                         }
  206                         A = EXTRACT_LONG(&p[k]);
  207                         continue;
  208 
  209                 case BPF_LD|BPF_H|BPF_ABS:
  210                         k = pc->k;
  211                         if (k + sizeof(int16_t) > buflen) {
  212 #ifdef _KERNEL
  213                                 int merr;
  214 
  215                                 if (buflen != 0)
  216                                         return 0;
  217                                 A = m_xhalf((struct mbuf *)p, k, &merr);
  218                                 if (merr != 0)
  219                                         return 0;
  220                                 continue;
  221 #else
  222                                 return 0;
  223 #endif
  224                         }
  225                         A = EXTRACT_SHORT(&p[k]);
  226                         continue;
  227 
  228                 case BPF_LD|BPF_B|BPF_ABS:
  229                         k = pc->k;
  230                         if (k >= buflen) {
  231 #ifdef _KERNEL
  232                                 struct mbuf *m;
  233                                 int len;
  234 
  235                                 if (buflen != 0)
  236                                         return 0;
  237                                 m = (struct mbuf *)p;
  238                                 MINDEX(len, m, k);
  239                                 A = mtod(m, u_char *)[k];
  240                                 continue;
  241 #else
  242                                 return 0;
  243 #endif
  244                         }
  245                         A = p[k];
  246                         continue;
  247 
  248                 case BPF_LD|BPF_W|BPF_LEN:
  249                         A = wirelen;
  250                         continue;
  251 
  252                 case BPF_LDX|BPF_W|BPF_LEN:
  253                         X = wirelen;
  254                         continue;
  255 
  256                 case BPF_LD|BPF_W|BPF_IND:
  257                         k = X + pc->k;
  258                         if (k + sizeof(int32_t) > buflen) {
  259 #ifdef _KERNEL
  260                                 int merr;
  261 
  262                                 if (buflen != 0)
  263                                         return 0;
  264                                 A = m_xword((struct mbuf *)p, k, &merr);
  265                                 if (merr != 0)
  266                                         return 0;
  267                                 continue;
  268 #else
  269                                 return 0;
  270 #endif
  271                         }
  272                         A = EXTRACT_LONG(&p[k]);
  273                         continue;
  274 
  275                 case BPF_LD|BPF_H|BPF_IND:
  276                         k = X + pc->k;
  277                         if (k + sizeof(int16_t) > buflen) {
  278 #ifdef _KERNEL
  279                                 int merr;
  280 
  281                                 if (buflen != 0)
  282                                         return 0;
  283                                 A = m_xhalf((struct mbuf *)p, k, &merr);
  284                                 if (merr != 0)
  285                                         return 0;
  286                                 continue;
  287 #else
  288                                 return 0;
  289 #endif
  290                         }
  291                         A = EXTRACT_SHORT(&p[k]);
  292                         continue;
  293 
  294                 case BPF_LD|BPF_B|BPF_IND:
  295                         k = X + pc->k;
  296                         if (k >= buflen) {
  297 #ifdef _KERNEL
  298                                 struct mbuf *m;
  299                                 int len;
  300 
  301                                 if (buflen != 0)
  302                                         return 0;
  303                                 m = (struct mbuf *)p;
  304                                 MINDEX(len, m, k);
  305                                 A = mtod(m, u_char *)[k];
  306                                 continue;
  307 #else
  308                                 return 0;
  309 #endif
  310                         }
  311                         A = p[k];
  312                         continue;
  313 
  314                 case BPF_LDX|BPF_MSH|BPF_B:
  315                         k = pc->k;
  316                         if (k >= buflen) {
  317 #ifdef _KERNEL
  318                                 struct mbuf *m;
  319                                 int len;
  320 
  321                                 if (buflen != 0)
  322                                         return 0;
  323                                 m = (struct mbuf *)p;
  324                                 MINDEX(len, m, k);
  325                                 X = (mtod(m, char *)[k] & 0xf) << 2;
  326                                 continue;
  327 #else
  328                                 return 0;
  329 #endif
  330                         }
  331                         X = (p[pc->k] & 0xf) << 2;
  332                         continue;
  333 
  334                 case BPF_LD|BPF_IMM:
  335                         A = pc->k;
  336                         continue;
  337 
  338                 case BPF_LDX|BPF_IMM:
  339                         X = pc->k;
  340                         continue;
  341 
  342                 case BPF_LD|BPF_MEM:
  343                         A = mem[pc->k];
  344                         continue;
  345 
  346                 case BPF_LDX|BPF_MEM:
  347                         X = mem[pc->k];
  348                         continue;
  349 
  350                 case BPF_ST:
  351                         mem[pc->k] = A;
  352                         continue;
  353 
  354                 case BPF_STX:
  355                         mem[pc->k] = X;
  356                         continue;
  357 
  358                 case BPF_JMP|BPF_JA:
  359                         pc += pc->k;
  360                         continue;
  361 
  362                 case BPF_JMP|BPF_JGT|BPF_K:
  363                         pc += (A > pc->k) ? pc->jt : pc->jf;
  364                         continue;
  365 
  366                 case BPF_JMP|BPF_JGE|BPF_K:
  367                         pc += (A >= pc->k) ? pc->jt : pc->jf;
  368                         continue;
  369 
  370                 case BPF_JMP|BPF_JEQ|BPF_K:
  371                         pc += (A == pc->k) ? pc->jt : pc->jf;
  372                         continue;
  373 
  374                 case BPF_JMP|BPF_JSET|BPF_K:
  375                         pc += (A & pc->k) ? pc->jt : pc->jf;
  376                         continue;
  377 
  378                 case BPF_JMP|BPF_JGT|BPF_X:
  379                         pc += (A > X) ? pc->jt : pc->jf;
  380                         continue;
  381 
  382                 case BPF_JMP|BPF_JGE|BPF_X:
  383                         pc += (A >= X) ? pc->jt : pc->jf;
  384                         continue;
  385 
  386                 case BPF_JMP|BPF_JEQ|BPF_X:
  387                         pc += (A == X) ? pc->jt : pc->jf;
  388                         continue;
  389 
  390                 case BPF_JMP|BPF_JSET|BPF_X:
  391                         pc += (A & X) ? pc->jt : pc->jf;
  392                         continue;
  393 
  394                 case BPF_ALU|BPF_ADD|BPF_X:
  395                         A += X;
  396                         continue;
  397 
  398                 case BPF_ALU|BPF_SUB|BPF_X:
  399                         A -= X;
  400                         continue;
  401 
  402                 case BPF_ALU|BPF_MUL|BPF_X:
  403                         A *= X;
  404                         continue;
  405 
  406                 case BPF_ALU|BPF_DIV|BPF_X:
  407                         if (X == 0)
  408                                 return 0;
  409                         A /= X;
  410                         continue;
  411 
  412                 case BPF_ALU|BPF_AND|BPF_X:
  413                         A &= X;
  414                         continue;
  415 
  416                 case BPF_ALU|BPF_OR|BPF_X:
  417                         A |= X;
  418                         continue;
  419 
  420                 case BPF_ALU|BPF_LSH|BPF_X:
  421                         A <<= X;
  422                         continue;
  423 
  424                 case BPF_ALU|BPF_RSH|BPF_X:
  425                         A >>= X;
  426                         continue;
  427 
  428                 case BPF_ALU|BPF_ADD|BPF_K:
  429                         A += pc->k;
  430                         continue;
  431 
  432                 case BPF_ALU|BPF_SUB|BPF_K:
  433                         A -= pc->k;
  434                         continue;
  435 
  436                 case BPF_ALU|BPF_MUL|BPF_K:
  437                         A *= pc->k;
  438                         continue;
  439 
  440                 case BPF_ALU|BPF_DIV|BPF_K:
  441                         A /= pc->k;
  442                         continue;
  443 
  444                 case BPF_ALU|BPF_AND|BPF_K:
  445                         A &= pc->k;
  446                         continue;
  447 
  448                 case BPF_ALU|BPF_OR|BPF_K:
  449                         A |= pc->k;
  450                         continue;
  451 
  452                 case BPF_ALU|BPF_LSH|BPF_K:
  453                         A <<= pc->k;
  454                         continue;
  455 
  456                 case BPF_ALU|BPF_RSH|BPF_K:
  457                         A >>= pc->k;
  458                         continue;
  459 
  460                 case BPF_ALU|BPF_NEG:
  461                         A = -A;
  462                         continue;
  463 
  464                 case BPF_MISC|BPF_TAX:
  465                         X = A;
  466                         continue;
  467 
  468                 case BPF_MISC|BPF_TXA:
  469                         A = X;
  470                         continue;
  471                 }
  472         }
  473 }
  474 
  475 #ifdef _KERNEL
  476 /*
  477  * Return true if the 'fcode' is a valid filter program.
  478  * The constraints are that each jump be forward and to a valid
  479  * code.  The code must terminate with either an accept or reject.
  480  * 'valid' is an array for use by the routine (it must be at least
  481  * 'len' bytes long).
  482  *
  483  * The kernel needs to be able to verify an application's filter code.
  484  * Otherwise, a bogus program could easily crash the system.
  485  */
  486 int
  487 bpf_validate(f, len)
  488         struct bpf_insn *f;
  489         int len;
  490 {
  491         int i;
  492         struct bpf_insn *p;
  493 
  494         for (i = 0; i < len; ++i) {
  495                 /*
  496                  * Check that that jumps are forward, and within
  497                  * the code block.
  498                  */
  499                 p = &f[i];
  500                 if (BPF_CLASS(p->code) == BPF_JMP) {
  501                         int from = i + 1;
  502 
  503                         if (BPF_OP(p->code) == BPF_JA) {
  504                                 if ((p->k < 0) ||
  505                                     (from + p->k >= len) ||
  506                                     (from + p->k < 0))
  507                                         return 0;
  508                         }
  509                         else if (from + p->jt >= len || from + p->jf >= len)
  510                                 return 0;
  511                 }
  512                 /*
  513                  * Check that memory operations use valid addresses.
  514                  */
  515                 if ((BPF_CLASS(p->code) == BPF_ST ||
  516                      (BPF_CLASS(p->code) == BPF_LD &&
  517                       (p->code & 0xe0) == BPF_MEM)) &&
  518                     (p->k >= BPF_MEMWORDS || p->k < 0))
  519                         return 0;
  520                 /*
  521                  * Check for constant division by 0.
  522                  */
  523                 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
  524                         return 0;
  525         }
  526         return BPF_CLASS(f[len - 1].code) == BPF_RET;
  527 }
  528 #endif

Cache object: 31172e844c602bbba0d77f12b6348b02


[ 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.