]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/xt_bpf.c
fix "netfilter: xt_bpf: Fix XT_BPF_MODE_FD_PINNED mode of 'xt_bpf_info_v1'"
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / xt_bpf.c
CommitLineData
e6f30c73
WB
1/* 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>
98589a09 11#include <linux/syscalls.h>
e6f30c73
WB
12#include <linux/skbuff.h>
13#include <linux/filter.h>
2c16d603 14#include <linux/bpf.h>
e6f30c73
WB
15
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
2c16d603
WB
25static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len,
26 struct bpf_prog **ret)
e6f30c73 27{
b1fcd35c 28 struct sock_fprog_kern program;
e6f30c73 29
2c16d603
WB
30 program.len = len;
31 program.filter = insns;
b1fcd35c 32
2c16d603 33 if (bpf_prog_create(ret, &program)) {
e6f30c73
WB
34 pr_info("bpf: check failed: parse error\n");
35 return -EINVAL;
36 }
37
38 return 0;
39}
40
2c16d603
WB
41static int __bpf_mt_check_fd(int fd, struct bpf_prog **ret)
42{
43 struct bpf_prog *prog;
44
45 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
46 if (IS_ERR(prog))
47 return PTR_ERR(prog);
48
49 *ret = prog;
50 return 0;
51}
52
98589a09
SL
53static int __bpf_mt_check_path(const char *path, struct bpf_prog **ret)
54{
040ee692
AV
55 *ret = bpf_prog_get_type_path(path, BPF_PROG_TYPE_SOCKET_FILTER);
56 return PTR_ERR_OR_ZERO(*ret);
98589a09
SL
57}
58
2c16d603
WB
59static int bpf_mt_check(const struct xt_mtchk_param *par)
60{
61 struct xt_bpf_info *info = par->matchinfo;
62
63 return __bpf_mt_check_bytecode(info->bpf_program,
64 info->bpf_program_num_elem,
65 &info->filter);
66}
67
68static int bpf_mt_check_v1(const struct xt_mtchk_param *par)
69{
70 struct xt_bpf_info_v1 *info = par->matchinfo;
71
72 if (info->mode == XT_BPF_MODE_BYTECODE)
73 return __bpf_mt_check_bytecode(info->bpf_program,
74 info->bpf_program_num_elem,
75 &info->filter);
98589a09 76 else if (info->mode == XT_BPF_MODE_FD_ELF)
2c16d603 77 return __bpf_mt_check_fd(info->fd, &info->filter);
98589a09
SL
78 else if (info->mode == XT_BPF_MODE_PATH_PINNED)
79 return __bpf_mt_check_path(info->path, &info->filter);
2c16d603
WB
80 else
81 return -EINVAL;
82}
83
e6f30c73
WB
84static bool bpf_mt(const struct sk_buff *skb, struct xt_action_param *par)
85{
86 const struct xt_bpf_info *info = par->matchinfo;
87
7ae457c1 88 return BPF_PROG_RUN(info->filter, skb);
e6f30c73
WB
89}
90
2c16d603
WB
91static bool bpf_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
92{
93 const struct xt_bpf_info_v1 *info = par->matchinfo;
94
95 return !!bpf_prog_run_save_cb(info->filter, (struct sk_buff *) skb);
96}
97
e6f30c73
WB
98static void bpf_mt_destroy(const struct xt_mtdtor_param *par)
99{
100 const struct xt_bpf_info *info = par->matchinfo;
2c16d603
WB
101
102 bpf_prog_destroy(info->filter);
103}
104
105static void bpf_mt_destroy_v1(const struct xt_mtdtor_param *par)
106{
107 const struct xt_bpf_info_v1 *info = par->matchinfo;
108
7ae457c1 109 bpf_prog_destroy(info->filter);
e6f30c73
WB
110}
111
2c16d603
WB
112static struct xt_match bpf_mt_reg[] __read_mostly = {
113 {
114 .name = "bpf",
115 .revision = 0,
116 .family = NFPROTO_UNSPEC,
117 .checkentry = bpf_mt_check,
118 .match = bpf_mt,
119 .destroy = bpf_mt_destroy,
120 .matchsize = sizeof(struct xt_bpf_info),
ec231890 121 .usersize = offsetof(struct xt_bpf_info, filter),
2c16d603
WB
122 .me = THIS_MODULE,
123 },
124 {
125 .name = "bpf",
126 .revision = 1,
127 .family = NFPROTO_UNSPEC,
128 .checkentry = bpf_mt_check_v1,
129 .match = bpf_mt_v1,
130 .destroy = bpf_mt_destroy_v1,
131 .matchsize = sizeof(struct xt_bpf_info_v1),
ec231890 132 .usersize = offsetof(struct xt_bpf_info_v1, filter),
2c16d603
WB
133 .me = THIS_MODULE,
134 },
e6f30c73
WB
135};
136
137static int __init bpf_mt_init(void)
138{
2c16d603 139 return xt_register_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
e6f30c73
WB
140}
141
142static void __exit bpf_mt_exit(void)
143{
2c16d603 144 xt_unregister_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
e6f30c73
WB
145}
146
147module_init(bpf_mt_init);
148module_exit(bpf_mt_exit);