You need to sign in or sign up before continuing.
act_mirred.c 11.5 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 <linux/if_arp.h>
25
#include <net/net_namespace.h>
26
#include <net/netlink.h>
L
Linus Torvalds 已提交
27
#include <net/pkt_sched.h>
28
#include <net/pkt_cls.h>
L
Linus Torvalds 已提交
29 30 31
#include <linux/tc_act/tc_mirred.h>
#include <net/tc_act/tc_mirred.h>

32
static LIST_HEAD(mirred_list);
33
static DEFINE_SPINLOCK(mirred_list_lock);
L
Linus Torvalds 已提交
34

35 36 37 38 39
static bool tcf_mirred_is_act_redirect(int action)
{
	return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
}

40
static bool tcf_mirred_act_wants_ingress(int action)
41 42 43 44
{
	switch (action) {
	case TCA_EGRESS_REDIR:
	case TCA_EGRESS_MIRROR:
45
		return false;
46 47
	case TCA_INGRESS_REDIR:
	case TCA_INGRESS_MIRROR:
48
		return true;
49 50 51 52 53
	default:
		BUG();
	}
}

54 55 56 57 58 59 60 61 62 63 64 65
static bool tcf_mirred_can_reinsert(int action)
{
	switch (action) {
	case TC_ACT_SHOT:
	case TC_ACT_STOLEN:
	case TC_ACT_QUEUED:
	case TC_ACT_TRAP:
		return true;
	}
	return false;
}

66 67 68 69 70 71
static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
{
	return rcu_dereference_protected(m->tcfm_dev,
					 lockdep_is_held(&m->tcf_lock));
}

72
static void tcf_mirred_release(struct tc_action *a)
L
Linus Torvalds 已提交
73
{
74
	struct tcf_mirred *m = to_mirred(a);
75
	struct net_device *dev;
76

77
	spin_lock(&mirred_list_lock);
W
WANG Cong 已提交
78
	list_del(&m->tcfm_list);
79 80 81 82
	spin_unlock(&mirred_list_lock);

	/* last reference to action, no need to lock */
	dev = rcu_dereference_protected(m->tcfm_dev, 1);
83 84
	if (dev)
		dev_put(dev);
L
Linus Torvalds 已提交
85 86
}

87 88 89 90
static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
	[TCA_MIRRED_PARMS]	= { .len = sizeof(struct tc_mirred) },
};

91
static unsigned int mirred_net_id;
92
static struct tc_action_ops act_mirred_ops;
93

94
static int tcf_mirred_init(struct net *net, struct nlattr *nla,
95 96
			   struct nlattr *est, struct tc_action **a,
			   int ovr, int bind, bool rtnl_held,
97
			   struct tcf_proto *tp,
98
			   struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
99
{
100
	struct tc_action_net *tn = net_generic(net, mirred_net_id);
101
	struct nlattr *tb[TCA_MIRRED_MAX + 1];
102
	struct tcf_chain *goto_ch = NULL;
103
	bool mac_header_xmit = false;
L
Linus Torvalds 已提交
104
	struct tc_mirred *parm;
105
	struct tcf_mirred *m;
C
Changli Gao 已提交
106
	struct net_device *dev;
107
	bool exists = false;
108
	int ret, err;
L
Linus Torvalds 已提交
109

110 111
	if (!nla) {
		NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
L
Linus Torvalds 已提交
112
		return -EINVAL;
113 114
	}
	ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, extack);
C
Changli Gao 已提交
115 116
	if (ret < 0)
		return ret;
117 118
	if (!tb[TCA_MIRRED_PARMS]) {
		NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
L
Linus Torvalds 已提交
119
		return -EINVAL;
120
	}
121
	parm = nla_data(tb[TCA_MIRRED_PARMS]);
122

123 124 125 126
	err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
	if (err < 0)
		return err;
	exists = err;
127 128 129
	if (exists && bind)
		return 0;

C
Changli Gao 已提交
130 131 132
	switch (parm->eaction) {
	case TCA_EGRESS_MIRROR:
	case TCA_EGRESS_REDIR:
133 134
	case TCA_INGRESS_REDIR:
	case TCA_INGRESS_MIRROR:
C
Changli Gao 已提交
135 136
		break;
	default:
137
		if (exists)
138
			tcf_idr_release(*a, bind);
139 140
		else
			tcf_idr_cleanup(tn, parm->index);
141
		NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
C
Changli Gao 已提交
142 143
		return -EINVAL;
	}
L
Linus Torvalds 已提交
144

145
	if (!exists) {
146
		if (!parm->ifindex) {
147
			tcf_idr_cleanup(tn, parm->index);
148
			NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
L
Linus Torvalds 已提交
149
			return -EINVAL;
150
		}
151 152
		ret = tcf_idr_create(tn, parm->index, est, a,
				     &act_mirred_ops, bind, true);
153 154
		if (ret) {
			tcf_idr_cleanup(tn, parm->index);
155
			return ret;
156
		}
L
Linus Torvalds 已提交
157
		ret = ACT_P_CREATED;
158
	} else if (!ovr) {
159
		tcf_idr_release(*a, bind);
160
		return -EEXIST;
L
Linus Torvalds 已提交
161
	}
162 163 164 165
	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
	if (err < 0)
		goto release_idr;

166
	m = to_mirred(*a);
L
Linus Torvalds 已提交
167

168
	spin_lock_bh(&m->tcf_lock);
169 170 171 172

	if (parm->ifindex) {
		dev = dev_get_by_index(net, parm->ifindex);
		if (!dev) {
173
			spin_unlock_bh(&m->tcf_lock);
174 175
			err = -ENODEV;
			goto put_chain;
176 177 178 179 180 181
		}
		mac_header_xmit = dev_is_mac_header_xmit(dev);
		rcu_swap_protected(m->tcfm_dev, dev,
				   lockdep_is_held(&m->tcf_lock));
		if (dev)
			dev_put(dev);
182
		m->tcfm_mac_header_xmit = mac_header_xmit;
L
Linus Torvalds 已提交
183
	}
184 185
	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
	m->tcfm_eaction = parm->eaction;
186
	spin_unlock_bh(&m->tcf_lock);
187 188
	if (goto_ch)
		tcf_chain_put_by_act(goto_ch);
189

190
	if (ret == ACT_P_CREATED) {
191
		spin_lock(&mirred_list_lock);
192
		list_add(&m->tcfm_list, &mirred_list);
193 194
		spin_unlock(&mirred_list_lock);

195
		tcf_idr_insert(tn, *a);
196
	}
L
Linus Torvalds 已提交
197 198

	return ret;
199 200 201 202 203 204
put_chain:
	if (goto_ch)
		tcf_chain_put_by_act(goto_ch);
release_idr:
	tcf_idr_release(*a, bind);
	return err;
L
Linus Torvalds 已提交
205 206
}

207 208
static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
			  struct tcf_result *res)
L
Linus Torvalds 已提交
209
{
210
	struct tcf_mirred *m = to_mirred(a);
211
	struct sk_buff *skb2 = skb;
212
	bool m_mac_header_xmit;
L
Linus Torvalds 已提交
213
	struct net_device *dev;
214
	int retval, err = 0;
215 216 217
	bool use_reinsert;
	bool want_ingress;
	bool is_redirect;
218 219
	int m_eaction;
	int mac_len;
L
Linus Torvalds 已提交
220

221 222
	tcf_lastuse_update(&m->tcf_tm);
	bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
L
Linus Torvalds 已提交
223

224 225
	m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
	m_eaction = READ_ONCE(m->tcfm_eaction);
226
	retval = READ_ONCE(m->tcf_action);
227
	dev = rcu_dereference_bh(m->tcfm_dev);
228 229
	if (unlikely(!dev)) {
		pr_notice_once("tc mirred: target device is gone\n");
230 231 232
		goto out;
	}

233
	if (unlikely(!(dev->flags & IFF_UP))) {
234 235
		net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
				       dev->name);
C
Changli Gao 已提交
236
		goto out;
L
Linus Torvalds 已提交
237 238
	}

239 240 241 242 243 244 245 246 247 248 249 250
	/* we could easily avoid the clone only if called by ingress and clsact;
	 * since we can't easily detect the clsact caller, skip clone only for
	 * ingress - that covers the TC S/W datapath.
	 */
	is_redirect = tcf_mirred_is_act_redirect(m_eaction);
	use_reinsert = skb_at_tc_ingress(skb) && is_redirect &&
		       tcf_mirred_can_reinsert(retval);
	if (!use_reinsert) {
		skb2 = skb_clone(skb, GFP_ATOMIC);
		if (!skb2)
			goto out;
	}
L
Linus Torvalds 已提交
251

252 253 254 255
	/* If action's target direction differs than filter's direction,
	 * and devices expect a mac header on xmit, then mac push/pull is
	 * needed.
	 */
256 257
	want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
	if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) {
258
		if (!skb_at_tc_ingress(skb)) {
259 260 261 262 263
			/* caught at egress, act ingress: pull mac */
			mac_len = skb_network_header(skb) - skb_mac_header(skb);
			skb_pull_rcsum(skb2, mac_len);
		} else {
			/* caught at ingress, act egress: push mac */
264
			skb_push_rcsum(skb2, skb->mac_len);
265
		}
C
Changli Gao 已提交
266
	}
L
Linus Torvalds 已提交
267

268 269 270
	skb2->skb_iif = skb->dev->ifindex;
	skb2->dev = dev;

L
Linus Torvalds 已提交
271
	/* mirror is always swallowed */
272
	if (is_redirect) {
273 274
		skb2->tc_redirected = 1;
		skb2->tc_from_ingress = skb2->tc_at_ingress;
275 276
		if (skb2->tc_from_ingress)
			skb2->tstamp = 0;
277 278 279 280 281 282
		/* let's the caller reinsert the packet, if possible */
		if (use_reinsert) {
			res->ingress = want_ingress;
			res->qstats = this_cpu_ptr(m->common.cpu_qstats);
			return TC_ACT_REINSERT;
		}
283
	}
L
Linus Torvalds 已提交
284

285
	if (!want_ingress)
286 287 288
		err = dev_queue_xmit(skb2);
	else
		err = netif_receive_skb(skb2);
C
Changli Gao 已提交
289 290

	if (err) {
291 292
out:
		qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
293
		if (tcf_mirred_is_act_redirect(m_eaction))
294
			retval = TC_ACT_SHOT;
295
	}
C
Changli Gao 已提交
296 297

	return retval;
L
Linus Torvalds 已提交
298 299
}

300
static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
301
			     u64 lastuse, bool hw)
302
{
303 304 305
	struct tcf_mirred *m = to_mirred(a);
	struct tcf_t *tm = &m->tcf_tm;

306
	_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
307 308 309
	if (hw)
		_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
				   bytes, packets);
310
	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
311 312
}

J
Jamal Hadi Salim 已提交
313 314
static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
			   int ref)
L
Linus Torvalds 已提交
315
{
316
	unsigned char *b = skb_tail_pointer(skb);
317
	struct tcf_mirred *m = to_mirred(a);
318 319
	struct tc_mirred opt = {
		.index   = m->tcf_index,
320 321
		.refcnt  = refcount_read(&m->tcf_refcnt) - ref,
		.bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
322
	};
323
	struct net_device *dev;
L
Linus Torvalds 已提交
324 325
	struct tcf_t t;

326
	spin_lock_bh(&m->tcf_lock);
327 328 329 330 331 332
	opt.action = m->tcf_action;
	opt.eaction = m->tcfm_eaction;
	dev = tcf_mirred_dev_dereference(m);
	if (dev)
		opt.ifindex = dev->ifindex;

333 334
	if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
		goto nla_put_failure;
335 336

	tcf_tm_dump(&t, &m->tcf_tm);
337
	if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
338
		goto nla_put_failure;
339
	spin_unlock_bh(&m->tcf_lock);
340

L
Linus Torvalds 已提交
341 342
	return skb->len;

343
nla_put_failure:
344
	spin_unlock_bh(&m->tcf_lock);
345
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
346 347 348
	return -1;
}

349 350
static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
			     struct netlink_callback *cb, int type,
351 352
			     const struct tc_action_ops *ops,
			     struct netlink_ext_ack *extack)
353 354 355
{
	struct tc_action_net *tn = net_generic(net, mirred_net_id);

356
	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
357 358
}

359
static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
360 361 362
{
	struct tc_action_net *tn = net_generic(net, mirred_net_id);

363
	return tcf_idr_search(tn, a, index);
364 365
}

366 367 368
static int mirred_device_event(struct notifier_block *unused,
			       unsigned long event, void *ptr)
{
369
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
370 371
	struct tcf_mirred *m;

372
	ASSERT_RTNL();
373
	if (event == NETDEV_UNREGISTER) {
374
		spin_lock(&mirred_list_lock);
375
		list_for_each_entry(m, &mirred_list, tcfm_list) {
376
			spin_lock_bh(&m->tcf_lock);
377
			if (tcf_mirred_dev_dereference(m) == dev) {
378
				dev_put(dev);
379 380 381 382
				/* Note : no rcu grace period necessary, as
				 * net_device are already rcu protected.
				 */
				RCU_INIT_POINTER(m->tcfm_dev, NULL);
383
			}
384
			spin_unlock_bh(&m->tcf_lock);
385
		}
386
		spin_unlock(&mirred_list_lock);
387
	}
388 389 390 391 392 393 394 395

	return NOTIFY_DONE;
}

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

396
static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
397
{
398
	struct tcf_mirred *m = to_mirred(a);
399
	struct net_device *dev;
400

401 402
	rcu_read_lock();
	dev = rcu_dereference(m->tcfm_dev);
403 404
	if (dev)
		dev_hold(dev);
405
	rcu_read_unlock();
406

407 408 409 410 411 412
	return dev;
}

static void tcf_mirred_put_dev(struct net_device *dev)
{
	dev_put(dev);
413 414
}

L
Linus Torvalds 已提交
415 416
static struct tc_action_ops act_mirred_ops = {
	.kind		=	"mirred",
417
	.id		=	TCA_ID_MIRRED,
L
Linus Torvalds 已提交
418
	.owner		=	THIS_MODULE,
419
	.act		=	tcf_mirred_act,
420
	.stats_update	=	tcf_stats_update,
L
Linus Torvalds 已提交
421
	.dump		=	tcf_mirred_dump,
422
	.cleanup	=	tcf_mirred_release,
L
Linus Torvalds 已提交
423
	.init		=	tcf_mirred_init,
424 425
	.walk		=	tcf_mirred_walker,
	.lookup		=	tcf_mirred_search,
426
	.size		=	sizeof(struct tcf_mirred),
427
	.get_dev	=	tcf_mirred_get_dev,
428
	.put_dev	=	tcf_mirred_put_dev,
429 430 431 432 433 434
};

static __net_init int mirred_init_net(struct net *net)
{
	struct tc_action_net *tn = net_generic(net, mirred_net_id);

435
	return tc_action_net_init(tn, &act_mirred_ops);
436 437
}

438
static void __net_exit mirred_exit_net(struct list_head *net_list)
439
{
440
	tc_action_net_exit(net_list, mirred_net_id);
441 442 443 444
}

static struct pernet_operations mirred_net_ops = {
	.init = mirred_init_net,
445
	.exit_batch = mirred_exit_net,
446 447
	.id   = &mirred_net_id,
	.size = sizeof(struct tc_action_net),
L
Linus Torvalds 已提交
448 449 450 451 452 453
};

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

454
static int __init mirred_init_module(void)
L
Linus Torvalds 已提交
455
{
456 457 458 459
	int err = register_netdevice_notifier(&mirred_device_notifier);
	if (err)
		return err;

460
	pr_info("Mirror/redirect action on\n");
461
	return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
L
Linus Torvalds 已提交
462 463
}

464
static void __exit mirred_cleanup_module(void)
L
Linus Torvalds 已提交
465
{
466
	tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
467
	unregister_netdevice_notifier(&mirred_device_notifier);
L
Linus Torvalds 已提交
468 469 470 471
}

module_init(mirred_init_module);
module_exit(mirred_cleanup_module);