blob: 6a091c14e25723f64087b8c3ddf24d89004044a1 [file] [log] [blame]
Willem de Bruijne6f30c72013-01-18 07:17:30 +00001/* Xtables module to match packets using a BPF filter.
2 * Copyright 2013 Google Inc.
3 * Written by Willem de Bruijn <willemb@google.com>
4 *
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.
8 */
9
10#include <linux/module.h>
Shmulik Ladkani7e3c72f2017-10-09 15:27:15 +030011#include <linux/syscalls.h>
Willem de Bruijne6f30c72013-01-18 07:17:30 +000012#include <linux/skbuff.h>
13#include <linux/filter.h>
Willem de Bruijneb8925e2016-12-06 16:25:02 -050014#include <linux/bpf.h>
Willem de Bruijne6f30c72013-01-18 07:17:30 +000015
16#include <linux/netfilter/xt_bpf.h>
17#include <linux/netfilter/x_tables.h>
18
19MODULE_AUTHOR("Willem de Bruijn <willemb@google.com>");
20MODULE_DESCRIPTION("Xtables: BPF filter match");
21MODULE_LICENSE("GPL");
22MODULE_ALIAS("ipt_bpf");
23MODULE_ALIAS("ip6t_bpf");
24
Willem de Bruijneb8925e2016-12-06 16:25:02 -050025static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len,
26 struct bpf_prog **ret)
Willem de Bruijne6f30c72013-01-18 07:17:30 +000027{
Daniel Borkmannb1fcd352014-05-23 18:43:58 +020028 struct sock_fprog_kern program;
Willem de Bruijne6f30c72013-01-18 07:17:30 +000029
Jann Horn52447002017-12-01 01:46:07 +010030 if (len > XT_BPF_MAX_NUM_INSTR)
31 return -EINVAL;
32
Willem de Bruijneb8925e2016-12-06 16:25:02 -050033 program.len = len;
34 program.filter = insns;
Daniel Borkmannb1fcd352014-05-23 18:43:58 +020035
Willem de Bruijneb8925e2016-12-06 16:25:02 -050036 if (bpf_prog_create(ret, &program)) {
Willem de Bruijne6f30c72013-01-18 07:17:30 +000037 pr_info("bpf: check failed: parse error\n");
38 return -EINVAL;
39 }
40
41 return 0;
42}
43
Willem de Bruijneb8925e2016-12-06 16:25:02 -050044static int __bpf_mt_check_fd(int fd, struct bpf_prog **ret)
45{
46 struct bpf_prog *prog;
47
48 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
49 if (IS_ERR(prog))
50 return PTR_ERR(prog);
51
52 *ret = prog;
53 return 0;
54}
55
Shmulik Ladkani7e3c72f2017-10-09 15:27:15 +030056static int __bpf_mt_check_path(const char *path, struct bpf_prog **ret)
57{
58 mm_segment_t oldfs = get_fs();
59 int retval, fd;
60
Jann Horn52447002017-12-01 01:46:07 +010061 if (strnlen(path, XT_BPF_PATH_MAX) == XT_BPF_PATH_MAX)
62 return -EINVAL;
63
Shmulik Ladkani7e3c72f2017-10-09 15:27:15 +030064 set_fs(KERNEL_DS);
65 fd = bpf_obj_get_user(path);
66 set_fs(oldfs);
67 if (fd < 0)
68 return fd;
69
70 retval = __bpf_mt_check_fd(fd, ret);
71 sys_close(fd);
72 return retval;
73}
74
Willem de Bruijneb8925e2016-12-06 16:25:02 -050075static int bpf_mt_check(const struct xt_mtchk_param *par)
76{
77 struct xt_bpf_info *info = par->matchinfo;
78
79 return __bpf_mt_check_bytecode(info->bpf_program,
80 info->bpf_program_num_elem,
81 &info->filter);
82}
83
84static int bpf_mt_check_v1(const struct xt_mtchk_param *par)
85{
86 struct xt_bpf_info_v1 *info = par->matchinfo;
87
88 if (info->mode == XT_BPF_MODE_BYTECODE)
89 return __bpf_mt_check_bytecode(info->bpf_program,
90 info->bpf_program_num_elem,
91 &info->filter);
Shmulik Ladkani7e3c72f2017-10-09 15:27:15 +030092 else if (info->mode == XT_BPF_MODE_FD_ELF)
Willem de Bruijneb8925e2016-12-06 16:25:02 -050093 return __bpf_mt_check_fd(info->fd, &info->filter);
Shmulik Ladkani7e3c72f2017-10-09 15:27:15 +030094 else if (info->mode == XT_BPF_MODE_PATH_PINNED)
95 return __bpf_mt_check_path(info->path, &info->filter);
Willem de Bruijneb8925e2016-12-06 16:25:02 -050096 else
97 return -EINVAL;
98}
99
Willem de Bruijne6f30c72013-01-18 07:17:30 +0000100static bool bpf_mt(const struct sk_buff *skb, struct xt_action_param *par)
101{
102 const struct xt_bpf_info *info = par->matchinfo;
103
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700104 return BPF_PROG_RUN(info->filter, skb);
Willem de Bruijne6f30c72013-01-18 07:17:30 +0000105}
106
Willem de Bruijneb8925e2016-12-06 16:25:02 -0500107static bool bpf_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
108{
109 const struct xt_bpf_info_v1 *info = par->matchinfo;
110
111 return !!bpf_prog_run_save_cb(info->filter, (struct sk_buff *) skb);
112}
113
Willem de Bruijne6f30c72013-01-18 07:17:30 +0000114static void bpf_mt_destroy(const struct xt_mtdtor_param *par)
115{
116 const struct xt_bpf_info *info = par->matchinfo;
Willem de Bruijneb8925e2016-12-06 16:25:02 -0500117
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700118 bpf_prog_destroy(info->filter);
Willem de Bruijne6f30c72013-01-18 07:17:30 +0000119}
120
Willem de Bruijneb8925e2016-12-06 16:25:02 -0500121static void bpf_mt_destroy_v1(const struct xt_mtdtor_param *par)
122{
123 const struct xt_bpf_info_v1 *info = par->matchinfo;
124
125 bpf_prog_destroy(info->filter);
126}
127
128static struct xt_match bpf_mt_reg[] __read_mostly = {
129 {
130 .name = "bpf",
131 .revision = 0,
132 .family = NFPROTO_UNSPEC,
133 .checkentry = bpf_mt_check,
134 .match = bpf_mt,
135 .destroy = bpf_mt_destroy,
136 .matchsize = sizeof(struct xt_bpf_info),
137 .me = THIS_MODULE,
138 },
139 {
140 .name = "bpf",
141 .revision = 1,
142 .family = NFPROTO_UNSPEC,
143 .checkentry = bpf_mt_check_v1,
144 .match = bpf_mt_v1,
145 .destroy = bpf_mt_destroy_v1,
146 .matchsize = sizeof(struct xt_bpf_info_v1),
147 .me = THIS_MODULE,
148 },
Willem de Bruijne6f30c72013-01-18 07:17:30 +0000149};
150
151static int __init bpf_mt_init(void)
152{
Willem de Bruijneb8925e2016-12-06 16:25:02 -0500153 return xt_register_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
Willem de Bruijne6f30c72013-01-18 07:17:30 +0000154}
155
156static void __exit bpf_mt_exit(void)
157{
Willem de Bruijneb8925e2016-12-06 16:25:02 -0500158 xt_unregister_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
Willem de Bruijne6f30c72013-01-18 07:17:30 +0000159}
160
161module_init(bpf_mt_init);
162module_exit(bpf_mt_exit);