Blame view

net/sched/act_mirred.c 6.78 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
  /*
   * net/sched/mirred.c	packet mirroring and redirect actions
   *
   *		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)
   *
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
  #include <linux/types.h>
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
  #include <linux/string.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
  #include <linux/errno.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
21
  #include <linux/skbuff.h>
  #include <linux/rtnetlink.h>
  #include <linux/module.h>
  #include <linux/init.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
22
  #include <linux/gfp.h>
881d966b4   Eric W. Biederman   [NET]: Make the d...
23
  #include <net/net_namespace.h>
dc5fc579b   Arnaldo Carvalho de Melo   [NETLINK]: Use nl...
24
  #include <net/netlink.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
26
27
  #include <net/pkt_sched.h>
  #include <linux/tc_act/tc_mirred.h>
  #include <net/tc_act/tc_mirred.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
  #include <linux/if_arp.h>
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
29
30
31
  #define MIRRED_TAB_MASK     7
  static struct tcf_common *tcf_mirred_ht[MIRRED_TAB_MASK + 1];
  static u32 mirred_idx_gen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
  static DEFINE_RWLOCK(mirred_lock);
3b87956ea   stephen hemminger   net sched: fix ra...
33
  static LIST_HEAD(mirred_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34

e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
35
36
37
38
39
  static struct tcf_hashinfo mirred_hash_info = {
  	.htab	=	tcf_mirred_ht,
  	.hmask	=	MIRRED_TAB_MASK,
  	.lock	=	&mirred_lock,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40

cc7ec456f   Eric Dumazet   net_sched: cleanups
41
  static int tcf_mirred_release(struct tcf_mirred *m, int bind)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
  {
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
43
  	if (m) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
  		if (bind)
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
45
46
  			m->tcf_bindcnt--;
  		m->tcf_refcnt--;
cc7ec456f   Eric Dumazet   net_sched: cleanups
47
  		if (!m->tcf_bindcnt && m->tcf_refcnt <= 0) {
3b87956ea   stephen hemminger   net sched: fix ra...
48
49
50
  			list_del(&m->tcfm_list);
  			if (m->tcfm_dev)
  				dev_put(m->tcfm_dev);
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
51
  			tcf_hash_destroy(&m->common, &mirred_hash_info);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
53
54
55
56
  			return 1;
  		}
  	}
  	return 0;
  }
53b2bf3f8   Patrick McHardy   [NET_SCHED]: Use ...
57
58
59
  static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
  	[TCA_MIRRED_PARMS]	= { .len = sizeof(struct tc_mirred) },
  };
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
60
  static int tcf_mirred_init(struct nlattr *nla, struct nlattr *est,
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
61
  			   struct tc_action *a, int ovr, int bind)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
  {
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
63
  	struct nlattr *tb[TCA_MIRRED_MAX + 1];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
  	struct tc_mirred *parm;
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
65
66
  	struct tcf_mirred *m;
  	struct tcf_common *pc;
b76965e02   Changli Gao   act_mirred: optim...
67
68
  	struct net_device *dev;
  	int ret, ok_push = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69

cee63723b   Patrick McHardy   [NET_SCHED]: Prop...
70
  	if (nla == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
  		return -EINVAL;
b76965e02   Changli Gao   act_mirred: optim...
72
73
74
  	ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy);
  	if (ret < 0)
  		return ret;
53b2bf3f8   Patrick McHardy   [NET_SCHED]: Use ...
75
  	if (tb[TCA_MIRRED_PARMS] == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
  		return -EINVAL;
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
77
  	parm = nla_data(tb[TCA_MIRRED_PARMS]);
b76965e02   Changli Gao   act_mirred: optim...
78
79
80
81
82
83
84
  	switch (parm->eaction) {
  	case TCA_EGRESS_MIRROR:
  	case TCA_EGRESS_REDIR:
  		break;
  	default:
  		return -EINVAL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
  	if (parm->ifindex) {
881d966b4   Eric W. Biederman   [NET]: Make the d...
86
  		dev = __dev_get_by_index(&init_net, parm->ifindex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87
88
89
  		if (dev == NULL)
  			return -ENODEV;
  		switch (dev->type) {
b76965e02   Changli Gao   act_mirred: optim...
90
91
92
93
94
95
96
97
98
99
100
  		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;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
  		}
b76965e02   Changli Gao   act_mirred: optim...
102
103
  	} else {
  		dev = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  	}
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
105
106
  	pc = tcf_hash_check(parm->index, a, bind, &mirred_hash_info);
  	if (!pc) {
b76965e02   Changli Gao   act_mirred: optim...
107
  		if (dev == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
  			return -EINVAL;
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
109
110
  		pc = tcf_hash_create(parm->index, est, a, sizeof(*m), bind,
  				     &mirred_idx_gen, &mirred_hash_info);
0e991ec6a   Stephen Hemminger   tc: propogate err...
111
  		if (IS_ERR(pc))
b76965e02   Changli Gao   act_mirred: optim...
112
  			return PTR_ERR(pc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
114
115
  		ret = ACT_P_CREATED;
  	} else {
  		if (!ovr) {
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
116
  			tcf_mirred_release(to_mirred(pc), bind);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
118
119
  			return -EEXIST;
  		}
  	}
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
120
  	m = to_mirred(pc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121

e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
122
123
124
  	spin_lock_bh(&m->tcf_lock);
  	m->tcf_action = parm->action;
  	m->tcfm_eaction = parm->eaction;
b76965e02   Changli Gao   act_mirred: optim...
125
  	if (dev != NULL) {
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
126
  		m->tcfm_ifindex = parm->ifindex;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
  		if (ret != ACT_P_CREATED)
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
128
  			dev_put(m->tcfm_dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
  		dev_hold(dev);
b76965e02   Changli Gao   act_mirred: optim...
130
  		m->tcfm_dev = dev;
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
131
  		m->tcfm_ok_push = ok_push;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132
  	}
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
133
  	spin_unlock_bh(&m->tcf_lock);
3b87956ea   stephen hemminger   net sched: fix ra...
134
135
  	if (ret == ACT_P_CREATED) {
  		list_add(&m->tcfm_list, &mirred_list);
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
136
  		tcf_hash_insert(pc, &mirred_hash_info);
3b87956ea   stephen hemminger   net sched: fix ra...
137
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
  	return ret;
  }
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
141
  static int tcf_mirred_cleanup(struct tc_action *a, int bind)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
  {
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
143
  	struct tcf_mirred *m = a->priv;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144

e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
145
146
  	if (m)
  		return tcf_mirred_release(m, bind);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
  	return 0;
  }
dc7f9f6e8   Eric Dumazet   net: sched: const...
149
  static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
150
  		      struct tcf_result *res)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
  {
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
152
  	struct tcf_mirred *m = a->priv;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
  	struct net_device *dev;
feed1f172   Changli Gao   act_mirred: cleanup
154
155
156
  	struct sk_buff *skb2;
  	u32 at;
  	int retval, err = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157

e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
158
  	spin_lock(&m->tcf_lock);
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
159
  	m->tcf_tm.lastuse = jiffies;
bfe0d0298   Eric Dumazet   net_sched: factor...
160
  	bstats_update(&m->tcf_bstats, skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161

feed1f172   Changli Gao   act_mirred: cleanup
162
  	dev = m->tcfm_dev;
3b87956ea   stephen hemminger   net sched: fix ra...
163
164
165
166
167
  	if (!dev) {
  		printk_once(KERN_NOTICE "tc mirred: target device is gone
  ");
  		goto out;
  	}
feed1f172   Changli Gao   act_mirred: cleanup
168
  	if (!(dev->flags & IFF_UP)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
  		if (net_ratelimit())
3b87956ea   stephen hemminger   net sched: fix ra...
170
171
  			pr_notice("tc mirred to Houston: device %s is down
  ",
6ff9c3644   stephen hemminger   net sched: printk...
172
  				  dev->name);
feed1f172   Changli Gao   act_mirred: cleanup
173
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
  	}
210d6de78   Changli Gao   act_mirred: don't...
175
176
  	at = G_TC_AT(skb->tc_verd);
  	skb2 = skb_act_clone(skb, GFP_ATOMIC, m->tcf_action);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
  	if (skb2 == NULL)
feed1f172   Changli Gao   act_mirred: cleanup
178
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179

feed1f172   Changli Gao   act_mirred: cleanup
180
  	if (!(at & AT_EGRESS)) {
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
181
  		if (m->tcfm_ok_push)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
  			skb_push(skb2, skb2->dev->hard_header_len);
feed1f172   Changli Gao   act_mirred: cleanup
183
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
185
  
  	/* mirror is always swallowed */
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
186
  	if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
  		skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at);
8964be4a9   Eric Dumazet   net: rename skb->...
188
  	skb2->skb_iif = skb->dev->ifindex;
210d6de78   Changli Gao   act_mirred: don't...
189
  	skb2->dev = dev;
8919bc13e   Jamal Hadi Salim   net_sched: fix po...
190
  	err = dev_queue_xmit(skb2);
feed1f172   Changli Gao   act_mirred: cleanup
191
192
193
194
  
  out:
  	if (err) {
  		m->tcf_qstats.overlimits++;
feed1f172   Changli Gao   act_mirred: cleanup
195
196
197
198
199
200
201
  		/* should we be asking for packet to be dropped?
  		 * may make sense for redirect case only
  		 */
  		retval = TC_ACT_SHOT;
  	} else {
  		retval = m->tcf_action;
  	}
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
202
  	spin_unlock(&m->tcf_lock);
feed1f172   Changli Gao   act_mirred: cleanup
203
204
  
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
  }
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
206
  static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
  {
27a884dc3   Arnaldo Carvalho de Melo   [SK_BUFF]: Conver...
208
  	unsigned char *b = skb_tail_pointer(skb);
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
209
  	struct tcf_mirred *m = a->priv;
1c40be12f   Eric Dumazet   net sched: fix so...
210
211
212
213
214
215
216
217
  	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,
  	};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
  	struct tcf_t t;
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
219
  	NLA_PUT(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt);
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
220
221
222
  	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);
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
223
  	NLA_PUT(skb, TCA_MIRRED_TM, sizeof(t), &t);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224
  	return skb->len;
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
225
  nla_put_failure:
dc5fc579b   Arnaldo Carvalho de Melo   [NETLINK]: Use nl...
226
  	nlmsg_trim(skb, b);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
  	return -1;
  }
3b87956ea   stephen hemminger   net sched: fix ra...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  static int mirred_device_event(struct notifier_block *unused,
  			       unsigned long event, void *ptr)
  {
  	struct net_device *dev = ptr;
  	struct tcf_mirred *m;
  
  	if (event == NETDEV_UNREGISTER)
  		list_for_each_entry(m, &mirred_list, tcfm_list) {
  			if (m->tcfm_dev == dev) {
  				dev_put(dev);
  				m->tcfm_dev = NULL;
  			}
  		}
  
  	return NOTIFY_DONE;
  }
  
  static struct notifier_block mirred_device_notifier = {
  	.notifier_call = mirred_device_event,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
250
  static struct tc_action_ops act_mirred_ops = {
  	.kind		=	"mirred",
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
251
  	.hinfo		=	&mirred_hash_info,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  	.type		=	TCA_ACT_MIRRED,
  	.capab		=	TCA_CAP_NONE,
  	.owner		=	THIS_MODULE,
  	.act		=	tcf_mirred,
  	.dump		=	tcf_mirred_dump,
  	.cleanup	=	tcf_mirred_cleanup,
  	.lookup		=	tcf_hash_search,
  	.init		=	tcf_mirred_init,
  	.walk		=	tcf_generic_walker
  };
  
  MODULE_AUTHOR("Jamal Hadi Salim(2002)");
  MODULE_DESCRIPTION("Device Mirror/redirect actions");
  MODULE_LICENSE("GPL");
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
266
  static int __init mirred_init_module(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
  {
3b87956ea   stephen hemminger   net sched: fix ra...
268
269
270
  	int err = register_netdevice_notifier(&mirred_device_notifier);
  	if (err)
  		return err;
6ff9c3644   stephen hemminger   net sched: printk...
271
272
  	pr_info("Mirror/redirect action on
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
274
  	return tcf_register_action(&act_mirred_ops);
  }
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
275
  static void __exit mirred_cleanup_module(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
276
  {
3b87956ea   stephen hemminger   net sched: fix ra...
277
  	unregister_netdevice_notifier(&mirred_device_notifier);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
279
280
281
282
  	tcf_unregister_action(&act_mirred_ops);
  }
  
  module_init(mirred_init_module);
  module_exit(mirred_cleanup_module);