Blame view

net/sched/act_mirred.c 9.36 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
0c6965dd3   Jiri Pirko   sched: fix act fi...
2
   * net/sched/act_mirred.c	packet mirroring and redirect actions
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
5
6
7
8
9
10
11
12
13
   *
   *		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>
c491680f8   Daniel Borkmann   bpf: reuse dev_is...
23
  #include <linux/if_arp.h>
881d966b4   Eric W. Biederman   [NET]: Make the d...
24
  #include <net/net_namespace.h>
dc5fc579b   Arnaldo Carvalho de Melo   [NETLINK]: Use nl...
25
  #include <net/netlink.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
  #include <net/pkt_sched.h>
  #include <linux/tc_act/tc_mirred.h>
  #include <net/tc_act/tc_mirred.h>
3b87956ea   stephen hemminger   net sched: fix ra...
29
  static LIST_HEAD(mirred_list);
6bd00b850   WANG Cong   act_mirred: fix a...
30
  static DEFINE_SPINLOCK(mirred_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31

53592b364   Shmulik Ladkani   net/sched: act_mi...
32
33
34
35
  static bool tcf_mirred_is_act_redirect(int action)
  {
  	return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
  }
8dc07fdbf   Willem de Bruijn   net-tc: convert t...
36
  static bool tcf_mirred_act_wants_ingress(int action)
53592b364   Shmulik Ladkani   net/sched: act_mi...
37
38
39
40
  {
  	switch (action) {
  	case TCA_EGRESS_REDIR:
  	case TCA_EGRESS_MIRROR:
8dc07fdbf   Willem de Bruijn   net-tc: convert t...
41
  		return false;
53592b364   Shmulik Ladkani   net/sched: act_mi...
42
43
  	case TCA_INGRESS_REDIR:
  	case TCA_INGRESS_MIRROR:
8dc07fdbf   Willem de Bruijn   net-tc: convert t...
44
  		return true;
53592b364   Shmulik Ladkani   net/sched: act_mi...
45
46
47
48
  	default:
  		BUG();
  	}
  }
a5b5c958f   WANG Cong   net_sched: act: r...
49
  static void tcf_mirred_release(struct tc_action *a, int bind)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
  {
86062033f   WANG Cong   net_sched: act: h...
51
  	struct tcf_mirred *m = to_mirred(a);
dc327f893   WANG Cong   net_sched: close ...
52
  	struct net_device *dev;
2ee22a90c   Eric Dumazet   net_sched: act_mi...
53

6bd00b850   WANG Cong   act_mirred: fix a...
54
55
  	/* We could be called either in a RCU callback or with RTNL lock held. */
  	spin_lock_bh(&mirred_list_lock);
a5b5c958f   WANG Cong   net_sched: act: r...
56
  	list_del(&m->tcfm_list);
dc327f893   WANG Cong   net_sched: close ...
57
  	dev = rcu_dereference_protected(m->tcfm_dev, 1);
2ee22a90c   Eric Dumazet   net_sched: act_mi...
58
59
  	if (dev)
  		dev_put(dev);
dc327f893   WANG Cong   net_sched: close ...
60
  	spin_unlock_bh(&mirred_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
  }
53b2bf3f8   Patrick McHardy   [NET_SCHED]: Use ...
62
63
64
  static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
  	[TCA_MIRRED_PARMS]	= { .len = sizeof(struct tc_mirred) },
  };
c7d03a00b   Alexey Dobriyan   netns: make struc...
65
  static unsigned int mirred_net_id;
a85a970af   WANG Cong   net_sched: move t...
66
  static struct tc_action_ops act_mirred_ops;
ddf97ccdd   WANG Cong   net_sched: add ne...
67

c1b52739e   Benjamin LaHaise   pkt_sched: namesp...
68
  static int tcf_mirred_init(struct net *net, struct nlattr *nla,
a85a970af   WANG Cong   net_sched: move t...
69
  			   struct nlattr *est, struct tc_action **a, int ovr,
c1b52739e   Benjamin LaHaise   pkt_sched: namesp...
70
  			   int bind)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
  {
ddf97ccdd   WANG Cong   net_sched: add ne...
72
  	struct tc_action_net *tn = net_generic(net, mirred_net_id);
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
73
  	struct nlattr *tb[TCA_MIRRED_MAX + 1];
165779231   Shmulik Ladkani   net/sched: act_mi...
74
  	bool mac_header_xmit = false;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
  	struct tc_mirred *parm;
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
76
  	struct tcf_mirred *m;
b76965e02   Changli Gao   act_mirred: optim...
77
  	struct net_device *dev;
b2313077e   WANG Cong   net_sched: make t...
78
  	bool exists = false;
165779231   Shmulik Ladkani   net/sched: act_mi...
79
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80

cee63723b   Patrick McHardy   [NET_SCHED]: Prop...
81
  	if (nla == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
  		return -EINVAL;
fceb6435e   Johannes Berg   netlink: pass ext...
83
  	ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, NULL);
b76965e02   Changli Gao   act_mirred: optim...
84
85
  	if (ret < 0)
  		return ret;
53b2bf3f8   Patrick McHardy   [NET_SCHED]: Use ...
86
  	if (tb[TCA_MIRRED_PARMS] == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87
  		return -EINVAL;
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
88
  	parm = nla_data(tb[TCA_MIRRED_PARMS]);
87dfbdc6c   Jamal Hadi Salim   net sched: mirred...
89

65a206c01   Chris Mi   net/sched: Change...
90
  	exists = tcf_idr_check(tn, parm->index, a, bind);
87dfbdc6c   Jamal Hadi Salim   net sched: mirred...
91
92
  	if (exists && bind)
  		return 0;
b76965e02   Changli Gao   act_mirred: optim...
93
94
95
  	switch (parm->eaction) {
  	case TCA_EGRESS_MIRROR:
  	case TCA_EGRESS_REDIR:
53592b364   Shmulik Ladkani   net/sched: act_mi...
96
97
  	case TCA_INGRESS_REDIR:
  	case TCA_INGRESS_MIRROR:
b76965e02   Changli Gao   act_mirred: optim...
98
99
  		break;
  	default:
87dfbdc6c   Jamal Hadi Salim   net sched: mirred...
100
  		if (exists)
65a206c01   Chris Mi   net/sched: Change...
101
  			tcf_idr_release(*a, bind);
b76965e02   Changli Gao   act_mirred: optim...
102
103
  		return -EINVAL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  	if (parm->ifindex) {
c1b52739e   Benjamin LaHaise   pkt_sched: namesp...
105
  		dev = __dev_get_by_index(net, parm->ifindex);
87dfbdc6c   Jamal Hadi Salim   net sched: mirred...
106
107
  		if (dev == NULL) {
  			if (exists)
65a206c01   Chris Mi   net/sched: Change...
108
  				tcf_idr_release(*a, bind);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
  			return -ENODEV;
87dfbdc6c   Jamal Hadi Salim   net sched: mirred...
110
  		}
dcf800344   Shmulik Ladkani   net/sched: act_mi...
111
  		mac_header_xmit = dev_is_mac_header_xmit(dev);
b76965e02   Changli Gao   act_mirred: optim...
112
113
  	} else {
  		dev = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
  	}
87dfbdc6c   Jamal Hadi Salim   net sched: mirred...
115
  	if (!exists) {
b76965e02   Changli Gao   act_mirred: optim...
116
  		if (dev == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
  			return -EINVAL;
65a206c01   Chris Mi   net/sched: Change...
118
119
  		ret = tcf_idr_create(tn, parm->index, est, a,
  				     &act_mirred_ops, bind, true);
86062033f   WANG Cong   net_sched: act: h...
120
121
  		if (ret)
  			return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
  		ret = ACT_P_CREATED;
  	} else {
65a206c01   Chris Mi   net/sched: Change...
124
  		tcf_idr_release(*a, bind);
215c90afb   WANG Cong   act_mirred: alway...
125
  		if (!ovr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  			return -EEXIST;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
  	}
a85a970af   WANG Cong   net_sched: move t...
128
  	m = to_mirred(*a);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129

2ee22a90c   Eric Dumazet   net_sched: act_mi...
130
  	ASSERT_RTNL();
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
131
132
  	m->tcf_action = parm->action;
  	m->tcfm_eaction = parm->eaction;
b76965e02   Changli Gao   act_mirred: optim...
133
  	if (dev != NULL) {
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
134
  		m->tcfm_ifindex = parm->ifindex;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135
  		if (ret != ACT_P_CREATED)
2ee22a90c   Eric Dumazet   net_sched: act_mi...
136
  			dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
  		dev_hold(dev);
2ee22a90c   Eric Dumazet   net_sched: act_mi...
138
  		rcu_assign_pointer(m->tcfm_dev, dev);
165779231   Shmulik Ladkani   net/sched: act_mi...
139
  		m->tcfm_mac_header_xmit = mac_header_xmit;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
  	}
2ee22a90c   Eric Dumazet   net_sched: act_mi...
141

3b87956ea   stephen hemminger   net sched: fix ra...
142
  	if (ret == ACT_P_CREATED) {
6bd00b850   WANG Cong   act_mirred: fix a...
143
  		spin_lock_bh(&mirred_list_lock);
3b87956ea   stephen hemminger   net sched: fix ra...
144
  		list_add(&m->tcfm_list, &mirred_list);
6bd00b850   WANG Cong   act_mirred: fix a...
145
  		spin_unlock_bh(&mirred_list_lock);
65a206c01   Chris Mi   net/sched: Change...
146
  		tcf_idr_insert(tn, *a);
3b87956ea   stephen hemminger   net sched: fix ra...
147
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
150
  	return ret;
  }
dc7f9f6e8   Eric Dumazet   net: sched: const...
151
  static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
152
  		      struct tcf_result *res)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
  {
a85a970af   WANG Cong   net_sched: move t...
154
  	struct tcf_mirred *m = to_mirred(a);
53592b364   Shmulik Ladkani   net/sched: act_mi...
155
  	bool m_mac_header_xmit;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
  	struct net_device *dev;
feed1f172   Changli Gao   act_mirred: cleanup
157
  	struct sk_buff *skb2;
53592b364   Shmulik Ladkani   net/sched: act_mi...
158
159
160
  	int retval, err = 0;
  	int m_eaction;
  	int mac_len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161

2ee22a90c   Eric Dumazet   net_sched: act_mi...
162
  	tcf_lastuse_update(&m->tcf_tm);
2ee22a90c   Eric Dumazet   net_sched: act_mi...
163
  	bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164

2ee22a90c   Eric Dumazet   net_sched: act_mi...
165
  	rcu_read_lock();
53592b364   Shmulik Ladkani   net/sched: act_mi...
166
167
  	m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
  	m_eaction = READ_ONCE(m->tcfm_eaction);
2ee22a90c   Eric Dumazet   net_sched: act_mi...
168
169
170
171
172
  	retval = READ_ONCE(m->tcf_action);
  	dev = rcu_dereference(m->tcfm_dev);
  	if (unlikely(!dev)) {
  		pr_notice_once("tc mirred: target device is gone
  ");
3b87956ea   stephen hemminger   net sched: fix ra...
173
174
  		goto out;
  	}
2ee22a90c   Eric Dumazet   net_sched: act_mi...
175
  	if (unlikely(!(dev->flags & IFF_UP))) {
e87cc4728   Joe Perches   net: Convert net_...
176
177
178
  		net_notice_ratelimited("tc mirred to Houston: device %s is down
  ",
  				       dev->name);
feed1f172   Changli Gao   act_mirred: cleanup
179
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
  	}
e578d9c02   Florian Westphal   net: sched: use c...
181
  	skb2 = skb_clone(skb, GFP_ATOMIC);
2ee22a90c   Eric Dumazet   net_sched: act_mi...
182
  	if (!skb2)
feed1f172   Changli Gao   act_mirred: cleanup
183
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184

53592b364   Shmulik Ladkani   net/sched: act_mi...
185
186
187
188
  	/* If action's target direction differs than filter's direction,
  	 * and devices expect a mac header on xmit, then mac push/pull is
  	 * needed.
  	 */
8dc07fdbf   Willem de Bruijn   net-tc: convert t...
189
  	if (skb_at_tc_ingress(skb) != tcf_mirred_act_wants_ingress(m_eaction) &&
a5135bcfb   Willem de Bruijn   net-tc: convert t...
190
191
  	    m_mac_header_xmit) {
  		if (!skb_at_tc_ingress(skb)) {
53592b364   Shmulik Ladkani   net/sched: act_mi...
192
193
194
195
196
  			/* 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 */
82a31b923   WANG Cong   net_sched: fix mi...
197
  			skb_push_rcsum(skb2, skb->mac_len);
53592b364   Shmulik Ladkani   net/sched: act_mi...
198
  		}
feed1f172   Changli Gao   act_mirred: cleanup
199
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
  
  	/* mirror is always swallowed */
bc31c905e   Willem de Bruijn   net-tc: convert t...
202
203
204
205
  	if (tcf_mirred_is_act_redirect(m_eaction)) {
  		skb2->tc_redirected = 1;
  		skb2->tc_from_ingress = skb2->tc_at_ingress;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206

8964be4a9   Eric Dumazet   net: rename skb->...
207
  	skb2->skb_iif = skb->dev->ifindex;
210d6de78   Changli Gao   act_mirred: don't...
208
  	skb2->dev = dev;
8dc07fdbf   Willem de Bruijn   net-tc: convert t...
209
  	if (!tcf_mirred_act_wants_ingress(m_eaction))
53592b364   Shmulik Ladkani   net/sched: act_mi...
210
211
212
  		err = dev_queue_xmit(skb2);
  	else
  		err = netif_receive_skb(skb2);
feed1f172   Changli Gao   act_mirred: cleanup
213

feed1f172   Changli Gao   act_mirred: cleanup
214
  	if (err) {
2ee22a90c   Eric Dumazet   net_sched: act_mi...
215
216
  out:
  		qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
53592b364   Shmulik Ladkani   net/sched: act_mi...
217
  		if (tcf_mirred_is_act_redirect(m_eaction))
16c0b164b   Jason Wang   act_mirred: do no...
218
  			retval = TC_ACT_SHOT;
2ee22a90c   Eric Dumazet   net_sched: act_mi...
219
220
  	}
  	rcu_read_unlock();
feed1f172   Changli Gao   act_mirred: cleanup
221
222
  
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
223
  }
9798e6fe4   Jakub Kicinski   net: act_mirred: ...
224
225
226
  static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
  			     u64 lastuse)
  {
5712bf9c5   Paul Blakey   net/sched: act_mi...
227
228
  	struct tcf_mirred *m = to_mirred(a);
  	struct tcf_t *tm = &m->tcf_tm;
9798e6fe4   Jakub Kicinski   net: act_mirred: ...
229
  	_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
71e7f85e1   Roi Dayan   net/sched: Fix up...
230
  	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
9798e6fe4   Jakub Kicinski   net: act_mirred: ...
231
  }
5a7a5555a   Jamal Hadi Salim   net sched: stylis...
232
233
  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
234
  {
27a884dc3   Arnaldo Carvalho de Melo   [SK_BUFF]: Conver...
235
  	unsigned char *b = skb_tail_pointer(skb);
a85a970af   WANG Cong   net_sched: move t...
236
  	struct tcf_mirred *m = to_mirred(a);
1c40be12f   Eric Dumazet   net sched: fix so...
237
238
239
240
241
242
243
244
  	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
245
  	struct tcf_t t;
1b34ec43c   David S. Miller   pkt_sched: Stop u...
246
247
  	if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
  		goto nla_put_failure;
48d8ee169   Jamal Hadi Salim   net sched actions...
248
249
  
  	tcf_tm_dump(&t, &m->tcf_tm);
9854518ea   Nicolas Dichtel   sched: align nlat...
250
  	if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
1b34ec43c   David S. Miller   pkt_sched: Stop u...
251
  		goto nla_put_failure;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
  	return skb->len;
7ba699c60   Patrick McHardy   [NET_SCHED]: Conv...
253
  nla_put_failure:
dc5fc579b   Arnaldo Carvalho de Melo   [NETLINK]: Use nl...
254
  	nlmsg_trim(skb, b);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
256
  	return -1;
  }
ddf97ccdd   WANG Cong   net_sched: add ne...
257
258
  static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
  			     struct netlink_callback *cb, int type,
a85a970af   WANG Cong   net_sched: move t...
259
  			     const struct tc_action_ops *ops)
ddf97ccdd   WANG Cong   net_sched: add ne...
260
261
  {
  	struct tc_action_net *tn = net_generic(net, mirred_net_id);
a85a970af   WANG Cong   net_sched: move t...
262
  	return tcf_generic_walker(tn, skb, cb, type, ops);
ddf97ccdd   WANG Cong   net_sched: add ne...
263
  }
a85a970af   WANG Cong   net_sched: move t...
264
  static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccdd   WANG Cong   net_sched: add ne...
265
266
  {
  	struct tc_action_net *tn = net_generic(net, mirred_net_id);
65a206c01   Chris Mi   net/sched: Change...
267
  	return tcf_idr_search(tn, a, index);
ddf97ccdd   WANG Cong   net_sched: add ne...
268
  }
3b87956ea   stephen hemminger   net sched: fix ra...
269
270
271
  static int mirred_device_event(struct notifier_block *unused,
  			       unsigned long event, void *ptr)
  {
351638e7d   Jiri Pirko   net: pass info st...
272
  	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3b87956ea   stephen hemminger   net sched: fix ra...
273
  	struct tcf_mirred *m;
2ee22a90c   Eric Dumazet   net_sched: act_mi...
274
  	ASSERT_RTNL();
6bd00b850   WANG Cong   act_mirred: fix a...
275
276
  	if (event == NETDEV_UNREGISTER) {
  		spin_lock_bh(&mirred_list_lock);
3b87956ea   stephen hemminger   net sched: fix ra...
277
  		list_for_each_entry(m, &mirred_list, tcfm_list) {
2ee22a90c   Eric Dumazet   net_sched: act_mi...
278
  			if (rcu_access_pointer(m->tcfm_dev) == dev) {
3b87956ea   stephen hemminger   net sched: fix ra...
279
  				dev_put(dev);
2ee22a90c   Eric Dumazet   net_sched: act_mi...
280
281
282
283
  				/* Note : no rcu grace period necessary, as
  				 * net_device are already rcu protected.
  				 */
  				RCU_INIT_POINTER(m->tcfm_dev, NULL);
3b87956ea   stephen hemminger   net sched: fix ra...
284
285
  			}
  		}
6bd00b850   WANG Cong   act_mirred: fix a...
286
287
  		spin_unlock_bh(&mirred_list_lock);
  	}
3b87956ea   stephen hemminger   net sched: fix ra...
288
289
290
291
292
293
294
  
  	return NOTIFY_DONE;
  }
  
  static struct notifier_block mirred_device_notifier = {
  	.notifier_call = mirred_device_event,
  };
255cb3042   Hadar Hen Zion   net/sched: act_mi...
295
296
297
298
299
300
  static int tcf_mirred_device(const struct tc_action *a, struct net *net,
  			     struct net_device **mirred_dev)
  {
  	int ifindex = tcf_mirred_ifindex(a);
  
  	*mirred_dev = __dev_get_by_index(net, ifindex);
548ed7224   WANG Cong   act_mirred: fix a...
301
  	if (!*mirred_dev)
255cb3042   Hadar Hen Zion   net/sched: act_mi...
302
303
304
  		return -EINVAL;
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
306
307
  static struct tc_action_ops act_mirred_ops = {
  	.kind		=	"mirred",
  	.type		=	TCA_ACT_MIRRED,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
308
309
  	.owner		=	THIS_MODULE,
  	.act		=	tcf_mirred,
9798e6fe4   Jakub Kicinski   net: act_mirred: ...
310
  	.stats_update	=	tcf_stats_update,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311
  	.dump		=	tcf_mirred_dump,
86062033f   WANG Cong   net_sched: act: h...
312
  	.cleanup	=	tcf_mirred_release,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
313
  	.init		=	tcf_mirred_init,
ddf97ccdd   WANG Cong   net_sched: add ne...
314
315
  	.walk		=	tcf_mirred_walker,
  	.lookup		=	tcf_mirred_search,
a85a970af   WANG Cong   net_sched: move t...
316
  	.size		=	sizeof(struct tcf_mirred),
255cb3042   Hadar Hen Zion   net/sched: act_mi...
317
  	.get_dev	=	tcf_mirred_device,
ddf97ccdd   WANG Cong   net_sched: add ne...
318
319
320
321
322
  };
  
  static __net_init int mirred_init_net(struct net *net)
  {
  	struct tc_action_net *tn = net_generic(net, mirred_net_id);
c7e460ce5   Cong Wang   Revert "net_sched...
323
  	return tc_action_net_init(tn, &act_mirred_ops);
ddf97ccdd   WANG Cong   net_sched: add ne...
324
325
326
327
328
329
330
331
332
333
334
335
336
337
  }
  
  static void __net_exit mirred_exit_net(struct net *net)
  {
  	struct tc_action_net *tn = net_generic(net, mirred_net_id);
  
  	tc_action_net_exit(tn);
  }
  
  static struct pernet_operations mirred_net_ops = {
  	.init = mirred_init_net,
  	.exit = mirred_exit_net,
  	.id   = &mirred_net_id,
  	.size = sizeof(struct tc_action_net),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
339
340
341
342
  };
  
  MODULE_AUTHOR("Jamal Hadi Salim(2002)");
  MODULE_DESCRIPTION("Device Mirror/redirect actions");
  MODULE_LICENSE("GPL");
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
343
  static int __init mirred_init_module(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
  {
3b87956ea   stephen hemminger   net sched: fix ra...
345
346
347
  	int err = register_netdevice_notifier(&mirred_device_notifier);
  	if (err)
  		return err;
6ff9c3644   stephen hemminger   net sched: printk...
348
349
  	pr_info("Mirror/redirect action on
  ");
ddf97ccdd   WANG Cong   net_sched: add ne...
350
  	return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
351
  }
e9ce1cd3c   David S. Miller   [PKT_SCHED]: Kill...
352
  static void __exit mirred_cleanup_module(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
  {
ddf97ccdd   WANG Cong   net_sched: add ne...
354
  	tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
568a153a2   WANG Cong   net_sched: fix a ...
355
  	unregister_netdevice_notifier(&mirred_device_notifier);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
357
358
359
  }
  
  module_init(mirred_init_module);
  module_exit(mirred_cleanup_module);