3 * Politecnico di Torino. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the Politecnico
13 * di Torino, and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * Portions copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from the Stanford/CMU enet packet filter,
27 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
28 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
29 * Berkeley Laboratory.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * Initialize the filter machine
66 uint32 bpf_filter_init(register struct bpf_insn *pc, MEM_TYPE *mem_ex, TME_CORE *tme, struct time_conv *time_ref)
69 int32 mem[BPF_MEMWORDS];
76 * No filter means accept all.
89 /* RET INSTRUCTIONS */
95 /* END RET INSTRUCTIONS */
97 /* LD NO PACKET INSTRUCTIONS */
102 case BPF_LDX|BPF_IMM:
110 case BPF_LDX|BPF_MEM:
114 case BPF_LD|BPF_MEM_EX_IMM|BPF_B:
115 A= mem_ex->buffer[pc->k];
118 case BPF_LDX|BPF_MEM_EX_IMM|BPF_B:
119 X= mem_ex->buffer[pc->k];
122 case BPF_LD|BPF_MEM_EX_IMM|BPF_H:
123 tmp2=(uint16*)&mem_ex->buffer[pc->k];
147 :"=a"(A),"=c"(tmp2): );
151 case BPF_LDX|BPF_MEM_EX_IMM|BPF_H:
152 tmp2=(uint16*)&mem_ex->buffer[pc->k];
176 :"=a"(X),"=c"(tmp2): );
180 case BPF_LD|BPF_MEM_EX_IMM|BPF_W:
181 tmp=(uint32*)&mem_ex->buffer[pc->k];
204 :"=a"(A),"=c"(tmp): );
208 case BPF_LDX|BPF_MEM_EX_IMM|BPF_W:
209 tmp=(uint32*)&mem_ex->buffer[pc->k];
232 :"=a"(X),"=c"(tmp): );
236 case BPF_LD|BPF_MEM_EX_IND|BPF_B:
238 if ((int32)k>= (int32)mem_ex->size) {
241 A= mem_ex->buffer[k];
244 case BPF_LD|BPF_MEM_EX_IND|BPF_H:
246 if ((int32)(k+1)>= (int32)mem_ex->size) {
249 tmp2=(uint16*)&mem_ex->buffer[k];
273 :"=a"(A),"=c"(tmp2): );
277 case BPF_LD|BPF_MEM_EX_IND|BPF_W:
279 if ((int32)(k+3)>= (int32)mem_ex->size) {
282 tmp=(uint32*)&mem_ex->buffer[k];
305 :"=a"(A),"=c"(tmp): );
308 /* END LD NO PACKET INSTRUCTIONS */
310 /* STORE INSTRUCTIONS */
319 case BPF_ST|BPF_MEM_EX_IMM|BPF_B:
320 mem_ex->buffer[pc->k]=(uint8)A;
323 case BPF_STX|BPF_MEM_EX_IMM|BPF_B:
324 mem_ex->buffer[pc->k]=(uint8)X;
327 case BPF_ST|BPF_MEM_EX_IMM|BPF_W:
328 tmp=(uint32*)&mem_ex->buffer[pc->k];
351 :"=a"(A),"=c"(tmp): );
355 case BPF_STX|BPF_MEM_EX_IMM|BPF_W:
356 tmp=(uint32*)&mem_ex->buffer[pc->k];
379 :"=a"(X),"=c"(tmp): );
383 case BPF_ST|BPF_MEM_EX_IMM|BPF_H:
384 tmp2=(uint16*)&mem_ex->buffer[pc->k];
407 :"=a"(A),"=c"(tmp2): );
411 case BPF_STX|BPF_MEM_EX_IMM|BPF_H:
412 tmp2=(uint16*)&mem_ex->buffer[pc->k];
435 :"=a"(X),"=c"(tmp2): );
439 case BPF_ST|BPF_MEM_EX_IND|BPF_B:
440 mem_ex->buffer[pc->k+X]=(uint8)A;
442 case BPF_ST|BPF_MEM_EX_IND|BPF_W:
443 tmp=(uint32*)&mem_ex->buffer[pc->k+X];
466 :"=a"(A),"=c"(tmp): );
470 case BPF_ST|BPF_MEM_EX_IND|BPF_H:
471 tmp2=(uint16*)&mem_ex->buffer[pc->k+X];
494 :"=a"(A),"=c"(tmp2): );
497 /* END STORE INSTRUCTIONS */
499 /* JUMP INSTRUCTIONS */
504 case BPF_JMP|BPF_JGT|BPF_K:
505 pc += ((int32)A > (int32)pc->k) ? pc->jt : pc->jf;
508 case BPF_JMP|BPF_JGE|BPF_K:
509 pc += ((int32)A >= (int32)pc->k) ? pc->jt : pc->jf;
512 case BPF_JMP|BPF_JEQ|BPF_K:
513 pc += ((int32)A == (int32)pc->k) ? pc->jt : pc->jf;
516 case BPF_JMP|BPF_JSET|BPF_K:
517 pc += (A & pc->k) ? pc->jt : pc->jf;
520 case BPF_JMP|BPF_JGT|BPF_X:
521 pc += (A > X) ? pc->jt : pc->jf;
524 case BPF_JMP|BPF_JGE|BPF_X:
525 pc += (A >= X) ? pc->jt : pc->jf;
528 case BPF_JMP|BPF_JEQ|BPF_X:
529 pc += (A == X) ? pc->jt : pc->jf;
532 case BPF_JMP|BPF_JSET|BPF_X:
533 pc += (A & X) ? pc->jt : pc->jf;
535 /* END JUMP INSTRUCTIONS */
537 /* ARITHMETIC INSTRUCTIONS */
538 case BPF_ALU|BPF_ADD|BPF_X:
542 case BPF_ALU|BPF_SUB|BPF_X:
546 case BPF_ALU|BPF_MUL|BPF_X:
550 case BPF_ALU|BPF_DIV|BPF_X:
556 case BPF_ALU|BPF_AND|BPF_X:
560 case BPF_ALU|BPF_OR|BPF_X:
564 case BPF_ALU|BPF_LSH|BPF_X:
568 case BPF_ALU|BPF_RSH|BPF_X:
572 case BPF_ALU|BPF_ADD|BPF_K:
576 case BPF_ALU|BPF_SUB|BPF_K:
580 case BPF_ALU|BPF_MUL|BPF_K:
584 case BPF_ALU|BPF_DIV|BPF_K:
588 case BPF_ALU|BPF_AND|BPF_K:
592 case BPF_ALU|BPF_OR|BPF_K:
596 case BPF_ALU|BPF_LSH|BPF_K:
600 case BPF_ALU|BPF_RSH|BPF_K:
604 case BPF_ALU|BPF_NEG:
605 (int32)A = -((int32)A);
607 /* ARITHMETIC INSTRUCTIONS */
609 /* MISC INSTRUCTIONS */
610 case BPF_MISC|BPF_TAX:
614 case BPF_MISC|BPF_TXA:
617 /* END MISC INSTRUCTIONS */
619 /* TME INSTRUCTIONS */
620 case BPF_MISC|BPF_TME|BPF_LOOKUP:
621 j=lookup_frontend(mem_ex,tme,pc->k,time_ref);
624 pc += (j == TME_TRUE) ? pc->jt : pc->jf;
627 case BPF_MISC|BPF_TME|BPF_EXECUTE:
628 if (execute_frontend(mem_ex,tme,0,pc->k)==TME_ERROR)
632 case BPF_MISC|BPF_TME|BPF_INIT:
633 if (init_tme_block(tme,pc->k)==TME_ERROR)
637 case BPF_MISC|BPF_TME|BPF_VALIDATE:
638 if (validate_tme_block(mem_ex,tme,A,pc->k)==TME_ERROR)
642 case BPF_MISC|BPF_TME|BPF_SET_MEMORY:
643 if (init_extended_memory(pc->k,mem_ex)==TME_ERROR)
647 case BPF_MISC|BPF_TME|BPF_SET_ACTIVE:
648 if (set_active_tme_block(tme,pc->k)==TME_ERROR)
652 case BPF_MISC|BPF_TME|BPF_SET_ACTIVE_READ:
653 if (set_active_tme_block(tme,pc->k)==TME_ERROR)
656 case BPF_MISC|BPF_TME|BPF_SET_WORKING:
657 if ((pc->k<0)||(pc->k>=MAX_TME_DATA_BLOCKS))
664 case BPF_MISC|BPF_TME|BPF_RESET:
665 if (reset_tme(tme)==TME_ERROR)
669 case BPF_MISC|BPF_TME|BPF_GET_REGISTER_VALUE:
670 if (get_tme_block_register(&tme->block_data[tme->working],mem_ex,pc->k,&j)==TME_ERROR)
675 case BPF_MISC|BPF_TME|BPF_SET_REGISTER_VALUE:
676 if (set_tme_block_register(&tme->block_data[tme->working],mem_ex,pc->k,A,TRUE)==TME_ERROR)
680 case BPF_MISC|BPF_TME|BPF_SET_AUTODELETION:
681 set_autodeletion(&tme->block_data[tme->working],pc->k);
684 /* END TME INSTRUCTIONS */