xref: /openbmc/linux/net/sched/act_skbedit.c (revision fceb6435e85298f747fee938415057af837f5a8a)
1ca9b0e27SAlexander Duyck /*
2ca9b0e27SAlexander Duyck  * Copyright (c) 2008, Intel Corporation.
3ca9b0e27SAlexander Duyck  *
4ca9b0e27SAlexander Duyck  * This program is free software; you can redistribute it and/or modify it
5ca9b0e27SAlexander Duyck  * under the terms and conditions of the GNU General Public License,
6ca9b0e27SAlexander Duyck  * version 2, as published by the Free Software Foundation.
7ca9b0e27SAlexander Duyck  *
8ca9b0e27SAlexander Duyck  * This program is distributed in the hope it will be useful, but WITHOUT
9ca9b0e27SAlexander Duyck  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10ca9b0e27SAlexander Duyck  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11ca9b0e27SAlexander Duyck  * more details.
12ca9b0e27SAlexander Duyck  *
13ca9b0e27SAlexander Duyck  * You should have received a copy of the GNU General Public License along with
14c057b190SJeff Kirsher  * this program; if not, see <http://www.gnu.org/licenses/>.
15ca9b0e27SAlexander Duyck  *
16ca9b0e27SAlexander Duyck  * Author: Alexander Duyck <alexander.h.duyck@intel.com>
17ca9b0e27SAlexander Duyck  */
18ca9b0e27SAlexander Duyck 
19ca9b0e27SAlexander Duyck #include <linux/module.h>
20ca9b0e27SAlexander Duyck #include <linux/init.h>
21ca9b0e27SAlexander Duyck #include <linux/kernel.h>
22ca9b0e27SAlexander Duyck #include <linux/skbuff.h>
23ca9b0e27SAlexander Duyck #include <linux/rtnetlink.h>
24ca9b0e27SAlexander Duyck #include <net/netlink.h>
25ca9b0e27SAlexander Duyck #include <net/pkt_sched.h>
26ca9b0e27SAlexander Duyck 
27ca9b0e27SAlexander Duyck #include <linux/tc_act/tc_skbedit.h>
28ca9b0e27SAlexander Duyck #include <net/tc_act/tc_skbedit.h>
29ca9b0e27SAlexander Duyck 
30ca9b0e27SAlexander Duyck #define SKBEDIT_TAB_MASK     15
31ca9b0e27SAlexander Duyck 
32c7d03a00SAlexey Dobriyan static unsigned int skbedit_net_id;
33a85a970aSWANG Cong static struct tc_action_ops act_skbedit_ops;
34ddf97ccdSWANG Cong 
35dc7f9f6eSEric Dumazet static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
36ca9b0e27SAlexander Duyck 		       struct tcf_result *res)
37ca9b0e27SAlexander Duyck {
38a85a970aSWANG Cong 	struct tcf_skbedit *d = to_skbedit(a);
39ca9b0e27SAlexander Duyck 
40ca9b0e27SAlexander Duyck 	spin_lock(&d->tcf_lock);
419c4a4e48SJamal Hadi Salim 	tcf_lastuse_update(&d->tcf_tm);
42bfe0d029SEric Dumazet 	bstats_update(&d->tcf_bstats, skb);
43ca9b0e27SAlexander Duyck 
44ca9b0e27SAlexander Duyck 	if (d->flags & SKBEDIT_F_PRIORITY)
45ca9b0e27SAlexander Duyck 		skb->priority = d->priority;
46ca9b0e27SAlexander Duyck 	if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
47ca9b0e27SAlexander Duyck 	    skb->dev->real_num_tx_queues > d->queue_mapping)
48ca9b0e27SAlexander Duyck 		skb_set_queue_mapping(skb, d->queue_mapping);
494fe77d82SAntonio Quartulli 	if (d->flags & SKBEDIT_F_MARK) {
504fe77d82SAntonio Quartulli 		skb->mark &= ~d->mask;
514fe77d82SAntonio Quartulli 		skb->mark |= d->mark & d->mask;
524fe77d82SAntonio Quartulli 	}
53ff202ee1SJamal Hadi Salim 	if (d->flags & SKBEDIT_F_PTYPE)
54ff202ee1SJamal Hadi Salim 		skb->pkt_type = d->ptype;
55ca9b0e27SAlexander Duyck 
56ca9b0e27SAlexander Duyck 	spin_unlock(&d->tcf_lock);
57ca9b0e27SAlexander Duyck 	return d->tcf_action;
58ca9b0e27SAlexander Duyck }
59ca9b0e27SAlexander Duyck 
60ca9b0e27SAlexander Duyck static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
61ca9b0e27SAlexander Duyck 	[TCA_SKBEDIT_PARMS]		= { .len = sizeof(struct tc_skbedit) },
62ca9b0e27SAlexander Duyck 	[TCA_SKBEDIT_PRIORITY]		= { .len = sizeof(u32) },
63ca9b0e27SAlexander Duyck 	[TCA_SKBEDIT_QUEUE_MAPPING]	= { .len = sizeof(u16) },
641c55d62eSjamal 	[TCA_SKBEDIT_MARK]		= { .len = sizeof(u32) },
65ff202ee1SJamal Hadi Salim 	[TCA_SKBEDIT_PTYPE]		= { .len = sizeof(u16) },
664fe77d82SAntonio Quartulli 	[TCA_SKBEDIT_MASK]		= { .len = sizeof(u32) },
67ca9b0e27SAlexander Duyck };
68ca9b0e27SAlexander Duyck 
69c1b52739SBenjamin LaHaise static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
70a85a970aSWANG Cong 			    struct nlattr *est, struct tc_action **a,
71c1b52739SBenjamin LaHaise 			    int ovr, int bind)
72ca9b0e27SAlexander Duyck {
73ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
74ca9b0e27SAlexander Duyck 	struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
75ca9b0e27SAlexander Duyck 	struct tc_skbedit *parm;
76ca9b0e27SAlexander Duyck 	struct tcf_skbedit *d;
774fe77d82SAntonio Quartulli 	u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL;
78ff202ee1SJamal Hadi Salim 	u16 *queue_mapping = NULL, *ptype = NULL;
79b2313077SWANG Cong 	bool exists = false;
80b2313077SWANG Cong 	int ret = 0, err;
81ca9b0e27SAlexander Duyck 
82ca9b0e27SAlexander Duyck 	if (nla == NULL)
83ca9b0e27SAlexander Duyck 		return -EINVAL;
84ca9b0e27SAlexander Duyck 
85*fceb6435SJohannes Berg 	err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL);
86ca9b0e27SAlexander Duyck 	if (err < 0)
87ca9b0e27SAlexander Duyck 		return err;
88ca9b0e27SAlexander Duyck 
89ca9b0e27SAlexander Duyck 	if (tb[TCA_SKBEDIT_PARMS] == NULL)
90ca9b0e27SAlexander Duyck 		return -EINVAL;
91ca9b0e27SAlexander Duyck 
92ca9b0e27SAlexander Duyck 	if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
93ca9b0e27SAlexander Duyck 		flags |= SKBEDIT_F_PRIORITY;
94ca9b0e27SAlexander Duyck 		priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
95ca9b0e27SAlexander Duyck 	}
96ca9b0e27SAlexander Duyck 
97ca9b0e27SAlexander Duyck 	if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
98ca9b0e27SAlexander Duyck 		flags |= SKBEDIT_F_QUEUE_MAPPING;
99ca9b0e27SAlexander Duyck 		queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
100ca9b0e27SAlexander Duyck 	}
1011c55d62eSjamal 
102ff202ee1SJamal Hadi Salim 	if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
103ff202ee1SJamal Hadi Salim 		ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
104ff202ee1SJamal Hadi Salim 		if (!skb_pkt_type_ok(*ptype))
105ff202ee1SJamal Hadi Salim 			return -EINVAL;
106ff202ee1SJamal Hadi Salim 		flags |= SKBEDIT_F_PTYPE;
107ff202ee1SJamal Hadi Salim 	}
108ff202ee1SJamal Hadi Salim 
1091c55d62eSjamal 	if (tb[TCA_SKBEDIT_MARK] != NULL) {
1101c55d62eSjamal 		flags |= SKBEDIT_F_MARK;
1111c55d62eSjamal 		mark = nla_data(tb[TCA_SKBEDIT_MARK]);
1121c55d62eSjamal 	}
1131c55d62eSjamal 
1144fe77d82SAntonio Quartulli 	if (tb[TCA_SKBEDIT_MASK] != NULL) {
1154fe77d82SAntonio Quartulli 		flags |= SKBEDIT_F_MASK;
1164fe77d82SAntonio Quartulli 		mask = nla_data(tb[TCA_SKBEDIT_MASK]);
1174fe77d82SAntonio Quartulli 	}
1184fe77d82SAntonio Quartulli 
119ca9b0e27SAlexander Duyck 	parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
120ca9b0e27SAlexander Duyck 
1215e1567aeSJamal Hadi Salim 	exists = tcf_hash_check(tn, parm->index, a, bind);
1225e1567aeSJamal Hadi Salim 	if (exists && bind)
1235e1567aeSJamal Hadi Salim 		return 0;
1245e1567aeSJamal Hadi Salim 
1255e1567aeSJamal Hadi Salim 	if (!flags) {
126a85a970aSWANG Cong 		tcf_hash_release(*a, bind);
1275e1567aeSJamal Hadi Salim 		return -EINVAL;
1285e1567aeSJamal Hadi Salim 	}
1295e1567aeSJamal Hadi Salim 
1305e1567aeSJamal Hadi Salim 	if (!exists) {
131ddf97ccdSWANG Cong 		ret = tcf_hash_create(tn, parm->index, est, a,
132a85a970aSWANG Cong 				      &act_skbedit_ops, bind, false);
13386062033SWANG Cong 		if (ret)
13486062033SWANG Cong 			return ret;
135ca9b0e27SAlexander Duyck 
136a85a970aSWANG Cong 		d = to_skbedit(*a);
137ca9b0e27SAlexander Duyck 		ret = ACT_P_CREATED;
138ca9b0e27SAlexander Duyck 	} else {
139a85a970aSWANG Cong 		d = to_skbedit(*a);
140a85a970aSWANG Cong 		tcf_hash_release(*a, bind);
1411a29321eSJamal Hadi Salim 		if (!ovr)
142ca9b0e27SAlexander Duyck 			return -EEXIST;
143ca9b0e27SAlexander Duyck 	}
144ca9b0e27SAlexander Duyck 
145ca9b0e27SAlexander Duyck 	spin_lock_bh(&d->tcf_lock);
146ca9b0e27SAlexander Duyck 
147ca9b0e27SAlexander Duyck 	d->flags = flags;
148ca9b0e27SAlexander Duyck 	if (flags & SKBEDIT_F_PRIORITY)
149ca9b0e27SAlexander Duyck 		d->priority = *priority;
150ca9b0e27SAlexander Duyck 	if (flags & SKBEDIT_F_QUEUE_MAPPING)
151ca9b0e27SAlexander Duyck 		d->queue_mapping = *queue_mapping;
1521c55d62eSjamal 	if (flags & SKBEDIT_F_MARK)
1531c55d62eSjamal 		d->mark = *mark;
154ff202ee1SJamal Hadi Salim 	if (flags & SKBEDIT_F_PTYPE)
155ff202ee1SJamal Hadi Salim 		d->ptype = *ptype;
1564fe77d82SAntonio Quartulli 	/* default behaviour is to use all the bits */
1574fe77d82SAntonio Quartulli 	d->mask = 0xffffffff;
1584fe77d82SAntonio Quartulli 	if (flags & SKBEDIT_F_MASK)
1594fe77d82SAntonio Quartulli 		d->mask = *mask;
1601c55d62eSjamal 
161ca9b0e27SAlexander Duyck 	d->tcf_action = parm->action;
162ca9b0e27SAlexander Duyck 
163ca9b0e27SAlexander Duyck 	spin_unlock_bh(&d->tcf_lock);
164ca9b0e27SAlexander Duyck 
165ca9b0e27SAlexander Duyck 	if (ret == ACT_P_CREATED)
166a85a970aSWANG Cong 		tcf_hash_insert(tn, *a);
167ca9b0e27SAlexander Duyck 	return ret;
168ca9b0e27SAlexander Duyck }
169ca9b0e27SAlexander Duyck 
170cc7ec456SEric Dumazet static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
171ca9b0e27SAlexander Duyck 			    int bind, int ref)
172ca9b0e27SAlexander Duyck {
173ca9b0e27SAlexander Duyck 	unsigned char *b = skb_tail_pointer(skb);
174a85a970aSWANG Cong 	struct tcf_skbedit *d = to_skbedit(a);
1751c40be12SEric Dumazet 	struct tc_skbedit opt = {
1761c40be12SEric Dumazet 		.index   = d->tcf_index,
1771c40be12SEric Dumazet 		.refcnt  = d->tcf_refcnt - ref,
1781c40be12SEric Dumazet 		.bindcnt = d->tcf_bindcnt - bind,
1791c40be12SEric Dumazet 		.action  = d->tcf_action,
1801c40be12SEric Dumazet 	};
181ca9b0e27SAlexander Duyck 	struct tcf_t t;
182ca9b0e27SAlexander Duyck 
1831b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
1841b34ec43SDavid S. Miller 		goto nla_put_failure;
1851b34ec43SDavid S. Miller 	if ((d->flags & SKBEDIT_F_PRIORITY) &&
18661cc535dSJamal Hadi Salim 	    nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, d->priority))
1871b34ec43SDavid S. Miller 		goto nla_put_failure;
1881b34ec43SDavid S. Miller 	if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
18961cc535dSJamal Hadi Salim 	    nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, d->queue_mapping))
1901b34ec43SDavid S. Miller 		goto nla_put_failure;
1911b34ec43SDavid S. Miller 	if ((d->flags & SKBEDIT_F_MARK) &&
19261cc535dSJamal Hadi Salim 	    nla_put_u32(skb, TCA_SKBEDIT_MARK, d->mark))
1931b34ec43SDavid S. Miller 		goto nla_put_failure;
194ff202ee1SJamal Hadi Salim 	if ((d->flags & SKBEDIT_F_PTYPE) &&
19561cc535dSJamal Hadi Salim 	    nla_put_u16(skb, TCA_SKBEDIT_PTYPE, d->ptype))
196ff202ee1SJamal Hadi Salim 		goto nla_put_failure;
1974fe77d82SAntonio Quartulli 	if ((d->flags & SKBEDIT_F_MASK) &&
1984fe77d82SAntonio Quartulli 	    nla_put_u32(skb, TCA_SKBEDIT_MASK, d->mask))
1994fe77d82SAntonio Quartulli 		goto nla_put_failure;
20048d8ee16SJamal Hadi Salim 
20148d8ee16SJamal Hadi Salim 	tcf_tm_dump(&t, &d->tcf_tm);
2029854518eSNicolas Dichtel 	if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
2031b34ec43SDavid S. Miller 		goto nla_put_failure;
204ca9b0e27SAlexander Duyck 	return skb->len;
205ca9b0e27SAlexander Duyck 
206ca9b0e27SAlexander Duyck nla_put_failure:
207ca9b0e27SAlexander Duyck 	nlmsg_trim(skb, b);
208ca9b0e27SAlexander Duyck 	return -1;
209ca9b0e27SAlexander Duyck }
210ca9b0e27SAlexander Duyck 
211ddf97ccdSWANG Cong static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
212ddf97ccdSWANG Cong 			      struct netlink_callback *cb, int type,
213a85a970aSWANG Cong 			      const struct tc_action_ops *ops)
214ddf97ccdSWANG Cong {
215ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
216ddf97ccdSWANG Cong 
217a85a970aSWANG Cong 	return tcf_generic_walker(tn, skb, cb, type, ops);
218ddf97ccdSWANG Cong }
219ddf97ccdSWANG Cong 
220a85a970aSWANG Cong static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index)
221ddf97ccdSWANG Cong {
222ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
223ddf97ccdSWANG Cong 
224ddf97ccdSWANG Cong 	return tcf_hash_search(tn, a, index);
225ddf97ccdSWANG Cong }
226ddf97ccdSWANG Cong 
227ca9b0e27SAlexander Duyck static struct tc_action_ops act_skbedit_ops = {
228ca9b0e27SAlexander Duyck 	.kind		=	"skbedit",
229ca9b0e27SAlexander Duyck 	.type		=	TCA_ACT_SKBEDIT,
230ca9b0e27SAlexander Duyck 	.owner		=	THIS_MODULE,
231ca9b0e27SAlexander Duyck 	.act		=	tcf_skbedit,
232ca9b0e27SAlexander Duyck 	.dump		=	tcf_skbedit_dump,
233ca9b0e27SAlexander Duyck 	.init		=	tcf_skbedit_init,
234ddf97ccdSWANG Cong 	.walk		=	tcf_skbedit_walker,
235ddf97ccdSWANG Cong 	.lookup		=	tcf_skbedit_search,
236a85a970aSWANG Cong 	.size		=	sizeof(struct tcf_skbedit),
237ddf97ccdSWANG Cong };
238ddf97ccdSWANG Cong 
239ddf97ccdSWANG Cong static __net_init int skbedit_init_net(struct net *net)
240ddf97ccdSWANG Cong {
241ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
242ddf97ccdSWANG Cong 
243ddf97ccdSWANG Cong 	return tc_action_net_init(tn, &act_skbedit_ops, SKBEDIT_TAB_MASK);
244ddf97ccdSWANG Cong }
245ddf97ccdSWANG Cong 
246ddf97ccdSWANG Cong static void __net_exit skbedit_exit_net(struct net *net)
247ddf97ccdSWANG Cong {
248ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
249ddf97ccdSWANG Cong 
250ddf97ccdSWANG Cong 	tc_action_net_exit(tn);
251ddf97ccdSWANG Cong }
252ddf97ccdSWANG Cong 
253ddf97ccdSWANG Cong static struct pernet_operations skbedit_net_ops = {
254ddf97ccdSWANG Cong 	.init = skbedit_init_net,
255ddf97ccdSWANG Cong 	.exit = skbedit_exit_net,
256ddf97ccdSWANG Cong 	.id   = &skbedit_net_id,
257ddf97ccdSWANG Cong 	.size = sizeof(struct tc_action_net),
258ca9b0e27SAlexander Duyck };
259ca9b0e27SAlexander Duyck 
260ca9b0e27SAlexander Duyck MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
261ca9b0e27SAlexander Duyck MODULE_DESCRIPTION("SKB Editing");
262ca9b0e27SAlexander Duyck MODULE_LICENSE("GPL");
263ca9b0e27SAlexander Duyck 
264ca9b0e27SAlexander Duyck static int __init skbedit_init_module(void)
265ca9b0e27SAlexander Duyck {
266ddf97ccdSWANG Cong 	return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
267ca9b0e27SAlexander Duyck }
268ca9b0e27SAlexander Duyck 
269ca9b0e27SAlexander Duyck static void __exit skbedit_cleanup_module(void)
270ca9b0e27SAlexander Duyck {
271ddf97ccdSWANG Cong 	tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
272ca9b0e27SAlexander Duyck }
273ca9b0e27SAlexander Duyck 
274ca9b0e27SAlexander Duyck module_init(skbedit_init_module);
275ca9b0e27SAlexander Duyck module_exit(skbedit_cleanup_module);
276