Blame view
net/sched/act_mirred.c
8.17 KB
1da177e4c Linux-2.6.12-rc2 |
1 |
/* |
0c6965dd3 sched: fix act fi... |
2 |
* net/sched/act_mirred.c packet mirroring and redirect actions |
1da177e4c 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 Linux-2.6.12-rc2 |
14 15 |
#include <linux/types.h> #include <linux/kernel.h> |
1da177e4c Linux-2.6.12-rc2 |
16 |
#include <linux/string.h> |
1da177e4c Linux-2.6.12-rc2 |
17 |
#include <linux/errno.h> |
1da177e4c 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 include cleanup: ... |
22 |
#include <linux/gfp.h> |
881d966b4 [NET]: Make the d... |
23 |
#include <net/net_namespace.h> |
dc5fc579b [NETLINK]: Use nl... |
24 |
#include <net/netlink.h> |
1da177e4c 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 Linux-2.6.12-rc2 |
28 |
#include <linux/if_arp.h> |
e9ce1cd3c [PKT_SCHED]: Kill... |
29 |
#define MIRRED_TAB_MASK 7 |
3b87956ea net sched: fix ra... |
30 |
static LIST_HEAD(mirred_list); |
6bd00b850 act_mirred: fix a... |
31 |
static DEFINE_SPINLOCK(mirred_list_lock); |
1da177e4c Linux-2.6.12-rc2 |
32 |
|
a5b5c958f net_sched: act: r... |
33 |
static void tcf_mirred_release(struct tc_action *a, int bind) |
1da177e4c Linux-2.6.12-rc2 |
34 |
{ |
86062033f net_sched: act: h... |
35 |
struct tcf_mirred *m = to_mirred(a); |
dc327f893 net_sched: close ... |
36 |
struct net_device *dev; |
2ee22a90c net_sched: act_mi... |
37 |
|
6bd00b850 act_mirred: fix a... |
38 39 |
/* We could be called either in a RCU callback or with RTNL lock held. */ spin_lock_bh(&mirred_list_lock); |
a5b5c958f net_sched: act: r... |
40 |
list_del(&m->tcfm_list); |
dc327f893 net_sched: close ... |
41 |
dev = rcu_dereference_protected(m->tcfm_dev, 1); |
2ee22a90c net_sched: act_mi... |
42 43 |
if (dev) dev_put(dev); |
dc327f893 net_sched: close ... |
44 |
spin_unlock_bh(&mirred_list_lock); |
1da177e4c Linux-2.6.12-rc2 |
45 |
} |
53b2bf3f8 [NET_SCHED]: Use ... |
46 47 48 |
static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = { [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) }, }; |
ddf97ccdd net_sched: add ne... |
49 |
static int mirred_net_id; |
a85a970af net_sched: move t... |
50 |
static struct tc_action_ops act_mirred_ops; |
ddf97ccdd net_sched: add ne... |
51 |
|
c1b52739e pkt_sched: namesp... |
52 |
static int tcf_mirred_init(struct net *net, struct nlattr *nla, |
a85a970af net_sched: move t... |
53 |
struct nlattr *est, struct tc_action **a, int ovr, |
c1b52739e pkt_sched: namesp... |
54 |
int bind) |
1da177e4c Linux-2.6.12-rc2 |
55 |
{ |
ddf97ccdd net_sched: add ne... |
56 |
struct tc_action_net *tn = net_generic(net, mirred_net_id); |
7ba699c60 [NET_SCHED]: Conv... |
57 |
struct nlattr *tb[TCA_MIRRED_MAX + 1]; |
1da177e4c Linux-2.6.12-rc2 |
58 |
struct tc_mirred *parm; |
e9ce1cd3c [PKT_SCHED]: Kill... |
59 |
struct tcf_mirred *m; |
b76965e02 act_mirred: optim... |
60 |
struct net_device *dev; |
b2313077e net_sched: make t... |
61 62 |
int ret, ok_push = 0; bool exists = false; |
1da177e4c Linux-2.6.12-rc2 |
63 |
|
cee63723b [NET_SCHED]: Prop... |
64 |
if (nla == NULL) |
1da177e4c Linux-2.6.12-rc2 |
65 |
return -EINVAL; |
b76965e02 act_mirred: optim... |
66 67 68 |
ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy); if (ret < 0) return ret; |
53b2bf3f8 [NET_SCHED]: Use ... |
69 |
if (tb[TCA_MIRRED_PARMS] == NULL) |
1da177e4c Linux-2.6.12-rc2 |
70 |
return -EINVAL; |
7ba699c60 [NET_SCHED]: Conv... |
71 |
parm = nla_data(tb[TCA_MIRRED_PARMS]); |
87dfbdc6c net sched: mirred... |
72 73 74 75 |
exists = tcf_hash_check(tn, parm->index, a, bind); if (exists && bind) return 0; |
b76965e02 act_mirred: optim... |
76 77 78 79 80 |
switch (parm->eaction) { case TCA_EGRESS_MIRROR: case TCA_EGRESS_REDIR: break; default: |
87dfbdc6c net sched: mirred... |
81 |
if (exists) |
a85a970af net_sched: move t... |
82 |
tcf_hash_release(*a, bind); |
b76965e02 act_mirred: optim... |
83 84 |
return -EINVAL; } |
1da177e4c Linux-2.6.12-rc2 |
85 |
if (parm->ifindex) { |
c1b52739e pkt_sched: namesp... |
86 |
dev = __dev_get_by_index(net, parm->ifindex); |
87dfbdc6c net sched: mirred... |
87 88 |
if (dev == NULL) { if (exists) |
a85a970af net_sched: move t... |
89 |
tcf_hash_release(*a, bind); |
1da177e4c Linux-2.6.12-rc2 |
90 |
return -ENODEV; |
87dfbdc6c net sched: mirred... |
91 |
} |
1da177e4c Linux-2.6.12-rc2 |
92 |
switch (dev->type) { |
b76965e02 act_mirred: optim... |
93 94 95 96 97 98 99 100 101 102 103 |
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 Linux-2.6.12-rc2 |
104 |
} |
b76965e02 act_mirred: optim... |
105 106 |
} else { dev = NULL; |
1da177e4c Linux-2.6.12-rc2 |
107 |
} |
87dfbdc6c net sched: mirred... |
108 |
if (!exists) { |
b76965e02 act_mirred: optim... |
109 |
if (dev == NULL) |
1da177e4c Linux-2.6.12-rc2 |
110 |
return -EINVAL; |
ddf97ccdd net_sched: add ne... |
111 |
ret = tcf_hash_create(tn, parm->index, est, a, |
a85a970af net_sched: move t... |
112 |
&act_mirred_ops, bind, true); |
86062033f net_sched: act: h... |
113 114 |
if (ret) return ret; |
1da177e4c Linux-2.6.12-rc2 |
115 116 |
ret = ACT_P_CREATED; } else { |
a85a970af net_sched: move t... |
117 |
tcf_hash_release(*a, bind); |
215c90afb act_mirred: alway... |
118 |
if (!ovr) |
1da177e4c Linux-2.6.12-rc2 |
119 |
return -EEXIST; |
1da177e4c Linux-2.6.12-rc2 |
120 |
} |
a85a970af net_sched: move t... |
121 |
m = to_mirred(*a); |
1da177e4c Linux-2.6.12-rc2 |
122 |
|
2ee22a90c net_sched: act_mi... |
123 |
ASSERT_RTNL(); |
e9ce1cd3c [PKT_SCHED]: Kill... |
124 125 |
m->tcf_action = parm->action; m->tcfm_eaction = parm->eaction; |
b76965e02 act_mirred: optim... |
126 |
if (dev != NULL) { |
e9ce1cd3c [PKT_SCHED]: Kill... |
127 |
m->tcfm_ifindex = parm->ifindex; |
1da177e4c Linux-2.6.12-rc2 |
128 |
if (ret != ACT_P_CREATED) |
2ee22a90c net_sched: act_mi... |
129 |
dev_put(rcu_dereference_protected(m->tcfm_dev, 1)); |
1da177e4c Linux-2.6.12-rc2 |
130 |
dev_hold(dev); |
2ee22a90c net_sched: act_mi... |
131 |
rcu_assign_pointer(m->tcfm_dev, dev); |
e9ce1cd3c [PKT_SCHED]: Kill... |
132 |
m->tcfm_ok_push = ok_push; |
1da177e4c Linux-2.6.12-rc2 |
133 |
} |
2ee22a90c net_sched: act_mi... |
134 |
|
3b87956ea net sched: fix ra... |
135 |
if (ret == ACT_P_CREATED) { |
6bd00b850 act_mirred: fix a... |
136 |
spin_lock_bh(&mirred_list_lock); |
3b87956ea net sched: fix ra... |
137 |
list_add(&m->tcfm_list, &mirred_list); |
6bd00b850 act_mirred: fix a... |
138 |
spin_unlock_bh(&mirred_list_lock); |
a85a970af net_sched: move t... |
139 |
tcf_hash_insert(tn, *a); |
3b87956ea net sched: fix ra... |
140 |
} |
1da177e4c Linux-2.6.12-rc2 |
141 |
|
1da177e4c Linux-2.6.12-rc2 |
142 143 |
return ret; } |
dc7f9f6e8 net: sched: const... |
144 |
static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a, |
e9ce1cd3c [PKT_SCHED]: Kill... |
145 |
struct tcf_result *res) |
1da177e4c Linux-2.6.12-rc2 |
146 |
{ |
a85a970af net_sched: move t... |
147 |
struct tcf_mirred *m = to_mirred(a); |
1da177e4c Linux-2.6.12-rc2 |
148 |
struct net_device *dev; |
feed1f172 act_mirred: cleanup |
149 |
struct sk_buff *skb2; |
2ee22a90c net_sched: act_mi... |
150 |
int retval, err; |
feed1f172 act_mirred: cleanup |
151 |
u32 at; |
1da177e4c Linux-2.6.12-rc2 |
152 |
|
2ee22a90c net_sched: act_mi... |
153 |
tcf_lastuse_update(&m->tcf_tm); |
2ee22a90c net_sched: act_mi... |
154 |
bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb); |
1da177e4c Linux-2.6.12-rc2 |
155 |
|
2ee22a90c net_sched: act_mi... |
156 157 158 159 160 161 |
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 "); |
3b87956ea net sched: fix ra... |
162 163 |
goto out; } |
2ee22a90c net_sched: act_mi... |
164 |
if (unlikely(!(dev->flags & IFF_UP))) { |
e87cc4728 net: Convert net_... |
165 166 167 |
net_notice_ratelimited("tc mirred to Houston: device %s is down ", dev->name); |
feed1f172 act_mirred: cleanup |
168 |
goto out; |
1da177e4c Linux-2.6.12-rc2 |
169 |
} |
210d6de78 act_mirred: don't... |
170 |
at = G_TC_AT(skb->tc_verd); |
e578d9c02 net: sched: use c... |
171 |
skb2 = skb_clone(skb, GFP_ATOMIC); |
2ee22a90c net_sched: act_mi... |
172 |
if (!skb2) |
feed1f172 act_mirred: cleanup |
173 |
goto out; |
1da177e4c Linux-2.6.12-rc2 |
174 |
|
feed1f172 act_mirred: cleanup |
175 |
if (!(at & AT_EGRESS)) { |
e9ce1cd3c [PKT_SCHED]: Kill... |
176 |
if (m->tcfm_ok_push) |
82a31b923 net_sched: fix mi... |
177 |
skb_push_rcsum(skb2, skb->mac_len); |
feed1f172 act_mirred: cleanup |
178 |
} |
1da177e4c Linux-2.6.12-rc2 |
179 180 |
/* mirror is always swallowed */ |
e9ce1cd3c [PKT_SCHED]: Kill... |
181 |
if (m->tcfm_eaction != TCA_EGRESS_MIRROR) |
1da177e4c Linux-2.6.12-rc2 |
182 |
skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at); |
8964be4a9 net: rename skb->... |
183 |
skb2->skb_iif = skb->dev->ifindex; |
210d6de78 act_mirred: don't... |
184 |
skb2->dev = dev; |
8919bc13e net_sched: fix po... |
185 |
err = dev_queue_xmit(skb2); |
feed1f172 act_mirred: cleanup |
186 |
|
feed1f172 act_mirred: cleanup |
187 |
if (err) { |
2ee22a90c net_sched: act_mi... |
188 189 |
out: qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats)); |
16c0b164b act_mirred: do no... |
190 191 |
if (m->tcfm_eaction != TCA_EGRESS_MIRROR) retval = TC_ACT_SHOT; |
2ee22a90c net_sched: act_mi... |
192 193 |
} rcu_read_unlock(); |
feed1f172 act_mirred: cleanup |
194 195 |
return retval; |
1da177e4c Linux-2.6.12-rc2 |
196 |
} |
9798e6fe4 net: act_mirred: ... |
197 198 199 |
static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets, u64 lastuse) { |
5712bf9c5 net/sched: act_mi... |
200 201 |
struct tcf_mirred *m = to_mirred(a); struct tcf_t *tm = &m->tcf_tm; |
9798e6fe4 net: act_mirred: ... |
202 |
_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets); |
5712bf9c5 net/sched: act_mi... |
203 |
tm->lastuse = lastuse; |
9798e6fe4 net: act_mirred: ... |
204 |
} |
5a7a5555a net sched: stylis... |
205 206 |
static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
1da177e4c Linux-2.6.12-rc2 |
207 |
{ |
27a884dc3 [SK_BUFF]: Conver... |
208 |
unsigned char *b = skb_tail_pointer(skb); |
a85a970af net_sched: move t... |
209 |
struct tcf_mirred *m = to_mirred(a); |
1c40be12f 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 Linux-2.6.12-rc2 |
218 |
struct tcf_t t; |
1b34ec43c pkt_sched: Stop u... |
219 220 |
if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt)) goto nla_put_failure; |
48d8ee169 net sched actions... |
221 222 |
tcf_tm_dump(&t, &m->tcf_tm); |
9854518ea sched: align nlat... |
223 |
if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD)) |
1b34ec43c pkt_sched: Stop u... |
224 |
goto nla_put_failure; |
1da177e4c Linux-2.6.12-rc2 |
225 |
return skb->len; |
7ba699c60 [NET_SCHED]: Conv... |
226 |
nla_put_failure: |
dc5fc579b [NETLINK]: Use nl... |
227 |
nlmsg_trim(skb, b); |
1da177e4c Linux-2.6.12-rc2 |
228 229 |
return -1; } |
ddf97ccdd net_sched: add ne... |
230 231 |
static int tcf_mirred_walker(struct net *net, struct sk_buff *skb, struct netlink_callback *cb, int type, |
a85a970af net_sched: move t... |
232 |
const struct tc_action_ops *ops) |
ddf97ccdd net_sched: add ne... |
233 234 |
{ struct tc_action_net *tn = net_generic(net, mirred_net_id); |
a85a970af net_sched: move t... |
235 |
return tcf_generic_walker(tn, skb, cb, type, ops); |
ddf97ccdd net_sched: add ne... |
236 |
} |
a85a970af net_sched: move t... |
237 |
static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index) |
ddf97ccdd net_sched: add ne... |
238 239 240 241 242 |
{ struct tc_action_net *tn = net_generic(net, mirred_net_id); return tcf_hash_search(tn, a, index); } |
3b87956ea net sched: fix ra... |
243 244 245 |
static int mirred_device_event(struct notifier_block *unused, unsigned long event, void *ptr) { |
351638e7d net: pass info st... |
246 |
struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
3b87956ea net sched: fix ra... |
247 |
struct tcf_mirred *m; |
2ee22a90c net_sched: act_mi... |
248 |
ASSERT_RTNL(); |
6bd00b850 act_mirred: fix a... |
249 250 |
if (event == NETDEV_UNREGISTER) { spin_lock_bh(&mirred_list_lock); |
3b87956ea net sched: fix ra... |
251 |
list_for_each_entry(m, &mirred_list, tcfm_list) { |
2ee22a90c net_sched: act_mi... |
252 |
if (rcu_access_pointer(m->tcfm_dev) == dev) { |
3b87956ea net sched: fix ra... |
253 |
dev_put(dev); |
2ee22a90c net_sched: act_mi... |
254 255 256 257 |
/* Note : no rcu grace period necessary, as * net_device are already rcu protected. */ RCU_INIT_POINTER(m->tcfm_dev, NULL); |
3b87956ea net sched: fix ra... |
258 259 |
} } |
6bd00b850 act_mirred: fix a... |
260 261 |
spin_unlock_bh(&mirred_list_lock); } |
3b87956ea net sched: fix ra... |
262 263 264 265 266 267 268 |
return NOTIFY_DONE; } static struct notifier_block mirred_device_notifier = { .notifier_call = mirred_device_event, }; |
1da177e4c Linux-2.6.12-rc2 |
269 270 271 |
static struct tc_action_ops act_mirred_ops = { .kind = "mirred", .type = TCA_ACT_MIRRED, |
1da177e4c Linux-2.6.12-rc2 |
272 273 |
.owner = THIS_MODULE, .act = tcf_mirred, |
9798e6fe4 net: act_mirred: ... |
274 |
.stats_update = tcf_stats_update, |
1da177e4c Linux-2.6.12-rc2 |
275 |
.dump = tcf_mirred_dump, |
86062033f net_sched: act: h... |
276 |
.cleanup = tcf_mirred_release, |
1da177e4c Linux-2.6.12-rc2 |
277 |
.init = tcf_mirred_init, |
ddf97ccdd net_sched: add ne... |
278 279 |
.walk = tcf_mirred_walker, .lookup = tcf_mirred_search, |
a85a970af net_sched: move t... |
280 |
.size = sizeof(struct tcf_mirred), |
ddf97ccdd net_sched: add ne... |
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
}; static __net_init int mirred_init_net(struct net *net) { struct tc_action_net *tn = net_generic(net, mirred_net_id); return tc_action_net_init(tn, &act_mirred_ops, MIRRED_TAB_MASK); } 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 Linux-2.6.12-rc2 |
302 303 304 305 306 |
}; MODULE_AUTHOR("Jamal Hadi Salim(2002)"); MODULE_DESCRIPTION("Device Mirror/redirect actions"); MODULE_LICENSE("GPL"); |
e9ce1cd3c [PKT_SCHED]: Kill... |
307 |
static int __init mirred_init_module(void) |
1da177e4c Linux-2.6.12-rc2 |
308 |
{ |
3b87956ea net sched: fix ra... |
309 310 311 |
int err = register_netdevice_notifier(&mirred_device_notifier); if (err) return err; |
6ff9c3644 net sched: printk... |
312 313 |
pr_info("Mirror/redirect action on "); |
ddf97ccdd net_sched: add ne... |
314 |
return tcf_register_action(&act_mirred_ops, &mirred_net_ops); |
1da177e4c Linux-2.6.12-rc2 |
315 |
} |
e9ce1cd3c [PKT_SCHED]: Kill... |
316 |
static void __exit mirred_cleanup_module(void) |
1da177e4c Linux-2.6.12-rc2 |
317 |
{ |
ddf97ccdd net_sched: add ne... |
318 |
tcf_unregister_action(&act_mirred_ops, &mirred_net_ops); |
568a153a2 net_sched: fix a ... |
319 |
unregister_netdevice_notifier(&mirred_device_notifier); |
1da177e4c Linux-2.6.12-rc2 |
320 321 322 323 |
} module_init(mirred_init_module); module_exit(mirred_cleanup_module); |