Skip to content

Commit

Permalink
net: filter: split 'struct sk_filter' into socket and bpf parts
Browse files Browse the repository at this point in the history
clean up names related to socket filtering and bpf in the following way:
- everything that deals with sockets keeps 'sk_*' prefix
- everything that is pure BPF is changed to 'bpf_*' prefix

split 'struct sk_filter' into
struct sk_filter {
	atomic_t        refcnt;
	struct rcu_head rcu;
	struct bpf_prog *prog;
};
and
struct bpf_prog {
        u32                     jited:1,
                                len:31;
        struct sock_fprog_kern  *orig_prog;
        unsigned int            (*bpf_func)(const struct sk_buff *skb,
                                            const struct bpf_insn *filter);
        union {
                struct sock_filter      insns[0];
                struct bpf_insn         insnsi[0];
                struct work_struct      work;
        };
};
so that 'struct bpf_prog' can be used independent of sockets and cleans up
'unattached' bpf use cases

split SK_RUN_FILTER macro into:
    SK_RUN_FILTER to be used with 'struct sk_filter *' and
    BPF_PROG_RUN to be used with 'struct bpf_prog *'

__sk_filter_release(struct sk_filter *) gains
__bpf_prog_release(struct bpf_prog *) helper function

also perform related renames for the functions that work
with 'struct bpf_prog *', since they're on the same lines:

sk_filter_size -> bpf_prog_size
sk_filter_select_runtime -> bpf_prog_select_runtime
sk_filter_free -> bpf_prog_free
sk_unattached_filter_create -> bpf_prog_create
sk_unattached_filter_destroy -> bpf_prog_destroy
sk_store_orig_filter -> bpf_prog_store_orig_filter
sk_release_orig_filter -> bpf_release_orig_filter
__sk_migrate_filter -> bpf_migrate_filter
__sk_prepare_filter -> bpf_prepare_filter

API for attaching classic BPF to a socket stays the same:
sk_attach_filter(prog, struct sock *)/sk_detach_filter(struct sock *)
and SK_RUN_FILTER(struct sk_filter *, ctx) to execute a program
which is used by sockets, tun, af_packet

API for 'unattached' BPF programs becomes:
bpf_prog_create(struct bpf_prog **)/bpf_prog_destroy(struct bpf_prog *)
and BPF_PROG_RUN(struct bpf_prog *, ctx) to execute a program
which is used by isdn, ppp, team, seccomp, ptp, xt_bpf, cls_bpf, test_bpf

Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Alexei Starovoitov authored and David S. Miller committed Aug 2, 2014
1 parent 8fb575c commit 7ae457c
Show file tree
Hide file tree
Showing 21 changed files with 183 additions and 169 deletions.
10 changes: 5 additions & 5 deletions Documentation/networking/filter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,11 @@ team driver's classifier for its load-balancing mode, netfilter's xt_bpf
extension, PTP dissector/classifier, and much more. They are all internally
converted by the kernel into the new instruction set representation and run
in the eBPF interpreter. For in-kernel handlers, this all works transparently
by using sk_unattached_filter_create() for setting up the filter, resp.
sk_unattached_filter_destroy() for destroying it. The macro
SK_RUN_FILTER(filter, ctx) transparently invokes eBPF interpreter or JITed
code to run the filter. 'filter' is a pointer to struct sk_filter that we
got from sk_unattached_filter_create(), and 'ctx' the given context (e.g.
by using bpf_prog_create() for setting up the filter, resp.
bpf_prog_destroy() for destroying it. The macro
BPF_PROG_RUN(filter, ctx) transparently invokes eBPF interpreter or JITed
code to run the filter. 'filter' is a pointer to struct bpf_prog that we
got from bpf_prog_create(), and 'ctx' the given context (e.g.
skb pointer). All constraints and restrictions from bpf_check_classic() apply
before a conversion to the new layout is being done behind the scenes!

Expand Down
8 changes: 4 additions & 4 deletions arch/arm/net/bpf_jit_32.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#define FLAG_NEED_X_RESET (1 << 0)

struct jit_ctx {
const struct sk_filter *skf;
const struct bpf_prog *skf;
unsigned idx;
unsigned prologue_bytes;
int ret0_fp_idx;
Expand Down Expand Up @@ -465,7 +465,7 @@ static inline void update_on_xread(struct jit_ctx *ctx)
static int build_body(struct jit_ctx *ctx)
{
void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
const struct sk_filter *prog = ctx->skf;
const struct bpf_prog *prog = ctx->skf;
const struct sock_filter *inst;
unsigned i, load_order, off, condt;
int imm12;
Expand Down Expand Up @@ -857,7 +857,7 @@ static int build_body(struct jit_ctx *ctx)
}


void bpf_jit_compile(struct sk_filter *fp)
void bpf_jit_compile(struct bpf_prog *fp)
{
struct jit_ctx ctx;
unsigned tmp_idx;
Expand Down Expand Up @@ -926,7 +926,7 @@ void bpf_jit_compile(struct sk_filter *fp)
return;
}

void bpf_jit_free(struct sk_filter *fp)
void bpf_jit_free(struct bpf_prog *fp)
{
if (fp->jited)
module_free(NULL, fp->bpf_func);
Expand Down
8 changes: 4 additions & 4 deletions arch/mips/net/bpf_jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
* @target: Memory location for the compiled filter
*/
struct jit_ctx {
const struct sk_filter *skf;
const struct bpf_prog *skf;
unsigned int prologue_bytes;
u32 idx;
u32 flags;
Expand Down Expand Up @@ -789,7 +789,7 @@ static int pkt_type_offset(void)
static int build_body(struct jit_ctx *ctx)
{
void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
const struct sk_filter *prog = ctx->skf;
const struct bpf_prog *prog = ctx->skf;
const struct sock_filter *inst;
unsigned int i, off, load_order, condt;
u32 k, b_off __maybe_unused;
Expand Down Expand Up @@ -1369,7 +1369,7 @@ static int build_body(struct jit_ctx *ctx)

int bpf_jit_enable __read_mostly;

void bpf_jit_compile(struct sk_filter *fp)
void bpf_jit_compile(struct bpf_prog *fp)
{
struct jit_ctx ctx;
unsigned int alloc_size, tmp_idx;
Expand Down Expand Up @@ -1423,7 +1423,7 @@ void bpf_jit_compile(struct sk_filter *fp)
kfree(ctx.offsets);
}

void bpf_jit_free(struct sk_filter *fp)
void bpf_jit_free(struct bpf_prog *fp)
{
if (fp->jited)
module_free(NULL, fp->bpf_func);
Expand Down
8 changes: 4 additions & 4 deletions arch/powerpc/net/bpf_jit_comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static inline void bpf_flush_icache(void *start, void *end)
flush_icache_range((unsigned long)start, (unsigned long)end);
}

static void bpf_jit_build_prologue(struct sk_filter *fp, u32 *image,
static void bpf_jit_build_prologue(struct bpf_prog *fp, u32 *image,
struct codegen_context *ctx)
{
int i;
Expand Down Expand Up @@ -121,7 +121,7 @@ static void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)

/* Assemble the body code between the prologue & epilogue. */
static int bpf_jit_build_body(struct sk_filter *fp, u32 *image,
static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
struct codegen_context *ctx,
unsigned int *addrs)
{
Expand Down Expand Up @@ -569,7 +569,7 @@ static int bpf_jit_build_body(struct sk_filter *fp, u32 *image,
return 0;
}

void bpf_jit_compile(struct sk_filter *fp)
void bpf_jit_compile(struct bpf_prog *fp)
{
unsigned int proglen;
unsigned int alloclen;
Expand Down Expand Up @@ -693,7 +693,7 @@ void bpf_jit_compile(struct sk_filter *fp)
return;
}

void bpf_jit_free(struct sk_filter *fp)
void bpf_jit_free(struct bpf_prog *fp)
{
if (fp->jited)
module_free(NULL, fp->bpf_func);
Expand Down
4 changes: 2 additions & 2 deletions arch/s390/net/bpf_jit_comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ static struct bpf_binary_header *bpf_alloc_binary(unsigned int bpfsize,
return header;
}

void bpf_jit_compile(struct sk_filter *fp)
void bpf_jit_compile(struct bpf_prog *fp)
{
struct bpf_binary_header *header = NULL;
unsigned long size, prg_len, lit_len;
Expand Down Expand Up @@ -875,7 +875,7 @@ void bpf_jit_compile(struct sk_filter *fp)
kfree(addrs);
}

void bpf_jit_free(struct sk_filter *fp)
void bpf_jit_free(struct bpf_prog *fp)
{
unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
struct bpf_binary_header *header = (void *)addr;
Expand Down
4 changes: 2 additions & 2 deletions arch/sparc/net/bpf_jit_comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ do { *prog++ = BR_OPC | WDISP22(OFF); \
* emit_jump() calls with adjusted offsets.
*/

void bpf_jit_compile(struct sk_filter *fp)
void bpf_jit_compile(struct bpf_prog *fp)
{
unsigned int cleanup_addr, proglen, oldproglen = 0;
u32 temp[8], *prog, *func, seen = 0, pass;
Expand Down Expand Up @@ -808,7 +808,7 @@ cond_branch: f_offset = addrs[i + filter[i].jf];
return;
}

void bpf_jit_free(struct sk_filter *fp)
void bpf_jit_free(struct bpf_prog *fp)
{
if (fp->jited)
module_free(NULL, fp->bpf_func);
Expand Down
12 changes: 6 additions & 6 deletions arch/x86/net/bpf_jit_comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ struct jit_context {
bool seen_ld_abs;
};

static int do_jit(struct sk_filter *bpf_prog, int *addrs, u8 *image,
static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
int oldproglen, struct jit_context *ctx)
{
struct bpf_insn *insn = bpf_prog->insnsi;
Expand Down Expand Up @@ -841,7 +841,7 @@ common_load: ctx->seen_ld_abs = true;
/* By design x64 JIT should support all BPF instructions
* This error will be seen if new instruction was added
* to interpreter, but not to JIT
* or if there is junk in sk_filter
* or if there is junk in bpf_prog
*/
pr_err("bpf_jit: unknown opcode %02x\n", insn->code);
return -EINVAL;
Expand All @@ -862,11 +862,11 @@ common_load: ctx->seen_ld_abs = true;
return proglen;
}

void bpf_jit_compile(struct sk_filter *prog)
void bpf_jit_compile(struct bpf_prog *prog)
{
}

void bpf_int_jit_compile(struct sk_filter *prog)
void bpf_int_jit_compile(struct bpf_prog *prog)
{
struct bpf_binary_header *header = NULL;
int proglen, oldproglen = 0;
Expand Down Expand Up @@ -932,7 +932,7 @@ void bpf_int_jit_compile(struct sk_filter *prog)

static void bpf_jit_free_deferred(struct work_struct *work)
{
struct sk_filter *fp = container_of(work, struct sk_filter, work);
struct bpf_prog *fp = container_of(work, struct bpf_prog, work);
unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
struct bpf_binary_header *header = (void *)addr;

Expand All @@ -941,7 +941,7 @@ static void bpf_jit_free_deferred(struct work_struct *work)
kfree(fp);
}

void bpf_jit_free(struct sk_filter *fp)
void bpf_jit_free(struct bpf_prog *fp)
{
if (fp->jited) {
INIT_WORK(&fp->work, bpf_jit_free_deferred);
Expand Down
26 changes: 12 additions & 14 deletions drivers/isdn/i4l/isdn_ppp.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,12 @@ isdn_ppp_release(int min, struct file *file)
#endif
#ifdef CONFIG_IPPP_FILTER
if (is->pass_filter) {
sk_unattached_filter_destroy(is->pass_filter);
bpf_prog_destroy(is->pass_filter);
is->pass_filter = NULL;
}

if (is->active_filter) {
sk_unattached_filter_destroy(is->active_filter);
bpf_prog_destroy(is->active_filter);
is->active_filter = NULL;
}
#endif
Expand Down Expand Up @@ -639,12 +639,11 @@ isdn_ppp_ioctl(int min, struct file *file, unsigned int cmd, unsigned long arg)
fprog.filter = code;

if (is->pass_filter) {
sk_unattached_filter_destroy(is->pass_filter);
bpf_prog_destroy(is->pass_filter);
is->pass_filter = NULL;
}
if (fprog.filter != NULL)
err = sk_unattached_filter_create(&is->pass_filter,
&fprog);
err = bpf_prog_create(&is->pass_filter, &fprog);
else
err = 0;
kfree(code);
Expand All @@ -664,12 +663,11 @@ isdn_ppp_ioctl(int min, struct file *file, unsigned int cmd, unsigned long arg)
fprog.filter = code;

if (is->active_filter) {
sk_unattached_filter_destroy(is->active_filter);
bpf_prog_destroy(is->active_filter);
is->active_filter = NULL;
}
if (fprog.filter != NULL)
err = sk_unattached_filter_create(&is->active_filter,
&fprog);
err = bpf_prog_create(&is->active_filter, &fprog);
else
err = 0;
kfree(code);
Expand Down Expand Up @@ -1174,14 +1172,14 @@ isdn_ppp_push_higher(isdn_net_dev *net_dev, isdn_net_local *lp, struct sk_buff *
}

if (is->pass_filter
&& SK_RUN_FILTER(is->pass_filter, skb) == 0) {
&& BPF_PROG_RUN(is->pass_filter, skb) == 0) {
if (is->debug & 0x2)
printk(KERN_DEBUG "IPPP: inbound frame filtered.\n");
kfree_skb(skb);
return;
}
if (!(is->active_filter
&& SK_RUN_FILTER(is->active_filter, skb) == 0)) {
&& BPF_PROG_RUN(is->active_filter, skb) == 0)) {
if (is->debug & 0x2)
printk(KERN_DEBUG "IPPP: link-active filter: resetting huptimer.\n");
lp->huptimer = 0;
Expand Down Expand Up @@ -1320,14 +1318,14 @@ isdn_ppp_xmit(struct sk_buff *skb, struct net_device *netdev)
}

if (ipt->pass_filter
&& SK_RUN_FILTER(ipt->pass_filter, skb) == 0) {
&& BPF_PROG_RUN(ipt->pass_filter, skb) == 0) {
if (ipt->debug & 0x4)
printk(KERN_DEBUG "IPPP: outbound frame filtered.\n");
kfree_skb(skb);
goto unlock;
}
if (!(ipt->active_filter
&& SK_RUN_FILTER(ipt->active_filter, skb) == 0)) {
&& BPF_PROG_RUN(ipt->active_filter, skb) == 0)) {
if (ipt->debug & 0x4)
printk(KERN_DEBUG "IPPP: link-active filter: resetting huptimer.\n");
lp->huptimer = 0;
Expand Down Expand Up @@ -1517,9 +1515,9 @@ int isdn_ppp_autodial_filter(struct sk_buff *skb, isdn_net_local *lp)
}

drop |= is->pass_filter
&& SK_RUN_FILTER(is->pass_filter, skb) == 0;
&& BPF_PROG_RUN(is->pass_filter, skb) == 0;
drop |= is->active_filter
&& SK_RUN_FILTER(is->active_filter, skb) == 0;
&& BPF_PROG_RUN(is->active_filter, skb) == 0;

skb_push(skb, IPPP_MAX_HEADER - 4);
return drop;
Expand Down
28 changes: 14 additions & 14 deletions drivers/net/ppp/ppp_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ struct ppp {
struct sk_buff_head mrq; /* MP: receive reconstruction queue */
#endif /* CONFIG_PPP_MULTILINK */
#ifdef CONFIG_PPP_FILTER
struct sk_filter *pass_filter; /* filter for packets to pass */
struct sk_filter *active_filter;/* filter for pkts to reset idle */
struct bpf_prog *pass_filter; /* filter for packets to pass */
struct bpf_prog *active_filter; /* filter for pkts to reset idle */
#endif /* CONFIG_PPP_FILTER */
struct net *ppp_net; /* the net we belong to */
struct ppp_link_stats stats64; /* 64 bit network stats */
Expand Down Expand Up @@ -762,12 +762,12 @@ static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)

ppp_lock(ppp);
if (ppp->pass_filter) {
sk_unattached_filter_destroy(ppp->pass_filter);
bpf_prog_destroy(ppp->pass_filter);
ppp->pass_filter = NULL;
}
if (fprog.filter != NULL)
err = sk_unattached_filter_create(&ppp->pass_filter,
&fprog);
err = bpf_prog_create(&ppp->pass_filter,
&fprog);
else
err = 0;
kfree(code);
Expand All @@ -788,12 +788,12 @@ static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)

ppp_lock(ppp);
if (ppp->active_filter) {
sk_unattached_filter_destroy(ppp->active_filter);
bpf_prog_destroy(ppp->active_filter);
ppp->active_filter = NULL;
}
if (fprog.filter != NULL)
err = sk_unattached_filter_create(&ppp->active_filter,
&fprog);
err = bpf_prog_create(&ppp->active_filter,
&fprog);
else
err = 0;
kfree(code);
Expand Down Expand Up @@ -1205,7 +1205,7 @@ ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
a four-byte PPP header on each packet */
*skb_push(skb, 2) = 1;
if (ppp->pass_filter &&
SK_RUN_FILTER(ppp->pass_filter, skb) == 0) {
BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
if (ppp->debug & 1)
netdev_printk(KERN_DEBUG, ppp->dev,
"PPP: outbound frame "
Expand All @@ -1215,7 +1215,7 @@ ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
}
/* if this packet passes the active filter, record the time */
if (!(ppp->active_filter &&
SK_RUN_FILTER(ppp->active_filter, skb) == 0))
BPF_PROG_RUN(ppp->active_filter, skb) == 0))
ppp->last_xmit = jiffies;
skb_pull(skb, 2);
#else
Expand Down Expand Up @@ -1839,7 +1839,7 @@ ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)

*skb_push(skb, 2) = 0;
if (ppp->pass_filter &&
SK_RUN_FILTER(ppp->pass_filter, skb) == 0) {
BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
if (ppp->debug & 1)
netdev_printk(KERN_DEBUG, ppp->dev,
"PPP: inbound frame "
Expand All @@ -1848,7 +1848,7 @@ ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
return;
}
if (!(ppp->active_filter &&
SK_RUN_FILTER(ppp->active_filter, skb) == 0))
BPF_PROG_RUN(ppp->active_filter, skb) == 0))
ppp->last_recv = jiffies;
__skb_pull(skb, 2);
} else
Expand Down Expand Up @@ -2829,12 +2829,12 @@ static void ppp_destroy_interface(struct ppp *ppp)
#endif /* CONFIG_PPP_MULTILINK */
#ifdef CONFIG_PPP_FILTER
if (ppp->pass_filter) {
sk_unattached_filter_destroy(ppp->pass_filter);
bpf_prog_destroy(ppp->pass_filter);
ppp->pass_filter = NULL;
}

if (ppp->active_filter) {
sk_unattached_filter_destroy(ppp->active_filter);
bpf_prog_destroy(ppp->active_filter);
ppp->active_filter = NULL;
}
#endif /* CONFIG_PPP_FILTER */
Expand Down
Loading

0 comments on commit 7ae457c

Please sign in to comment.