0001
0002
0003
0004
0005
0006
0007 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0008
0009 #include <linux/module.h>
0010 #include <linux/syscalls.h>
0011 #include <linux/skbuff.h>
0012 #include <linux/filter.h>
0013 #include <linux/bpf.h>
0014
0015 #include <linux/netfilter/xt_bpf.h>
0016 #include <linux/netfilter/x_tables.h>
0017
0018 MODULE_AUTHOR("Willem de Bruijn <willemb@google.com>");
0019 MODULE_DESCRIPTION("Xtables: BPF filter match");
0020 MODULE_LICENSE("GPL");
0021 MODULE_ALIAS("ipt_bpf");
0022 MODULE_ALIAS("ip6t_bpf");
0023
0024 static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len,
0025 struct bpf_prog **ret)
0026 {
0027 struct sock_fprog_kern program;
0028
0029 if (len > XT_BPF_MAX_NUM_INSTR)
0030 return -EINVAL;
0031
0032 program.len = len;
0033 program.filter = insns;
0034
0035 if (bpf_prog_create(ret, &program)) {
0036 pr_info_ratelimited("check failed: parse error\n");
0037 return -EINVAL;
0038 }
0039
0040 return 0;
0041 }
0042
0043 static int __bpf_mt_check_fd(int fd, struct bpf_prog **ret)
0044 {
0045 struct bpf_prog *prog;
0046
0047 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
0048 if (IS_ERR(prog))
0049 return PTR_ERR(prog);
0050
0051 *ret = prog;
0052 return 0;
0053 }
0054
0055 static int __bpf_mt_check_path(const char *path, struct bpf_prog **ret)
0056 {
0057 if (strnlen(path, XT_BPF_PATH_MAX) == XT_BPF_PATH_MAX)
0058 return -EINVAL;
0059
0060 *ret = bpf_prog_get_type_path(path, BPF_PROG_TYPE_SOCKET_FILTER);
0061 return PTR_ERR_OR_ZERO(*ret);
0062 }
0063
0064 static int bpf_mt_check(const struct xt_mtchk_param *par)
0065 {
0066 struct xt_bpf_info *info = par->matchinfo;
0067
0068 return __bpf_mt_check_bytecode(info->bpf_program,
0069 info->bpf_program_num_elem,
0070 &info->filter);
0071 }
0072
0073 static int bpf_mt_check_v1(const struct xt_mtchk_param *par)
0074 {
0075 struct xt_bpf_info_v1 *info = par->matchinfo;
0076
0077 if (info->mode == XT_BPF_MODE_BYTECODE)
0078 return __bpf_mt_check_bytecode(info->bpf_program,
0079 info->bpf_program_num_elem,
0080 &info->filter);
0081 else if (info->mode == XT_BPF_MODE_FD_ELF)
0082 return __bpf_mt_check_fd(info->fd, &info->filter);
0083 else if (info->mode == XT_BPF_MODE_PATH_PINNED)
0084 return __bpf_mt_check_path(info->path, &info->filter);
0085 else
0086 return -EINVAL;
0087 }
0088
0089 static bool bpf_mt(const struct sk_buff *skb, struct xt_action_param *par)
0090 {
0091 const struct xt_bpf_info *info = par->matchinfo;
0092
0093 return bpf_prog_run(info->filter, skb);
0094 }
0095
0096 static bool bpf_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
0097 {
0098 const struct xt_bpf_info_v1 *info = par->matchinfo;
0099
0100 return !!bpf_prog_run_save_cb(info->filter, (struct sk_buff *) skb);
0101 }
0102
0103 static void bpf_mt_destroy(const struct xt_mtdtor_param *par)
0104 {
0105 const struct xt_bpf_info *info = par->matchinfo;
0106
0107 bpf_prog_destroy(info->filter);
0108 }
0109
0110 static void bpf_mt_destroy_v1(const struct xt_mtdtor_param *par)
0111 {
0112 const struct xt_bpf_info_v1 *info = par->matchinfo;
0113
0114 bpf_prog_destroy(info->filter);
0115 }
0116
0117 static struct xt_match bpf_mt_reg[] __read_mostly = {
0118 {
0119 .name = "bpf",
0120 .revision = 0,
0121 .family = NFPROTO_UNSPEC,
0122 .checkentry = bpf_mt_check,
0123 .match = bpf_mt,
0124 .destroy = bpf_mt_destroy,
0125 .matchsize = sizeof(struct xt_bpf_info),
0126 .usersize = offsetof(struct xt_bpf_info, filter),
0127 .me = THIS_MODULE,
0128 },
0129 {
0130 .name = "bpf",
0131 .revision = 1,
0132 .family = NFPROTO_UNSPEC,
0133 .checkentry = bpf_mt_check_v1,
0134 .match = bpf_mt_v1,
0135 .destroy = bpf_mt_destroy_v1,
0136 .matchsize = sizeof(struct xt_bpf_info_v1),
0137 .usersize = offsetof(struct xt_bpf_info_v1, filter),
0138 .me = THIS_MODULE,
0139 },
0140 };
0141
0142 static int __init bpf_mt_init(void)
0143 {
0144 return xt_register_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
0145 }
0146
0147 static void __exit bpf_mt_exit(void)
0148 {
0149 xt_unregister_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
0150 }
0151
0152 module_init(bpf_mt_init);
0153 module_exit(bpf_mt_exit);