xref: /openbmc/linux/net/sched/act_skbmod.c (revision 4e8ddd7f1758ca4ddd0c1f7cf3e66fce736241d2)
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 
23 static unsigned int skbmod_net_id;
24 static struct tc_action_ops act_skbmod_ops;
25 
26 #define MAX_EDIT_LEN ETH_HLEN
27 static int tcf_skbmod_run(struct sk_buff *skb, const struct tc_action *a,
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);
44 	if (unlikely(err)) { /* best policy is to drop on the floor */
45 		qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
46 		return TC_ACT_SHOT;
47 	}
48 
49 	rcu_read_lock();
50 	action = READ_ONCE(d->tcf_action);
51 	if (unlikely(action == TC_ACT_SHOT)) {
52 		qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
53 		rcu_read_unlock();
54 		return action;
55 	}
56 
57 	p = rcu_dereference(d->skbmod_p);
58 	flags = p->flags;
59 	if (flags & SKBMOD_F_DMAC)
60 		ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
61 	if (flags & SKBMOD_F_SMAC)
62 		ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
63 	if (flags & SKBMOD_F_ETYPE)
64 		eth_hdr(skb)->h_proto = p->eth_type;
65 	rcu_read_unlock();
66 
67 	if (flags & SKBMOD_F_SWAPMAC) {
68 		u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
69 		/*XXX: I am sure we can come up with more efficient swapping*/
70 		ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
71 		ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
72 		ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
73 	}
74 
75 	return action;
76 }
77 
78 static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
79 	[TCA_SKBMOD_PARMS]		= { .len = sizeof(struct tc_skbmod) },
80 	[TCA_SKBMOD_DMAC]		= { .len = ETH_ALEN },
81 	[TCA_SKBMOD_SMAC]		= { .len = ETH_ALEN },
82 	[TCA_SKBMOD_ETYPE]		= { .type = NLA_U16 },
83 };
84 
85 static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
86 			   struct nlattr *est, struct tc_action **a,
87 			   int ovr, int bind, bool rtnl_held,
88 			   struct netlink_ext_ack *extack)
89 {
90 	struct tc_action_net *tn = net_generic(net, skbmod_net_id);
91 	struct nlattr *tb[TCA_SKBMOD_MAX + 1];
92 	struct tcf_skbmod_params *p, *p_old;
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(tb, TCA_SKBMOD_MAX, nla, skbmod_policy, NULL);
106 	if (err < 0)
107 		return err;
108 
109 	if (!tb[TCA_SKBMOD_PARMS])
110 		return -EINVAL;
111 
112 	if (tb[TCA_SKBMOD_DMAC]) {
113 		daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
114 		lflags |= SKBMOD_F_DMAC;
115 	}
116 
117 	if (tb[TCA_SKBMOD_SMAC]) {
118 		saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
119 		lflags |= SKBMOD_F_SMAC;
120 	}
121 
122 	if (tb[TCA_SKBMOD_ETYPE]) {
123 		eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
124 		lflags |= SKBMOD_F_ETYPE;
125 	}
126 
127 	parm = nla_data(tb[TCA_SKBMOD_PARMS]);
128 	if (parm->flags & SKBMOD_F_SWAPMAC)
129 		lflags = SKBMOD_F_SWAPMAC;
130 
131 	exists = tcf_idr_check(tn, parm->index, a, bind);
132 	if (exists && bind)
133 		return 0;
134 
135 	if (!lflags) {
136 		if (exists)
137 			tcf_idr_release(*a, bind);
138 		return -EINVAL;
139 	}
140 
141 	if (!exists) {
142 		ret = tcf_idr_create(tn, parm->index, est, a,
143 				     &act_skbmod_ops, bind, true);
144 		if (ret)
145 			return ret;
146 
147 		ret = ACT_P_CREATED;
148 	} else if (!ovr) {
149 		tcf_idr_release(*a, bind);
150 		return -EEXIST;
151 	}
152 
153 	d = to_skbmod(*a);
154 
155 	ASSERT_RTNL();
156 	p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
157 	if (unlikely(!p)) {
158 		tcf_idr_release(*a, bind);
159 		return -ENOMEM;
160 	}
161 
162 	p->flags = lflags;
163 	d->tcf_action = parm->action;
164 
165 	p_old = rtnl_dereference(d->skbmod_p);
166 
167 	if (ovr)
168 		spin_lock_bh(&d->tcf_lock);
169 
170 	if (lflags & SKBMOD_F_DMAC)
171 		ether_addr_copy(p->eth_dst, daddr);
172 	if (lflags & SKBMOD_F_SMAC)
173 		ether_addr_copy(p->eth_src, saddr);
174 	if (lflags & SKBMOD_F_ETYPE)
175 		p->eth_type = htons(eth_type);
176 
177 	rcu_assign_pointer(d->skbmod_p, p);
178 	if (ovr)
179 		spin_unlock_bh(&d->tcf_lock);
180 
181 	if (p_old)
182 		kfree_rcu(p_old, rcu);
183 
184 	if (ret == ACT_P_CREATED)
185 		tcf_idr_insert(tn, *a);
186 	return ret;
187 }
188 
189 static void tcf_skbmod_cleanup(struct tc_action *a)
190 {
191 	struct tcf_skbmod *d = to_skbmod(a);
192 	struct tcf_skbmod_params  *p;
193 
194 	p = rcu_dereference_protected(d->skbmod_p, 1);
195 	if (p)
196 		kfree_rcu(p, rcu);
197 }
198 
199 static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
200 			   int bind, int ref)
201 {
202 	struct tcf_skbmod *d = to_skbmod(a);
203 	unsigned char *b = skb_tail_pointer(skb);
204 	struct tcf_skbmod_params  *p = rtnl_dereference(d->skbmod_p);
205 	struct tc_skbmod opt = {
206 		.index   = d->tcf_index,
207 		.refcnt  = refcount_read(&d->tcf_refcnt) - ref,
208 		.bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
209 		.action  = d->tcf_action,
210 	};
211 	struct tcf_t t;
212 
213 	opt.flags  = p->flags;
214 	if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
215 		goto nla_put_failure;
216 	if ((p->flags & SKBMOD_F_DMAC) &&
217 	    nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
218 		goto nla_put_failure;
219 	if ((p->flags & SKBMOD_F_SMAC) &&
220 	    nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
221 		goto nla_put_failure;
222 	if ((p->flags & SKBMOD_F_ETYPE) &&
223 	    nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
224 		goto nla_put_failure;
225 
226 	tcf_tm_dump(&t, &d->tcf_tm);
227 	if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
228 		goto nla_put_failure;
229 
230 	return skb->len;
231 nla_put_failure:
232 	nlmsg_trim(skb, b);
233 	return -1;
234 }
235 
236 static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
237 			     struct netlink_callback *cb, int type,
238 			     const struct tc_action_ops *ops,
239 			     struct netlink_ext_ack *extack)
240 {
241 	struct tc_action_net *tn = net_generic(net, skbmod_net_id);
242 
243 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
244 }
245 
246 static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index,
247 			     struct netlink_ext_ack *extack)
248 {
249 	struct tc_action_net *tn = net_generic(net, skbmod_net_id);
250 
251 	return tcf_idr_search(tn, a, index);
252 }
253 
254 static int tcf_skbmod_delete(struct net *net, u32 index)
255 {
256 	struct tc_action_net *tn = net_generic(net, skbmod_net_id);
257 
258 	return tcf_idr_delete_index(tn, index);
259 }
260 
261 static struct tc_action_ops act_skbmod_ops = {
262 	.kind		=	"skbmod",
263 	.type		=	TCA_ACT_SKBMOD,
264 	.owner		=	THIS_MODULE,
265 	.act		=	tcf_skbmod_run,
266 	.dump		=	tcf_skbmod_dump,
267 	.init		=	tcf_skbmod_init,
268 	.cleanup	=	tcf_skbmod_cleanup,
269 	.walk		=	tcf_skbmod_walker,
270 	.lookup		=	tcf_skbmod_search,
271 	.delete		=	tcf_skbmod_delete,
272 	.size		=	sizeof(struct tcf_skbmod),
273 };
274 
275 static __net_init int skbmod_init_net(struct net *net)
276 {
277 	struct tc_action_net *tn = net_generic(net, skbmod_net_id);
278 
279 	return tc_action_net_init(tn, &act_skbmod_ops);
280 }
281 
282 static void __net_exit skbmod_exit_net(struct list_head *net_list)
283 {
284 	tc_action_net_exit(net_list, skbmod_net_id);
285 }
286 
287 static struct pernet_operations skbmod_net_ops = {
288 	.init = skbmod_init_net,
289 	.exit_batch = skbmod_exit_net,
290 	.id   = &skbmod_net_id,
291 	.size = sizeof(struct tc_action_net),
292 };
293 
294 MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
295 MODULE_DESCRIPTION("SKB data mod-ing");
296 MODULE_LICENSE("GPL");
297 
298 static int __init skbmod_init_module(void)
299 {
300 	return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
301 }
302 
303 static void __exit skbmod_cleanup_module(void)
304 {
305 	tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
306 }
307 
308 module_init(skbmod_init_module);
309 module_exit(skbmod_cleanup_module);
310