GNU Linux-libre 4.19.245-gnu1
[releases.git] / net / sched / act_skbmod.c
1 /*
2  * net/sched/act_skbmod.c  skb data modifier
3  *
4  * Copyright (c) 2016 Jamal Hadi Salim <jhs@mojatatu.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10 */
11
12 #include <linux/module.h>
13 #include <linux/if_arp.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/skbuff.h>
17 #include <linux/rtnetlink.h>
18 #include <net/netlink.h>
19 #include <net/pkt_sched.h>
20
21 #include <linux/tc_act/tc_skbmod.h>
22 #include <net/tc_act/tc_skbmod.h>
23
24 static unsigned int skbmod_net_id;
25 static struct tc_action_ops act_skbmod_ops;
26
27 #define MAX_EDIT_LEN ETH_HLEN
28 static int tcf_skbmod_act(struct sk_buff *skb, const struct tc_action *a,
29                           struct tcf_result *res)
30 {
31         struct tcf_skbmod *d = to_skbmod(a);
32         int action;
33         struct tcf_skbmod_params *p;
34         u64 flags;
35         int err;
36
37         tcf_lastuse_update(&d->tcf_tm);
38         bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb);
39
40         action = READ_ONCE(d->tcf_action);
41         if (unlikely(action == TC_ACT_SHOT))
42                 goto drop;
43
44         if (!skb->dev || skb->dev->type != ARPHRD_ETHER)
45                 return action;
46
47         /* XXX: if you are going to edit more fields beyond ethernet header
48          * (example when you add IP header replacement or vlan swap)
49          * then MAX_EDIT_LEN needs to change appropriately
50         */
51         err = skb_ensure_writable(skb, MAX_EDIT_LEN);
52         if (unlikely(err)) /* best policy is to drop on the floor */
53                 goto drop;
54
55         p = rcu_dereference_bh(d->skbmod_p);
56         flags = p->flags;
57         if (flags & SKBMOD_F_DMAC)
58                 ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
59         if (flags & SKBMOD_F_SMAC)
60                 ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
61         if (flags & SKBMOD_F_ETYPE)
62                 eth_hdr(skb)->h_proto = p->eth_type;
63
64         if (flags & SKBMOD_F_SWAPMAC) {
65                 u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
66                 /*XXX: I am sure we can come up with more efficient swapping*/
67                 ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
68                 ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
69                 ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
70         }
71
72         return action;
73
74 drop:
75         qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
76         return TC_ACT_SHOT;
77 }
78
79 static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
80         [TCA_SKBMOD_PARMS]              = { .len = sizeof(struct tc_skbmod) },
81         [TCA_SKBMOD_DMAC]               = { .len = ETH_ALEN },
82         [TCA_SKBMOD_SMAC]               = { .len = ETH_ALEN },
83         [TCA_SKBMOD_ETYPE]              = { .type = NLA_U16 },
84 };
85
86 static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
87                            struct nlattr *est, struct tc_action **a,
88                            int ovr, int bind, bool rtnl_held,
89                            struct netlink_ext_ack *extack)
90 {
91         struct tc_action_net *tn = net_generic(net, skbmod_net_id);
92         struct nlattr *tb[TCA_SKBMOD_MAX + 1];
93         struct tcf_skbmod_params *p, *p_old;
94         struct tc_skbmod *parm;
95         u32 lflags = 0, index;
96         struct tcf_skbmod *d;
97         bool exists = false;
98         u8 *daddr = NULL;
99         u8 *saddr = NULL;
100         u16 eth_type = 0;
101         int ret = 0, err;
102
103         if (!nla)
104                 return -EINVAL;
105
106         err = nla_parse_nested(tb, TCA_SKBMOD_MAX, nla, skbmod_policy, NULL);
107         if (err < 0)
108                 return err;
109
110         if (!tb[TCA_SKBMOD_PARMS])
111                 return -EINVAL;
112
113         if (tb[TCA_SKBMOD_DMAC]) {
114                 daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
115                 lflags |= SKBMOD_F_DMAC;
116         }
117
118         if (tb[TCA_SKBMOD_SMAC]) {
119                 saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
120                 lflags |= SKBMOD_F_SMAC;
121         }
122
123         if (tb[TCA_SKBMOD_ETYPE]) {
124                 eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
125                 lflags |= SKBMOD_F_ETYPE;
126         }
127
128         parm = nla_data(tb[TCA_SKBMOD_PARMS]);
129         index = parm->index;
130         if (parm->flags & SKBMOD_F_SWAPMAC)
131                 lflags = SKBMOD_F_SWAPMAC;
132
133         err = tcf_idr_check_alloc(tn, &index, a, bind);
134         if (err < 0)
135                 return err;
136         exists = err;
137         if (exists && bind)
138                 return 0;
139
140         if (!lflags) {
141                 if (exists)
142                         tcf_idr_release(*a, bind);
143                 else
144                         tcf_idr_cleanup(tn, index);
145                 return -EINVAL;
146         }
147
148         if (!exists) {
149                 ret = tcf_idr_create(tn, index, est, a,
150                                      &act_skbmod_ops, bind, true);
151                 if (ret) {
152                         tcf_idr_cleanup(tn, index);
153                         return ret;
154                 }
155
156                 ret = ACT_P_CREATED;
157         } else if (!ovr) {
158                 tcf_idr_release(*a, bind);
159                 return -EEXIST;
160         }
161
162         d = to_skbmod(*a);
163
164         p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
165         if (unlikely(!p)) {
166                 tcf_idr_release(*a, bind);
167                 return -ENOMEM;
168         }
169
170         p->flags = lflags;
171         d->tcf_action = parm->action;
172
173         if (ovr)
174                 spin_lock_bh(&d->tcf_lock);
175         /* Protected by tcf_lock if overwriting existing action. */
176         p_old = rcu_dereference_protected(d->skbmod_p, 1);
177
178         if (lflags & SKBMOD_F_DMAC)
179                 ether_addr_copy(p->eth_dst, daddr);
180         if (lflags & SKBMOD_F_SMAC)
181                 ether_addr_copy(p->eth_src, saddr);
182         if (lflags & SKBMOD_F_ETYPE)
183                 p->eth_type = htons(eth_type);
184
185         rcu_assign_pointer(d->skbmod_p, p);
186         if (ovr)
187                 spin_unlock_bh(&d->tcf_lock);
188
189         if (p_old)
190                 kfree_rcu(p_old, rcu);
191
192         if (ret == ACT_P_CREATED)
193                 tcf_idr_insert(tn, *a);
194         return ret;
195 }
196
197 static void tcf_skbmod_cleanup(struct tc_action *a)
198 {
199         struct tcf_skbmod *d = to_skbmod(a);
200         struct tcf_skbmod_params  *p;
201
202         p = rcu_dereference_protected(d->skbmod_p, 1);
203         if (p)
204                 kfree_rcu(p, rcu);
205 }
206
207 static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
208                            int bind, int ref)
209 {
210         struct tcf_skbmod *d = to_skbmod(a);
211         unsigned char *b = skb_tail_pointer(skb);
212         struct tcf_skbmod_params  *p;
213         struct tc_skbmod opt = {
214                 .index   = d->tcf_index,
215                 .refcnt  = refcount_read(&d->tcf_refcnt) - ref,
216                 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
217         };
218         struct tcf_t t;
219
220         spin_lock_bh(&d->tcf_lock);
221         opt.action = d->tcf_action;
222         p = rcu_dereference_protected(d->skbmod_p,
223                                       lockdep_is_held(&d->tcf_lock));
224         opt.flags  = p->flags;
225         if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
226                 goto nla_put_failure;
227         if ((p->flags & SKBMOD_F_DMAC) &&
228             nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
229                 goto nla_put_failure;
230         if ((p->flags & SKBMOD_F_SMAC) &&
231             nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
232                 goto nla_put_failure;
233         if ((p->flags & SKBMOD_F_ETYPE) &&
234             nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
235                 goto nla_put_failure;
236
237         tcf_tm_dump(&t, &d->tcf_tm);
238         if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
239                 goto nla_put_failure;
240
241         spin_unlock_bh(&d->tcf_lock);
242         return skb->len;
243 nla_put_failure:
244         spin_unlock_bh(&d->tcf_lock);
245         nlmsg_trim(skb, b);
246         return -1;
247 }
248
249 static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
250                              struct netlink_callback *cb, int type,
251                              const struct tc_action_ops *ops,
252                              struct netlink_ext_ack *extack)
253 {
254         struct tc_action_net *tn = net_generic(net, skbmod_net_id);
255
256         return tcf_generic_walker(tn, skb, cb, type, ops, extack);
257 }
258
259 static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index,
260                              struct netlink_ext_ack *extack)
261 {
262         struct tc_action_net *tn = net_generic(net, skbmod_net_id);
263
264         return tcf_idr_search(tn, a, index);
265 }
266
267 static struct tc_action_ops act_skbmod_ops = {
268         .kind           =       "skbmod",
269         .type           =       TCA_ACT_SKBMOD,
270         .owner          =       THIS_MODULE,
271         .act            =       tcf_skbmod_act,
272         .dump           =       tcf_skbmod_dump,
273         .init           =       tcf_skbmod_init,
274         .cleanup        =       tcf_skbmod_cleanup,
275         .walk           =       tcf_skbmod_walker,
276         .lookup         =       tcf_skbmod_search,
277         .size           =       sizeof(struct tcf_skbmod),
278 };
279
280 static __net_init int skbmod_init_net(struct net *net)
281 {
282         struct tc_action_net *tn = net_generic(net, skbmod_net_id);
283
284         return tc_action_net_init(net, tn, &act_skbmod_ops);
285 }
286
287 static void __net_exit skbmod_exit_net(struct list_head *net_list)
288 {
289         tc_action_net_exit(net_list, skbmod_net_id);
290 }
291
292 static struct pernet_operations skbmod_net_ops = {
293         .init = skbmod_init_net,
294         .exit_batch = skbmod_exit_net,
295         .id   = &skbmod_net_id,
296         .size = sizeof(struct tc_action_net),
297 };
298
299 MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
300 MODULE_DESCRIPTION("SKB data mod-ing");
301 MODULE_LICENSE("GPL");
302
303 static int __init skbmod_init_module(void)
304 {
305         return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
306 }
307
308 static void __exit skbmod_cleanup_module(void)
309 {
310         tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
311 }
312
313 module_init(skbmod_init_module);
314 module_exit(skbmod_cleanup_module);