xref: /linux/net/sched/act_skbedit.c (revision ec63e2a4897075e427c121d863bd89c44578094f)
1 /*
2  * Copyright (c) 2008, Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, see <http://www.gnu.org/licenses/>.
15  *
16  * Author: Alexander Duyck <alexander.h.duyck@intel.com>
17  */
18 
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/skbuff.h>
23 #include <linux/rtnetlink.h>
24 #include <net/netlink.h>
25 #include <net/pkt_sched.h>
26 #include <net/ip.h>
27 #include <net/ipv6.h>
28 #include <net/dsfield.h>
29 #include <net/pkt_cls.h>
30 
31 #include <linux/tc_act/tc_skbedit.h>
32 #include <net/tc_act/tc_skbedit.h>
33 
34 static unsigned int skbedit_net_id;
35 static struct tc_action_ops act_skbedit_ops;
36 
37 static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a,
38 			   struct tcf_result *res)
39 {
40 	struct tcf_skbedit *d = to_skbedit(a);
41 	struct tcf_skbedit_params *params;
42 	int action;
43 
44 	tcf_lastuse_update(&d->tcf_tm);
45 	bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb);
46 
47 	params = rcu_dereference_bh(d->params);
48 	action = READ_ONCE(d->tcf_action);
49 
50 	if (params->flags & SKBEDIT_F_PRIORITY)
51 		skb->priority = params->priority;
52 	if (params->flags & SKBEDIT_F_INHERITDSFIELD) {
53 		int wlen = skb_network_offset(skb);
54 
55 		switch (tc_skb_protocol(skb)) {
56 		case htons(ETH_P_IP):
57 			wlen += sizeof(struct iphdr);
58 			if (!pskb_may_pull(skb, wlen))
59 				goto err;
60 			skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
61 			break;
62 
63 		case htons(ETH_P_IPV6):
64 			wlen += sizeof(struct ipv6hdr);
65 			if (!pskb_may_pull(skb, wlen))
66 				goto err;
67 			skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
68 			break;
69 		}
70 	}
71 	if (params->flags & SKBEDIT_F_QUEUE_MAPPING &&
72 	    skb->dev->real_num_tx_queues > params->queue_mapping)
73 		skb_set_queue_mapping(skb, params->queue_mapping);
74 	if (params->flags & SKBEDIT_F_MARK) {
75 		skb->mark &= ~params->mask;
76 		skb->mark |= params->mark & params->mask;
77 	}
78 	if (params->flags & SKBEDIT_F_PTYPE)
79 		skb->pkt_type = params->ptype;
80 	return action;
81 
82 err:
83 	qstats_drop_inc(this_cpu_ptr(d->common.cpu_qstats));
84 	return TC_ACT_SHOT;
85 }
86 
87 static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
88 	[TCA_SKBEDIT_PARMS]		= { .len = sizeof(struct tc_skbedit) },
89 	[TCA_SKBEDIT_PRIORITY]		= { .len = sizeof(u32) },
90 	[TCA_SKBEDIT_QUEUE_MAPPING]	= { .len = sizeof(u16) },
91 	[TCA_SKBEDIT_MARK]		= { .len = sizeof(u32) },
92 	[TCA_SKBEDIT_PTYPE]		= { .len = sizeof(u16) },
93 	[TCA_SKBEDIT_MASK]		= { .len = sizeof(u32) },
94 	[TCA_SKBEDIT_FLAGS]		= { .len = sizeof(u64) },
95 };
96 
97 static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
98 			    struct nlattr *est, struct tc_action **a,
99 			    int ovr, int bind, bool rtnl_held,
100 			    struct tcf_proto *tp,
101 			    struct netlink_ext_ack *extack)
102 {
103 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
104 	struct tcf_skbedit_params *params_new;
105 	struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
106 	struct tcf_chain *goto_ch = NULL;
107 	struct tc_skbedit *parm;
108 	struct tcf_skbedit *d;
109 	u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL;
110 	u16 *queue_mapping = NULL, *ptype = NULL;
111 	bool exists = false;
112 	int ret = 0, err;
113 
114 	if (nla == NULL)
115 		return -EINVAL;
116 
117 	err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL);
118 	if (err < 0)
119 		return err;
120 
121 	if (tb[TCA_SKBEDIT_PARMS] == NULL)
122 		return -EINVAL;
123 
124 	if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
125 		flags |= SKBEDIT_F_PRIORITY;
126 		priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
127 	}
128 
129 	if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
130 		flags |= SKBEDIT_F_QUEUE_MAPPING;
131 		queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
132 	}
133 
134 	if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
135 		ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
136 		if (!skb_pkt_type_ok(*ptype))
137 			return -EINVAL;
138 		flags |= SKBEDIT_F_PTYPE;
139 	}
140 
141 	if (tb[TCA_SKBEDIT_MARK] != NULL) {
142 		flags |= SKBEDIT_F_MARK;
143 		mark = nla_data(tb[TCA_SKBEDIT_MARK]);
144 	}
145 
146 	if (tb[TCA_SKBEDIT_MASK] != NULL) {
147 		flags |= SKBEDIT_F_MASK;
148 		mask = nla_data(tb[TCA_SKBEDIT_MASK]);
149 	}
150 
151 	if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
152 		u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]);
153 
154 		if (*pure_flags & SKBEDIT_F_INHERITDSFIELD)
155 			flags |= SKBEDIT_F_INHERITDSFIELD;
156 	}
157 
158 	parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
159 
160 	err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
161 	if (err < 0)
162 		return err;
163 	exists = err;
164 	if (exists && bind)
165 		return 0;
166 
167 	if (!flags) {
168 		if (exists)
169 			tcf_idr_release(*a, bind);
170 		else
171 			tcf_idr_cleanup(tn, parm->index);
172 		return -EINVAL;
173 	}
174 
175 	if (!exists) {
176 		ret = tcf_idr_create(tn, parm->index, est, a,
177 				     &act_skbedit_ops, bind, true);
178 		if (ret) {
179 			tcf_idr_cleanup(tn, parm->index);
180 			return ret;
181 		}
182 
183 		d = to_skbedit(*a);
184 		ret = ACT_P_CREATED;
185 	} else {
186 		d = to_skbedit(*a);
187 		if (!ovr) {
188 			tcf_idr_release(*a, bind);
189 			return -EEXIST;
190 		}
191 	}
192 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
193 	if (err < 0)
194 		goto release_idr;
195 
196 	params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
197 	if (unlikely(!params_new)) {
198 		err = -ENOMEM;
199 		goto put_chain;
200 	}
201 
202 	params_new->flags = flags;
203 	if (flags & SKBEDIT_F_PRIORITY)
204 		params_new->priority = *priority;
205 	if (flags & SKBEDIT_F_QUEUE_MAPPING)
206 		params_new->queue_mapping = *queue_mapping;
207 	if (flags & SKBEDIT_F_MARK)
208 		params_new->mark = *mark;
209 	if (flags & SKBEDIT_F_PTYPE)
210 		params_new->ptype = *ptype;
211 	/* default behaviour is to use all the bits */
212 	params_new->mask = 0xffffffff;
213 	if (flags & SKBEDIT_F_MASK)
214 		params_new->mask = *mask;
215 
216 	spin_lock_bh(&d->tcf_lock);
217 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
218 	rcu_swap_protected(d->params, params_new,
219 			   lockdep_is_held(&d->tcf_lock));
220 	spin_unlock_bh(&d->tcf_lock);
221 	if (params_new)
222 		kfree_rcu(params_new, rcu);
223 	if (goto_ch)
224 		tcf_chain_put_by_act(goto_ch);
225 
226 	if (ret == ACT_P_CREATED)
227 		tcf_idr_insert(tn, *a);
228 	return ret;
229 put_chain:
230 	if (goto_ch)
231 		tcf_chain_put_by_act(goto_ch);
232 release_idr:
233 	tcf_idr_release(*a, bind);
234 	return err;
235 }
236 
237 static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
238 			    int bind, int ref)
239 {
240 	unsigned char *b = skb_tail_pointer(skb);
241 	struct tcf_skbedit *d = to_skbedit(a);
242 	struct tcf_skbedit_params *params;
243 	struct tc_skbedit opt = {
244 		.index   = d->tcf_index,
245 		.refcnt  = refcount_read(&d->tcf_refcnt) - ref,
246 		.bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
247 	};
248 	u64 pure_flags = 0;
249 	struct tcf_t t;
250 
251 	spin_lock_bh(&d->tcf_lock);
252 	params = rcu_dereference_protected(d->params,
253 					   lockdep_is_held(&d->tcf_lock));
254 	opt.action = d->tcf_action;
255 
256 	if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
257 		goto nla_put_failure;
258 	if ((params->flags & SKBEDIT_F_PRIORITY) &&
259 	    nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority))
260 		goto nla_put_failure;
261 	if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) &&
262 	    nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping))
263 		goto nla_put_failure;
264 	if ((params->flags & SKBEDIT_F_MARK) &&
265 	    nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark))
266 		goto nla_put_failure;
267 	if ((params->flags & SKBEDIT_F_PTYPE) &&
268 	    nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype))
269 		goto nla_put_failure;
270 	if ((params->flags & SKBEDIT_F_MASK) &&
271 	    nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask))
272 		goto nla_put_failure;
273 	if (params->flags & SKBEDIT_F_INHERITDSFIELD)
274 		pure_flags |= SKBEDIT_F_INHERITDSFIELD;
275 	if (pure_flags != 0 &&
276 	    nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags))
277 		goto nla_put_failure;
278 
279 	tcf_tm_dump(&t, &d->tcf_tm);
280 	if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
281 		goto nla_put_failure;
282 	spin_unlock_bh(&d->tcf_lock);
283 
284 	return skb->len;
285 
286 nla_put_failure:
287 	spin_unlock_bh(&d->tcf_lock);
288 	nlmsg_trim(skb, b);
289 	return -1;
290 }
291 
292 static void tcf_skbedit_cleanup(struct tc_action *a)
293 {
294 	struct tcf_skbedit *d = to_skbedit(a);
295 	struct tcf_skbedit_params *params;
296 
297 	params = rcu_dereference_protected(d->params, 1);
298 	if (params)
299 		kfree_rcu(params, rcu);
300 }
301 
302 static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
303 			      struct netlink_callback *cb, int type,
304 			      const struct tc_action_ops *ops,
305 			      struct netlink_ext_ack *extack)
306 {
307 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
308 
309 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
310 }
311 
312 static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index)
313 {
314 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
315 
316 	return tcf_idr_search(tn, a, index);
317 }
318 
319 static struct tc_action_ops act_skbedit_ops = {
320 	.kind		=	"skbedit",
321 	.id		=	TCA_ID_SKBEDIT,
322 	.owner		=	THIS_MODULE,
323 	.act		=	tcf_skbedit_act,
324 	.dump		=	tcf_skbedit_dump,
325 	.init		=	tcf_skbedit_init,
326 	.cleanup	=	tcf_skbedit_cleanup,
327 	.walk		=	tcf_skbedit_walker,
328 	.lookup		=	tcf_skbedit_search,
329 	.size		=	sizeof(struct tcf_skbedit),
330 };
331 
332 static __net_init int skbedit_init_net(struct net *net)
333 {
334 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
335 
336 	return tc_action_net_init(tn, &act_skbedit_ops);
337 }
338 
339 static void __net_exit skbedit_exit_net(struct list_head *net_list)
340 {
341 	tc_action_net_exit(net_list, skbedit_net_id);
342 }
343 
344 static struct pernet_operations skbedit_net_ops = {
345 	.init = skbedit_init_net,
346 	.exit_batch = skbedit_exit_net,
347 	.id   = &skbedit_net_id,
348 	.size = sizeof(struct tc_action_net),
349 };
350 
351 MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
352 MODULE_DESCRIPTION("SKB Editing");
353 MODULE_LICENSE("GPL");
354 
355 static int __init skbedit_init_module(void)
356 {
357 	return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
358 }
359 
360 static void __exit skbedit_cleanup_module(void)
361 {
362 	tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
363 }
364 
365 module_init(skbedit_init_module);
366 module_exit(skbedit_cleanup_module);
367