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