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