blob: 2d1be4a760fdc4361f23d0aa93a861298eaafe45 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jiri Pirko0c6965d2014-11-05 20:51:51 +01002 * net/sched/act_mirred.c packet mirroring and redirect actions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/types.h>
16#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
20#include <linux/rtnetlink.h>
21#include <linux/module.h>
22#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/gfp.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070024#include <net/net_namespace.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070025#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/pkt_sched.h>
27#include <linux/tc_act/tc_mirred.h>
28#include <net/tc_act/tc_mirred.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/if_arp.h>
31
David S. Millere9ce1cd2006-08-21 23:54:55 -070032#define MIRRED_TAB_MASK 7
stephen hemminger3b879562010-07-22 18:45:04 +000033static LIST_HEAD(mirred_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
WANG Conga5b5c952014-02-11 17:07:32 -080035static void tcf_mirred_release(struct tc_action *a, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
WANG Cong86062032014-02-11 17:07:31 -080037 struct tcf_mirred *m = to_mirred(a);
Eric Dumazet2ee22a92015-07-06 05:18:09 -070038 struct net_device *dev = rcu_dereference_protected(m->tcfm_dev, 1);
39
WANG Conga5b5c952014-02-11 17:07:32 -080040 list_del(&m->tcfm_list);
Eric Dumazet2ee22a92015-07-06 05:18:09 -070041 if (dev)
42 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
Patrick McHardy53b2bf32008-01-23 20:36:30 -080045static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
46 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
47};
48
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000049static int tcf_mirred_init(struct net *net, struct nlattr *nla,
50 struct nlattr *est, struct tc_action *a, int ovr,
51 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Patrick McHardy7ba699c2008-01-22 22:11:50 -080053 struct nlattr *tb[TCA_MIRRED_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct tc_mirred *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -070055 struct tcf_mirred *m;
Changli Gaob76965e2009-11-17 04:15:16 -080056 struct net_device *dev;
57 int ret, ok_push = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Patrick McHardycee63722008-01-23 20:33:32 -080059 if (nla == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return -EINVAL;
Changli Gaob76965e2009-11-17 04:15:16 -080061 ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy);
62 if (ret < 0)
63 return ret;
Patrick McHardy53b2bf32008-01-23 20:36:30 -080064 if (tb[TCA_MIRRED_PARMS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080066 parm = nla_data(tb[TCA_MIRRED_PARMS]);
Changli Gaob76965e2009-11-17 04:15:16 -080067 switch (parm->eaction) {
68 case TCA_EGRESS_MIRROR:
69 case TCA_EGRESS_REDIR:
70 break;
71 default:
72 return -EINVAL;
73 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (parm->ifindex) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000075 dev = __dev_get_by_index(net, parm->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (dev == NULL)
77 return -ENODEV;
78 switch (dev->type) {
Changli Gaob76965e2009-11-17 04:15:16 -080079 case ARPHRD_TUNNEL:
80 case ARPHRD_TUNNEL6:
81 case ARPHRD_SIT:
82 case ARPHRD_IPGRE:
83 case ARPHRD_VOID:
84 case ARPHRD_NONE:
85 ok_push = 0;
86 break;
87 default:
88 ok_push = 1;
89 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Changli Gaob76965e2009-11-17 04:15:16 -080091 } else {
92 dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
WANG Cong86062032014-02-11 17:07:31 -080095 if (!tcf_hash_check(parm->index, a, bind)) {
Changli Gaob76965e2009-11-17 04:15:16 -080096 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return -EINVAL;
Eric Dumazet519c8182015-07-06 05:18:04 -070098 ret = tcf_hash_create(parm->index, est, a, sizeof(*m),
Eric Dumazet2ee22a92015-07-06 05:18:09 -070099 bind, true);
WANG Cong86062032014-02-11 17:07:31 -0800100 if (ret)
101 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 ret = ACT_P_CREATED;
103 } else {
WANG Cong636dba82015-07-30 17:12:20 -0700104 if (bind)
105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (!ovr) {
WANG Conga5b5c952014-02-11 17:07:32 -0800107 tcf_hash_release(a, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -EEXIST;
109 }
110 }
WANG Cong86062032014-02-11 17:07:31 -0800111 m = to_mirred(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700113 ASSERT_RTNL();
David S. Millere9ce1cd2006-08-21 23:54:55 -0700114 m->tcf_action = parm->action;
115 m->tcfm_eaction = parm->eaction;
Changli Gaob76965e2009-11-17 04:15:16 -0800116 if (dev != NULL) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700117 m->tcfm_ifindex = parm->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (ret != ACT_P_CREATED)
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700119 dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 dev_hold(dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700121 rcu_assign_pointer(m->tcfm_dev, dev);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700122 m->tcfm_ok_push = ok_push;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700124
stephen hemminger3b879562010-07-22 18:45:04 +0000125 if (ret == ACT_P_CREATED) {
126 list_add(&m->tcfm_list, &mirred_list);
WANG Cong86062032014-02-11 17:07:31 -0800127 tcf_hash_insert(a);
stephen hemminger3b879562010-07-22 18:45:04 +0000128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return ret;
131}
132
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000133static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
David S. Millere9ce1cd2006-08-21 23:54:55 -0700134 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700136 struct tcf_mirred *m = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 struct net_device *dev;
Changli Gaofeed1f12009-11-17 04:14:00 -0800138 struct sk_buff *skb2;
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700139 int retval, err;
Changli Gaofeed1f12009-11-17 04:14:00 -0800140 u32 at;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700142 tcf_lastuse_update(&m->tcf_tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700144 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
145
146 rcu_read_lock();
147 retval = READ_ONCE(m->tcf_action);
148 dev = rcu_dereference(m->tcfm_dev);
149 if (unlikely(!dev)) {
150 pr_notice_once("tc mirred: target device is gone\n");
stephen hemminger3b879562010-07-22 18:45:04 +0000151 goto out;
152 }
153
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700154 if (unlikely(!(dev->flags & IFF_UP))) {
Joe Perchese87cc472012-05-13 21:56:26 +0000155 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
156 dev->name);
Changli Gaofeed1f12009-11-17 04:14:00 -0800157 goto out;
158 }
159
Changli Gao210d6de2010-06-24 16:25:12 +0000160 at = G_TC_AT(skb->tc_verd);
Florian Westphale578d9c2015-05-11 19:50:41 +0200161 skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700162 if (!skb2)
Changli Gaofeed1f12009-11-17 04:14:00 -0800163 goto out;
164
Changli Gaofeed1f12009-11-17 04:14:00 -0800165 if (!(at & AT_EGRESS)) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700166 if (m->tcfm_ok_push)
Herbert Xuf40ae912015-04-17 13:32:09 +0800167 skb_push(skb2, skb->mac_len);
Changli Gaofeed1f12009-11-17 04:14:00 -0800168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* mirror is always swallowed */
David S. Millere9ce1cd2006-08-21 23:54:55 -0700171 if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at);
173
Eric Dumazet8964be42009-11-20 15:35:04 -0800174 skb2->skb_iif = skb->dev->ifindex;
Changli Gao210d6de2010-06-24 16:25:12 +0000175 skb2->dev = dev;
Jamal Hadi Salim8919bc12011-08-15 05:25:40 +0000176 err = dev_queue_xmit(skb2);
Changli Gaofeed1f12009-11-17 04:14:00 -0800177
Changli Gaofeed1f12009-11-17 04:14:00 -0800178 if (err) {
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700179out:
180 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
Jason Wang16c0b162012-08-15 20:44:27 +0000181 if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
182 retval = TC_ACT_SHOT;
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700183 }
184 rcu_read_unlock();
Changli Gaofeed1f12009-11-17 04:14:00 -0800185
186 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
David S. Millere9ce1cd2006-08-21 23:54:55 -0700189static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700191 unsigned char *b = skb_tail_pointer(skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700192 struct tcf_mirred *m = a->priv;
Eric Dumazet1c40be12010-08-16 20:04:22 +0000193 struct tc_mirred opt = {
194 .index = m->tcf_index,
195 .action = m->tcf_action,
196 .refcnt = m->tcf_refcnt - ref,
197 .bindcnt = m->tcf_bindcnt - bind,
198 .eaction = m->tcfm_eaction,
199 .ifindex = m->tcfm_ifindex,
200 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct tcf_t t;
202
David S. Miller1b34ec42012-03-29 05:11:39 -0400203 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
204 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700205 t.install = jiffies_to_clock_t(jiffies - m->tcf_tm.install);
206 t.lastuse = jiffies_to_clock_t(jiffies - m->tcf_tm.lastuse);
207 t.expires = jiffies_to_clock_t(m->tcf_tm.expires);
David S. Miller1b34ec42012-03-29 05:11:39 -0400208 if (nla_put(skb, TCA_MIRRED_TM, sizeof(t), &t))
209 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return skb->len;
211
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800212nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700213 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return -1;
215}
216
stephen hemminger3b879562010-07-22 18:45:04 +0000217static int mirred_device_event(struct notifier_block *unused,
218 unsigned long event, void *ptr)
219{
Jiri Pirko351638e2013-05-28 01:30:21 +0000220 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
stephen hemminger3b879562010-07-22 18:45:04 +0000221 struct tcf_mirred *m;
222
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700223 ASSERT_RTNL();
stephen hemminger3b879562010-07-22 18:45:04 +0000224 if (event == NETDEV_UNREGISTER)
225 list_for_each_entry(m, &mirred_list, tcfm_list) {
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700226 if (rcu_access_pointer(m->tcfm_dev) == dev) {
stephen hemminger3b879562010-07-22 18:45:04 +0000227 dev_put(dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700228 /* Note : no rcu grace period necessary, as
229 * net_device are already rcu protected.
230 */
231 RCU_INIT_POINTER(m->tcfm_dev, NULL);
stephen hemminger3b879562010-07-22 18:45:04 +0000232 }
233 }
234
235 return NOTIFY_DONE;
236}
237
238static struct notifier_block mirred_device_notifier = {
239 .notifier_call = mirred_device_event,
240};
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static struct tc_action_ops act_mirred_ops = {
243 .kind = "mirred",
244 .type = TCA_ACT_MIRRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .owner = THIS_MODULE,
246 .act = tcf_mirred,
247 .dump = tcf_mirred_dump,
WANG Cong86062032014-02-11 17:07:31 -0800248 .cleanup = tcf_mirred_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .init = tcf_mirred_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
252MODULE_AUTHOR("Jamal Hadi Salim(2002)");
253MODULE_DESCRIPTION("Device Mirror/redirect actions");
254MODULE_LICENSE("GPL");
255
David S. Millere9ce1cd2006-08-21 23:54:55 -0700256static int __init mirred_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
stephen hemminger3b879562010-07-22 18:45:04 +0000258 int err = register_netdevice_notifier(&mirred_device_notifier);
259 if (err)
260 return err;
261
stephen hemminger6ff9c362010-05-12 06:37:05 +0000262 pr_info("Mirror/redirect action on\n");
WANG Cong4f1e9d82014-02-11 17:07:33 -0800263 return tcf_register_action(&act_mirred_ops, MIRRED_TAB_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
David S. Millere9ce1cd2006-08-21 23:54:55 -0700266static void __exit mirred_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 tcf_unregister_action(&act_mirred_ops);
WANG Cong568a1532013-12-20 00:08:51 -0800269 unregister_netdevice_notifier(&mirred_device_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272module_init(mirred_init_module);
273module_exit(mirred_cleanup_module);