4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/skbuff.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/filter.h>
16 #include <linux/bpf.h>
18 #include <net/netlink.h>
19 #include <net/pkt_sched.h>
21 #include <linux/tc_act/tc_bpf.h>
22 #include <net/tc_act/tc_bpf.h>
24 #define BPF_TAB_MASK 15
25 #define ACT_BPF_NAME_LEN 256
28 struct bpf_prog *filter;
29 struct sock_filter *bpf_ops;
35 static int tcf_bpf(struct sk_buff *skb, const struct tc_action *act,
36 struct tcf_result *res)
38 struct tcf_bpf *prog = act->priv;
39 int action, filter_res;
41 if (unlikely(!skb_mac_header_was_set(skb)))
44 spin_lock(&prog->tcf_lock);
46 prog->tcf_tm.lastuse = jiffies;
47 bstats_update(&prog->tcf_bstats, skb);
49 /* Needed here for accessing maps. */
51 filter_res = BPF_PROG_RUN(prog->filter, skb);
54 /* A BPF program may overwrite the default action opcode.
55 * Similarly as in cls_bpf, if filter_res == -1 we use the
56 * default action specified from tc.
58 * In case a different well-known TC_ACT opcode has been
59 * returned, it will overwrite the default one.
61 * For everything else that is unkown, TC_ACT_UNSPEC is
66 case TC_ACT_RECLASSIFY:
72 prog->tcf_qstats.drops++;
75 action = prog->tcf_action;
78 action = TC_ACT_UNSPEC;
82 spin_unlock(&prog->tcf_lock);
86 static bool tcf_bpf_is_ebpf(const struct tcf_bpf *prog)
88 return !prog->bpf_ops;
91 static int tcf_bpf_dump_bpf_info(const struct tcf_bpf *prog,
96 if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, prog->bpf_num_ops))
99 nla = nla_reserve(skb, TCA_ACT_BPF_OPS, prog->bpf_num_ops *
100 sizeof(struct sock_filter));
104 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
109 static int tcf_bpf_dump_ebpf_info(const struct tcf_bpf *prog,
112 if (nla_put_u32(skb, TCA_ACT_BPF_FD, prog->bpf_fd))
115 if (prog->bpf_name &&
116 nla_put_string(skb, TCA_ACT_BPF_NAME, prog->bpf_name))
122 static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *act,
125 unsigned char *tp = skb_tail_pointer(skb);
126 struct tcf_bpf *prog = act->priv;
127 struct tc_act_bpf opt = {
128 .index = prog->tcf_index,
129 .refcnt = prog->tcf_refcnt - ref,
130 .bindcnt = prog->tcf_bindcnt - bind,
131 .action = prog->tcf_action,
136 if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
137 goto nla_put_failure;
139 if (tcf_bpf_is_ebpf(prog))
140 ret = tcf_bpf_dump_ebpf_info(prog, skb);
142 ret = tcf_bpf_dump_bpf_info(prog, skb);
144 goto nla_put_failure;
146 tm.install = jiffies_to_clock_t(jiffies - prog->tcf_tm.install);
147 tm.lastuse = jiffies_to_clock_t(jiffies - prog->tcf_tm.lastuse);
148 tm.expires = jiffies_to_clock_t(prog->tcf_tm.expires);
150 if (nla_put(skb, TCA_ACT_BPF_TM, sizeof(tm), &tm))
151 goto nla_put_failure;
160 static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
161 [TCA_ACT_BPF_PARMS] = { .len = sizeof(struct tc_act_bpf) },
162 [TCA_ACT_BPF_FD] = { .type = NLA_U32 },
163 [TCA_ACT_BPF_NAME] = { .type = NLA_NUL_STRING, .len = ACT_BPF_NAME_LEN },
164 [TCA_ACT_BPF_OPS_LEN] = { .type = NLA_U16 },
165 [TCA_ACT_BPF_OPS] = { .type = NLA_BINARY,
166 .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
169 static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
171 struct sock_filter *bpf_ops;
172 struct sock_fprog_kern fprog_tmp;
174 u16 bpf_size, bpf_num_ops;
177 bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
178 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
181 bpf_size = bpf_num_ops * sizeof(*bpf_ops);
182 if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
185 bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
189 memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size);
191 fprog_tmp.len = bpf_num_ops;
192 fprog_tmp.filter = bpf_ops;
194 ret = bpf_prog_create(&fp, &fprog_tmp);
200 cfg->bpf_ops = bpf_ops;
201 cfg->bpf_num_ops = bpf_num_ops;
207 static int tcf_bpf_init_from_efd(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
213 bpf_fd = nla_get_u32(tb[TCA_ACT_BPF_FD]);
215 fp = bpf_prog_get(bpf_fd);
219 if (fp->type != BPF_PROG_TYPE_SCHED_ACT) {
224 if (tb[TCA_ACT_BPF_NAME]) {
225 name = kmemdup(nla_data(tb[TCA_ACT_BPF_NAME]),
226 nla_len(tb[TCA_ACT_BPF_NAME]),
234 cfg->bpf_fd = bpf_fd;
235 cfg->bpf_name = name;
241 static int tcf_bpf_init(struct net *net, struct nlattr *nla,
242 struct nlattr *est, struct tc_action *act,
243 int replace, int bind)
245 struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
246 struct tc_act_bpf *parm;
247 struct tcf_bpf *prog;
248 struct tcf_bpf_cfg cfg;
249 bool is_bpf, is_ebpf;
255 ret = nla_parse_nested(tb, TCA_ACT_BPF_MAX, nla, act_bpf_policy);
259 is_bpf = tb[TCA_ACT_BPF_OPS_LEN] && tb[TCA_ACT_BPF_OPS];
260 is_ebpf = tb[TCA_ACT_BPF_FD];
262 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf) ||
263 !tb[TCA_ACT_BPF_PARMS])
266 parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
268 memset(&cfg, 0, sizeof(cfg));
270 ret = is_bpf ? tcf_bpf_init_from_ops(tb, &cfg) :
271 tcf_bpf_init_from_efd(tb, &cfg);
275 if (!tcf_hash_check(parm->index, act, bind)) {
276 ret = tcf_hash_create(parm->index, est, act,
277 sizeof(*prog), bind);
283 /* Don't override defaults. */
287 tcf_hash_release(act, bind);
295 spin_lock_bh(&prog->tcf_lock);
297 prog->bpf_ops = cfg.bpf_ops;
298 prog->bpf_name = cfg.bpf_name;
301 prog->bpf_num_ops = cfg.bpf_num_ops;
303 prog->bpf_fd = cfg.bpf_fd;
305 prog->tcf_action = parm->action;
306 prog->filter = cfg.filter;
308 spin_unlock_bh(&prog->tcf_lock);
310 if (ret == ACT_P_CREATED)
311 tcf_hash_insert(act);
317 bpf_prog_put(cfg.filter);
319 bpf_prog_destroy(cfg.filter);
327 static void tcf_bpf_cleanup(struct tc_action *act, int bind)
329 const struct tcf_bpf *prog = act->priv;
331 if (tcf_bpf_is_ebpf(prog))
332 bpf_prog_put(prog->filter);
334 bpf_prog_destroy(prog->filter);
337 static struct tc_action_ops act_bpf_ops __read_mostly = {
340 .owner = THIS_MODULE,
342 .dump = tcf_bpf_dump,
343 .cleanup = tcf_bpf_cleanup,
344 .init = tcf_bpf_init,
347 static int __init bpf_init_module(void)
349 return tcf_register_action(&act_bpf_ops, BPF_TAB_MASK);
352 static void __exit bpf_cleanup_module(void)
354 tcf_unregister_action(&act_bpf_ops);
357 module_init(bpf_init_module);
358 module_exit(bpf_cleanup_module);
361 MODULE_DESCRIPTION("TC BPF based action");
362 MODULE_LICENSE("GPL v2");