]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
0c6965dd | 2 | * net/sched/act_mirred.c packet mirroring and redirect actions |
1da177e4 LT |
3 | * |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Jamal Hadi Salim (2002-4) | |
10 | * | |
11 | * TODO: Add ingress support (and socket redirect support) | |
12 | * | |
13 | */ | |
14 | ||
1da177e4 LT |
15 | #include <linux/types.h> |
16 | #include <linux/kernel.h> | |
1da177e4 | 17 | #include <linux/string.h> |
1da177e4 | 18 | #include <linux/errno.h> |
1da177e4 LT |
19 | #include <linux/skbuff.h> |
20 | #include <linux/rtnetlink.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/init.h> | |
5a0e3ad6 | 23 | #include <linux/gfp.h> |
c491680f | 24 | #include <linux/if_arp.h> |
881d966b | 25 | #include <net/net_namespace.h> |
dc5fc579 | 26 | #include <net/netlink.h> |
1da177e4 LT |
27 | #include <net/pkt_sched.h> |
28 | #include <linux/tc_act/tc_mirred.h> | |
29 | #include <net/tc_act/tc_mirred.h> | |
30 | ||
1da177e4 LT |
31 | #include <linux/if_arp.h> |
32 | ||
e9ce1cd3 | 33 | #define MIRRED_TAB_MASK 7 |
3b87956e | 34 | static LIST_HEAD(mirred_list); |
6bd00b85 | 35 | static DEFINE_SPINLOCK(mirred_list_lock); |
1da177e4 | 36 | |
53592b36 SL |
37 | static bool tcf_mirred_is_act_redirect(int action) |
38 | { | |
39 | return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR; | |
40 | } | |
41 | ||
42 | static u32 tcf_mirred_act_direction(int action) | |
43 | { | |
44 | switch (action) { | |
45 | case TCA_EGRESS_REDIR: | |
46 | case TCA_EGRESS_MIRROR: | |
47 | return AT_EGRESS; | |
48 | case TCA_INGRESS_REDIR: | |
49 | case TCA_INGRESS_MIRROR: | |
50 | return AT_INGRESS; | |
51 | default: | |
52 | BUG(); | |
53 | } | |
54 | } | |
55 | ||
a5b5c958 | 56 | static void tcf_mirred_release(struct tc_action *a, int bind) |
1da177e4 | 57 | { |
86062033 | 58 | struct tcf_mirred *m = to_mirred(a); |
dc327f89 | 59 | struct net_device *dev; |
2ee22a90 | 60 | |
6bd00b85 WC |
61 | /* We could be called either in a RCU callback or with RTNL lock held. */ |
62 | spin_lock_bh(&mirred_list_lock); | |
a5b5c958 | 63 | list_del(&m->tcfm_list); |
dc327f89 | 64 | dev = rcu_dereference_protected(m->tcfm_dev, 1); |
2ee22a90 ED |
65 | if (dev) |
66 | dev_put(dev); | |
dc327f89 | 67 | spin_unlock_bh(&mirred_list_lock); |
1da177e4 LT |
68 | } |
69 | ||
53b2bf3f PM |
70 | static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = { |
71 | [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) }, | |
72 | }; | |
73 | ||
c7d03a00 | 74 | static unsigned int mirred_net_id; |
a85a970a | 75 | static struct tc_action_ops act_mirred_ops; |
ddf97ccd | 76 | |
c1b52739 | 77 | static int tcf_mirred_init(struct net *net, struct nlattr *nla, |
a85a970a | 78 | struct nlattr *est, struct tc_action **a, int ovr, |
c1b52739 | 79 | int bind) |
1da177e4 | 80 | { |
ddf97ccd | 81 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
7ba699c6 | 82 | struct nlattr *tb[TCA_MIRRED_MAX + 1]; |
16577923 | 83 | bool mac_header_xmit = false; |
1da177e4 | 84 | struct tc_mirred *parm; |
e9ce1cd3 | 85 | struct tcf_mirred *m; |
b76965e0 | 86 | struct net_device *dev; |
b2313077 | 87 | bool exists = false; |
16577923 | 88 | int ret; |
1da177e4 | 89 | |
cee63723 | 90 | if (nla == NULL) |
1da177e4 | 91 | return -EINVAL; |
b76965e0 CG |
92 | ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy); |
93 | if (ret < 0) | |
94 | return ret; | |
53b2bf3f | 95 | if (tb[TCA_MIRRED_PARMS] == NULL) |
1da177e4 | 96 | return -EINVAL; |
7ba699c6 | 97 | parm = nla_data(tb[TCA_MIRRED_PARMS]); |
87dfbdc6 JHS |
98 | |
99 | exists = tcf_hash_check(tn, parm->index, a, bind); | |
100 | if (exists && bind) | |
101 | return 0; | |
102 | ||
b76965e0 CG |
103 | switch (parm->eaction) { |
104 | case TCA_EGRESS_MIRROR: | |
105 | case TCA_EGRESS_REDIR: | |
53592b36 SL |
106 | case TCA_INGRESS_REDIR: |
107 | case TCA_INGRESS_MIRROR: | |
b76965e0 CG |
108 | break; |
109 | default: | |
87dfbdc6 | 110 | if (exists) |
a85a970a | 111 | tcf_hash_release(*a, bind); |
b76965e0 CG |
112 | return -EINVAL; |
113 | } | |
1da177e4 | 114 | if (parm->ifindex) { |
c1b52739 | 115 | dev = __dev_get_by_index(net, parm->ifindex); |
87dfbdc6 JHS |
116 | if (dev == NULL) { |
117 | if (exists) | |
a85a970a | 118 | tcf_hash_release(*a, bind); |
1da177e4 | 119 | return -ENODEV; |
87dfbdc6 | 120 | } |
dcf80034 | 121 | mac_header_xmit = dev_is_mac_header_xmit(dev); |
b76965e0 CG |
122 | } else { |
123 | dev = NULL; | |
1da177e4 LT |
124 | } |
125 | ||
87dfbdc6 | 126 | if (!exists) { |
b76965e0 | 127 | if (dev == NULL) |
1da177e4 | 128 | return -EINVAL; |
ddf97ccd | 129 | ret = tcf_hash_create(tn, parm->index, est, a, |
a85a970a | 130 | &act_mirred_ops, bind, true); |
86062033 WC |
131 | if (ret) |
132 | return ret; | |
1da177e4 LT |
133 | ret = ACT_P_CREATED; |
134 | } else { | |
a85a970a | 135 | tcf_hash_release(*a, bind); |
215c90af | 136 | if (!ovr) |
1da177e4 | 137 | return -EEXIST; |
1da177e4 | 138 | } |
a85a970a | 139 | m = to_mirred(*a); |
1da177e4 | 140 | |
2ee22a90 | 141 | ASSERT_RTNL(); |
e9ce1cd3 DM |
142 | m->tcf_action = parm->action; |
143 | m->tcfm_eaction = parm->eaction; | |
b76965e0 | 144 | if (dev != NULL) { |
e9ce1cd3 | 145 | m->tcfm_ifindex = parm->ifindex; |
1da177e4 | 146 | if (ret != ACT_P_CREATED) |
2ee22a90 | 147 | dev_put(rcu_dereference_protected(m->tcfm_dev, 1)); |
1da177e4 | 148 | dev_hold(dev); |
2ee22a90 | 149 | rcu_assign_pointer(m->tcfm_dev, dev); |
16577923 | 150 | m->tcfm_mac_header_xmit = mac_header_xmit; |
1da177e4 | 151 | } |
2ee22a90 | 152 | |
3b87956e | 153 | if (ret == ACT_P_CREATED) { |
6bd00b85 | 154 | spin_lock_bh(&mirred_list_lock); |
3b87956e | 155 | list_add(&m->tcfm_list, &mirred_list); |
6bd00b85 | 156 | spin_unlock_bh(&mirred_list_lock); |
a85a970a | 157 | tcf_hash_insert(tn, *a); |
3b87956e | 158 | } |
1da177e4 | 159 | |
1da177e4 LT |
160 | return ret; |
161 | } | |
162 | ||
dc7f9f6e | 163 | static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a, |
e9ce1cd3 | 164 | struct tcf_result *res) |
1da177e4 | 165 | { |
a85a970a | 166 | struct tcf_mirred *m = to_mirred(a); |
53592b36 | 167 | bool m_mac_header_xmit; |
1da177e4 | 168 | struct net_device *dev; |
feed1f17 | 169 | struct sk_buff *skb2; |
53592b36 SL |
170 | int retval, err = 0; |
171 | int m_eaction; | |
172 | int mac_len; | |
feed1f17 | 173 | u32 at; |
1da177e4 | 174 | |
2ee22a90 | 175 | tcf_lastuse_update(&m->tcf_tm); |
2ee22a90 | 176 | bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb); |
1da177e4 | 177 | |
2ee22a90 | 178 | rcu_read_lock(); |
53592b36 SL |
179 | m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit); |
180 | m_eaction = READ_ONCE(m->tcfm_eaction); | |
2ee22a90 ED |
181 | retval = READ_ONCE(m->tcf_action); |
182 | dev = rcu_dereference(m->tcfm_dev); | |
183 | if (unlikely(!dev)) { | |
184 | pr_notice_once("tc mirred: target device is gone\n"); | |
3b87956e | 185 | goto out; |
186 | } | |
187 | ||
2ee22a90 | 188 | if (unlikely(!(dev->flags & IFF_UP))) { |
e87cc472 JP |
189 | net_notice_ratelimited("tc mirred to Houston: device %s is down\n", |
190 | dev->name); | |
feed1f17 | 191 | goto out; |
1da177e4 LT |
192 | } |
193 | ||
210d6de7 | 194 | at = G_TC_AT(skb->tc_verd); |
e578d9c0 | 195 | skb2 = skb_clone(skb, GFP_ATOMIC); |
2ee22a90 | 196 | if (!skb2) |
feed1f17 | 197 | goto out; |
1da177e4 | 198 | |
53592b36 SL |
199 | /* If action's target direction differs than filter's direction, |
200 | * and devices expect a mac header on xmit, then mac push/pull is | |
201 | * needed. | |
202 | */ | |
203 | if (at != tcf_mirred_act_direction(m_eaction) && m_mac_header_xmit) { | |
204 | if (at & AT_EGRESS) { | |
205 | /* caught at egress, act ingress: pull mac */ | |
206 | mac_len = skb_network_header(skb) - skb_mac_header(skb); | |
207 | skb_pull_rcsum(skb2, mac_len); | |
208 | } else { | |
209 | /* caught at ingress, act egress: push mac */ | |
82a31b92 | 210 | skb_push_rcsum(skb2, skb->mac_len); |
53592b36 | 211 | } |
feed1f17 | 212 | } |
1da177e4 LT |
213 | |
214 | /* mirror is always swallowed */ | |
53592b36 | 215 | if (tcf_mirred_is_act_redirect(m_eaction)) |
1da177e4 LT |
216 | skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at); |
217 | ||
8964be4a | 218 | skb2->skb_iif = skb->dev->ifindex; |
210d6de7 | 219 | skb2->dev = dev; |
53592b36 SL |
220 | if (tcf_mirred_act_direction(m_eaction) & AT_EGRESS) |
221 | err = dev_queue_xmit(skb2); | |
222 | else | |
223 | err = netif_receive_skb(skb2); | |
feed1f17 | 224 | |
feed1f17 | 225 | if (err) { |
2ee22a90 ED |
226 | out: |
227 | qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats)); | |
53592b36 | 228 | if (tcf_mirred_is_act_redirect(m_eaction)) |
16c0b164 | 229 | retval = TC_ACT_SHOT; |
2ee22a90 ED |
230 | } |
231 | rcu_read_unlock(); | |
feed1f17 CG |
232 | |
233 | return retval; | |
1da177e4 LT |
234 | } |
235 | ||
9798e6fe JK |
236 | static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets, |
237 | u64 lastuse) | |
238 | { | |
5712bf9c PB |
239 | struct tcf_mirred *m = to_mirred(a); |
240 | struct tcf_t *tm = &m->tcf_tm; | |
241 | ||
9798e6fe | 242 | _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets); |
5712bf9c | 243 | tm->lastuse = lastuse; |
9798e6fe JK |
244 | } |
245 | ||
5a7a5555 JHS |
246 | static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, |
247 | int ref) | |
1da177e4 | 248 | { |
27a884dc | 249 | unsigned char *b = skb_tail_pointer(skb); |
a85a970a | 250 | struct tcf_mirred *m = to_mirred(a); |
1c40be12 ED |
251 | struct tc_mirred opt = { |
252 | .index = m->tcf_index, | |
253 | .action = m->tcf_action, | |
254 | .refcnt = m->tcf_refcnt - ref, | |
255 | .bindcnt = m->tcf_bindcnt - bind, | |
256 | .eaction = m->tcfm_eaction, | |
257 | .ifindex = m->tcfm_ifindex, | |
258 | }; | |
1da177e4 LT |
259 | struct tcf_t t; |
260 | ||
1b34ec43 DM |
261 | if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt)) |
262 | goto nla_put_failure; | |
48d8ee16 JHS |
263 | |
264 | tcf_tm_dump(&t, &m->tcf_tm); | |
9854518e | 265 | if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD)) |
1b34ec43 | 266 | goto nla_put_failure; |
1da177e4 LT |
267 | return skb->len; |
268 | ||
7ba699c6 | 269 | nla_put_failure: |
dc5fc579 | 270 | nlmsg_trim(skb, b); |
1da177e4 LT |
271 | return -1; |
272 | } | |
273 | ||
ddf97ccd WC |
274 | static int tcf_mirred_walker(struct net *net, struct sk_buff *skb, |
275 | struct netlink_callback *cb, int type, | |
a85a970a | 276 | const struct tc_action_ops *ops) |
ddf97ccd WC |
277 | { |
278 | struct tc_action_net *tn = net_generic(net, mirred_net_id); | |
279 | ||
a85a970a | 280 | return tcf_generic_walker(tn, skb, cb, type, ops); |
ddf97ccd WC |
281 | } |
282 | ||
a85a970a | 283 | static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index) |
ddf97ccd WC |
284 | { |
285 | struct tc_action_net *tn = net_generic(net, mirred_net_id); | |
286 | ||
287 | return tcf_hash_search(tn, a, index); | |
288 | } | |
289 | ||
3b87956e | 290 | static int mirred_device_event(struct notifier_block *unused, |
291 | unsigned long event, void *ptr) | |
292 | { | |
351638e7 | 293 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
3b87956e | 294 | struct tcf_mirred *m; |
295 | ||
2ee22a90 | 296 | ASSERT_RTNL(); |
6bd00b85 WC |
297 | if (event == NETDEV_UNREGISTER) { |
298 | spin_lock_bh(&mirred_list_lock); | |
3b87956e | 299 | list_for_each_entry(m, &mirred_list, tcfm_list) { |
2ee22a90 | 300 | if (rcu_access_pointer(m->tcfm_dev) == dev) { |
3b87956e | 301 | dev_put(dev); |
2ee22a90 ED |
302 | /* Note : no rcu grace period necessary, as |
303 | * net_device are already rcu protected. | |
304 | */ | |
305 | RCU_INIT_POINTER(m->tcfm_dev, NULL); | |
3b87956e | 306 | } |
307 | } | |
6bd00b85 WC |
308 | spin_unlock_bh(&mirred_list_lock); |
309 | } | |
3b87956e | 310 | |
311 | return NOTIFY_DONE; | |
312 | } | |
313 | ||
314 | static struct notifier_block mirred_device_notifier = { | |
315 | .notifier_call = mirred_device_event, | |
316 | }; | |
317 | ||
255cb304 HHZ |
318 | static int tcf_mirred_device(const struct tc_action *a, struct net *net, |
319 | struct net_device **mirred_dev) | |
320 | { | |
321 | int ifindex = tcf_mirred_ifindex(a); | |
322 | ||
323 | *mirred_dev = __dev_get_by_index(net, ifindex); | |
548ed722 | 324 | if (!*mirred_dev) |
255cb304 HHZ |
325 | return -EINVAL; |
326 | return 0; | |
327 | } | |
328 | ||
1da177e4 LT |
329 | static struct tc_action_ops act_mirred_ops = { |
330 | .kind = "mirred", | |
331 | .type = TCA_ACT_MIRRED, | |
1da177e4 LT |
332 | .owner = THIS_MODULE, |
333 | .act = tcf_mirred, | |
9798e6fe | 334 | .stats_update = tcf_stats_update, |
1da177e4 | 335 | .dump = tcf_mirred_dump, |
86062033 | 336 | .cleanup = tcf_mirred_release, |
1da177e4 | 337 | .init = tcf_mirred_init, |
ddf97ccd WC |
338 | .walk = tcf_mirred_walker, |
339 | .lookup = tcf_mirred_search, | |
a85a970a | 340 | .size = sizeof(struct tcf_mirred), |
255cb304 | 341 | .get_dev = tcf_mirred_device, |
ddf97ccd WC |
342 | }; |
343 | ||
344 | static __net_init int mirred_init_net(struct net *net) | |
345 | { | |
346 | struct tc_action_net *tn = net_generic(net, mirred_net_id); | |
347 | ||
348 | return tc_action_net_init(tn, &act_mirred_ops, MIRRED_TAB_MASK); | |
349 | } | |
350 | ||
351 | static void __net_exit mirred_exit_net(struct net *net) | |
352 | { | |
353 | struct tc_action_net *tn = net_generic(net, mirred_net_id); | |
354 | ||
355 | tc_action_net_exit(tn); | |
356 | } | |
357 | ||
358 | static struct pernet_operations mirred_net_ops = { | |
359 | .init = mirred_init_net, | |
360 | .exit = mirred_exit_net, | |
361 | .id = &mirred_net_id, | |
362 | .size = sizeof(struct tc_action_net), | |
1da177e4 LT |
363 | }; |
364 | ||
365 | MODULE_AUTHOR("Jamal Hadi Salim(2002)"); | |
366 | MODULE_DESCRIPTION("Device Mirror/redirect actions"); | |
367 | MODULE_LICENSE("GPL"); | |
368 | ||
e9ce1cd3 | 369 | static int __init mirred_init_module(void) |
1da177e4 | 370 | { |
3b87956e | 371 | int err = register_netdevice_notifier(&mirred_device_notifier); |
372 | if (err) | |
373 | return err; | |
374 | ||
6ff9c364 | 375 | pr_info("Mirror/redirect action on\n"); |
ddf97ccd | 376 | return tcf_register_action(&act_mirred_ops, &mirred_net_ops); |
1da177e4 LT |
377 | } |
378 | ||
e9ce1cd3 | 379 | static void __exit mirred_cleanup_module(void) |
1da177e4 | 380 | { |
ddf97ccd | 381 | tcf_unregister_action(&act_mirred_ops, &mirred_net_ops); |
568a153a | 382 | unregister_netdevice_notifier(&mirred_device_notifier); |
1da177e4 LT |
383 | } |
384 | ||
385 | module_init(mirred_init_module); | |
386 | module_exit(mirred_cleanup_module); |