xref: /openbmc/linux/net/sched/act_mirred.c (revision 4fc4dca8)
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 tcf_proto *tp,
98 			   struct netlink_ext_ack *extack)
99 {
100 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
101 	struct nlattr *tb[TCA_MIRRED_MAX + 1];
102 	struct tcf_chain *goto_ch = NULL;
103 	bool mac_header_xmit = false;
104 	struct tc_mirred *parm;
105 	struct tcf_mirred *m;
106 	struct net_device *dev;
107 	bool exists = false;
108 	int ret, err;
109 
110 	if (!nla) {
111 		NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
112 		return -EINVAL;
113 	}
114 	ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla,
115 					  mirred_policy, extack);
116 	if (ret < 0)
117 		return ret;
118 	if (!tb[TCA_MIRRED_PARMS]) {
119 		NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
120 		return -EINVAL;
121 	}
122 	parm = nla_data(tb[TCA_MIRRED_PARMS]);
123 
124 	err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
125 	if (err < 0)
126 		return err;
127 	exists = err;
128 	if (exists && bind)
129 		return 0;
130 
131 	switch (parm->eaction) {
132 	case TCA_EGRESS_MIRROR:
133 	case TCA_EGRESS_REDIR:
134 	case TCA_INGRESS_REDIR:
135 	case TCA_INGRESS_MIRROR:
136 		break;
137 	default:
138 		if (exists)
139 			tcf_idr_release(*a, bind);
140 		else
141 			tcf_idr_cleanup(tn, parm->index);
142 		NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
143 		return -EINVAL;
144 	}
145 
146 	if (!exists) {
147 		if (!parm->ifindex) {
148 			tcf_idr_cleanup(tn, parm->index);
149 			NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
150 			return -EINVAL;
151 		}
152 		ret = tcf_idr_create(tn, parm->index, est, a,
153 				     &act_mirred_ops, bind, true);
154 		if (ret) {
155 			tcf_idr_cleanup(tn, parm->index);
156 			return ret;
157 		}
158 		ret = ACT_P_CREATED;
159 	} else if (!ovr) {
160 		tcf_idr_release(*a, bind);
161 		return -EEXIST;
162 	}
163 
164 	m = to_mirred(*a);
165 	if (ret == ACT_P_CREATED)
166 		INIT_LIST_HEAD(&m->tcfm_list);
167 
168 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
169 	if (err < 0)
170 		goto release_idr;
171 
172 	spin_lock_bh(&m->tcf_lock);
173 
174 	if (parm->ifindex) {
175 		dev = dev_get_by_index(net, parm->ifindex);
176 		if (!dev) {
177 			spin_unlock_bh(&m->tcf_lock);
178 			err = -ENODEV;
179 			goto put_chain;
180 		}
181 		mac_header_xmit = dev_is_mac_header_xmit(dev);
182 		rcu_swap_protected(m->tcfm_dev, dev,
183 				   lockdep_is_held(&m->tcf_lock));
184 		if (dev)
185 			dev_put(dev);
186 		m->tcfm_mac_header_xmit = mac_header_xmit;
187 	}
188 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
189 	m->tcfm_eaction = parm->eaction;
190 	spin_unlock_bh(&m->tcf_lock);
191 	if (goto_ch)
192 		tcf_chain_put_by_act(goto_ch);
193 
194 	if (ret == ACT_P_CREATED) {
195 		spin_lock(&mirred_list_lock);
196 		list_add(&m->tcfm_list, &mirred_list);
197 		spin_unlock(&mirred_list_lock);
198 
199 		tcf_idr_insert(tn, *a);
200 	}
201 
202 	return ret;
203 put_chain:
204 	if (goto_ch)
205 		tcf_chain_put_by_act(goto_ch);
206 release_idr:
207 	tcf_idr_release(*a, bind);
208 	return err;
209 }
210 
211 static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
212 			  struct tcf_result *res)
213 {
214 	struct tcf_mirred *m = to_mirred(a);
215 	struct sk_buff *skb2 = skb;
216 	bool m_mac_header_xmit;
217 	struct net_device *dev;
218 	int retval, err = 0;
219 	bool use_reinsert;
220 	bool want_ingress;
221 	bool is_redirect;
222 	int m_eaction;
223 	int mac_len;
224 
225 	tcf_lastuse_update(&m->tcf_tm);
226 	bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
227 
228 	m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
229 	m_eaction = READ_ONCE(m->tcfm_eaction);
230 	retval = READ_ONCE(m->tcf_action);
231 	dev = rcu_dereference_bh(m->tcfm_dev);
232 	if (unlikely(!dev)) {
233 		pr_notice_once("tc mirred: target device is gone\n");
234 		goto out;
235 	}
236 
237 	if (unlikely(!(dev->flags & IFF_UP))) {
238 		net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
239 				       dev->name);
240 		goto out;
241 	}
242 
243 	/* we could easily avoid the clone only if called by ingress and clsact;
244 	 * since we can't easily detect the clsact caller, skip clone only for
245 	 * ingress - that covers the TC S/W datapath.
246 	 */
247 	is_redirect = tcf_mirred_is_act_redirect(m_eaction);
248 	use_reinsert = skb_at_tc_ingress(skb) && is_redirect &&
249 		       tcf_mirred_can_reinsert(retval);
250 	if (!use_reinsert) {
251 		skb2 = skb_clone(skb, GFP_ATOMIC);
252 		if (!skb2)
253 			goto out;
254 	}
255 
256 	/* If action's target direction differs than filter's direction,
257 	 * and devices expect a mac header on xmit, then mac push/pull is
258 	 * needed.
259 	 */
260 	want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
261 	if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) {
262 		if (!skb_at_tc_ingress(skb)) {
263 			/* caught at egress, act ingress: pull mac */
264 			mac_len = skb_network_header(skb) - skb_mac_header(skb);
265 			skb_pull_rcsum(skb2, mac_len);
266 		} else {
267 			/* caught at ingress, act egress: push mac */
268 			skb_push_rcsum(skb2, skb->mac_len);
269 		}
270 	}
271 
272 	skb2->skb_iif = skb->dev->ifindex;
273 	skb2->dev = dev;
274 
275 	/* mirror is always swallowed */
276 	if (is_redirect) {
277 		skb2->tc_redirected = 1;
278 		skb2->tc_from_ingress = skb2->tc_at_ingress;
279 		if (skb2->tc_from_ingress)
280 			skb2->tstamp = 0;
281 		/* let's the caller reinsert the packet, if possible */
282 		if (use_reinsert) {
283 			res->ingress = want_ingress;
284 			res->qstats = this_cpu_ptr(m->common.cpu_qstats);
285 			return TC_ACT_REINSERT;
286 		}
287 	}
288 
289 	if (!want_ingress)
290 		err = dev_queue_xmit(skb2);
291 	else
292 		err = netif_receive_skb(skb2);
293 
294 	if (err) {
295 out:
296 		qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
297 		if (tcf_mirred_is_act_redirect(m_eaction))
298 			retval = TC_ACT_SHOT;
299 	}
300 
301 	return retval;
302 }
303 
304 static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
305 			     u64 lastuse, bool hw)
306 {
307 	struct tcf_mirred *m = to_mirred(a);
308 	struct tcf_t *tm = &m->tcf_tm;
309 
310 	_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
311 	if (hw)
312 		_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
313 				   bytes, packets);
314 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
315 }
316 
317 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
318 			   int ref)
319 {
320 	unsigned char *b = skb_tail_pointer(skb);
321 	struct tcf_mirred *m = to_mirred(a);
322 	struct tc_mirred opt = {
323 		.index   = m->tcf_index,
324 		.refcnt  = refcount_read(&m->tcf_refcnt) - ref,
325 		.bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
326 	};
327 	struct net_device *dev;
328 	struct tcf_t t;
329 
330 	spin_lock_bh(&m->tcf_lock);
331 	opt.action = m->tcf_action;
332 	opt.eaction = m->tcfm_eaction;
333 	dev = tcf_mirred_dev_dereference(m);
334 	if (dev)
335 		opt.ifindex = dev->ifindex;
336 
337 	if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
338 		goto nla_put_failure;
339 
340 	tcf_tm_dump(&t, &m->tcf_tm);
341 	if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
342 		goto nla_put_failure;
343 	spin_unlock_bh(&m->tcf_lock);
344 
345 	return skb->len;
346 
347 nla_put_failure:
348 	spin_unlock_bh(&m->tcf_lock);
349 	nlmsg_trim(skb, b);
350 	return -1;
351 }
352 
353 static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
354 			     struct netlink_callback *cb, int type,
355 			     const struct tc_action_ops *ops,
356 			     struct netlink_ext_ack *extack)
357 {
358 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
359 
360 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
361 }
362 
363 static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
364 {
365 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
366 
367 	return tcf_idr_search(tn, a, index);
368 }
369 
370 static int mirred_device_event(struct notifier_block *unused,
371 			       unsigned long event, void *ptr)
372 {
373 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
374 	struct tcf_mirred *m;
375 
376 	ASSERT_RTNL();
377 	if (event == NETDEV_UNREGISTER) {
378 		spin_lock(&mirred_list_lock);
379 		list_for_each_entry(m, &mirred_list, tcfm_list) {
380 			spin_lock_bh(&m->tcf_lock);
381 			if (tcf_mirred_dev_dereference(m) == dev) {
382 				dev_put(dev);
383 				/* Note : no rcu grace period necessary, as
384 				 * net_device are already rcu protected.
385 				 */
386 				RCU_INIT_POINTER(m->tcfm_dev, NULL);
387 			}
388 			spin_unlock_bh(&m->tcf_lock);
389 		}
390 		spin_unlock(&mirred_list_lock);
391 	}
392 
393 	return NOTIFY_DONE;
394 }
395 
396 static struct notifier_block mirred_device_notifier = {
397 	.notifier_call = mirred_device_event,
398 };
399 
400 static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
401 {
402 	struct tcf_mirred *m = to_mirred(a);
403 	struct net_device *dev;
404 
405 	rcu_read_lock();
406 	dev = rcu_dereference(m->tcfm_dev);
407 	if (dev)
408 		dev_hold(dev);
409 	rcu_read_unlock();
410 
411 	return dev;
412 }
413 
414 static void tcf_mirred_put_dev(struct net_device *dev)
415 {
416 	dev_put(dev);
417 }
418 
419 static struct tc_action_ops act_mirred_ops = {
420 	.kind		=	"mirred",
421 	.id		=	TCA_ID_MIRRED,
422 	.owner		=	THIS_MODULE,
423 	.act		=	tcf_mirred_act,
424 	.stats_update	=	tcf_stats_update,
425 	.dump		=	tcf_mirred_dump,
426 	.cleanup	=	tcf_mirred_release,
427 	.init		=	tcf_mirred_init,
428 	.walk		=	tcf_mirred_walker,
429 	.lookup		=	tcf_mirred_search,
430 	.size		=	sizeof(struct tcf_mirred),
431 	.get_dev	=	tcf_mirred_get_dev,
432 	.put_dev	=	tcf_mirred_put_dev,
433 };
434 
435 static __net_init int mirred_init_net(struct net *net)
436 {
437 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
438 
439 	return tc_action_net_init(tn, &act_mirred_ops);
440 }
441 
442 static void __net_exit mirred_exit_net(struct list_head *net_list)
443 {
444 	tc_action_net_exit(net_list, mirred_net_id);
445 }
446 
447 static struct pernet_operations mirred_net_ops = {
448 	.init = mirred_init_net,
449 	.exit_batch = mirred_exit_net,
450 	.id   = &mirred_net_id,
451 	.size = sizeof(struct tc_action_net),
452 };
453 
454 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
455 MODULE_DESCRIPTION("Device Mirror/redirect actions");
456 MODULE_LICENSE("GPL");
457 
458 static int __init mirred_init_module(void)
459 {
460 	int err = register_netdevice_notifier(&mirred_device_notifier);
461 	if (err)
462 		return err;
463 
464 	pr_info("Mirror/redirect action on\n");
465 	return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
466 }
467 
468 static void __exit mirred_cleanup_module(void)
469 {
470 	tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
471 	unregister_netdevice_notifier(&mirred_device_notifier);
472 }
473 
474 module_init(mirred_init_module);
475 module_exit(mirred_cleanup_module);
476