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

Cache object: 763b8403cb83e13ac21eb768cb1c8c97


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