xref: /openbmc/linux/net/sched/act_mirred.c (revision a06c488d)
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 <net/net_namespace.h>
25 #include <net/netlink.h>
26 #include <net/pkt_sched.h>
27 #include <linux/tc_act/tc_mirred.h>
28 #include <net/tc_act/tc_mirred.h>
29 
30 #include <linux/if_arp.h>
31 
32 #define MIRRED_TAB_MASK     7
33 static LIST_HEAD(mirred_list);
34 static DEFINE_SPINLOCK(mirred_list_lock);
35 
36 static void tcf_mirred_release(struct tc_action *a, int bind)
37 {
38 	struct tcf_mirred *m = to_mirred(a);
39 	struct net_device *dev = rcu_dereference_protected(m->tcfm_dev, 1);
40 
41 	/* We could be called either in a RCU callback or with RTNL lock held. */
42 	spin_lock_bh(&mirred_list_lock);
43 	list_del(&m->tcfm_list);
44 	spin_unlock_bh(&mirred_list_lock);
45 	if (dev)
46 		dev_put(dev);
47 }
48 
49 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
50 	[TCA_MIRRED_PARMS]	= { .len = sizeof(struct tc_mirred) },
51 };
52 
53 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
54 			   struct nlattr *est, struct tc_action *a, int ovr,
55 			   int bind)
56 {
57 	struct nlattr *tb[TCA_MIRRED_MAX + 1];
58 	struct tc_mirred *parm;
59 	struct tcf_mirred *m;
60 	struct net_device *dev;
61 	int ret, ok_push = 0;
62 
63 	if (nla == NULL)
64 		return -EINVAL;
65 	ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy);
66 	if (ret < 0)
67 		return ret;
68 	if (tb[TCA_MIRRED_PARMS] == NULL)
69 		return -EINVAL;
70 	parm = nla_data(tb[TCA_MIRRED_PARMS]);
71 	switch (parm->eaction) {
72 	case TCA_EGRESS_MIRROR:
73 	case TCA_EGRESS_REDIR:
74 		break;
75 	default:
76 		return -EINVAL;
77 	}
78 	if (parm->ifindex) {
79 		dev = __dev_get_by_index(net, parm->ifindex);
80 		if (dev == NULL)
81 			return -ENODEV;
82 		switch (dev->type) {
83 		case ARPHRD_TUNNEL:
84 		case ARPHRD_TUNNEL6:
85 		case ARPHRD_SIT:
86 		case ARPHRD_IPGRE:
87 		case ARPHRD_VOID:
88 		case ARPHRD_NONE:
89 			ok_push = 0;
90 			break;
91 		default:
92 			ok_push = 1;
93 			break;
94 		}
95 	} else {
96 		dev = NULL;
97 	}
98 
99 	if (!tcf_hash_check(parm->index, a, bind)) {
100 		if (dev == NULL)
101 			return -EINVAL;
102 		ret = tcf_hash_create(parm->index, est, a, sizeof(*m),
103 				      bind, true);
104 		if (ret)
105 			return ret;
106 		ret = ACT_P_CREATED;
107 	} else {
108 		if (bind)
109 			return 0;
110 
111 		tcf_hash_release(a, bind);
112 		if (!ovr)
113 			return -EEXIST;
114 	}
115 	m = to_mirred(a);
116 
117 	ASSERT_RTNL();
118 	m->tcf_action = parm->action;
119 	m->tcfm_eaction = parm->eaction;
120 	if (dev != NULL) {
121 		m->tcfm_ifindex = parm->ifindex;
122 		if (ret != ACT_P_CREATED)
123 			dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
124 		dev_hold(dev);
125 		rcu_assign_pointer(m->tcfm_dev, dev);
126 		m->tcfm_ok_push = ok_push;
127 	}
128 
129 	if (ret == ACT_P_CREATED) {
130 		spin_lock_bh(&mirred_list_lock);
131 		list_add(&m->tcfm_list, &mirred_list);
132 		spin_unlock_bh(&mirred_list_lock);
133 		tcf_hash_insert(a);
134 	}
135 
136 	return ret;
137 }
138 
139 static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
140 		      struct tcf_result *res)
141 {
142 	struct tcf_mirred *m = a->priv;
143 	struct net_device *dev;
144 	struct sk_buff *skb2;
145 	int retval, err;
146 	u32 at;
147 
148 	tcf_lastuse_update(&m->tcf_tm);
149 
150 	bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
151 
152 	rcu_read_lock();
153 	retval = READ_ONCE(m->tcf_action);
154 	dev = rcu_dereference(m->tcfm_dev);
155 	if (unlikely(!dev)) {
156 		pr_notice_once("tc mirred: target device is gone\n");
157 		goto out;
158 	}
159 
160 	if (unlikely(!(dev->flags & IFF_UP))) {
161 		net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
162 				       dev->name);
163 		goto out;
164 	}
165 
166 	at = G_TC_AT(skb->tc_verd);
167 	skb2 = skb_clone(skb, GFP_ATOMIC);
168 	if (!skb2)
169 		goto out;
170 
171 	if (!(at & AT_EGRESS)) {
172 		if (m->tcfm_ok_push)
173 			skb_push(skb2, skb->mac_len);
174 	}
175 
176 	/* mirror is always swallowed */
177 	if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
178 		skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at);
179 
180 	skb2->skb_iif = skb->dev->ifindex;
181 	skb2->dev = dev;
182 	skb_sender_cpu_clear(skb2);
183 	err = dev_queue_xmit(skb2);
184 
185 	if (err) {
186 out:
187 		qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
188 		if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
189 			retval = TC_ACT_SHOT;
190 	}
191 	rcu_read_unlock();
192 
193 	return retval;
194 }
195 
196 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
197 {
198 	unsigned char *b = skb_tail_pointer(skb);
199 	struct tcf_mirred *m = a->priv;
200 	struct tc_mirred opt = {
201 		.index   = m->tcf_index,
202 		.action  = m->tcf_action,
203 		.refcnt  = m->tcf_refcnt - ref,
204 		.bindcnt = m->tcf_bindcnt - bind,
205 		.eaction = m->tcfm_eaction,
206 		.ifindex = m->tcfm_ifindex,
207 	};
208 	struct tcf_t t;
209 
210 	if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
211 		goto nla_put_failure;
212 	t.install = jiffies_to_clock_t(jiffies - m->tcf_tm.install);
213 	t.lastuse = jiffies_to_clock_t(jiffies - m->tcf_tm.lastuse);
214 	t.expires = jiffies_to_clock_t(m->tcf_tm.expires);
215 	if (nla_put(skb, TCA_MIRRED_TM, sizeof(t), &t))
216 		goto nla_put_failure;
217 	return skb->len;
218 
219 nla_put_failure:
220 	nlmsg_trim(skb, b);
221 	return -1;
222 }
223 
224 static int mirred_device_event(struct notifier_block *unused,
225 			       unsigned long event, void *ptr)
226 {
227 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
228 	struct tcf_mirred *m;
229 
230 	ASSERT_RTNL();
231 	if (event == NETDEV_UNREGISTER) {
232 		spin_lock_bh(&mirred_list_lock);
233 		list_for_each_entry(m, &mirred_list, tcfm_list) {
234 			if (rcu_access_pointer(m->tcfm_dev) == dev) {
235 				dev_put(dev);
236 				/* Note : no rcu grace period necessary, as
237 				 * net_device are already rcu protected.
238 				 */
239 				RCU_INIT_POINTER(m->tcfm_dev, NULL);
240 			}
241 		}
242 		spin_unlock_bh(&mirred_list_lock);
243 	}
244 
245 	return NOTIFY_DONE;
246 }
247 
248 static struct notifier_block mirred_device_notifier = {
249 	.notifier_call = mirred_device_event,
250 };
251 
252 static struct tc_action_ops act_mirred_ops = {
253 	.kind		=	"mirred",
254 	.type		=	TCA_ACT_MIRRED,
255 	.owner		=	THIS_MODULE,
256 	.act		=	tcf_mirred,
257 	.dump		=	tcf_mirred_dump,
258 	.cleanup	=	tcf_mirred_release,
259 	.init		=	tcf_mirred_init,
260 };
261 
262 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
263 MODULE_DESCRIPTION("Device Mirror/redirect actions");
264 MODULE_LICENSE("GPL");
265 
266 static int __init mirred_init_module(void)
267 {
268 	int err = register_netdevice_notifier(&mirred_device_notifier);
269 	if (err)
270 		return err;
271 
272 	pr_info("Mirror/redirect action on\n");
273 	return tcf_register_action(&act_mirred_ops, MIRRED_TAB_MASK);
274 }
275 
276 static void __exit mirred_cleanup_module(void)
277 {
278 	tcf_unregister_action(&act_mirred_ops);
279 	unregister_netdevice_notifier(&mirred_device_notifier);
280 }
281 
282 module_init(mirred_init_module);
283 module_exit(mirred_cleanup_module);
284