xref: /linux/net/sched/act_mirred.c (revision 4461568aa4e565de2c336f4875ddf912f26da8a5)
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/dst.h>
23 #include <net/pkt_sched.h>
24 #include <net/pkt_cls.h>
25 #include <linux/tc_act/tc_mirred.h>
26 #include <net/tc_act/tc_mirred.h>
27 
28 static LIST_HEAD(mirred_list);
29 static DEFINE_SPINLOCK(mirred_list_lock);
30 
31 #define MIRRED_RECURSION_LIMIT    4
32 static DEFINE_PER_CPU(unsigned int, mirred_rec_level);
33 
34 static bool tcf_mirred_is_act_redirect(int action)
35 {
36 	return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
37 }
38 
39 static bool tcf_mirred_act_wants_ingress(int action)
40 {
41 	switch (action) {
42 	case TCA_EGRESS_REDIR:
43 	case TCA_EGRESS_MIRROR:
44 		return false;
45 	case TCA_INGRESS_REDIR:
46 	case TCA_INGRESS_MIRROR:
47 		return true;
48 	default:
49 		BUG();
50 	}
51 }
52 
53 static bool tcf_mirred_can_reinsert(int action)
54 {
55 	switch (action) {
56 	case TC_ACT_SHOT:
57 	case TC_ACT_STOLEN:
58 	case TC_ACT_QUEUED:
59 	case TC_ACT_TRAP:
60 		return true;
61 	}
62 	return false;
63 }
64 
65 static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
66 {
67 	return rcu_dereference_protected(m->tcfm_dev,
68 					 lockdep_is_held(&m->tcf_lock));
69 }
70 
71 static void tcf_mirred_release(struct tc_action *a)
72 {
73 	struct tcf_mirred *m = to_mirred(a);
74 	struct net_device *dev;
75 
76 	spin_lock(&mirred_list_lock);
77 	list_del(&m->tcfm_list);
78 	spin_unlock(&mirred_list_lock);
79 
80 	/* last reference to action, no need to lock */
81 	dev = rcu_dereference_protected(m->tcfm_dev, 1);
82 	netdev_put(dev, &m->tcfm_dev_tracker);
83 }
84 
85 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
86 	[TCA_MIRRED_PARMS]	= { .len = sizeof(struct tc_mirred) },
87 };
88 
89 static struct tc_action_ops act_mirred_ops;
90 
91 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
92 			   struct nlattr *est, struct tc_action **a,
93 			   struct tcf_proto *tp,
94 			   u32 flags, struct netlink_ext_ack *extack)
95 {
96 	struct tc_action_net *tn = net_generic(net, act_mirred_ops.net_id);
97 	bool bind = flags & TCA_ACT_FLAGS_BIND;
98 	struct nlattr *tb[TCA_MIRRED_MAX + 1];
99 	struct tcf_chain *goto_ch = NULL;
100 	bool mac_header_xmit = false;
101 	struct tc_mirred *parm;
102 	struct tcf_mirred *m;
103 	bool exists = false;
104 	int ret, err;
105 	u32 index;
106 
107 	if (!nla) {
108 		NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
109 		return -EINVAL;
110 	}
111 	ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla,
112 					  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 	index = parm->index;
121 	err = tcf_idr_check_alloc(tn, &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, 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, index);
146 			NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
147 			return -EINVAL;
148 		}
149 		ret = tcf_idr_create_from_flags(tn, index, est, a,
150 						&act_mirred_ops, bind, flags);
151 		if (ret) {
152 			tcf_idr_cleanup(tn, index);
153 			return ret;
154 		}
155 		ret = ACT_P_CREATED;
156 	} else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
157 		tcf_idr_release(*a, bind);
158 		return -EEXIST;
159 	}
160 
161 	m = to_mirred(*a);
162 	if (ret == ACT_P_CREATED)
163 		INIT_LIST_HEAD(&m->tcfm_list);
164 
165 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
166 	if (err < 0)
167 		goto release_idr;
168 
169 	spin_lock_bh(&m->tcf_lock);
170 
171 	if (parm->ifindex) {
172 		struct net_device *odev, *ndev;
173 
174 		ndev = dev_get_by_index(net, parm->ifindex);
175 		if (!ndev) {
176 			spin_unlock_bh(&m->tcf_lock);
177 			err = -ENODEV;
178 			goto put_chain;
179 		}
180 		mac_header_xmit = dev_is_mac_header_xmit(ndev);
181 		odev = rcu_replace_pointer(m->tcfm_dev, ndev,
182 					  lockdep_is_held(&m->tcf_lock));
183 		netdev_put(odev, &m->tcfm_dev_tracker);
184 		netdev_tracker_alloc(ndev, &m->tcfm_dev_tracker, GFP_ATOMIC);
185 		m->tcfm_mac_header_xmit = mac_header_xmit;
186 	}
187 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
188 	m->tcfm_eaction = parm->eaction;
189 	spin_unlock_bh(&m->tcf_lock);
190 	if (goto_ch)
191 		tcf_chain_put_by_act(goto_ch);
192 
193 	if (ret == ACT_P_CREATED) {
194 		spin_lock(&mirred_list_lock);
195 		list_add(&m->tcfm_list, &mirred_list);
196 		spin_unlock(&mirred_list_lock);
197 	}
198 
199 	return ret;
200 put_chain:
201 	if (goto_ch)
202 		tcf_chain_put_by_act(goto_ch);
203 release_idr:
204 	tcf_idr_release(*a, bind);
205 	return err;
206 }
207 
208 static int tcf_mirred_forward(bool want_ingress, struct sk_buff *skb)
209 {
210 	int err;
211 
212 	if (!want_ingress)
213 		err = tcf_dev_queue_xmit(skb, dev_queue_xmit);
214 	else
215 		err = netif_receive_skb(skb);
216 
217 	return err;
218 }
219 
220 static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
221 			  struct tcf_result *res)
222 {
223 	struct tcf_mirred *m = to_mirred(a);
224 	struct sk_buff *skb2 = skb;
225 	bool m_mac_header_xmit;
226 	struct net_device *dev;
227 	unsigned int rec_level;
228 	int retval, err = 0;
229 	bool use_reinsert;
230 	bool want_ingress;
231 	bool is_redirect;
232 	bool expects_nh;
233 	bool at_ingress;
234 	int m_eaction;
235 	int mac_len;
236 	bool at_nh;
237 
238 	rec_level = __this_cpu_inc_return(mirred_rec_level);
239 	if (unlikely(rec_level > MIRRED_RECURSION_LIMIT)) {
240 		net_warn_ratelimited("Packet exceeded mirred recursion limit on dev %s\n",
241 				     netdev_name(skb->dev));
242 		__this_cpu_dec(mirred_rec_level);
243 		return TC_ACT_SHOT;
244 	}
245 
246 	tcf_lastuse_update(&m->tcf_tm);
247 	tcf_action_update_bstats(&m->common, skb);
248 
249 	m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
250 	m_eaction = READ_ONCE(m->tcfm_eaction);
251 	retval = READ_ONCE(m->tcf_action);
252 	dev = rcu_dereference_bh(m->tcfm_dev);
253 	if (unlikely(!dev)) {
254 		pr_notice_once("tc mirred: target device is gone\n");
255 		goto out;
256 	}
257 
258 	if (unlikely(!(dev->flags & IFF_UP))) {
259 		net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
260 				       dev->name);
261 		goto out;
262 	}
263 
264 	/* we could easily avoid the clone only if called by ingress and clsact;
265 	 * since we can't easily detect the clsact caller, skip clone only for
266 	 * ingress - that covers the TC S/W datapath.
267 	 */
268 	is_redirect = tcf_mirred_is_act_redirect(m_eaction);
269 	at_ingress = skb_at_tc_ingress(skb);
270 	use_reinsert = at_ingress && is_redirect &&
271 		       tcf_mirred_can_reinsert(retval);
272 	if (!use_reinsert) {
273 		skb2 = skb_clone(skb, GFP_ATOMIC);
274 		if (!skb2)
275 			goto out;
276 	}
277 
278 	want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
279 
280 	/* All mirred/redirected skbs should clear previous ct info */
281 	nf_reset_ct(skb2);
282 	if (want_ingress && !at_ingress) /* drop dst for egress -> ingress */
283 		skb_dst_drop(skb2);
284 
285 	expects_nh = want_ingress || !m_mac_header_xmit;
286 	at_nh = skb->data == skb_network_header(skb);
287 	if (at_nh != expects_nh) {
288 		mac_len = skb_at_tc_ingress(skb) ? skb->mac_len :
289 			  skb_network_header(skb) - skb_mac_header(skb);
290 		if (expects_nh) {
291 			/* target device/action expect data at nh */
292 			skb_pull_rcsum(skb2, mac_len);
293 		} else {
294 			/* target device/action expect data at mac */
295 			skb_push_rcsum(skb2, mac_len);
296 		}
297 	}
298 
299 	skb2->skb_iif = skb->dev->ifindex;
300 	skb2->dev = dev;
301 
302 	/* mirror is always swallowed */
303 	if (is_redirect) {
304 		skb_set_redirected(skb2, skb2->tc_at_ingress);
305 
306 		/* let's the caller reinsert the packet, if possible */
307 		if (use_reinsert) {
308 			res->ingress = want_ingress;
309 			err = tcf_mirred_forward(res->ingress, skb);
310 			if (err)
311 				tcf_action_inc_overlimit_qstats(&m->common);
312 			__this_cpu_dec(mirred_rec_level);
313 			return TC_ACT_CONSUMED;
314 		}
315 	}
316 
317 	err = tcf_mirred_forward(want_ingress, skb2);
318 	if (err) {
319 out:
320 		tcf_action_inc_overlimit_qstats(&m->common);
321 		if (tcf_mirred_is_act_redirect(m_eaction))
322 			retval = TC_ACT_SHOT;
323 	}
324 	__this_cpu_dec(mirred_rec_level);
325 
326 	return retval;
327 }
328 
329 static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
330 			     u64 drops, u64 lastuse, bool hw)
331 {
332 	struct tcf_mirred *m = to_mirred(a);
333 	struct tcf_t *tm = &m->tcf_tm;
334 
335 	tcf_action_update_stats(a, bytes, packets, drops, hw);
336 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
337 }
338 
339 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
340 			   int ref)
341 {
342 	unsigned char *b = skb_tail_pointer(skb);
343 	struct tcf_mirred *m = to_mirred(a);
344 	struct tc_mirred opt = {
345 		.index   = m->tcf_index,
346 		.refcnt  = refcount_read(&m->tcf_refcnt) - ref,
347 		.bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
348 	};
349 	struct net_device *dev;
350 	struct tcf_t t;
351 
352 	spin_lock_bh(&m->tcf_lock);
353 	opt.action = m->tcf_action;
354 	opt.eaction = m->tcfm_eaction;
355 	dev = tcf_mirred_dev_dereference(m);
356 	if (dev)
357 		opt.ifindex = dev->ifindex;
358 
359 	if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
360 		goto nla_put_failure;
361 
362 	tcf_tm_dump(&t, &m->tcf_tm);
363 	if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
364 		goto nla_put_failure;
365 	spin_unlock_bh(&m->tcf_lock);
366 
367 	return skb->len;
368 
369 nla_put_failure:
370 	spin_unlock_bh(&m->tcf_lock);
371 	nlmsg_trim(skb, b);
372 	return -1;
373 }
374 
375 static int mirred_device_event(struct notifier_block *unused,
376 			       unsigned long event, void *ptr)
377 {
378 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
379 	struct tcf_mirred *m;
380 
381 	ASSERT_RTNL();
382 	if (event == NETDEV_UNREGISTER) {
383 		spin_lock(&mirred_list_lock);
384 		list_for_each_entry(m, &mirred_list, tcfm_list) {
385 			spin_lock_bh(&m->tcf_lock);
386 			if (tcf_mirred_dev_dereference(m) == dev) {
387 				netdev_put(dev, &m->tcfm_dev_tracker);
388 				/* Note : no rcu grace period necessary, as
389 				 * net_device are already rcu protected.
390 				 */
391 				RCU_INIT_POINTER(m->tcfm_dev, NULL);
392 			}
393 			spin_unlock_bh(&m->tcf_lock);
394 		}
395 		spin_unlock(&mirred_list_lock);
396 	}
397 
398 	return NOTIFY_DONE;
399 }
400 
401 static struct notifier_block mirred_device_notifier = {
402 	.notifier_call = mirred_device_event,
403 };
404 
405 static void tcf_mirred_dev_put(void *priv)
406 {
407 	struct net_device *dev = priv;
408 
409 	dev_put(dev);
410 }
411 
412 static struct net_device *
413 tcf_mirred_get_dev(const struct tc_action *a,
414 		   tc_action_priv_destructor *destructor)
415 {
416 	struct tcf_mirred *m = to_mirred(a);
417 	struct net_device *dev;
418 
419 	rcu_read_lock();
420 	dev = rcu_dereference(m->tcfm_dev);
421 	if (dev) {
422 		dev_hold(dev);
423 		*destructor = tcf_mirred_dev_put;
424 	}
425 	rcu_read_unlock();
426 
427 	return dev;
428 }
429 
430 static size_t tcf_mirred_get_fill_size(const struct tc_action *act)
431 {
432 	return nla_total_size(sizeof(struct tc_mirred));
433 }
434 
435 static void tcf_offload_mirred_get_dev(struct flow_action_entry *entry,
436 				       const struct tc_action *act)
437 {
438 	entry->dev = act->ops->get_dev(act, &entry->destructor);
439 	if (!entry->dev)
440 		return;
441 	entry->destructor_priv = entry->dev;
442 }
443 
444 static int tcf_mirred_offload_act_setup(struct tc_action *act, void *entry_data,
445 					u32 *index_inc, bool bind,
446 					struct netlink_ext_ack *extack)
447 {
448 	if (bind) {
449 		struct flow_action_entry *entry = entry_data;
450 
451 		if (is_tcf_mirred_egress_redirect(act)) {
452 			entry->id = FLOW_ACTION_REDIRECT;
453 			tcf_offload_mirred_get_dev(entry, act);
454 		} else if (is_tcf_mirred_egress_mirror(act)) {
455 			entry->id = FLOW_ACTION_MIRRED;
456 			tcf_offload_mirred_get_dev(entry, act);
457 		} else if (is_tcf_mirred_ingress_redirect(act)) {
458 			entry->id = FLOW_ACTION_REDIRECT_INGRESS;
459 			tcf_offload_mirred_get_dev(entry, act);
460 		} else if (is_tcf_mirred_ingress_mirror(act)) {
461 			entry->id = FLOW_ACTION_MIRRED_INGRESS;
462 			tcf_offload_mirred_get_dev(entry, act);
463 		} else {
464 			NL_SET_ERR_MSG_MOD(extack, "Unsupported mirred offload");
465 			return -EOPNOTSUPP;
466 		}
467 		*index_inc = 1;
468 	} else {
469 		struct flow_offload_action *fl_action = entry_data;
470 
471 		if (is_tcf_mirred_egress_redirect(act))
472 			fl_action->id = FLOW_ACTION_REDIRECT;
473 		else if (is_tcf_mirred_egress_mirror(act))
474 			fl_action->id = FLOW_ACTION_MIRRED;
475 		else if (is_tcf_mirred_ingress_redirect(act))
476 			fl_action->id = FLOW_ACTION_REDIRECT_INGRESS;
477 		else if (is_tcf_mirred_ingress_mirror(act))
478 			fl_action->id = FLOW_ACTION_MIRRED_INGRESS;
479 		else
480 			return -EOPNOTSUPP;
481 	}
482 
483 	return 0;
484 }
485 
486 static struct tc_action_ops act_mirred_ops = {
487 	.kind		=	"mirred",
488 	.id		=	TCA_ID_MIRRED,
489 	.owner		=	THIS_MODULE,
490 	.act		=	tcf_mirred_act,
491 	.stats_update	=	tcf_stats_update,
492 	.dump		=	tcf_mirred_dump,
493 	.cleanup	=	tcf_mirred_release,
494 	.init		=	tcf_mirred_init,
495 	.get_fill_size	=	tcf_mirred_get_fill_size,
496 	.offload_act_setup =	tcf_mirred_offload_act_setup,
497 	.size		=	sizeof(struct tcf_mirred),
498 	.get_dev	=	tcf_mirred_get_dev,
499 };
500 
501 static __net_init int mirred_init_net(struct net *net)
502 {
503 	struct tc_action_net *tn = net_generic(net, act_mirred_ops.net_id);
504 
505 	return tc_action_net_init(net, tn, &act_mirred_ops);
506 }
507 
508 static void __net_exit mirred_exit_net(struct list_head *net_list)
509 {
510 	tc_action_net_exit(net_list, act_mirred_ops.net_id);
511 }
512 
513 static struct pernet_operations mirred_net_ops = {
514 	.init = mirred_init_net,
515 	.exit_batch = mirred_exit_net,
516 	.id   = &act_mirred_ops.net_id,
517 	.size = sizeof(struct tc_action_net),
518 };
519 
520 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
521 MODULE_DESCRIPTION("Device Mirror/redirect actions");
522 MODULE_LICENSE("GPL");
523 
524 static int __init mirred_init_module(void)
525 {
526 	int err = register_netdevice_notifier(&mirred_device_notifier);
527 	if (err)
528 		return err;
529 
530 	pr_info("Mirror/redirect action on\n");
531 	err = tcf_register_action(&act_mirred_ops, &mirred_net_ops);
532 	if (err)
533 		unregister_netdevice_notifier(&mirred_device_notifier);
534 
535 	return err;
536 }
537 
538 static void __exit mirred_cleanup_module(void)
539 {
540 	tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
541 	unregister_netdevice_notifier(&mirred_device_notifier);
542 }
543 
544 module_init(mirred_init_module);
545 module_exit(mirred_cleanup_module);
546