1 /* Xtables module to match packets using a BPF filter.
2 * Copyright 2013 Google Inc.
3 * Written by Willem de Bruijn <willemb@google.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/module.h>
11 #include <linux/syscalls.h>
12 #include <linux/skbuff.h>
13 #include <linux/filter.h>
14 #include <linux/bpf.h>
16 #include <linux/netfilter/xt_bpf.h>
17 #include <linux/netfilter/x_tables.h>
19 MODULE_AUTHOR("Willem de Bruijn <willemb@google.com>");
20 MODULE_DESCRIPTION("Xtables: BPF filter match");
21 MODULE_LICENSE("GPL");
22 MODULE_ALIAS("ipt_bpf");
23 MODULE_ALIAS("ip6t_bpf");
25 static int __bpf_mt_check_bytecode(struct sock_filter
*insns
, __u16 len
,
26 struct bpf_prog
**ret
)
28 struct sock_fprog_kern program
;
30 if (len
> XT_BPF_MAX_NUM_INSTR
)
34 program
.filter
= insns
;
36 if (bpf_prog_create(ret
, &program
)) {
37 pr_info("bpf: check failed: parse error\n");
44 static int __bpf_mt_check_fd(int fd
, struct bpf_prog
**ret
)
46 struct bpf_prog
*prog
;
48 prog
= bpf_prog_get_type(fd
, BPF_PROG_TYPE_SOCKET_FILTER
);
56 static int __bpf_mt_check_path(const char *path
, struct bpf_prog
**ret
)
58 mm_segment_t oldfs
= get_fs();
61 if (strnlen(path
, XT_BPF_PATH_MAX
) == XT_BPF_PATH_MAX
)
65 fd
= bpf_obj_get_user(path
, 0);
70 retval
= __bpf_mt_check_fd(fd
, ret
);
75 static int bpf_mt_check(const struct xt_mtchk_param
*par
)
77 struct xt_bpf_info
*info
= par
->matchinfo
;
79 return __bpf_mt_check_bytecode(info
->bpf_program
,
80 info
->bpf_program_num_elem
,
84 static int bpf_mt_check_v1(const struct xt_mtchk_param
*par
)
86 struct xt_bpf_info_v1
*info
= par
->matchinfo
;
88 if (info
->mode
== XT_BPF_MODE_BYTECODE
)
89 return __bpf_mt_check_bytecode(info
->bpf_program
,
90 info
->bpf_program_num_elem
,
92 else if (info
->mode
== XT_BPF_MODE_FD_ELF
)
93 return __bpf_mt_check_fd(info
->fd
, &info
->filter
);
94 else if (info
->mode
== XT_BPF_MODE_PATH_PINNED
)
95 return __bpf_mt_check_path(info
->path
, &info
->filter
);
100 static bool bpf_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
102 const struct xt_bpf_info
*info
= par
->matchinfo
;
104 return BPF_PROG_RUN(info
->filter
, skb
);
107 static bool bpf_mt_v1(const struct sk_buff
*skb
, struct xt_action_param
*par
)
109 const struct xt_bpf_info_v1
*info
= par
->matchinfo
;
111 return !!bpf_prog_run_save_cb(info
->filter
, (struct sk_buff
*) skb
);
114 static void bpf_mt_destroy(const struct xt_mtdtor_param
*par
)
116 const struct xt_bpf_info
*info
= par
->matchinfo
;
118 bpf_prog_destroy(info
->filter
);
121 static void bpf_mt_destroy_v1(const struct xt_mtdtor_param
*par
)
123 const struct xt_bpf_info_v1
*info
= par
->matchinfo
;
125 bpf_prog_destroy(info
->filter
);
128 static struct xt_match bpf_mt_reg
[] __read_mostly
= {
132 .family
= NFPROTO_UNSPEC
,
133 .checkentry
= bpf_mt_check
,
135 .destroy
= bpf_mt_destroy
,
136 .matchsize
= sizeof(struct xt_bpf_info
),
137 .usersize
= offsetof(struct xt_bpf_info
, filter
),
143 .family
= NFPROTO_UNSPEC
,
144 .checkentry
= bpf_mt_check_v1
,
146 .destroy
= bpf_mt_destroy_v1
,
147 .matchsize
= sizeof(struct xt_bpf_info_v1
),
148 .usersize
= offsetof(struct xt_bpf_info_v1
, filter
),
153 static int __init
bpf_mt_init(void)
155 return xt_register_matches(bpf_mt_reg
, ARRAY_SIZE(bpf_mt_reg
));
158 static void __exit
bpf_mt_exit(void)
160 xt_unregister_matches(bpf_mt_reg
, ARRAY_SIZE(bpf_mt_reg
));
163 module_init(bpf_mt_init
);
164 module_exit(bpf_mt_exit
);