act_mirred.c 6.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * net/sched/act_mirred.c	packet mirroring and redirect actions
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * Authors:	Jamal Hadi Salim (2002-4)
 *
 * TODO: Add ingress support (and socket redirect support)
 *
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/module.h>
#include <linux/init.h>
23
#include <linux/gfp.h>
24
#include <net/net_namespace.h>
25
#include <net/netlink.h>
L
Linus Torvalds 已提交
26 27 28 29 30 31
#include <net/pkt_sched.h>
#include <linux/tc_act/tc_mirred.h>
#include <net/tc_act/tc_mirred.h>

#include <linux/if_arp.h>

32
#define MIRRED_TAB_MASK     7
33
static LIST_HEAD(mirred_list);
34
static DEFINE_SPINLOCK(mirred_list_lock);
L
Linus Torvalds 已提交
35

W
WANG Cong 已提交
36
static void tcf_mirred_release(struct tc_action *a, int bind)
L
Linus Torvalds 已提交
37
{
38
	struct tcf_mirred *m = to_mirred(a);
39 40
	struct net_device *dev = rcu_dereference_protected(m->tcfm_dev, 1);

41 42
	/* We could be called either in a RCU callback or with RTNL lock held. */
	spin_lock_bh(&mirred_list_lock);
W
WANG Cong 已提交
43
	list_del(&m->tcfm_list);
44
	spin_unlock_bh(&mirred_list_lock);
45 46
	if (dev)
		dev_put(dev);
L
Linus Torvalds 已提交
47 48
}

49 50 51 52
static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
	[TCA_MIRRED_PARMS]	= { .len = sizeof(struct tc_mirred) },
};

53 54 55
static int tcf_mirred_init(struct net *net, struct nlattr *nla,
			   struct nlattr *est, struct tc_action *a, int ovr,
			   int bind)
L
Linus Torvalds 已提交
56
{
57
	struct nlattr *tb[TCA_MIRRED_MAX + 1];
L
Linus Torvalds 已提交
58
	struct tc_mirred *parm;
59
	struct tcf_mirred *m;
C
Changli Gao 已提交
60 61
	struct net_device *dev;
	int ret, ok_push = 0;
L
Linus Torvalds 已提交
62

63
	if (nla == NULL)
L
Linus Torvalds 已提交
64
		return -EINVAL;
C
Changli Gao 已提交
65 66 67
	ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy);
	if (ret < 0)
		return ret;
68
	if (tb[TCA_MIRRED_PARMS] == NULL)
L
Linus Torvalds 已提交
69
		return -EINVAL;
70
	parm = nla_data(tb[TCA_MIRRED_PARMS]);
C
Changli Gao 已提交
71 72 73 74 75 76 77
	switch (parm->eaction) {
	case TCA_EGRESS_MIRROR:
	case TCA_EGRESS_REDIR:
		break;
	default:
		return -EINVAL;
	}
L
Linus Torvalds 已提交
78
	if (parm->ifindex) {
79
		dev = __dev_get_by_index(net, parm->ifindex);
L
Linus Torvalds 已提交
80 81 82
		if (dev == NULL)
			return -ENODEV;
		switch (dev->type) {
C
Changli Gao 已提交
83 84 85 86 87 88 89 90 91 92 93
		case ARPHRD_TUNNEL:
		case ARPHRD_TUNNEL6:
		case ARPHRD_SIT:
		case ARPHRD_IPGRE:
		case ARPHRD_VOID:
		case ARPHRD_NONE:
			ok_push = 0;
			break;
		default:
			ok_push = 1;
			break;
L
Linus Torvalds 已提交
94
		}
C
Changli Gao 已提交
95 96
	} else {
		dev = NULL;
L
Linus Torvalds 已提交
97 98
	}

99
	if (!tcf_hash_check(parm->index, a, bind)) {
C
Changli Gao 已提交
100
		if (dev == NULL)
L
Linus Torvalds 已提交
101
			return -EINVAL;
102
		ret = tcf_hash_create(parm->index, est, a, sizeof(*m),
103
				      bind, true);
104 105
		if (ret)
			return ret;
L
Linus Torvalds 已提交
106 107
		ret = ACT_P_CREATED;
	} else {
108 109
		if (bind)
			return 0;
W
WANG Cong 已提交
110 111 112

		tcf_hash_release(a, bind);
		if (!ovr)
L
Linus Torvalds 已提交
113 114
			return -EEXIST;
	}
115
	m = to_mirred(a);
L
Linus Torvalds 已提交
116

117
	ASSERT_RTNL();
118 119
	m->tcf_action = parm->action;
	m->tcfm_eaction = parm->eaction;
C
Changli Gao 已提交
120
	if (dev != NULL) {
121
		m->tcfm_ifindex = parm->ifindex;
L
Linus Torvalds 已提交
122
		if (ret != ACT_P_CREATED)
123
			dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
L
Linus Torvalds 已提交
124
		dev_hold(dev);
125
		rcu_assign_pointer(m->tcfm_dev, dev);
126
		m->tcfm_ok_push = ok_push;
L
Linus Torvalds 已提交
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
	}
L
Linus Torvalds 已提交
135 136 137 138

	return ret;
}

139
static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
140
		      struct tcf_result *res)
L
Linus Torvalds 已提交
141
{
142
	struct tcf_mirred *m = a->priv;
L
Linus Torvalds 已提交
143
	struct net_device *dev;
C
Changli Gao 已提交
144
	struct sk_buff *skb2;
145
	int retval, err;
C
Changli Gao 已提交
146
	u32 at;
L
Linus Torvalds 已提交
147

148 149 150
	tcf_lastuse_update(&m->tcf_tm);

	bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
L
Linus Torvalds 已提交
151

152 153 154 155 156
	rcu_read_lock();
	retval = READ_ONCE(m->tcf_action);
	dev = rcu_dereference(m->tcfm_dev);
	if (unlikely(!dev)) {
		pr_notice_once("tc mirred: target device is gone\n");
157 158 159
		goto out;
	}

160
	if (unlikely(!(dev->flags & IFF_UP))) {
161 162
		net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
				       dev->name);
C
Changli Gao 已提交
163
		goto out;
L
Linus Torvalds 已提交
164 165
	}

166
	at = G_TC_AT(skb->tc_verd);
167
	skb2 = skb_clone(skb, GFP_ATOMIC);
168
	if (!skb2)
C
Changli Gao 已提交
169
		goto out;
L
Linus Torvalds 已提交
170

C
Changli Gao 已提交
171
	if (!(at & AT_EGRESS)) {
172
		if (m->tcfm_ok_push)
173
			skb_push(skb2, skb->mac_len);
C
Changli Gao 已提交
174
	}
L
Linus Torvalds 已提交
175 176

	/* mirror is always swallowed */
177
	if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
L
Linus Torvalds 已提交
178 179
		skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at);

180
	skb2->skb_iif = skb->dev->ifindex;
181
	skb2->dev = dev;
182
	skb_sender_cpu_clear(skb2);
183
	err = dev_queue_xmit(skb2);
C
Changli Gao 已提交
184 185

	if (err) {
186 187
out:
		qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
188 189
		if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
			retval = TC_ACT_SHOT;
190 191
	}
	rcu_read_unlock();
C
Changli Gao 已提交
192 193

	return retval;
L
Linus Torvalds 已提交
194 195
}

196
static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
L
Linus Torvalds 已提交
197
{
198
	unsigned char *b = skb_tail_pointer(skb);
199
	struct tcf_mirred *m = a->priv;
200 201 202 203 204 205 206 207
	struct tc_mirred opt = {
		.index   = m->tcf_index,
		.action  = m->tcf_action,
		.refcnt  = m->tcf_refcnt - ref,
		.bindcnt = m->tcf_bindcnt - bind,
		.eaction = m->tcfm_eaction,
		.ifindex = m->tcfm_ifindex,
	};
L
Linus Torvalds 已提交
208 209
	struct tcf_t t;

210 211
	if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
		goto nla_put_failure;
212 213 214
	t.install = jiffies_to_clock_t(jiffies - m->tcf_tm.install);
	t.lastuse = jiffies_to_clock_t(jiffies - m->tcf_tm.lastuse);
	t.expires = jiffies_to_clock_t(m->tcf_tm.expires);
215 216
	if (nla_put(skb, TCA_MIRRED_TM, sizeof(t), &t))
		goto nla_put_failure;
L
Linus Torvalds 已提交
217 218
	return skb->len;

219
nla_put_failure:
220
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
221 222 223
	return -1;
}

224 225 226
static int mirred_device_event(struct notifier_block *unused,
			       unsigned long event, void *ptr)
{
227
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
228 229
	struct tcf_mirred *m;

230
	ASSERT_RTNL();
231 232
	if (event == NETDEV_UNREGISTER) {
		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 237 238 239
				/* Note : no rcu grace period necessary, as
				 * net_device are already rcu protected.
				 */
				RCU_INIT_POINTER(m->tcfm_dev, NULL);
240 241
			}
		}
242 243
		spin_unlock_bh(&mirred_list_lock);
	}
244 245 246 247 248 249 250 251

	return NOTIFY_DONE;
}

static struct notifier_block mirred_device_notifier = {
	.notifier_call = mirred_device_event,
};

L
Linus Torvalds 已提交
252 253 254 255 256 257
static struct tc_action_ops act_mirred_ops = {
	.kind		=	"mirred",
	.type		=	TCA_ACT_MIRRED,
	.owner		=	THIS_MODULE,
	.act		=	tcf_mirred,
	.dump		=	tcf_mirred_dump,
258
	.cleanup	=	tcf_mirred_release,
L
Linus Torvalds 已提交
259 260 261 262 263 264 265
	.init		=	tcf_mirred_init,
};

MODULE_AUTHOR("Jamal Hadi Salim(2002)");
MODULE_DESCRIPTION("Device Mirror/redirect actions");
MODULE_LICENSE("GPL");

266
static int __init mirred_init_module(void)
L
Linus Torvalds 已提交
267
{
268 269 270 271
	int err = register_netdevice_notifier(&mirred_device_notifier);
	if (err)
		return err;

272
	pr_info("Mirror/redirect action on\n");
273
	return tcf_register_action(&act_mirred_ops, MIRRED_TAB_MASK);
L
Linus Torvalds 已提交
274 275
}

276
static void __exit mirred_cleanup_module(void)
L
Linus Torvalds 已提交
277 278
{
	tcf_unregister_action(&act_mirred_ops);
279
	unregister_netdevice_notifier(&mirred_device_notifier);
L
Linus Torvalds 已提交
280 281 282 283
}

module_init(mirred_init_module);
module_exit(mirred_cleanup_module);