ASoC: dapm: Fix build warning
[linux-drm-fsl-dcu.git] / net / sched / act_bpf.c
1 /*
2  * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
3  *
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.
8  */
9
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 <net/netlink.h>
17 #include <net/pkt_sched.h>
18
19 #include <linux/tc_act/tc_bpf.h>
20 #include <net/tc_act/tc_bpf.h>
21
22 #define BPF_TAB_MASK     15
23
24 static int tcf_bpf(struct sk_buff *skb, const struct tc_action *a,
25                    struct tcf_result *res)
26 {
27         struct tcf_bpf *b = a->priv;
28         int action;
29         int filter_res;
30
31         spin_lock(&b->tcf_lock);
32         b->tcf_tm.lastuse = jiffies;
33         bstats_update(&b->tcf_bstats, skb);
34         action = b->tcf_action;
35
36         filter_res = BPF_PROG_RUN(b->filter, skb);
37         if (filter_res == 0) {
38                 /* Return code 0 from the BPF program
39                  * is being interpreted as a drop here.
40                  */
41                 action = TC_ACT_SHOT;
42                 b->tcf_qstats.drops++;
43         }
44
45         spin_unlock(&b->tcf_lock);
46         return action;
47 }
48
49 static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *a,
50                         int bind, int ref)
51 {
52         unsigned char *tp = skb_tail_pointer(skb);
53         struct tcf_bpf *b = a->priv;
54         struct tc_act_bpf opt = {
55                 .index    = b->tcf_index,
56                 .refcnt   = b->tcf_refcnt - ref,
57                 .bindcnt  = b->tcf_bindcnt - bind,
58                 .action   = b->tcf_action,
59         };
60         struct tcf_t t;
61         struct nlattr *nla;
62
63         if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
64                 goto nla_put_failure;
65
66         if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, b->bpf_num_ops))
67                 goto nla_put_failure;
68
69         nla = nla_reserve(skb, TCA_ACT_BPF_OPS, b->bpf_num_ops *
70                           sizeof(struct sock_filter));
71         if (!nla)
72                 goto nla_put_failure;
73
74         memcpy(nla_data(nla), b->bpf_ops, nla_len(nla));
75
76         t.install = jiffies_to_clock_t(jiffies - b->tcf_tm.install);
77         t.lastuse = jiffies_to_clock_t(jiffies - b->tcf_tm.lastuse);
78         t.expires = jiffies_to_clock_t(b->tcf_tm.expires);
79         if (nla_put(skb, TCA_ACT_BPF_TM, sizeof(t), &t))
80                 goto nla_put_failure;
81         return skb->len;
82
83 nla_put_failure:
84         nlmsg_trim(skb, tp);
85         return -1;
86 }
87
88 static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
89         [TCA_ACT_BPF_PARMS]     = { .len = sizeof(struct tc_act_bpf) },
90         [TCA_ACT_BPF_OPS_LEN]   = { .type = NLA_U16 },
91         [TCA_ACT_BPF_OPS]       = { .type = NLA_BINARY,
92                                     .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
93 };
94
95 static int tcf_bpf_init(struct net *net, struct nlattr *nla,
96                         struct nlattr *est, struct tc_action *a,
97                         int ovr, int bind)
98 {
99         struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
100         struct tc_act_bpf *parm;
101         struct tcf_bpf *b;
102         u16 bpf_size, bpf_num_ops;
103         struct sock_filter *bpf_ops;
104         struct sock_fprog_kern tmp;
105         struct bpf_prog *fp;
106         int ret;
107
108         if (!nla)
109                 return -EINVAL;
110
111         ret = nla_parse_nested(tb, TCA_ACT_BPF_MAX, nla, act_bpf_policy);
112         if (ret < 0)
113                 return ret;
114
115         if (!tb[TCA_ACT_BPF_PARMS] ||
116             !tb[TCA_ACT_BPF_OPS_LEN] || !tb[TCA_ACT_BPF_OPS])
117                 return -EINVAL;
118         parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
119
120         bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
121         if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
122                 return -EINVAL;
123
124         bpf_size = bpf_num_ops * sizeof(*bpf_ops);
125         if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
126                 return -EINVAL;
127
128         bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
129         if (!bpf_ops)
130                 return -ENOMEM;
131
132         memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size);
133
134         tmp.len = bpf_num_ops;
135         tmp.filter = bpf_ops;
136
137         ret = bpf_prog_create(&fp, &tmp);
138         if (ret)
139                 goto free_bpf_ops;
140
141         if (!tcf_hash_check(parm->index, a, bind)) {
142                 ret = tcf_hash_create(parm->index, est, a, sizeof(*b), bind);
143                 if (ret)
144                         goto destroy_fp;
145
146                 ret = ACT_P_CREATED;
147         } else {
148                 if (bind)
149                         goto destroy_fp;
150                 tcf_hash_release(a, bind);
151                 if (!ovr) {
152                         ret = -EEXIST;
153                         goto destroy_fp;
154                 }
155         }
156
157         b = to_bpf(a);
158         spin_lock_bh(&b->tcf_lock);
159         b->tcf_action = parm->action;
160         b->bpf_num_ops = bpf_num_ops;
161         b->bpf_ops = bpf_ops;
162         b->filter = fp;
163         spin_unlock_bh(&b->tcf_lock);
164
165         if (ret == ACT_P_CREATED)
166                 tcf_hash_insert(a);
167         return ret;
168
169 destroy_fp:
170         bpf_prog_destroy(fp);
171 free_bpf_ops:
172         kfree(bpf_ops);
173         return ret;
174 }
175
176 static void tcf_bpf_cleanup(struct tc_action *a, int bind)
177 {
178         struct tcf_bpf *b = a->priv;
179
180         bpf_prog_destroy(b->filter);
181 }
182
183 static struct tc_action_ops act_bpf_ops = {
184         .kind =         "bpf",
185         .type =         TCA_ACT_BPF,
186         .owner =        THIS_MODULE,
187         .act =          tcf_bpf,
188         .dump =         tcf_bpf_dump,
189         .cleanup =      tcf_bpf_cleanup,
190         .init =         tcf_bpf_init,
191 };
192
193 static int __init bpf_init_module(void)
194 {
195         return tcf_register_action(&act_bpf_ops, BPF_TAB_MASK);
196 }
197
198 static void __exit bpf_cleanup_module(void)
199 {
200         tcf_unregister_action(&act_bpf_ops);
201 }
202
203 module_init(bpf_init_module);
204 module_exit(bpf_cleanup_module);
205
206 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
207 MODULE_DESCRIPTION("TC BPF based action");
208 MODULE_LICENSE("GPL v2");