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