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