--- libaitio/src/bpf.c 2013/07/09 00:35:36 1.3 +++ libaitio/src/bpf.c 2013/10/18 14:21:01 1.3.8.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: bpf.c,v 1.3 2013/07/09 00:35:36 misho Exp $ +* $Id: bpf.c,v 1.3.8.1 2013/10/18 14:21:01 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -365,4 +365,51 @@ io_etherRecv(int eth, void * __restrict buf, size_t bu memmove(buf, buf + h->bh_hdrlen, rlen); return rlen; +} + +/* + * io_etherFilter() - BPF filter routine + * + * @eth = bpf handle + * @io = filter direction + * (IO_ETHER_FILTER_PROMISC|IO_ETHER_FILTER_NOTREAD|IO_ETHER_FILTER_READ|IO_ETHER_FILTER_WRITE) + * @insn = BPF filter instruction array + * @insnlen = Length of BPF filter instruction array + * return: -1 error or 0 ok + */ +int +io_etherFilter(int eth, int io, struct bpf_insn * __restrict insn, size_t insnlen) +{ + int ret = 0; + struct bpf_program fcode = { 0 }; + + if (io != IO_ETHER_FILTER_PROMISC && (!insn || !insnlen)) { + io_SetErr(EINVAL, "invalid arguments"); + return -1; + } + + switch (io) { + case IO_ETHER_FILTER_PROMISC: /* promiscuous mode */ + ret = ioctl(eth, BIOCPROMISC, NULL); + break; + case IO_ETHER_FILTER_NOTREAD: /* read not filter */ + fcode.bf_len = insnlen / sizeof(struct bpf_insn); + fcode.bf_insns = insn; + ret = ioctl(eth, BIOCSETFNR, &fcode); + break; + case IO_ETHER_FILTER_READ: /* read filter */ + fcode.bf_len = insnlen / sizeof(struct bpf_insn); + fcode.bf_insns = insn; + ret = ioctl(eth, BIOCSETF, &fcode); + break; + case IO_ETHER_FILTER_WRITE: /* write filter */ + fcode.bf_len = insnlen / sizeof(struct bpf_insn); + fcode.bf_insns = insn; + ret = ioctl(eth, BIOCSETWF, &fcode); + break; + } + + if (ret == -1) + LOGERR; + return ret; }