xref: /openbmc/linux/net/sched/act_mirred.c (revision 8ba605b6)
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 <linux/tc_act/tc_mirred.h>
29 #include <net/tc_act/tc_mirred.h>
30 
31 #include <linux/if_arp.h>
32 
33 #define MIRRED_TAB_MASK     7
34 static LIST_HEAD(mirred_list);
35 static DEFINE_SPINLOCK(mirred_list_lock);
36 
37 static bool tcf_mirred_is_act_redirect(int action)
38 {
39 	return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
40 }
41 
42 static u32 tcf_mirred_act_direction(int action)
43 {
44 	switch (action) {
45 	case TCA_EGRESS_REDIR:
46 	case TCA_EGRESS_MIRROR:
47 		return AT_EGRESS;
48 	case TCA_INGRESS_REDIR:
49 	case TCA_INGRESS_MIRROR:
50 		return AT_INGRESS;
51 	default:
52 		BUG();
53 	}
54 }
55 
56 static void tcf_mirred_release(struct tc_action *a, int bind)
57 {
58 	struct tcf_mirred *m = to_mirred(a);
59 	struct net_device *dev;
60 
61 	/* We could be called either in a RCU callback or with RTNL lock held. */
62 	spin_lock_bh(&mirred_list_lock);
63 	list_del(&m->tcfm_list);
64 	dev = rcu_dereference_protected(m->tcfm_dev, 1);
65 	if (dev)
66 		dev_put(dev);
67 	spin_unlock_bh(&mirred_list_lock);
68 }
69 
70 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
71 	[TCA_MIRRED_PARMS]	= { .len = sizeof(struct tc_mirred) },
72 };
73 
74 static unsigned int mirred_net_id;
75 static struct tc_action_ops act_mirred_ops;
76 
77 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
78 			   struct nlattr *est, struct tc_action **a, int ovr,
79 			   int bind)
80 {
81 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
82 	struct nlattr *tb[TCA_MIRRED_MAX + 1];
83 	bool mac_header_xmit = false;
84 	struct tc_mirred *parm;
85 	struct tcf_mirred *m;
86 	struct net_device *dev;
87 	bool exists = false;
88 	int ret;
89 
90 	if (nla == NULL)
91 		return -EINVAL;
92 	ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy);
93 	if (ret < 0)
94 		return ret;
95 	if (tb[TCA_MIRRED_PARMS] == NULL)
96 		return -EINVAL;
97 	parm = nla_data(tb[TCA_MIRRED_PARMS]);
98 
99 	exists = tcf_hash_check(tn, parm->index, a, bind);
100 	if (exists && bind)
101 		return 0;
102 
103 	switch (parm->eaction) {
104 	case TCA_EGRESS_MIRROR:
105 	case TCA_EGRESS_REDIR:
106 	case TCA_INGRESS_REDIR:
107 	case TCA_INGRESS_MIRROR:
108 		break;
109 	default:
110 		if (exists)
111 			tcf_hash_release(*a, bind);
112 		return -EINVAL;
113 	}
114 	if (parm->ifindex) {
115 		dev = __dev_get_by_index(net, parm->ifindex);
116 		if (dev == NULL) {
117 			if (exists)
118 				tcf_hash_release(*a, bind);
119 			return -ENODEV;
120 		}
121 		mac_header_xmit = dev_is_mac_header_xmit(dev);
122 	} else {
123 		dev = NULL;
124 	}
125 
126 	if (!exists) {
127 		if (dev == NULL)
128 			return -EINVAL;
129 		ret = tcf_hash_create(tn, parm->index, est, a,
130 				      &act_mirred_ops, bind, true);
131 		if (ret)
132 			return ret;
133 		ret = ACT_P_CREATED;
134 	} else {
135 		tcf_hash_release(*a, bind);
136 		if (!ovr)
137 			return -EEXIST;
138 	}
139 	m = to_mirred(*a);
140 
141 	ASSERT_RTNL();
142 	m->tcf_action = parm->action;
143 	m->tcfm_eaction = parm->eaction;
144 	if (dev != NULL) {
145 		m->tcfm_ifindex = parm->ifindex;
146 		if (ret != ACT_P_CREATED)
147 			dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
148 		dev_hold(dev);
149 		rcu_assign_pointer(m->tcfm_dev, dev);
150 		m->tcfm_mac_header_xmit = mac_header_xmit;
151 	}
152 
153 	if (ret == ACT_P_CREATED) {
154 		spin_lock_bh(&mirred_list_lock);
155 		list_add(&m->tcfm_list, &mirred_list);
156 		spin_unlock_bh(&mirred_list_lock);
157 		tcf_hash_insert(tn, *a);
158 	}
159 
160 	return ret;
161 }
162 
163 static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
164 		      struct tcf_result *res)
165 {
166 	struct tcf_mirred *m = to_mirred(a);
167 	bool m_mac_header_xmit;
168 	struct net_device *dev;
169 	struct sk_buff *skb2;
170 	int retval, err = 0;
171 	int m_eaction;
172 	int mac_len;
173 	u32 at;
174 
175 	tcf_lastuse_update(&m->tcf_tm);
176 	bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
177 
178 	rcu_read_lock();
179 	m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
180 	m_eaction = READ_ONCE(m->tcfm_eaction);
181 	retval = READ_ONCE(m->tcf_action);
182 	dev = rcu_dereference(m->tcfm_dev);
183 	if (unlikely(!dev)) {
184 		pr_notice_once("tc mirred: target device is gone\n");
185 		goto out;
186 	}
187 
188 	if (unlikely(!(dev->flags & IFF_UP))) {
189 		net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
190 				       dev->name);
191 		goto out;
192 	}
193 
194 	at = G_TC_AT(skb->tc_verd);
195 	skb2 = skb_clone(skb, GFP_ATOMIC);
196 	if (!skb2)
197 		goto out;
198 
199 	/* If action's target direction differs than filter's direction,
200 	 * and devices expect a mac header on xmit, then mac push/pull is
201 	 * needed.
202 	 */
203 	if (at != tcf_mirred_act_direction(m_eaction) && m_mac_header_xmit) {
204 		if (at & AT_EGRESS) {
205 			/* caught at egress, act ingress: pull mac */
206 			mac_len = skb_network_header(skb) - skb_mac_header(skb);
207 			skb_pull_rcsum(skb2, mac_len);
208 		} else {
209 			/* caught at ingress, act egress: push mac */
210 			skb_push_rcsum(skb2, skb->mac_len);
211 		}
212 	}
213 
214 	/* mirror is always swallowed */
215 	if (tcf_mirred_is_act_redirect(m_eaction))
216 		skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at);
217 
218 	skb2->skb_iif = skb->dev->ifindex;
219 	skb2->dev = dev;
220 	if (tcf_mirred_act_direction(m_eaction) & AT_EGRESS)
221 		err = dev_queue_xmit(skb2);
222 	else
223 		err = netif_receive_skb(skb2);
224 
225 	if (err) {
226 out:
227 		qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
228 		if (tcf_mirred_is_act_redirect(m_eaction))
229 			retval = TC_ACT_SHOT;
230 	}
231 	rcu_read_unlock();
232 
233 	return retval;
234 }
235 
236 static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
237 			     u64 lastuse)
238 {
239 	struct tcf_mirred *m = to_mirred(a);
240 	struct tcf_t *tm = &m->tcf_tm;
241 
242 	_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
243 	tm->lastuse = lastuse;
244 }
245 
246 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
247 			   int ref)
248 {
249 	unsigned char *b = skb_tail_pointer(skb);
250 	struct tcf_mirred *m = to_mirred(a);
251 	struct tc_mirred opt = {
252 		.index   = m->tcf_index,
253 		.action  = m->tcf_action,
254 		.refcnt  = m->tcf_refcnt - ref,
255 		.bindcnt = m->tcf_bindcnt - bind,
256 		.eaction = m->tcfm_eaction,
257 		.ifindex = m->tcfm_ifindex,
258 	};
259 	struct tcf_t t;
260 
261 	if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
262 		goto nla_put_failure;
263 
264 	tcf_tm_dump(&t, &m->tcf_tm);
265 	if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
266 		goto nla_put_failure;
267 	return skb->len;
268 
269 nla_put_failure:
270 	nlmsg_trim(skb, b);
271 	return -1;
272 }
273 
274 static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
275 			     struct netlink_callback *cb, int type,
276 			     const struct tc_action_ops *ops)
277 {
278 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
279 
280 	return tcf_generic_walker(tn, skb, cb, type, ops);
281 }
282 
283 static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
284 {
285 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
286 
287 	return tcf_hash_search(tn, a, index);
288 }
289 
290 static int mirred_device_event(struct notifier_block *unused,
291 			       unsigned long event, void *ptr)
292 {
293 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
294 	struct tcf_mirred *m;
295 
296 	ASSERT_RTNL();
297 	if (event == NETDEV_UNREGISTER) {
298 		spin_lock_bh(&mirred_list_lock);
299 		list_for_each_entry(m, &mirred_list, tcfm_list) {
300 			if (rcu_access_pointer(m->tcfm_dev) == dev) {
301 				dev_put(dev);
302 				/* Note : no rcu grace period necessary, as
303 				 * net_device are already rcu protected.
304 				 */
305 				RCU_INIT_POINTER(m->tcfm_dev, NULL);
306 			}
307 		}
308 		spin_unlock_bh(&mirred_list_lock);
309 	}
310 
311 	return NOTIFY_DONE;
312 }
313 
314 static struct notifier_block mirred_device_notifier = {
315 	.notifier_call = mirred_device_event,
316 };
317 
318 static int tcf_mirred_device(const struct tc_action *a, struct net *net,
319 			     struct net_device **mirred_dev)
320 {
321 	int ifindex = tcf_mirred_ifindex(a);
322 
323 	*mirred_dev = __dev_get_by_index(net, ifindex);
324 	if (!*mirred_dev)
325 		return -EINVAL;
326 	return 0;
327 }
328 
329 static struct tc_action_ops act_mirred_ops = {
330 	.kind		=	"mirred",
331 	.type		=	TCA_ACT_MIRRED,
332 	.owner		=	THIS_MODULE,
333 	.act		=	tcf_mirred,
334 	.stats_update	=	tcf_stats_update,
335 	.dump		=	tcf_mirred_dump,
336 	.cleanup	=	tcf_mirred_release,
337 	.init		=	tcf_mirred_init,
338 	.walk		=	tcf_mirred_walker,
339 	.lookup		=	tcf_mirred_search,
340 	.size		=	sizeof(struct tcf_mirred),
341 	.get_dev	=	tcf_mirred_device,
342 };
343 
344 static __net_init int mirred_init_net(struct net *net)
345 {
346 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
347 
348 	return tc_action_net_init(tn, &act_mirred_ops, MIRRED_TAB_MASK);
349 }
350 
351 static void __net_exit mirred_exit_net(struct net *net)
352 {
353 	struct tc_action_net *tn = net_generic(net, mirred_net_id);
354 
355 	tc_action_net_exit(tn);
356 }
357 
358 static struct pernet_operations mirred_net_ops = {
359 	.init = mirred_init_net,
360 	.exit = mirred_exit_net,
361 	.id   = &mirred_net_id,
362 	.size = sizeof(struct tc_action_net),
363 };
364 
365 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
366 MODULE_DESCRIPTION("Device Mirror/redirect actions");
367 MODULE_LICENSE("GPL");
368 
369 static int __init mirred_init_module(void)
370 {
371 	int err = register_netdevice_notifier(&mirred_device_notifier);
372 	if (err)
373 		return err;
374 
375 	pr_info("Mirror/redirect action on\n");
376 	return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
377 }
378 
379 static void __exit mirred_cleanup_module(void)
380 {
381 	tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
382 	unregister_netdevice_notifier(&mirred_device_notifier);
383 }
384 
385 module_init(mirred_init_module);
386 module_exit(mirred_cleanup_module);
387