]> Git Repo - linux.git/blame - net/sched/act_mirred.c
net: Move all TC actions identifiers to one place
[linux.git] / net / sched / act_mirred.c
CommitLineData
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 32static LIST_HEAD(mirred_list);
4e232818 33static DEFINE_SPINLOCK(mirred_list_lock);
1da177e4 34
53592b36
SL
35static bool tcf_mirred_is_act_redirect(int action)
36{
37 return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
38}
39
8dc07fdb 40static 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
54static 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
66static 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 72static 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
87static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
88 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
89};
90
c7d03a00 91static unsigned int mirred_net_id;
a85a970a 92static struct tc_action_ops act_mirred_ops;
ddf97ccd 93
c1b52739 94static 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
193static 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;
7236ead1
ED
261 if (skb2->tc_from_ingress)
262 skb2->tstamp = 0;
e5cf1baf
PA
263 /* let's the caller reinsert the packet, if possible */
264 if (use_reinsert) {
265 res->ingress = want_ingress;
266 res->qstats = this_cpu_ptr(m->common.cpu_qstats);
267 return TC_ACT_REINSERT;
268 }
bc31c905 269 }
1da177e4 270
e5cf1baf 271 if (!want_ingress)
53592b36
SL
272 err = dev_queue_xmit(skb2);
273 else
274 err = netif_receive_skb(skb2);
feed1f17 275
feed1f17 276 if (err) {
2ee22a90
ED
277out:
278 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
53592b36 279 if (tcf_mirred_is_act_redirect(m_eaction))
16c0b164 280 retval = TC_ACT_SHOT;
2ee22a90 281 }
feed1f17
CG
282
283 return retval;
1da177e4
LT
284}
285
9798e6fe 286static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
28169aba 287 u64 lastuse, bool hw)
9798e6fe 288{
5712bf9c
PB
289 struct tcf_mirred *m = to_mirred(a);
290 struct tcf_t *tm = &m->tcf_tm;
291
9798e6fe 292 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
28169aba
EC
293 if (hw)
294 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
295 bytes, packets);
3bb23421 296 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
9798e6fe
JK
297}
298
5a7a5555
JHS
299static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
300 int ref)
1da177e4 301{
27a884dc 302 unsigned char *b = skb_tail_pointer(skb);
a85a970a 303 struct tcf_mirred *m = to_mirred(a);
1c40be12
ED
304 struct tc_mirred opt = {
305 .index = m->tcf_index,
036bb443
VB
306 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
307 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
1c40be12 308 };
4e232818 309 struct net_device *dev;
1da177e4
LT
310 struct tcf_t t;
311
653cd284 312 spin_lock_bh(&m->tcf_lock);
4e232818
VB
313 opt.action = m->tcf_action;
314 opt.eaction = m->tcfm_eaction;
315 dev = tcf_mirred_dev_dereference(m);
316 if (dev)
317 opt.ifindex = dev->ifindex;
318
1b34ec43
DM
319 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
320 goto nla_put_failure;
48d8ee16
JHS
321
322 tcf_tm_dump(&t, &m->tcf_tm);
9854518e 323 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
1b34ec43 324 goto nla_put_failure;
653cd284 325 spin_unlock_bh(&m->tcf_lock);
4e232818 326
1da177e4
LT
327 return skb->len;
328
7ba699c6 329nla_put_failure:
653cd284 330 spin_unlock_bh(&m->tcf_lock);
dc5fc579 331 nlmsg_trim(skb, b);
1da177e4
LT
332 return -1;
333}
334
ddf97ccd
WC
335static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
336 struct netlink_callback *cb, int type,
41780105
AA
337 const struct tc_action_ops *ops,
338 struct netlink_ext_ack *extack)
ddf97ccd
WC
339{
340 struct tc_action_net *tn = net_generic(net, mirred_net_id);
341
b3620145 342 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
ddf97ccd
WC
343}
344
f061b48c 345static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccd
WC
346{
347 struct tc_action_net *tn = net_generic(net, mirred_net_id);
348
65a206c0 349 return tcf_idr_search(tn, a, index);
ddf97ccd
WC
350}
351
3b87956e 352static int mirred_device_event(struct notifier_block *unused,
353 unsigned long event, void *ptr)
354{
351638e7 355 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3b87956e 356 struct tcf_mirred *m;
357
2ee22a90 358 ASSERT_RTNL();
6bd00b85 359 if (event == NETDEV_UNREGISTER) {
4e232818 360 spin_lock(&mirred_list_lock);
3b87956e 361 list_for_each_entry(m, &mirred_list, tcfm_list) {
653cd284 362 spin_lock_bh(&m->tcf_lock);
4e232818 363 if (tcf_mirred_dev_dereference(m) == dev) {
3b87956e 364 dev_put(dev);
2ee22a90
ED
365 /* Note : no rcu grace period necessary, as
366 * net_device are already rcu protected.
367 */
368 RCU_INIT_POINTER(m->tcfm_dev, NULL);
3b87956e 369 }
653cd284 370 spin_unlock_bh(&m->tcf_lock);
3b87956e 371 }
4e232818 372 spin_unlock(&mirred_list_lock);
6bd00b85 373 }
3b87956e 374
375 return NOTIFY_DONE;
376}
377
378static struct notifier_block mirred_device_notifier = {
379 .notifier_call = mirred_device_event,
380};
381
843e79d0 382static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
255cb304 383{
843e79d0 384 struct tcf_mirred *m = to_mirred(a);
4e232818 385 struct net_device *dev;
84a75b32 386
4e232818
VB
387 rcu_read_lock();
388 dev = rcu_dereference(m->tcfm_dev);
84a75b32
VB
389 if (dev)
390 dev_hold(dev);
4e232818 391 rcu_read_unlock();
255cb304 392
84a75b32
VB
393 return dev;
394}
395
396static void tcf_mirred_put_dev(struct net_device *dev)
397{
398 dev_put(dev);
255cb304
HHZ
399}
400
1da177e4
LT
401static struct tc_action_ops act_mirred_ops = {
402 .kind = "mirred",
403 .type = TCA_ACT_MIRRED,
1da177e4 404 .owner = THIS_MODULE,
7c5790c4 405 .act = tcf_mirred_act,
9798e6fe 406 .stats_update = tcf_stats_update,
1da177e4 407 .dump = tcf_mirred_dump,
86062033 408 .cleanup = tcf_mirred_release,
1da177e4 409 .init = tcf_mirred_init,
ddf97ccd
WC
410 .walk = tcf_mirred_walker,
411 .lookup = tcf_mirred_search,
a85a970a 412 .size = sizeof(struct tcf_mirred),
843e79d0 413 .get_dev = tcf_mirred_get_dev,
84a75b32 414 .put_dev = tcf_mirred_put_dev,
ddf97ccd
WC
415};
416
417static __net_init int mirred_init_net(struct net *net)
418{
419 struct tc_action_net *tn = net_generic(net, mirred_net_id);
420
c7e460ce 421 return tc_action_net_init(tn, &act_mirred_ops);
ddf97ccd
WC
422}
423
039af9c6 424static void __net_exit mirred_exit_net(struct list_head *net_list)
ddf97ccd 425{
039af9c6 426 tc_action_net_exit(net_list, mirred_net_id);
ddf97ccd
WC
427}
428
429static struct pernet_operations mirred_net_ops = {
430 .init = mirred_init_net,
039af9c6 431 .exit_batch = mirred_exit_net,
ddf97ccd
WC
432 .id = &mirred_net_id,
433 .size = sizeof(struct tc_action_net),
1da177e4
LT
434};
435
436MODULE_AUTHOR("Jamal Hadi Salim(2002)");
437MODULE_DESCRIPTION("Device Mirror/redirect actions");
438MODULE_LICENSE("GPL");
439
e9ce1cd3 440static int __init mirred_init_module(void)
1da177e4 441{
3b87956e 442 int err = register_netdevice_notifier(&mirred_device_notifier);
443 if (err)
444 return err;
445
6ff9c364 446 pr_info("Mirror/redirect action on\n");
ddf97ccd 447 return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
1da177e4
LT
448}
449
e9ce1cd3 450static void __exit mirred_cleanup_module(void)
1da177e4 451{
ddf97ccd 452 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
568a153a 453 unregister_netdevice_notifier(&mirred_device_notifier);
1da177e4
LT
454}
455
456module_init(mirred_init_module);
457module_exit(mirred_cleanup_module);
This page took 1.108422 seconds and 4 git commands to generate.