media: staging: media: use relevant lock
[linux-2.6-block.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
b2606644
FW
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
e6f30c73 12#include <linux/module.h>
98589a09 13#include <linux/syscalls.h>
e6f30c73
WB
14#include <linux/skbuff.h>
15#include <linux/filter.h>
2c16d603 16#include <linux/bpf.h>
e6f30c73
WB
17
18#include <linux/netfilter/xt_bpf.h>
19#include <linux/netfilter/x_tables.h>
20
21MODULE_AUTHOR("Willem de Bruijn <willemb@google.com>");
22MODULE_DESCRIPTION("Xtables: BPF filter match");
23MODULE_LICENSE("GPL");
24MODULE_ALIAS("ipt_bpf");
25MODULE_ALIAS("ip6t_bpf");
26
2c16d603
WB
27static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len,
28 struct bpf_prog **ret)
e6f30c73 29{
b1fcd35c 30 struct sock_fprog_kern program;
e6f30c73 31
6ab40511
JH
32 if (len > XT_BPF_MAX_NUM_INSTR)
33 return -EINVAL;
34
2c16d603
WB
35 program.len = len;
36 program.filter = insns;
b1fcd35c 37
2c16d603 38 if (bpf_prog_create(ret, &program)) {
b2606644 39 pr_info_ratelimited("check failed: parse error\n");
e6f30c73
WB
40 return -EINVAL;
41 }
42
43 return 0;
44}
45
2c16d603
WB
46static int __bpf_mt_check_fd(int fd, struct bpf_prog **ret)
47{
48 struct bpf_prog *prog;
49
50 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
51 if (IS_ERR(prog))
52 return PTR_ERR(prog);
53
54 *ret = prog;
55 return 0;
56}
57
98589a09
SL
58static int __bpf_mt_check_path(const char *path, struct bpf_prog **ret)
59{
6ab40511
JH
60 if (strnlen(path, XT_BPF_PATH_MAX) == XT_BPF_PATH_MAX)
61 return -EINVAL;
62
040ee692
AV
63 *ret = bpf_prog_get_type_path(path, BPF_PROG_TYPE_SOCKET_FILTER);
64 return PTR_ERR_OR_ZERO(*ret);
98589a09
SL
65}
66
2c16d603
WB
67static int bpf_mt_check(const struct xt_mtchk_param *par)
68{
69 struct xt_bpf_info *info = par->matchinfo;
70
71 return __bpf_mt_check_bytecode(info->bpf_program,
72 info->bpf_program_num_elem,
73 &info->filter);
74}
75
76static int bpf_mt_check_v1(const struct xt_mtchk_param *par)
77{
78 struct xt_bpf_info_v1 *info = par->matchinfo;
79
80 if (info->mode == XT_BPF_MODE_BYTECODE)
81 return __bpf_mt_check_bytecode(info->bpf_program,
82 info->bpf_program_num_elem,
83 &info->filter);
98589a09 84 else if (info->mode == XT_BPF_MODE_FD_ELF)
2c16d603 85 return __bpf_mt_check_fd(info->fd, &info->filter);
98589a09
SL
86 else if (info->mode == XT_BPF_MODE_PATH_PINNED)
87 return __bpf_mt_check_path(info->path, &info->filter);
2c16d603
WB
88 else
89 return -EINVAL;
90}
91
e6f30c73
WB
92static bool bpf_mt(const struct sk_buff *skb, struct xt_action_param *par)
93{
94 const struct xt_bpf_info *info = par->matchinfo;
95
7ae457c1 96 return BPF_PROG_RUN(info->filter, skb);
e6f30c73
WB
97}
98
2c16d603
WB
99static bool bpf_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
100{
101 const struct xt_bpf_info_v1 *info = par->matchinfo;
102
103 return !!bpf_prog_run_save_cb(info->filter, (struct sk_buff *) skb);
104}
105
e6f30c73
WB
106static void bpf_mt_destroy(const struct xt_mtdtor_param *par)
107{
108 const struct xt_bpf_info *info = par->matchinfo;
2c16d603
WB
109
110 bpf_prog_destroy(info->filter);
111}
112
113static void bpf_mt_destroy_v1(const struct xt_mtdtor_param *par)
114{
115 const struct xt_bpf_info_v1 *info = par->matchinfo;
116
7ae457c1 117 bpf_prog_destroy(info->filter);
e6f30c73
WB
118}
119
2c16d603
WB
120static struct xt_match bpf_mt_reg[] __read_mostly = {
121 {
122 .name = "bpf",
123 .revision = 0,
124 .family = NFPROTO_UNSPEC,
125 .checkentry = bpf_mt_check,
126 .match = bpf_mt,
127 .destroy = bpf_mt_destroy,
128 .matchsize = sizeof(struct xt_bpf_info),
ec231890 129 .usersize = offsetof(struct xt_bpf_info, filter),
2c16d603
WB
130 .me = THIS_MODULE,
131 },
132 {
133 .name = "bpf",
134 .revision = 1,
135 .family = NFPROTO_UNSPEC,
136 .checkentry = bpf_mt_check_v1,
137 .match = bpf_mt_v1,
138 .destroy = bpf_mt_destroy_v1,
139 .matchsize = sizeof(struct xt_bpf_info_v1),
ec231890 140 .usersize = offsetof(struct xt_bpf_info_v1, filter),
2c16d603
WB
141 .me = THIS_MODULE,
142 },
e6f30c73
WB
143};
144
145static int __init bpf_mt_init(void)
146{
2c16d603 147 return xt_register_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
e6f30c73
WB
148}
149
150static void __exit bpf_mt_exit(void)
151{
2c16d603 152 xt_unregister_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
e6f30c73
WB
153}
154
155module_init(bpf_mt_init);
156module_exit(bpf_mt_exit);