]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/netfilter/xt_bpf.c
net: ethernet: arc: fix error handling in emac_rockchip_probe
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / xt_bpf.c
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>
11 #include <linux/syscalls.h>
12 #include <linux/skbuff.h>
13 #include <linux/filter.h>
14 #include <linux/bpf.h>
15
16 #include <linux/netfilter/xt_bpf.h>
17 #include <linux/netfilter/x_tables.h>
18
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");
24
25 static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len,
26 struct bpf_prog **ret)
27 {
28 struct sock_fprog_kern program;
29
30 program.len = len;
31 program.filter = insns;
32
33 if (bpf_prog_create(ret, &program)) {
34 pr_info("bpf: check failed: parse error\n");
35 return -EINVAL;
36 }
37
38 return 0;
39 }
40
41 static 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
53 static int __bpf_mt_check_path(const char *path, struct bpf_prog **ret)
54 {
55 mm_segment_t oldfs = get_fs();
56 int retval, fd;
57
58 set_fs(KERNEL_DS);
59 fd = bpf_obj_get_user(path, 0);
60 set_fs(oldfs);
61 if (fd < 0)
62 return fd;
63
64 retval = __bpf_mt_check_fd(fd, ret);
65 sys_close(fd);
66 return retval;
67 }
68
69 static int bpf_mt_check(const struct xt_mtchk_param *par)
70 {
71 struct xt_bpf_info *info = par->matchinfo;
72
73 return __bpf_mt_check_bytecode(info->bpf_program,
74 info->bpf_program_num_elem,
75 &info->filter);
76 }
77
78 static int bpf_mt_check_v1(const struct xt_mtchk_param *par)
79 {
80 struct xt_bpf_info_v1 *info = par->matchinfo;
81
82 if (info->mode == XT_BPF_MODE_BYTECODE)
83 return __bpf_mt_check_bytecode(info->bpf_program,
84 info->bpf_program_num_elem,
85 &info->filter);
86 else if (info->mode == XT_BPF_MODE_FD_ELF)
87 return __bpf_mt_check_fd(info->fd, &info->filter);
88 else if (info->mode == XT_BPF_MODE_PATH_PINNED)
89 return __bpf_mt_check_path(info->path, &info->filter);
90 else
91 return -EINVAL;
92 }
93
94 static bool bpf_mt(const struct sk_buff *skb, struct xt_action_param *par)
95 {
96 const struct xt_bpf_info *info = par->matchinfo;
97
98 return BPF_PROG_RUN(info->filter, skb);
99 }
100
101 static bool bpf_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
102 {
103 const struct xt_bpf_info_v1 *info = par->matchinfo;
104
105 return !!bpf_prog_run_save_cb(info->filter, (struct sk_buff *) skb);
106 }
107
108 static void bpf_mt_destroy(const struct xt_mtdtor_param *par)
109 {
110 const struct xt_bpf_info *info = par->matchinfo;
111
112 bpf_prog_destroy(info->filter);
113 }
114
115 static void bpf_mt_destroy_v1(const struct xt_mtdtor_param *par)
116 {
117 const struct xt_bpf_info_v1 *info = par->matchinfo;
118
119 bpf_prog_destroy(info->filter);
120 }
121
122 static struct xt_match bpf_mt_reg[] __read_mostly = {
123 {
124 .name = "bpf",
125 .revision = 0,
126 .family = NFPROTO_UNSPEC,
127 .checkentry = bpf_mt_check,
128 .match = bpf_mt,
129 .destroy = bpf_mt_destroy,
130 .matchsize = sizeof(struct xt_bpf_info),
131 .usersize = offsetof(struct xt_bpf_info, filter),
132 .me = THIS_MODULE,
133 },
134 {
135 .name = "bpf",
136 .revision = 1,
137 .family = NFPROTO_UNSPEC,
138 .checkentry = bpf_mt_check_v1,
139 .match = bpf_mt_v1,
140 .destroy = bpf_mt_destroy_v1,
141 .matchsize = sizeof(struct xt_bpf_info_v1),
142 .usersize = offsetof(struct xt_bpf_info_v1, filter),
143 .me = THIS_MODULE,
144 },
145 };
146
147 static int __init bpf_mt_init(void)
148 {
149 return xt_register_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
150 }
151
152 static void __exit bpf_mt_exit(void)
153 {
154 xt_unregister_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
155 }
156
157 module_init(bpf_mt_init);
158 module_exit(bpf_mt_exit);