From patchwork Sun Dec 9 22:52:38 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Willem de Bruijn X-Patchwork-Id: 204793 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 12FB82C023B for ; Mon, 10 Dec 2012 09:53:02 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934579Ab2LIWws (ORCPT ); Sun, 9 Dec 2012 17:52:48 -0500 Received: from mail-bk0-f74.google.com ([209.85.214.74]:35489 "EHLO mail-bk0-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934578Ab2LIWwr (ORCPT ); Sun, 9 Dec 2012 17:52:47 -0500 Received: by mail-bk0-f74.google.com with SMTP id je9so133988bkc.1 for ; Sun, 09 Dec 2012 14:52:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=5i91a3QNqNDnHUFYAor7YOvp61uXoOQjqReoKzfPznU=; b=Xjg0ZpcqsWwzNq7HjrI9OEvFArSq2gl+IIUrOXSKBPxkh3MFles2pb3mjjZPBjKq2H dzOzJJa3k6PxkdJMVhQ3y14dDID/xPMQJmGvUWVfaQ0ooYC0a5YRAEIzS4FY38J9W+lO LCPVyC6EO7yI3e3XkwGrSqsfKZUgc+opJEoZaAnMKm9hWrCiQlwfBK+WSk/3xjvIP60z yurnmek428PSjpVkn3v+pU/iSjoKEFBS9kMpbVelZ+brqI+MccIIcJN1dZV91v8DBYnV SzztVePL6aIaQ8KEi+exgCdW+WR7ffpE7u8saZ1kwfVKxOniiMtun2Zco5QNJiWJyNVJ Mq9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :x-gm-message-state; bh=5i91a3QNqNDnHUFYAor7YOvp61uXoOQjqReoKzfPznU=; b=RYzDvv64BhDUrkSCLJ9NVq+rYoVuugOJWEu8rP/SSeoSOnaniY4F6b84e4ixfjPnmz RbwBrZv56Z94v+rwKgIu8dvmsjyomeRrwY2m9S/rS12Va6oLs+07nNQ8ZJTFHvdhiWhk T9IIisIAOz/nwdQ7ipsDAUH37da0eJjLSGcpyPldjirlZNRpGak6tzfjmeW5a96Qexyn sP450LRd9gKCMjU3sYox7zrKsJnL51K4pve8WuvKohnslX97qWXmEIPY+iczHEmIGgW5 OFisBKpBURBdmkzeYK8bI5WLxq5bGTt/EhXXajziJxhj/KaOPnO4LrpZ/dMAmaPWWoC0 4ybg== Received: by 10.14.208.198 with SMTP id q46mr11439095eeo.0.1355093565823; Sun, 09 Dec 2012 14:52:45 -0800 (PST) Received: from hpza10.eem.corp.google.com ([74.125.121.33]) by gmr-mx.google.com with ESMTPS id z44si7733483een.0.2012.12.09.14.52.45 (version=TLSv1/SSLv3 cipher=AES128-SHA); Sun, 09 Dec 2012 14:52:45 -0800 (PST) Received: from gopher.nyc.corp.google.com (gopher.nyc.corp.google.com [172.26.106.37]) by hpza10.eem.corp.google.com (Postfix) with ESMTP id 8624420004E; Sun, 9 Dec 2012 14:52:45 -0800 (PST) Received: by gopher.nyc.corp.google.com (Postfix, from userid 29878) id D28411E0BF5; Sun, 9 Dec 2012 17:52:44 -0500 (EST) From: Willem de Bruijn To: netfilter-devel@vger.kernel.org, pablo@netfilter.org Cc: Willem de Bruijn Subject: [PATCH] [rfc] a bpf compilation tool for xt_bpf Date: Sun, 9 Dec 2012 17:52:38 -0500 Message-Id: <1355093558-27931-1-git-send-email-willemb@google.com> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1355082854-31853-1-git-send-email-willemb@google.com> References: <1355082854-31853-1-git-send-email-willemb@google.com> X-Gm-Message-State: ALoCoQmMbdK3nYM3e+qwtjhnc56ZYFO7XbV1OylznfN3ciaJWonIna7Xxugi46666ZyTpvzgyCi9br/rRkBRF8ao8HqPM84V8loRtpTx5EkXNTQBChpO6SN1KlqNEu2aW3Qbrf+1yNbHZI9787SHcINeMUh3NFycSqYikGIRQvm4jabXUSZiYi2GP8zW73P8u/TNVemK8zhOOIYNCGXXvsmZQhu/Ecxs0Q== Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org An example tool to convert textual BPF into the instructions acceptable by xt_bpf. This is mostly boilerplate around existing pcap calls. I do not intend this for submission as is. It adds a dependency on pcap, which would at the least have to be optional and detected by autoconf. --- utils/Makefile.am | 3 ++- utils/bpf_compile.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletions(-) create mode 100644 utils/bpf_compile.c diff --git a/utils/Makefile.am b/utils/Makefile.am index f1bbfc5..4c67292 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -4,7 +4,8 @@ AM_CFLAGS = ${regular_CFLAGS} AM_CPPFLAGS = ${regular_CPPFLAGS} -I${top_builddir}/include \ -I${top_srcdir}/include ${libnfnetlink_CFLAGS} -sbin_PROGRAMS = nfnl_osf +sbin_PROGRAMS = nfnl_osf bpf_compile pkgdata_DATA = pf.os nfnl_osf_LDADD = -lnfnetlink +bpf_compile_LDADD = -lpcap diff --git a/utils/bpf_compile.c b/utils/bpf_compile.c new file mode 100644 index 0000000..62f7bc8 --- /dev/null +++ b/utils/bpf_compile.c @@ -0,0 +1,47 @@ +/* + * BPF program compilation tool + * + * Written by Willem de Bruijn (willemb@google.com) + * Copyright Google, Inc. 2012 + * Licensed under the GNU General Public License version 2 (GPLv2) +*/ + +#include +#include + +int main(int argc, char **argv) +{ + struct bpf_program program; + struct bpf_insn *ins; + int i, dlt = DLT_RAW; + + if (argc < 2 || argc > 3) { + fprintf(stderr, "Usage: %s [linktype] \n" + " linktype is one of EN10MB, RAW, ...\n" + " program must be one parameter\n", + argv[0]); + return 1; + } + + if (argc == 3) { + dlt = pcap_datalink_name_to_val(argv[1]); + if (dlt == -1) { + fprintf(stderr, "Unknown datalinktype: %s\n", argv[1]); + return 1; + } + } + + printf("Using datalinktype %s\n", pcap_datalink_val_to_name(dlt)); + if (pcap_compile_nopcap(65535, dlt, &program, argv[argc - 1], 1, 0)) { + fprintf(stderr, "Compilation error\n"); + return 1; + } + + printf("%d\n", program.bf_len); + ins = program.bf_insns; + for (i = 0; i < program.bf_len; ++ins, ++i) + printf("%u %u %u %u\n", ins->code, ins->jt, ins->jf, ins->k); + + return 0; +} +