]> asedeno.scripts.mit.edu Git - linux.git/blob - net/sched/act_skbmod.c
Merge tag 'sound-fix-5.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[linux.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/init.h>
14 #include <linux/kernel.h>
15 #include <linux/skbuff.h>
16 #include <linux/rtnetlink.h>
17 #include <net/netlink.h>
18 #include <net/pkt_sched.h>
19 #include <net/pkt_cls.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         /* XXX: if you are going to edit more fields beyond ethernet header
41          * (example when you add IP header replacement or vlan swap)
42          * then MAX_EDIT_LEN needs to change appropriately
43         */
44         err = skb_ensure_writable(skb, MAX_EDIT_LEN);
45         if (unlikely(err)) /* best policy is to drop on the floor */
46                 goto drop;
47
48         action = READ_ONCE(d->tcf_action);
49         if (unlikely(action == TC_ACT_SHOT))
50                 goto drop;
51
52         p = rcu_dereference_bh(d->skbmod_p);
53         flags = p->flags;
54         if (flags & SKBMOD_F_DMAC)
55                 ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
56         if (flags & SKBMOD_F_SMAC)
57                 ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
58         if (flags & SKBMOD_F_ETYPE)
59                 eth_hdr(skb)->h_proto = p->eth_type;
60
61         if (flags & SKBMOD_F_SWAPMAC) {
62                 u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
63                 /*XXX: I am sure we can come up with more efficient swapping*/
64                 ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
65                 ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
66                 ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
67         }
68
69         return action;
70
71 drop:
72         qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
73         return TC_ACT_SHOT;
74 }
75
76 static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
77         [TCA_SKBMOD_PARMS]              = { .len = sizeof(struct tc_skbmod) },
78         [TCA_SKBMOD_DMAC]               = { .len = ETH_ALEN },
79         [TCA_SKBMOD_SMAC]               = { .len = ETH_ALEN },
80         [TCA_SKBMOD_ETYPE]              = { .type = NLA_U16 },
81 };
82
83 static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
84                            struct nlattr *est, struct tc_action **a,
85                            int ovr, int bind, bool rtnl_held,
86                            struct tcf_proto *tp,
87                            struct netlink_ext_ack *extack)
88 {
89         struct tc_action_net *tn = net_generic(net, skbmod_net_id);
90         struct nlattr *tb[TCA_SKBMOD_MAX + 1];
91         struct tcf_skbmod_params *p, *p_old;
92         struct tcf_chain *goto_ch = NULL;
93         struct tc_skbmod *parm;
94         struct tcf_skbmod *d;
95         bool exists = false;
96         u8 *daddr = NULL;
97         u8 *saddr = NULL;
98         u16 eth_type = 0;
99         u32 lflags = 0;
100         int ret = 0, err;
101
102         if (!nla)
103                 return -EINVAL;
104
105         err = nla_parse_nested_deprecated(tb, TCA_SKBMOD_MAX, nla,
106                                           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         if (parm->flags & SKBMOD_F_SWAPMAC)
130                 lflags = SKBMOD_F_SWAPMAC;
131
132         err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
133         if (err < 0)
134                 return err;
135         exists = err;
136         if (exists && bind)
137                 return 0;
138
139         if (!lflags) {
140                 if (exists)
141                         tcf_idr_release(*a, bind);
142                 else
143                         tcf_idr_cleanup(tn, parm->index);
144                 return -EINVAL;
145         }
146
147         if (!exists) {
148                 ret = tcf_idr_create(tn, parm->index, est, a,
149                                      &act_skbmod_ops, bind, true);
150                 if (ret) {
151                         tcf_idr_cleanup(tn, parm->index);
152                         return ret;
153                 }
154
155                 ret = ACT_P_CREATED;
156         } else if (!ovr) {
157                 tcf_idr_release(*a, bind);
158                 return -EEXIST;
159         }
160         err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
161         if (err < 0)
162                 goto release_idr;
163
164         d = to_skbmod(*a);
165
166         p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
167         if (unlikely(!p)) {
168                 err = -ENOMEM;
169                 goto put_chain;
170         }
171
172         p->flags = lflags;
173
174         if (ovr)
175                 spin_lock_bh(&d->tcf_lock);
176         /* Protected by tcf_lock if overwriting existing action. */
177         goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
178         p_old = rcu_dereference_protected(d->skbmod_p, 1);
179
180         if (lflags & SKBMOD_F_DMAC)
181                 ether_addr_copy(p->eth_dst, daddr);
182         if (lflags & SKBMOD_F_SMAC)
183                 ether_addr_copy(p->eth_src, saddr);
184         if (lflags & SKBMOD_F_ETYPE)
185                 p->eth_type = htons(eth_type);
186
187         rcu_assign_pointer(d->skbmod_p, p);
188         if (ovr)
189                 spin_unlock_bh(&d->tcf_lock);
190
191         if (p_old)
192                 kfree_rcu(p_old, rcu);
193         if (goto_ch)
194                 tcf_chain_put_by_act(goto_ch);
195
196         if (ret == ACT_P_CREATED)
197                 tcf_idr_insert(tn, *a);
198         return ret;
199 put_chain:
200         if (goto_ch)
201                 tcf_chain_put_by_act(goto_ch);
202 release_idr:
203         tcf_idr_release(*a, bind);
204         return err;
205 }
206
207 static void tcf_skbmod_cleanup(struct tc_action *a)
208 {
209         struct tcf_skbmod *d = to_skbmod(a);
210         struct tcf_skbmod_params  *p;
211
212         p = rcu_dereference_protected(d->skbmod_p, 1);
213         if (p)
214                 kfree_rcu(p, rcu);
215 }
216
217 static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
218                            int bind, int ref)
219 {
220         struct tcf_skbmod *d = to_skbmod(a);
221         unsigned char *b = skb_tail_pointer(skb);
222         struct tcf_skbmod_params  *p;
223         struct tc_skbmod opt = {
224                 .index   = d->tcf_index,
225                 .refcnt  = refcount_read(&d->tcf_refcnt) - ref,
226                 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
227         };
228         struct tcf_t t;
229
230         spin_lock_bh(&d->tcf_lock);
231         opt.action = d->tcf_action;
232         p = rcu_dereference_protected(d->skbmod_p,
233                                       lockdep_is_held(&d->tcf_lock));
234         opt.flags  = p->flags;
235         if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
236                 goto nla_put_failure;
237         if ((p->flags & SKBMOD_F_DMAC) &&
238             nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
239                 goto nla_put_failure;
240         if ((p->flags & SKBMOD_F_SMAC) &&
241             nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
242                 goto nla_put_failure;
243         if ((p->flags & SKBMOD_F_ETYPE) &&
244             nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
245                 goto nla_put_failure;
246
247         tcf_tm_dump(&t, &d->tcf_tm);
248         if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
249                 goto nla_put_failure;
250
251         spin_unlock_bh(&d->tcf_lock);
252         return skb->len;
253 nla_put_failure:
254         spin_unlock_bh(&d->tcf_lock);
255         nlmsg_trim(skb, b);
256         return -1;
257 }
258
259 static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
260                              struct netlink_callback *cb, int type,
261                              const struct tc_action_ops *ops,
262                              struct netlink_ext_ack *extack)
263 {
264         struct tc_action_net *tn = net_generic(net, skbmod_net_id);
265
266         return tcf_generic_walker(tn, skb, cb, type, ops, extack);
267 }
268
269 static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index)
270 {
271         struct tc_action_net *tn = net_generic(net, skbmod_net_id);
272
273         return tcf_idr_search(tn, a, index);
274 }
275
276 static struct tc_action_ops act_skbmod_ops = {
277         .kind           =       "skbmod",
278         .id             =       TCA_ACT_SKBMOD,
279         .owner          =       THIS_MODULE,
280         .act            =       tcf_skbmod_act,
281         .dump           =       tcf_skbmod_dump,
282         .init           =       tcf_skbmod_init,
283         .cleanup        =       tcf_skbmod_cleanup,
284         .walk           =       tcf_skbmod_walker,
285         .lookup         =       tcf_skbmod_search,
286         .size           =       sizeof(struct tcf_skbmod),
287 };
288
289 static __net_init int skbmod_init_net(struct net *net)
290 {
291         struct tc_action_net *tn = net_generic(net, skbmod_net_id);
292
293         return tc_action_net_init(tn, &act_skbmod_ops);
294 }
295
296 static void __net_exit skbmod_exit_net(struct list_head *net_list)
297 {
298         tc_action_net_exit(net_list, skbmod_net_id);
299 }
300
301 static struct pernet_operations skbmod_net_ops = {
302         .init = skbmod_init_net,
303         .exit_batch = skbmod_exit_net,
304         .id   = &skbmod_net_id,
305         .size = sizeof(struct tc_action_net),
306 };
307
308 MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
309 MODULE_DESCRIPTION("SKB data mod-ing");
310 MODULE_LICENSE("GPL");
311
312 static int __init skbmod_init_module(void)
313 {
314         return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
315 }
316
317 static void __exit skbmod_cleanup_module(void)
318 {
319         tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
320 }
321
322 module_init(skbmod_init_module);
323 module_exit(skbmod_cleanup_module);