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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/module.h>
13 #include <linux/syscalls.h>
14 #include <linux/skbuff.h>
15 #include <linux/filter.h>
16 #include <linux/bpf.h>
18 #include <linux/netfilter/xt_bpf.h>
19 #include <linux/netfilter/x_tables.h>
21 MODULE_AUTHOR("Willem de Bruijn <willemb@google.com>");
22 MODULE_DESCRIPTION("Xtables: BPF filter match");
23 MODULE_LICENSE("GPL");
24 MODULE_ALIAS("ipt_bpf");
25 MODULE_ALIAS("ip6t_bpf");
27 static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len,
28 struct bpf_prog **ret)
30 struct sock_fprog_kern program;
32 if (len > XT_BPF_MAX_NUM_INSTR)
36 program.filter = insns;
38 if (bpf_prog_create(ret, &program)) {
39 pr_info_ratelimited("check failed: parse error\n");
46 static int __bpf_mt_check_fd(int fd, struct bpf_prog **ret)
48 struct bpf_prog *prog;
50 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
58 static int __bpf_mt_check_path(const char *path, struct bpf_prog **ret)
60 if (strnlen(path, XT_BPF_PATH_MAX) == XT_BPF_PATH_MAX)
63 *ret = bpf_prog_get_type_path(path, BPF_PROG_TYPE_SOCKET_FILTER);
64 return PTR_ERR_OR_ZERO(*ret);
67 static int bpf_mt_check(const struct xt_mtchk_param *par)
69 struct xt_bpf_info *info = par->matchinfo;
71 return __bpf_mt_check_bytecode(info->bpf_program,
72 info->bpf_program_num_elem,
76 static int bpf_mt_check_v1(const struct xt_mtchk_param *par)
78 struct xt_bpf_info_v1 *info = par->matchinfo;
80 if (info->mode == XT_BPF_MODE_BYTECODE)
81 return __bpf_mt_check_bytecode(info->bpf_program,
82 info->bpf_program_num_elem,
84 else if (info->mode == XT_BPF_MODE_FD_ELF)
85 return __bpf_mt_check_fd(info->fd, &info->filter);
86 else if (info->mode == XT_BPF_MODE_PATH_PINNED)
87 return __bpf_mt_check_path(info->path, &info->filter);
92 static bool bpf_mt(const struct sk_buff *skb, struct xt_action_param *par)
94 const struct xt_bpf_info *info = par->matchinfo;
96 return BPF_PROG_RUN(info->filter, skb);
99 static bool bpf_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
101 const struct xt_bpf_info_v1 *info = par->matchinfo;
103 return !!bpf_prog_run_save_cb(info->filter, (struct sk_buff *) skb);
106 static void bpf_mt_destroy(const struct xt_mtdtor_param *par)
108 const struct xt_bpf_info *info = par->matchinfo;
110 bpf_prog_destroy(info->filter);
113 static void bpf_mt_destroy_v1(const struct xt_mtdtor_param *par)
115 const struct xt_bpf_info_v1 *info = par->matchinfo;
117 bpf_prog_destroy(info->filter);
120 static struct xt_match bpf_mt_reg[] __read_mostly = {
124 .family = NFPROTO_UNSPEC,
125 .checkentry = bpf_mt_check,
127 .destroy = bpf_mt_destroy,
128 .matchsize = sizeof(struct xt_bpf_info),
129 .usersize = offsetof(struct xt_bpf_info, filter),
135 .family = NFPROTO_UNSPEC,
136 .checkentry = bpf_mt_check_v1,
138 .destroy = bpf_mt_destroy_v1,
139 .matchsize = sizeof(struct xt_bpf_info_v1),
140 .usersize = offsetof(struct xt_bpf_info_v1, filter),
145 static int __init bpf_mt_init(void)
147 return xt_register_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
150 static void __exit bpf_mt_exit(void)
152 xt_unregister_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
155 module_init(bpf_mt_init);
156 module_exit(bpf_mt_exit);