xref: /openbmc/linux/net/sched/act_mirred.c (revision 96de2506)
1 /*
2  * net/sched/act_mirred.c	packet mirroring and redirect actions
3  *
4  *		This program is free software; you can redistribute it and/or
5  *		modify it under the terms of the GNU General Public License
6  *		as published by the Free Software Foundation; either version
7  *		2 of the License, or (at your option) any later version.
8  *
9  * Authors:	Jamal Hadi Salim (2002-4)
10  *
11  * TODO: Add ingress support (and socket redirect support)
12  *
13  */
14 
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/skbuff.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/gfp.h>
24 #include <linux/if_arp.h>
25 #include <net/net_namespace.h>
26 #include <net/netlink.h>
27 #include <net/pkt_sched.h>
28 #include <net/pkt_cls.h>
29 #include <linux/tc_act/tc_mirred.h>
30 #include <net/tc_act/tc_mirred.h>
31 
32 static LIST_HEAD(mirred_list);
33 static DEFINE_SPINLOCK(mirred_list_lock);
34 
35 static bool tcf_mirred_is_act_redirect(int action)
36 {
37 	return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
38 }
39 
40 static bool tcf_mirred_act_wants_ingress(int action)
41 {
42 	switch (action) {
43 	case TCA_EGRESS_REDIR:
44 	case TCA_EGRESS_MIRROR:
45 		return false;
46 	case TCA_INGRESS_REDIR:
47 	case TCA_INGRESS_MIRROR:
48 		return true;
49 	default:
50 		BUG();
51 	}
52 }
53 
54 static bool tcf_mirred_can_reinsert(int action)
55 {
56 	switch (action) {
57 	case TC_ACT_SHOT:
58 	case TC_ACT_STOLEN:
59 	case TC_ACT_QUEUED:
60 	case TC_ACT_TRAP:
61 		return true;
62 	}
63 	return false;
64 }
65 
66 static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
67 {
68 	return rcu_dereference_protected(m->tcfm_dev,
69 					 lockdep_is_held(&m->tcf_lock));
70 }
71 
72 static void tcf_mirred_release(struct tc_action *a)
73 {
74 	struct tcf_mirred *m = to_mirred(a);
75 	struct net_device *dev;
76 
77 	spin_lock(&mirred_list_lock);
78 	list_del(&m->tcfm_list);
79 	spin_unlock(&mirred_list_lock);
80 
81 	/* last reference to action, no need to lock */
82 	dev = rcu_dereference_protected(m->tcfm_dev, 1);
83 	if (dev)
84 		dev_put(dev);
85 }
86 
87 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
88 	[TCA_MIRRED_PARMS]	= { .len = sizeof(struct tc_mirred) },
89 };
90 
91 static unsigned int mirred_net_id;
92 static struct tc_action_ops act_mirred_ops;
93 
94 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
95 			   struct nlattr *est, struct tc_action **a,
96 			   int ovr, int bind, bool rtnl_held,
97 			   struct netlink_ext_ack *extack)
98 {
99 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
100 	struct nlattr *tb[TCA_MIRRED_MAX + 1];
101 	bool mac_header_xmit = false;
102 	struct tc_mirred *parm;
103 	struct tcf_mirred *m;
104 	struct net_device *dev;
105 	bool exists = false;
106 	int ret, err;
107 
108 	if (!nla) {
109 		NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
110 		return -EINVAL;
111 	}
112 	ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, extack);
113 	if (ret < 0)
114 		return ret;
115 	if (!tb[TCA_MIRRED_PARMS]) {
116 		NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
117 		return -EINVAL;
118 	}
119 	parm = nla_data(tb[TCA_MIRRED_PARMS]);
120 
121 	err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
122 	if (err < 0)
123 		return err;
124 	exists = err;
125 	if (exists && bind)
126 		return 0;
127 
128 	switch (parm->eaction) {
129 	case TCA_EGRESS_MIRROR:
130 	case TCA_EGRESS_REDIR:
131 	case TCA_INGRESS_REDIR:
132 	case TCA_INGRESS_MIRROR:
133 		break;
134 	default:
135 		if (exists)
136 			tcf_idr_release(*a, bind);
137 		else
138 			tcf_idr_cleanup(tn, parm->index);
139 		NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
140 		return -EINVAL;
141 	}
142 
143 	if (!exists) {
144 		if (!parm->ifindex) {
145 			tcf_idr_cleanup(tn, parm->index);
146 			NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
147 			return -EINVAL;
148 		}
149 		ret = tcf_idr_create(tn, parm->index, est, a,
150 				     &act_mirred_ops, bind, true);
151 		if (ret) {
152 			tcf_idr_cleanup(tn, parm->index);
153 			return ret;
154 		}
155 		ret = ACT_P_CREATED;
156 	} else if (!ovr) {
157 		tcf_idr_release(*a, bind);
158 		return -EEXIST;
159 	}
160 	m = to_mirred(*a);
161 
162 	spin_lock_bh(&m->tcf_lock);
163 	m->tcf_action = parm->action;
164 	m->tcfm_eaction = parm->eaction;
165 
166 	if (parm->ifindex) {
167 		dev = dev_get_by_index(net, parm->ifindex);
168 		if (!dev) {
169 			spin_unlock_bh(&m->tcf_lock);
170 			tcf_idr_release(*a, bind);
171 			return -ENODEV;
172 		}
173 		mac_header_xmit = dev_is_mac_header_xmit(dev);
174 		rcu_swap_protected(m->tcfm_dev, dev,
175 				   lockdep_is_held(&m->tcf_lock));
176 		if (dev)
177 			dev_put(dev);
178 		m->tcfm_mac_header_xmit = mac_header_xmit;
179 	}
180 	spin_unlock_bh(&m->tcf_lock);
181 
182 	if (ret == ACT_P_CREATED) {
183 		spin_lock(&mirred_list_lock);
184 		list_add(&m->tcfm_list, &mirred_list);
185 		spin_unlock(&mirred_list_lock);
186 
187 		tcf_idr_insert(tn, *a);
188 	}
189 
190 	return ret;
191 }
192 
193 static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
194 			  struct tcf_result *res)
195 {
196 	struct tcf_mirred *m = to_mirred(a);
197 	struct sk_buff *skb2 = skb;
198 	bool m_mac_header_xmit;
199 	struct net_device *dev;
200 	int retval, err = 0;
201 	bool use_reinsert;
202 	bool want_ingress;
203 	bool is_redirect;
204 	int m_eaction;
205 	int mac_len;
206 
207 	tcf_lastuse_update(&m->tcf_tm);
208 	bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
209 
210 	m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
211 	m_eaction = READ_ONCE(m->tcfm_eaction);
212 	retval = READ_ONCE(m->tcf_action);
213 	dev = rcu_dereference_bh(m->tcfm_dev);
214 	if (unlikely(!dev)) {
215 		pr_notice_once("tc mirred: target device is gone\n");
216 		goto out;
217 	}
218 
219 	if (unlikely(!(dev->flags & IFF_UP))) {
220 		net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
221 				       dev->name);
222 		goto out;
223 	}
224 
225 	/* we could easily avoid the clone only if called by ingress and clsact;
226 	 * since we can't easily detect the clsact caller, skip clone only for
227 	 * ingress - that covers the TC S/W datapath.
228 	 */
229 	is_redirect = tcf_mirred_is_act_redirect(m_eaction);
230 	use_reinsert = skb_at_tc_ingress(skb) && is_redirect &&
231 		       tcf_mirred_can_reinsert(retval);
232 	if (!use_reinsert) {
233 		skb2 = skb_clone(skb, GFP_ATOMIC);
234 		if (!skb2)
235 			goto out;
236 	}
237 
238 	/* If action's target direction differs than filter's direction,
239 	 * and devices expect a mac header on xmit, then mac push/pull is
240 	 * needed.
241 	 */
242 	want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
243 	if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) {
244 		if (!skb_at_tc_ingress(skb)) {
245 			/* caught at egress, act ingress: pull mac */
246 			mac_len = skb_network_header(skb) - skb_mac_header(skb);
247 			skb_pull_rcsum(skb2, mac_len);
248 		} else {
249 			/* caught at ingress, act egress: push mac */
250 			skb_push_rcsum(skb2, skb->mac_len);
251 		}
252 	}
253 
254 	skb2->skb_iif = skb->dev->ifindex;
255 	skb2->dev = dev;
256 
257 	/* mirror is always swallowed */
258 	if (is_redirect) {
259 		skb2->tc_redirected = 1;
260 		skb2->tc_from_ingress = skb2->tc_at_ingress;
261 
262 		/* let's the caller reinsert the packet, if possible */
263 		if (use_reinsert) {
264 			res->ingress = want_ingress;
265 			res->qstats = this_cpu_ptr(m->common.cpu_qstats);
266 			return TC_ACT_REINSERT;
267 		}
268 	}
269 
270 	if (!want_ingress)
271 		err = dev_queue_xmit(skb2);
272 	else
273 		err = netif_receive_skb(skb2);
274 
275 	if (err) {
276 out:
277 		qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
278 		if (tcf_mirred_is_act_redirect(m_eaction))
279 			retval = TC_ACT_SHOT;
280 	}
281 
282 	return retval;
283 }
284 
285 static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
286 			     u64 lastuse, bool hw)
287 {
288 	struct tcf_mirred *m = to_mirred(a);
289 	struct tcf_t *tm = &m->tcf_tm;
290 
291 	_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
292 	if (hw)
293 		_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
294 				   bytes, packets);
295 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
296 }
297 
298 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
299 			   int ref)
300 {
301 	unsigned char *b = skb_tail_pointer(skb);
302 	struct tcf_mirred *m = to_mirred(a);
303 	struct tc_mirred opt = {
304 		.index   = m->tcf_index,
305 		.refcnt  = refcount_read(&m->tcf_refcnt) - ref,
306 		.bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
307 	};
308 	struct net_device *dev;
309 	struct tcf_t t;
310 
311 	spin_lock_bh(&m->tcf_lock);
312 	opt.action = m->tcf_action;
313 	opt.eaction = m->tcfm_eaction;
314 	dev = tcf_mirred_dev_dereference(m);
315 	if (dev)
316 		opt.ifindex = dev->ifindex;
317 
318 	if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
319 		goto nla_put_failure;
320 
321 	tcf_tm_dump(&t, &m->tcf_tm);
322 	if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
323 		goto nla_put_failure;
324 	spin_unlock_bh(&m->tcf_lock);
325 
326 	return skb->len;
327 
328 nla_put_failure:
329 	spin_unlock_bh(&m->tcf_lock);
330 	nlmsg_trim(skb, b);
331 	return -1;
332 }
333 
334 static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
335 			     struct netlink_callback *cb, int type,
336 			     const struct tc_action_ops *ops,
337 			     struct netlink_ext_ack *extack)
338 {
339 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
340 
341 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
342 }
343 
344 static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
345 {
346 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
347 
348 	return tcf_idr_search(tn, a, index);
349 }
350 
351 static int mirred_device_event(struct notifier_block *unused,
352 			       unsigned long event, void *ptr)
353 {
354 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
355 	struct tcf_mirred *m;
356 
357 	ASSERT_RTNL();
358 	if (event == NETDEV_UNREGISTER) {
359 		spin_lock(&mirred_list_lock);
360 		list_for_each_entry(m, &mirred_list, tcfm_list) {
361 			spin_lock_bh(&m->tcf_lock);
362 			if (tcf_mirred_dev_dereference(m) == dev) {
363 				dev_put(dev);
364 				/* Note : no rcu grace period necessary, as
365 				 * net_device are already rcu protected.
366 				 */
367 				RCU_INIT_POINTER(m->tcfm_dev, NULL);
368 			}
369 			spin_unlock_bh(&m->tcf_lock);
370 		}
371 		spin_unlock(&mirred_list_lock);
372 	}
373 
374 	return NOTIFY_DONE;
375 }
376 
377 static struct notifier_block mirred_device_notifier = {
378 	.notifier_call = mirred_device_event,
379 };
380 
381 static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
382 {
383 	struct tcf_mirred *m = to_mirred(a);
384 	struct net_device *dev;
385 
386 	rcu_read_lock();
387 	dev = rcu_dereference(m->tcfm_dev);
388 	if (dev)
389 		dev_hold(dev);
390 	rcu_read_unlock();
391 
392 	return dev;
393 }
394 
395 static void tcf_mirred_put_dev(struct net_device *dev)
396 {
397 	dev_put(dev);
398 }
399 
400 static struct tc_action_ops act_mirred_ops = {
401 	.kind		=	"mirred",
402 	.type		=	TCA_ACT_MIRRED,
403 	.owner		=	THIS_MODULE,
404 	.act		=	tcf_mirred_act,
405 	.stats_update	=	tcf_stats_update,
406 	.dump		=	tcf_mirred_dump,
407 	.cleanup	=	tcf_mirred_release,
408 	.init		=	tcf_mirred_init,
409 	.walk		=	tcf_mirred_walker,
410 	.lookup		=	tcf_mirred_search,
411 	.size		=	sizeof(struct tcf_mirred),
412 	.get_dev	=	tcf_mirred_get_dev,
413 	.put_dev	=	tcf_mirred_put_dev,
414 };
415 
416 static __net_init int mirred_init_net(struct net *net)
417 {
418 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
419 
420 	return tc_action_net_init(tn, &act_mirred_ops);
421 }
422 
423 static void __net_exit mirred_exit_net(struct list_head *net_list)
424 {
425 	tc_action_net_exit(net_list, mirred_net_id);
426 }
427 
428 static struct pernet_operations mirred_net_ops = {
429 	.init = mirred_init_net,
430 	.exit_batch = mirred_exit_net,
431 	.id   = &mirred_net_id,
432 	.size = sizeof(struct tc_action_net),
433 };
434 
435 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
436 MODULE_DESCRIPTION("Device Mirror/redirect actions");
437 MODULE_LICENSE("GPL");
438 
439 static int __init mirred_init_module(void)
440 {
441 	int err = register_netdevice_notifier(&mirred_device_notifier);
442 	if (err)
443 		return err;
444 
445 	pr_info("Mirror/redirect action on\n");
446 	return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
447 }
448 
449 static void __exit mirred_cleanup_module(void)
450 {
451 	tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
452 	unregister_netdevice_notifier(&mirred_device_notifier);
453 }
454 
455 module_init(mirred_init_module);
456 module_exit(mirred_cleanup_module);
457