1*76404edcSAsim Jamshed /*- 2*76404edcSAsim Jamshed * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 3*76404edcSAsim Jamshed * The Regents of the University of California. All rights reserved. 4*76404edcSAsim Jamshed * 5*76404edcSAsim Jamshed * Some portions Copyright (C) 2010-2013 Sourcefire, Inc. 6*76404edcSAsim Jamshed * 7*76404edcSAsim Jamshed * This code is derived from the Stanford/CMU enet packet filter, 8*76404edcSAsim Jamshed * (net/enet.c) distributed as part of 4.3BSD, and code contributed 9*76404edcSAsim Jamshed * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 10*76404edcSAsim Jamshed * Berkeley Laboratory. 11*76404edcSAsim Jamshed * 12*76404edcSAsim Jamshed * Redistribution and use in source and binary forms, with or without 13*76404edcSAsim Jamshed * modification, are permitted provided that the following conditions 14*76404edcSAsim Jamshed * are met: 15*76404edcSAsim Jamshed * 1. Redistributions of source code must retain the above copyright 16*76404edcSAsim Jamshed * notice, this list of conditions and the following disclaimer. 17*76404edcSAsim Jamshed * 2. Redistributions in binary form must reproduce the above copyright 18*76404edcSAsim Jamshed * notice, this list of conditions and the following disclaimer in the 19*76404edcSAsim Jamshed * documentation and/or other materials provided with the distribution. 20*76404edcSAsim Jamshed * 3. All advertising materials mentioning features or use of this software 21*76404edcSAsim Jamshed * must display the following acknowledgement: 22*76404edcSAsim Jamshed * This product includes software developed by the University of 23*76404edcSAsim Jamshed * California, Berkeley and its contributors. 24*76404edcSAsim Jamshed * 4. Neither the name of the University nor the names of its contributors 25*76404edcSAsim Jamshed * may be used to endorse or promote products derived from this software 26*76404edcSAsim Jamshed * without specific prior written permission. 27*76404edcSAsim Jamshed * 28*76404edcSAsim Jamshed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29*76404edcSAsim Jamshed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30*76404edcSAsim Jamshed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31*76404edcSAsim Jamshed * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32*76404edcSAsim Jamshed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33*76404edcSAsim Jamshed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34*76404edcSAsim Jamshed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35*76404edcSAsim Jamshed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36*76404edcSAsim Jamshed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37*76404edcSAsim Jamshed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38*76404edcSAsim Jamshed * SUCH DAMAGE. 39*76404edcSAsim Jamshed * 40*76404edcSAsim Jamshed * @(#)bpf.h 7.1 (Berkeley) 5/7/91 41*76404edcSAsim Jamshed * 42*76404edcSAsim Jamshed * @(#) $Header: /usr/cvsroot/sfeng/ims/src/libraries/daq/daq/sfbpf/sfbpf.h,v 1.3 2013/06/28 14:57:30 rcombs Exp $ (LBL) 43*76404edcSAsim Jamshed */ 44*76404edcSAsim Jamshed 45*76404edcSAsim Jamshed /* 46*76404edcSAsim Jamshed * This is libDAQ's cut-down version of libpcap's cut-down version of bpf.h; 47*76404edcSAsim Jamshed * it includes only the stuff needed for the code generator and the userland 48*76404edcSAsim Jamshed * BPF interpreter, and the libDAQ APIs for setting filters, etc.. 49*76404edcSAsim Jamshed * 50*76404edcSAsim Jamshed * Mostly things have been renamed so as to not conflict with the original 51*76404edcSAsim Jamshed * libpcap BPF headers. 52*76404edcSAsim Jamshed * 53*76404edcSAsim Jamshed * Datalink type definitions have been extracted and placed in sfbpf_dlt.h. 54*76404edcSAsim Jamshed */ 55*76404edcSAsim Jamshed 56*76404edcSAsim Jamshed #ifndef _SFBPF_H 57*76404edcSAsim Jamshed #define _SFBPF_H 58*76404edcSAsim Jamshed 59*76404edcSAsim Jamshed #include <sys/types.h> 60*76404edcSAsim Jamshed 61*76404edcSAsim Jamshed #ifdef __cplusplus 62*76404edcSAsim Jamshed extern "C" { 63*76404edcSAsim Jamshed #endif 64*76404edcSAsim Jamshed 65*76404edcSAsim Jamshed /* BSD style release date */ 66*76404edcSAsim Jamshed #define SFBPF_RELEASE 199606 67*76404edcSAsim Jamshed 68*76404edcSAsim Jamshed typedef int sfbpf_int32; 69*76404edcSAsim Jamshed typedef u_int sfbpf_u_int32; 70*76404edcSAsim Jamshed 71*76404edcSAsim Jamshed /* 72*76404edcSAsim Jamshed * Alignment macros. SFBPF_WORDALIGN rounds up to the next 73*76404edcSAsim Jamshed * even multiple of SFBPF_ALIGNMENT. 74*76404edcSAsim Jamshed */ 75*76404edcSAsim Jamshed #define SFBPF_ALIGNMENT sizeof(sfbpf_int32) 76*76404edcSAsim Jamshed #define SFBPF_WORDALIGN(x) (((x)+(SFBPF_ALIGNMENT-1))&~(SFBPF_ALIGNMENT-1)) 77*76404edcSAsim Jamshed 78*76404edcSAsim Jamshed #define SFBPF_MAXBUFSIZE 0x8000 79*76404edcSAsim Jamshed #define SFBPF_MINBUFSIZE 32 80*76404edcSAsim Jamshed 81*76404edcSAsim Jamshed #define ISSET_BPFFILTER(x) (x.bf_insns) 82*76404edcSAsim Jamshed #define CLR_BPFFILTER(x) x.bf_insns = 0 83*76404edcSAsim Jamshed #define SET_BPFFILTER(x, y) sfbpf_compile(ETH_FRAME_LEN, DLT_EN10MB, x, y, 1, 0) 84*76404edcSAsim Jamshed #define EVAL_BPFFILTER(x, y, z) sfbpf_filter(x.bf_insns, y, z, z) 85*76404edcSAsim Jamshed 86*76404edcSAsim Jamshed /* 87*76404edcSAsim Jamshed * Structure for "pcap_compile()", "pcap_setfilter()", etc.. 88*76404edcSAsim Jamshed */ 89*76404edcSAsim Jamshed struct sfbpf_program { 90*76404edcSAsim Jamshed u_int bf_len; 91*76404edcSAsim Jamshed struct sfbpf_insn *bf_insns; 92*76404edcSAsim Jamshed }; 93*76404edcSAsim Jamshed 94*76404edcSAsim Jamshed /* 95*76404edcSAsim Jamshed * Struct return by BIOCVERSION. This represents the version number of 96*76404edcSAsim Jamshed * the filter language described by the instruction encodings below. 97*76404edcSAsim Jamshed * bpf understands a program iff kernel_major == filter_major && 98*76404edcSAsim Jamshed * kernel_minor >= filter_minor, that is, if the value returned by the 99*76404edcSAsim Jamshed * running kernel has the same major number and a minor number equal 100*76404edcSAsim Jamshed * equal to or less than the filter being downloaded. Otherwise, the 101*76404edcSAsim Jamshed * results are undefined, meaning an error may be returned or packets 102*76404edcSAsim Jamshed * may be accepted haphazardly. 103*76404edcSAsim Jamshed * It has nothing to do with the source code version. 104*76404edcSAsim Jamshed */ 105*76404edcSAsim Jamshed struct sfbpf_version { 106*76404edcSAsim Jamshed u_short bv_major; 107*76404edcSAsim Jamshed u_short bv_minor; 108*76404edcSAsim Jamshed }; 109*76404edcSAsim Jamshed /* Current version number of filter architecture. */ 110*76404edcSAsim Jamshed #define SFBPF_MAJOR_VERSION 1 111*76404edcSAsim Jamshed #define SFBPF_MINOR_VERSION 1 112*76404edcSAsim Jamshed 113*76404edcSAsim Jamshed #include "sfbpf_dlt.h" 114*76404edcSAsim Jamshed 115*76404edcSAsim Jamshed /* 116*76404edcSAsim Jamshed * The instruction encodings. 117*76404edcSAsim Jamshed */ 118*76404edcSAsim Jamshed /* instruction classes */ 119*76404edcSAsim Jamshed #define SFBPF_CLASS(code) ((code) & 0x07) 120*76404edcSAsim Jamshed #define SFBPF_LD 0x00 121*76404edcSAsim Jamshed #define SFBPF_LDX 0x01 122*76404edcSAsim Jamshed #define SFBPF_ST 0x02 123*76404edcSAsim Jamshed #define SFBPF_STX 0x03 124*76404edcSAsim Jamshed #define SFBPF_ALU 0x04 125*76404edcSAsim Jamshed #define SFBPF_JMP 0x05 126*76404edcSAsim Jamshed #define SFBPF_RET 0x06 127*76404edcSAsim Jamshed #define SFBPF_MISC 0x07 128*76404edcSAsim Jamshed 129*76404edcSAsim Jamshed /* ld/ldx fields */ 130*76404edcSAsim Jamshed #define SFBPF_SIZE(code) ((code) & 0x18) 131*76404edcSAsim Jamshed #define SFBPF_W 0x00 132*76404edcSAsim Jamshed #define SFBPF_H 0x08 133*76404edcSAsim Jamshed #define SFBPF_B 0x10 134*76404edcSAsim Jamshed #define SFBPF_MODE(code) ((code) & 0xe0) 135*76404edcSAsim Jamshed #define SFBPF_IMM 0x00 136*76404edcSAsim Jamshed #define SFBPF_ABS 0x20 137*76404edcSAsim Jamshed #define SFBPF_IND 0x40 138*76404edcSAsim Jamshed #define SFBPF_MEM 0x60 139*76404edcSAsim Jamshed #define SFBPF_LEN 0x80 140*76404edcSAsim Jamshed #define SFBPF_MSH 0xa0 141*76404edcSAsim Jamshed 142*76404edcSAsim Jamshed /* alu/jmp fields */ 143*76404edcSAsim Jamshed #define SFBPF_OP(code) ((code) & 0xf0) 144*76404edcSAsim Jamshed #define SFBPF_ADD 0x00 145*76404edcSAsim Jamshed #define SFBPF_SUB 0x10 146*76404edcSAsim Jamshed #define SFBPF_MUL 0x20 147*76404edcSAsim Jamshed #define SFBPF_DIV 0x30 148*76404edcSAsim Jamshed #define SFBPF_OR 0x40 149*76404edcSAsim Jamshed #define SFBPF_AND 0x50 150*76404edcSAsim Jamshed #define SFBPF_LSH 0x60 151*76404edcSAsim Jamshed #define SFBPF_RSH 0x70 152*76404edcSAsim Jamshed #define SFBPF_NEG 0x80 153*76404edcSAsim Jamshed #define SFBPF_JA 0x00 154*76404edcSAsim Jamshed #define SFBPF_JEQ 0x10 155*76404edcSAsim Jamshed #define SFBPF_JGT 0x20 156*76404edcSAsim Jamshed #define SFBPF_JGE 0x30 157*76404edcSAsim Jamshed #define SFBPF_JSET 0x40 158*76404edcSAsim Jamshed #define SFBPF_SRC(code) ((code) & 0x08) 159*76404edcSAsim Jamshed #define SFBPF_K 0x00 160*76404edcSAsim Jamshed #define SFBPF_X 0x08 161*76404edcSAsim Jamshed 162*76404edcSAsim Jamshed /* ret - SFBPF_K and SFBPF_X also apply */ 163*76404edcSAsim Jamshed #define SFBPF_RVAL(code) ((code) & 0x18) 164*76404edcSAsim Jamshed #define SFBPF_A 0x10 165*76404edcSAsim Jamshed 166*76404edcSAsim Jamshed /* misc */ 167*76404edcSAsim Jamshed #define SFBPF_MISCOP(code) ((code) & 0xf8) 168*76404edcSAsim Jamshed #define SFBPF_TAX 0x00 169*76404edcSAsim Jamshed #define SFBPF_TXA 0x80 170*76404edcSAsim Jamshed 171*76404edcSAsim Jamshed /* 172*76404edcSAsim Jamshed * The instruction data structure. 173*76404edcSAsim Jamshed */ 174*76404edcSAsim Jamshed struct sfbpf_insn { 175*76404edcSAsim Jamshed u_short code; 176*76404edcSAsim Jamshed u_char jt; 177*76404edcSAsim Jamshed u_char jf; 178*76404edcSAsim Jamshed sfbpf_u_int32 k; 179*76404edcSAsim Jamshed }; 180*76404edcSAsim Jamshed 181*76404edcSAsim Jamshed /* 182*76404edcSAsim Jamshed * Macros for insn array initializers. 183*76404edcSAsim Jamshed */ 184*76404edcSAsim Jamshed #define SFBPF_STMT(code, k) { (u_short)(code), 0, 0, k } 185*76404edcSAsim Jamshed #define SFBPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k } 186*76404edcSAsim Jamshed 187*76404edcSAsim Jamshed //#if __STDC__ || defined(__cplusplus) 188*76404edcSAsim Jamshed int sfbpf_compile(int snaplen_arg, int linktype_arg, struct sfbpf_program *program, const char *buf, int optimize, sfbpf_u_int32 mask); 189*76404edcSAsim Jamshed int sfbpf_validate(const struct sfbpf_insn *f, int len); 190*76404edcSAsim Jamshed u_int sfbpf_filter(const struct sfbpf_insn *pc, const u_char *p, u_int wirelen, u_int buflen); 191*76404edcSAsim Jamshed void sfbpf_freecode(struct sfbpf_program *program); 192*76404edcSAsim Jamshed void sfbpf_print(struct sfbpf_program *fp, int verbose); 193*76404edcSAsim Jamshed /* 194*76404edcSAsim Jamshed #else 195*76404edcSAsim Jamshed int sfbpf_compile(); 196*76404edcSAsim Jamshed int sfbpf_validate(); 197*76404edcSAsim Jamshed u_int sfbpf_filter(); 198*76404edcSAsim Jamshed void sfbpf_freecode(); 199*76404edcSAsim Jamshed #endif 200*76404edcSAsim Jamshed */ 201*76404edcSAsim Jamshed /* 202*76404edcSAsim Jamshed * Number of scratch memory words (for SFBPF_LD|SFBPF_MEM and SFBPF_ST). 203*76404edcSAsim Jamshed */ 204*76404edcSAsim Jamshed #define SFBPF_MEMWORDS 16 205*76404edcSAsim Jamshed 206*76404edcSAsim Jamshed #ifdef __cplusplus 207*76404edcSAsim Jamshed } 208*76404edcSAsim Jamshed #endif 209*76404edcSAsim Jamshed 210*76404edcSAsim Jamshed #endif 211