BPFC(8) netsniff-ng toolkit BPFC(8) (NAME) bpfc - (Berkeley Packet Filter) (SYNOPSIS) bpfc { [] | [-] } (DESCRIPTION) bpfc (Berkeley Packet Filter BPF) (mnemonics) BPF C netsniff-ng iptables (xt_bpf) . BPF (upstream) packet(7) seccomp-BPF . BSD " " JIT BPF bpfc . (truncate) . BPF . BPF pcap(3) tcpdump . pcap(3) . bpfc BPF . bpfc JIT . bpfc flex(1) bison(1) (tokenize) (AST) . (opcodes) . bpfc BPF . . BPF JIT netsniff-ng . ''echo "1" > /proc/sys/net/core/bpf_jit_enable'' ( ) ''echo "2" > /proc/sys/net/core/bpf_jit_enable'' ( ) . BPF JIT ''tools/net/bpf_jit_disasm.c'' netsniff-ng . (OPTIONS) -i , --input BPF (stdin). -p, --cpp bpf C bpfc. #define #include ( ) bpf . -D =, --define = C bpf. -p/--cpp . -f , --format netsniff-ng. C netsniff-ng xt_bpf tcpdump . -b, --bypass . BPF ( ) . -V, --verbose bpfc. -d, --dump (dump) (stdout). -v, --version . -h, --help . (SYNTAX) BPF : A (accumulator) X X M[] " (scratch memory store)" 0 15 bpfc '' (opcodes)'' : o:16, jt:8, jf:8, k:32 o jt jf '' (true)'' '' (false)''. k . (load) (store) (branch) - (alu) (return) bpfc . bpfc . (unsigned) : ld 1, 2, 3, 4, 10 (word) A ldi 4 A ldh 1, 2 (half-word) A ldb 1, 2 A ldx 3, 4, 5, 10 X ldxi 4 X ldxb 5 X st 3 A []M stx 3 X []M jmp 6 ja 6 jeq 7, 8 k == A jneq 8 k != A jne 8 k != A jlt 8 k < A jle 8 k <= A jgt 7, 8 k > A jge 7, 8 k >= A jset 7, 8 k & A add 0, 4 A + sub 0, 4 A - mul 0, 4 A * div 0, 4 A / mod 0, 4 A % neg 0, 4 !A and 0, 4 A & or 0, 4 A | xor 0, 4 A ^ lsh 0, 4 A << rsh 0, 4 A >> tax A X txa X A ret 4, 9 0 x/%x X 1 [k] BHW k 2 [x + k] BHW X + k 3 M[k] k []M 4 #k (literal) k 5 4*([k]&0xf) k 6 L L 7 #k,Lt,Lf Lt Lf 8 #k,Lt Lt 9 a/%a A 10 extension BPF ( ) ( ) #len, len, #pktlen, pktlen (skb->len) #pto, pto, #proto, proto (skb->protocol) #type, type (**) (skb->pkt_type) #poff, poff (payload) #ifx, ifx, #ifidx, ifidx (skb->dev->ifindex) #nla, nla Netlink X A #nlan, nlan Netlink X A #mark, mark (skb->mark) #que, que, #queue, queue, #Q, Q (skb->queue_mapping) #hat, hat, #hatype, hatype (**) (skb->dev->type) #rxh, rxh, #rxhash, rxhash (skb->rxhash) #cpu, cpu (raw_smp_processor_id()) #vlant, vlant, #vlan_tci, vlan_tci VLAN TCI (vlan_tx_tag_get(skb)) #vlanp, vlanp VLAN (vlan_tx_tag_present(skb)) (**) #type, type 0 - / (to us / host) 1 - / (to all / broadcast) 2 - / (to group / multicast) 3 - ( / promiscuous mode) 4 - #hat, hat, #hatype, hatype 1 - 8 - APPLEtalk 19 - ATM 24 - IEEE 1394 IPv4 - RFC 2734 32 - (InfiniBand) 768 - IPIP 769 - IP6IP6 772 - Loopback 778 - GRE IP 783 - Linux- IrDA 801 - IEEE 802.11 802 - IEEE 802.11 + Prism2 803 - IEEE 802.11 + radiotap 823 - GRE IP6 824 - Netlink [...] include/uapi/linux/if_arp.h BPF . () bpfc : 1. C: /* */ 2. : ; : BHW: (byte) (half-word) (word) (SOURCE EXAMPLES) bpfc : ( ): ld poff ret a ARP: ldh [12] jne #0x806, drop ret #-1 drop: ret #0 IPv4 TCP: ldh [12] jne #0x800, drop ldb [23] jneq #6, drop ret #-1 drop: ret #0 SSH IPv4 TCP: ldh [12] jne #0x800, drop ldb [23] jneq #6, drop ldh [20] jset #0x1fff, drop ldxb 4 * ([14] & 0xf) ldh [x + 14] jeq #0x16, pass ldh [x + 16] jne #0x16, drop pass: ret #-1 drop: ret #0 seccomp-BPF x86_64 : ld [4] /* offsetof(struct seccomp_data, arch) */ jne #0xc000003e, bad /* AUDIT_ARCH_X86_64 */ ld [0] /* offsetof(struct seccomp_data, nr) */ jeq #15, good /* __NR_rt_sigreturn */ jeq #231, good /* __NR_exit_group */ jeq #60, good /* __NR_exit */ jeq #0, good /* __NR_read */ jeq #1, good /* __NR_write */ jeq #5, good /* __NR_fstat */ jeq #9, good /* __NR_mmap */ jeq #14, good /* __NR_rt_sigprocmask */ jeq #13, good /* __NR_rt_sigaction */ jeq #35, good /* __NR_nanosleep */ bad: ret #0 /* SECCOMP_RET_KILL */ good: ret #0x7fff0000 /* SECCOMP_RET_ALLOW */ VLAN ( ): ld vlanp jeq #0, drop ret #-1 drop: ret #0 VLAN 10 ( ): ld vlant jneq #10, drop ret #-1 drop: ret #0 VLAN : ld vlanp jeq #0, drop ld vlant jneq #10, drop ret #-1 drop: ret #0 rtnetlink: ldh #proto /* A = skb->protocol */ jneq #0, skip /* NETLINK_ROUTE */ ldb [4] /* A = nlmsg_type */ jneq #0x10, skip /* type == RTNL_NEWLINK */ ldx #16 /* X = offset(ifinfomsg) */ ldb [x + 4] /* offset(ifi_index) */ jneq #0x3, skip /* ifindex == 3 */ ld #32 /* A = len(nlmsghdr) + len(ifinfomsg), */ ldx #16 /* X = IFLA_OPERSTATE */ ld #nla /* A = offset(IFLA_OPERSTATE) */ jeq #0, skip tax ldb [x + 4] /* A = value(IFLA_OPERSTATE) */ jneq #0x6, skip /* UP */ ret #-1 skip: ret #0 (USAGE EXAMPLES) bpfc fubar ''fubar'' BPF. . bpfc -f xt_bpf -b -p -i fubar, resp. iptables -A INPUT -m bpf --bytecode `bpfc -f xt_bpf -i fubar` -j LOG ''fubar'' BPF xt_bpf netfilter. ''fubar'' C bpfc . cat fubar | bpfc - bpfc . bpfc foo > bar && netsniff-ng -f bar ... foo bpfc bar netsniff-ng(8) -f . bpfc -f tcpdump -i fubar fubar ''tcpdump -ddd''. (LEGAL) bpfc . (GNU GPL) . (HISTORY) bpfc Daniel Borkmann netsniff-ng . Tobias Klauser Daniel Borkmann . (SEE ALSO) netsniff-ng(8), trafgen(8), mausezahn(8), ifpps(8), flowtop(8), astraceroute(8), curvetun(8) (AUTHOR) Daniel Borkmann . (COLOPHON) netsniff-ng . http://netsniff- ng.org/ . Linux 03 March 2013 BPFC(8)