]>
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 | 27 | #include <net/pkt_sched.h> |
e5cf1baf | 28 | #include <net/pkt_cls.h> |
1da177e4 LT |
29 | #include <linux/tc_act/tc_mirred.h> |
30 | #include <net/tc_act/tc_mirred.h> | |
31 | ||
3b87956e | 32 | static LIST_HEAD(mirred_list); |
4e232818 | 33 | static DEFINE_SPINLOCK(mirred_list_lock); |
1da177e4 | 34 | |
53592b36 SL |
35 | static bool tcf_mirred_is_act_redirect(int action) |
36 | { | |
37 | return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR; | |
38 | } | |
39 | ||
8dc07fdb | 40 | static bool tcf_mirred_act_wants_ingress(int action) |
53592b36 SL |
41 | { |
42 | switch (action) { | |
43 | case TCA_EGRESS_REDIR: | |
44 | case TCA_EGRESS_MIRROR: | |
8dc07fdb | 45 | return false; |
53592b36 SL |
46 | case TCA_INGRESS_REDIR: |
47 | case TCA_INGRESS_MIRROR: | |
8dc07fdb | 48 | return true; |
53592b36 SL |
49 | default: |
50 | BUG(); | |
51 | } | |
52 | } | |
53 | ||
e5cf1baf PA |
54 | static bool tcf_mirred_can_reinsert(int action) |
55 | { | |
56 | switch (action) { | |
57 | case TC_ACT_SHOT: | |
58 | case TC_ACT_STOLEN: | |
59 | case TC_ACT_QUEUED: | |
60 | case TC_ACT_TRAP: | |
61 | return true; | |
62 | } | |
63 | return false; | |
64 | } | |
65 | ||
4e232818 VB |
66 | static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m) |
67 | { | |
68 | return rcu_dereference_protected(m->tcfm_dev, | |
69 | lockdep_is_held(&m->tcf_lock)); | |
70 | } | |
71 | ||
9a63b255 | 72 | static void tcf_mirred_release(struct tc_action *a) |
1da177e4 | 73 | { |
86062033 | 74 | struct tcf_mirred *m = to_mirred(a); |
dc327f89 | 75 | struct net_device *dev; |
2ee22a90 | 76 | |
4e232818 | 77 | spin_lock(&mirred_list_lock); |
a5b5c958 | 78 | list_del(&m->tcfm_list); |
4e232818 VB |
79 | spin_unlock(&mirred_list_lock); |
80 | ||
81 | /* last reference to action, no need to lock */ | |
82 | dev = rcu_dereference_protected(m->tcfm_dev, 1); | |
2ee22a90 ED |
83 | if (dev) |
84 | dev_put(dev); | |
1da177e4 LT |
85 | } |
86 | ||
53b2bf3f PM |
87 | static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = { |
88 | [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) }, | |
89 | }; | |
90 | ||
c7d03a00 | 91 | static unsigned int mirred_net_id; |
a85a970a | 92 | static struct tc_action_ops act_mirred_ops; |
ddf97ccd | 93 | |
c1b52739 | 94 | static int tcf_mirred_init(struct net *net, struct nlattr *nla, |
789871bb VB |
95 | struct nlattr *est, struct tc_action **a, |
96 | int ovr, int bind, bool rtnl_held, | |
97 | struct netlink_ext_ack *extack) | |
1da177e4 | 98 | { |
ddf97ccd | 99 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
7ba699c6 | 100 | struct nlattr *tb[TCA_MIRRED_MAX + 1]; |
16577923 | 101 | bool mac_header_xmit = false; |
1da177e4 | 102 | struct tc_mirred *parm; |
e9ce1cd3 | 103 | struct tcf_mirred *m; |
b76965e0 | 104 | struct net_device *dev; |
b2313077 | 105 | bool exists = false; |
0190c1d4 | 106 | int ret, err; |
1da177e4 | 107 | |
1d4760c7 AA |
108 | if (!nla) { |
109 | NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed"); | |
1da177e4 | 110 | return -EINVAL; |
1d4760c7 AA |
111 | } |
112 | ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, extack); | |
b76965e0 CG |
113 | if (ret < 0) |
114 | return ret; | |
1d4760c7 AA |
115 | if (!tb[TCA_MIRRED_PARMS]) { |
116 | NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters"); | |
1da177e4 | 117 | return -EINVAL; |
1d4760c7 | 118 | } |
7ba699c6 | 119 | parm = nla_data(tb[TCA_MIRRED_PARMS]); |
87dfbdc6 | 120 | |
0190c1d4 VB |
121 | err = tcf_idr_check_alloc(tn, &parm->index, a, bind); |
122 | if (err < 0) | |
123 | return err; | |
124 | exists = err; | |
87dfbdc6 JHS |
125 | if (exists && bind) |
126 | return 0; | |
127 | ||
b76965e0 CG |
128 | switch (parm->eaction) { |
129 | case TCA_EGRESS_MIRROR: | |
130 | case TCA_EGRESS_REDIR: | |
53592b36 SL |
131 | case TCA_INGRESS_REDIR: |
132 | case TCA_INGRESS_MIRROR: | |
b76965e0 CG |
133 | break; |
134 | default: | |
87dfbdc6 | 135 | if (exists) |
65a206c0 | 136 | tcf_idr_release(*a, bind); |
0190c1d4 VB |
137 | else |
138 | tcf_idr_cleanup(tn, parm->index); | |
1d4760c7 | 139 | NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option"); |
b76965e0 CG |
140 | return -EINVAL; |
141 | } | |
1da177e4 | 142 | |
87dfbdc6 | 143 | if (!exists) { |
4e232818 | 144 | if (!parm->ifindex) { |
0190c1d4 | 145 | tcf_idr_cleanup(tn, parm->index); |
1d4760c7 | 146 | NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist"); |
1da177e4 | 147 | return -EINVAL; |
1d4760c7 | 148 | } |
65a206c0 CM |
149 | ret = tcf_idr_create(tn, parm->index, est, a, |
150 | &act_mirred_ops, bind, true); | |
0190c1d4 VB |
151 | if (ret) { |
152 | tcf_idr_cleanup(tn, parm->index); | |
86062033 | 153 | return ret; |
0190c1d4 | 154 | } |
1da177e4 | 155 | ret = ACT_P_CREATED; |
4e8ddd7f | 156 | } else if (!ovr) { |
65a206c0 | 157 | tcf_idr_release(*a, bind); |
4e8ddd7f | 158 | return -EEXIST; |
1da177e4 | 159 | } |
a85a970a | 160 | m = to_mirred(*a); |
1da177e4 | 161 | |
653cd284 | 162 | spin_lock_bh(&m->tcf_lock); |
e9ce1cd3 DM |
163 | m->tcf_action = parm->action; |
164 | m->tcfm_eaction = parm->eaction; | |
4e232818 VB |
165 | |
166 | if (parm->ifindex) { | |
167 | dev = dev_get_by_index(net, parm->ifindex); | |
168 | if (!dev) { | |
653cd284 | 169 | spin_unlock_bh(&m->tcf_lock); |
4e232818 VB |
170 | tcf_idr_release(*a, bind); |
171 | return -ENODEV; | |
172 | } | |
173 | mac_header_xmit = dev_is_mac_header_xmit(dev); | |
174 | rcu_swap_protected(m->tcfm_dev, dev, | |
175 | lockdep_is_held(&m->tcf_lock)); | |
176 | if (dev) | |
177 | dev_put(dev); | |
16577923 | 178 | m->tcfm_mac_header_xmit = mac_header_xmit; |
1da177e4 | 179 | } |
653cd284 | 180 | spin_unlock_bh(&m->tcf_lock); |
2ee22a90 | 181 | |
3b87956e | 182 | if (ret == ACT_P_CREATED) { |
4e232818 | 183 | spin_lock(&mirred_list_lock); |
3b87956e | 184 | list_add(&m->tcfm_list, &mirred_list); |
4e232818 VB |
185 | spin_unlock(&mirred_list_lock); |
186 | ||
65a206c0 | 187 | tcf_idr_insert(tn, *a); |
3b87956e | 188 | } |
1da177e4 | 189 | |
1da177e4 LT |
190 | return ret; |
191 | } | |
192 | ||
7c5790c4 JHS |
193 | static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a, |
194 | struct tcf_result *res) | |
1da177e4 | 195 | { |
a85a970a | 196 | struct tcf_mirred *m = to_mirred(a); |
e5cf1baf | 197 | struct sk_buff *skb2 = skb; |
53592b36 | 198 | bool m_mac_header_xmit; |
1da177e4 | 199 | struct net_device *dev; |
53592b36 | 200 | int retval, err = 0; |
e5cf1baf PA |
201 | bool use_reinsert; |
202 | bool want_ingress; | |
203 | bool is_redirect; | |
53592b36 SL |
204 | int m_eaction; |
205 | int mac_len; | |
1da177e4 | 206 | |
2ee22a90 | 207 | tcf_lastuse_update(&m->tcf_tm); |
2ee22a90 | 208 | bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb); |
1da177e4 | 209 | |
53592b36 SL |
210 | m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit); |
211 | m_eaction = READ_ONCE(m->tcfm_eaction); | |
2ee22a90 | 212 | retval = READ_ONCE(m->tcf_action); |
7fd4b288 | 213 | dev = rcu_dereference_bh(m->tcfm_dev); |
2ee22a90 ED |
214 | if (unlikely(!dev)) { |
215 | pr_notice_once("tc mirred: target device is gone\n"); | |
3b87956e | 216 | goto out; |
217 | } | |
218 | ||
2ee22a90 | 219 | if (unlikely(!(dev->flags & IFF_UP))) { |
e87cc472 JP |
220 | net_notice_ratelimited("tc mirred to Houston: device %s is down\n", |
221 | dev->name); | |
feed1f17 | 222 | goto out; |
1da177e4 LT |
223 | } |
224 | ||
e5cf1baf PA |
225 | /* we could easily avoid the clone only if called by ingress and clsact; |
226 | * since we can't easily detect the clsact caller, skip clone only for | |
227 | * ingress - that covers the TC S/W datapath. | |
228 | */ | |
229 | is_redirect = tcf_mirred_is_act_redirect(m_eaction); | |
230 | use_reinsert = skb_at_tc_ingress(skb) && is_redirect && | |
231 | tcf_mirred_can_reinsert(retval); | |
232 | if (!use_reinsert) { | |
233 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
234 | if (!skb2) | |
235 | goto out; | |
236 | } | |
1da177e4 | 237 | |
53592b36 SL |
238 | /* If action's target direction differs than filter's direction, |
239 | * and devices expect a mac header on xmit, then mac push/pull is | |
240 | * needed. | |
241 | */ | |
e5cf1baf PA |
242 | want_ingress = tcf_mirred_act_wants_ingress(m_eaction); |
243 | if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) { | |
a5135bcf | 244 | if (!skb_at_tc_ingress(skb)) { |
53592b36 SL |
245 | /* caught at egress, act ingress: pull mac */ |
246 | mac_len = skb_network_header(skb) - skb_mac_header(skb); | |
247 | skb_pull_rcsum(skb2, mac_len); | |
248 | } else { | |
249 | /* caught at ingress, act egress: push mac */ | |
82a31b92 | 250 | skb_push_rcsum(skb2, skb->mac_len); |
53592b36 | 251 | } |
feed1f17 | 252 | } |
1da177e4 | 253 | |
e5cf1baf PA |
254 | skb2->skb_iif = skb->dev->ifindex; |
255 | skb2->dev = dev; | |
256 | ||
1da177e4 | 257 | /* mirror is always swallowed */ |
e5cf1baf | 258 | if (is_redirect) { |
bc31c905 WB |
259 | skb2->tc_redirected = 1; |
260 | skb2->tc_from_ingress = skb2->tc_at_ingress; | |
e5cf1baf PA |
261 | |
262 | /* let's the caller reinsert the packet, if possible */ | |
263 | if (use_reinsert) { | |
264 | res->ingress = want_ingress; | |
265 | res->qstats = this_cpu_ptr(m->common.cpu_qstats); | |
266 | return TC_ACT_REINSERT; | |
267 | } | |
bc31c905 | 268 | } |
1da177e4 | 269 | |
e5cf1baf | 270 | if (!want_ingress) |
53592b36 SL |
271 | err = dev_queue_xmit(skb2); |
272 | else | |
273 | err = netif_receive_skb(skb2); | |
feed1f17 | 274 | |
feed1f17 | 275 | if (err) { |
2ee22a90 ED |
276 | out: |
277 | qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats)); | |
53592b36 | 278 | if (tcf_mirred_is_act_redirect(m_eaction)) |
16c0b164 | 279 | retval = TC_ACT_SHOT; |
2ee22a90 | 280 | } |
feed1f17 CG |
281 | |
282 | return retval; | |
1da177e4 LT |
283 | } |
284 | ||
9798e6fe JK |
285 | static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets, |
286 | u64 lastuse) | |
287 | { | |
5712bf9c PB |
288 | struct tcf_mirred *m = to_mirred(a); |
289 | struct tcf_t *tm = &m->tcf_tm; | |
290 | ||
9798e6fe | 291 | _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets); |
3bb23421 | 292 | tm->lastuse = max_t(u64, tm->lastuse, lastuse); |
9798e6fe JK |
293 | } |
294 | ||
5a7a5555 JHS |
295 | static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, |
296 | int ref) | |
1da177e4 | 297 | { |
27a884dc | 298 | unsigned char *b = skb_tail_pointer(skb); |
a85a970a | 299 | struct tcf_mirred *m = to_mirred(a); |
1c40be12 ED |
300 | struct tc_mirred opt = { |
301 | .index = m->tcf_index, | |
036bb443 VB |
302 | .refcnt = refcount_read(&m->tcf_refcnt) - ref, |
303 | .bindcnt = atomic_read(&m->tcf_bindcnt) - bind, | |
1c40be12 | 304 | }; |
4e232818 | 305 | struct net_device *dev; |
1da177e4 LT |
306 | struct tcf_t t; |
307 | ||
653cd284 | 308 | spin_lock_bh(&m->tcf_lock); |
4e232818 VB |
309 | opt.action = m->tcf_action; |
310 | opt.eaction = m->tcfm_eaction; | |
311 | dev = tcf_mirred_dev_dereference(m); | |
312 | if (dev) | |
313 | opt.ifindex = dev->ifindex; | |
314 | ||
1b34ec43 DM |
315 | if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt)) |
316 | goto nla_put_failure; | |
48d8ee16 JHS |
317 | |
318 | tcf_tm_dump(&t, &m->tcf_tm); | |
9854518e | 319 | if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD)) |
1b34ec43 | 320 | goto nla_put_failure; |
653cd284 | 321 | spin_unlock_bh(&m->tcf_lock); |
4e232818 | 322 | |
1da177e4 LT |
323 | return skb->len; |
324 | ||
7ba699c6 | 325 | nla_put_failure: |
653cd284 | 326 | spin_unlock_bh(&m->tcf_lock); |
dc5fc579 | 327 | nlmsg_trim(skb, b); |
1da177e4 LT |
328 | return -1; |
329 | } | |
330 | ||
ddf97ccd WC |
331 | static int tcf_mirred_walker(struct net *net, struct sk_buff *skb, |
332 | struct netlink_callback *cb, int type, | |
41780105 AA |
333 | const struct tc_action_ops *ops, |
334 | struct netlink_ext_ack *extack) | |
ddf97ccd WC |
335 | { |
336 | struct tc_action_net *tn = net_generic(net, mirred_net_id); | |
337 | ||
b3620145 | 338 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
ddf97ccd WC |
339 | } |
340 | ||
331a9295 AA |
341 | static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index, |
342 | struct netlink_ext_ack *extack) | |
ddf97ccd WC |
343 | { |
344 | struct tc_action_net *tn = net_generic(net, mirred_net_id); | |
345 | ||
65a206c0 | 346 | return tcf_idr_search(tn, a, index); |
ddf97ccd WC |
347 | } |
348 | ||
3b87956e | 349 | static int mirred_device_event(struct notifier_block *unused, |
350 | unsigned long event, void *ptr) | |
351 | { | |
351638e7 | 352 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
3b87956e | 353 | struct tcf_mirred *m; |
354 | ||
2ee22a90 | 355 | ASSERT_RTNL(); |
6bd00b85 | 356 | if (event == NETDEV_UNREGISTER) { |
4e232818 | 357 | spin_lock(&mirred_list_lock); |
3b87956e | 358 | list_for_each_entry(m, &mirred_list, tcfm_list) { |
653cd284 | 359 | spin_lock_bh(&m->tcf_lock); |
4e232818 | 360 | if (tcf_mirred_dev_dereference(m) == dev) { |
3b87956e | 361 | dev_put(dev); |
2ee22a90 ED |
362 | /* Note : no rcu grace period necessary, as |
363 | * net_device are already rcu protected. | |
364 | */ | |
365 | RCU_INIT_POINTER(m->tcfm_dev, NULL); | |
3b87956e | 366 | } |
653cd284 | 367 | spin_unlock_bh(&m->tcf_lock); |
3b87956e | 368 | } |
4e232818 | 369 | spin_unlock(&mirred_list_lock); |
6bd00b85 | 370 | } |
3b87956e | 371 | |
372 | return NOTIFY_DONE; | |
373 | } | |
374 | ||
375 | static struct notifier_block mirred_device_notifier = { | |
376 | .notifier_call = mirred_device_event, | |
377 | }; | |
378 | ||
843e79d0 | 379 | static struct net_device *tcf_mirred_get_dev(const struct tc_action *a) |
255cb304 | 380 | { |
843e79d0 | 381 | struct tcf_mirred *m = to_mirred(a); |
4e232818 | 382 | struct net_device *dev; |
84a75b32 | 383 | |
4e232818 VB |
384 | rcu_read_lock(); |
385 | dev = rcu_dereference(m->tcfm_dev); | |
84a75b32 VB |
386 | if (dev) |
387 | dev_hold(dev); | |
4e232818 | 388 | rcu_read_unlock(); |
255cb304 | 389 | |
84a75b32 VB |
390 | return dev; |
391 | } | |
392 | ||
393 | static void tcf_mirred_put_dev(struct net_device *dev) | |
394 | { | |
395 | dev_put(dev); | |
255cb304 HHZ |
396 | } |
397 | ||
1da177e4 LT |
398 | static struct tc_action_ops act_mirred_ops = { |
399 | .kind = "mirred", | |
400 | .type = TCA_ACT_MIRRED, | |
1da177e4 | 401 | .owner = THIS_MODULE, |
7c5790c4 | 402 | .act = tcf_mirred_act, |
9798e6fe | 403 | .stats_update = tcf_stats_update, |
1da177e4 | 404 | .dump = tcf_mirred_dump, |
86062033 | 405 | .cleanup = tcf_mirred_release, |
1da177e4 | 406 | .init = tcf_mirred_init, |
ddf97ccd WC |
407 | .walk = tcf_mirred_walker, |
408 | .lookup = tcf_mirred_search, | |
a85a970a | 409 | .size = sizeof(struct tcf_mirred), |
843e79d0 | 410 | .get_dev = tcf_mirred_get_dev, |
84a75b32 | 411 | .put_dev = tcf_mirred_put_dev, |
ddf97ccd WC |
412 | }; |
413 | ||
414 | static __net_init int mirred_init_net(struct net *net) | |
415 | { | |
416 | struct tc_action_net *tn = net_generic(net, mirred_net_id); | |
417 | ||
c7e460ce | 418 | return tc_action_net_init(tn, &act_mirred_ops); |
ddf97ccd WC |
419 | } |
420 | ||
039af9c6 | 421 | static void __net_exit mirred_exit_net(struct list_head *net_list) |
ddf97ccd | 422 | { |
039af9c6 | 423 | tc_action_net_exit(net_list, mirred_net_id); |
ddf97ccd WC |
424 | } |
425 | ||
426 | static struct pernet_operations mirred_net_ops = { | |
427 | .init = mirred_init_net, | |
039af9c6 | 428 | .exit_batch = mirred_exit_net, |
ddf97ccd WC |
429 | .id = &mirred_net_id, |
430 | .size = sizeof(struct tc_action_net), | |
1da177e4 LT |
431 | }; |
432 | ||
433 | MODULE_AUTHOR("Jamal Hadi Salim(2002)"); | |
434 | MODULE_DESCRIPTION("Device Mirror/redirect actions"); | |
435 | MODULE_LICENSE("GPL"); | |
436 | ||
e9ce1cd3 | 437 | static int __init mirred_init_module(void) |
1da177e4 | 438 | { |
3b87956e | 439 | int err = register_netdevice_notifier(&mirred_device_notifier); |
440 | if (err) | |
441 | return err; | |
442 | ||
6ff9c364 | 443 | pr_info("Mirror/redirect action on\n"); |
ddf97ccd | 444 | return tcf_register_action(&act_mirred_ops, &mirred_net_ops); |
1da177e4 LT |
445 | } |
446 | ||
e9ce1cd3 | 447 | static void __exit mirred_cleanup_module(void) |
1da177e4 | 448 | { |
ddf97ccd | 449 | tcf_unregister_action(&act_mirred_ops, &mirred_net_ops); |
568a153a | 450 | unregister_netdevice_notifier(&mirred_device_notifier); |
1da177e4 LT |
451 | } |
452 | ||
453 | module_init(mirred_init_module); | |
454 | module_exit(mirred_cleanup_module); |