blob: 7a045cc7fe3bac33733d6b109263fb6f7b072bde [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>
Daniel Borkmannc4916802016-11-26 01:28:06 +010024#include <linux/if_arp.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070025#include <net/net_namespace.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070026#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/pkt_sched.h>
Paolo Abenie5cf1ba2018-07-30 14:30:45 +020028#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/tc_act/tc_mirred.h>
30#include <net/tc_act/tc_mirred.h>
31
stephen hemminger3b879562010-07-22 18:45:04 +000032static LIST_HEAD(mirred_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Shmulik Ladkani53592b32016-10-13 09:06:44 +030034static bool tcf_mirred_is_act_redirect(int action)
35{
36 return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
37}
38
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050039static bool tcf_mirred_act_wants_ingress(int action)
Shmulik Ladkani53592b32016-10-13 09:06:44 +030040{
41 switch (action) {
42 case TCA_EGRESS_REDIR:
43 case TCA_EGRESS_MIRROR:
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050044 return false;
Shmulik Ladkani53592b32016-10-13 09:06:44 +030045 case TCA_INGRESS_REDIR:
46 case TCA_INGRESS_MIRROR:
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050047 return true;
Shmulik Ladkani53592b32016-10-13 09:06:44 +030048 default:
49 BUG();
50 }
51}
52
Paolo Abenie5cf1ba2018-07-30 14:30:45 +020053static bool tcf_mirred_can_reinsert(int action)
54{
55 switch (action) {
56 case TC_ACT_SHOT:
57 case TC_ACT_STOLEN:
58 case TC_ACT_QUEUED:
59 case TC_ACT_TRAP:
60 return true;
61 }
62 return false;
63}
64
Cong Wang9a63b252017-12-05 12:53:07 -080065static void tcf_mirred_release(struct tc_action *a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
WANG Cong86062032014-02-11 17:07:31 -080067 struct tcf_mirred *m = to_mirred(a);
WANG Congdc327f82016-05-16 15:11:18 -070068 struct net_device *dev;
Eric Dumazet2ee22a92015-07-06 05:18:09 -070069
WANG Conga5b5c952014-02-11 17:07:32 -080070 list_del(&m->tcfm_list);
Cong Wang4bee00e2017-12-05 16:17:27 -080071 dev = rtnl_dereference(m->tcfm_dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -070072 if (dev)
73 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Patrick McHardy53b2bf32008-01-23 20:36:30 -080076static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
77 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
78};
79
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030080static unsigned int mirred_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070081static struct tc_action_ops act_mirred_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080082
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000083static int tcf_mirred_init(struct net *net, struct nlattr *nla,
Vlad Buslov789871b2018-07-05 17:24:25 +030084 struct nlattr *est, struct tc_action **a,
85 int ovr, int bind, bool rtnl_held,
86 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
WANG Congddf97cc2016-02-22 15:57:53 -080088 struct tc_action_net *tn = net_generic(net, mirred_net_id);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080089 struct nlattr *tb[TCA_MIRRED_MAX + 1];
Shmulik Ladkani16577922016-10-13 09:06:41 +030090 bool mac_header_xmit = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct tc_mirred *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -070092 struct tcf_mirred *m;
Changli Gaob76965e2009-11-17 04:15:16 -080093 struct net_device *dev;
WANG Congb2313072016-06-13 13:46:28 -070094 bool exists = false;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030095 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Alexander Aring1d4760c2018-02-15 10:55:00 -050097 if (!nla) {
98 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500100 }
101 ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, extack);
Changli Gaob76965e2009-11-17 04:15:16 -0800102 if (ret < 0)
103 return ret;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500104 if (!tb[TCA_MIRRED_PARMS]) {
105 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500107 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800108 parm = nla_data(tb[TCA_MIRRED_PARMS]);
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400109
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300110 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
111 if (err < 0)
112 return err;
113 exists = err;
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400114 if (exists && bind)
115 return 0;
116
Changli Gaob76965e2009-11-17 04:15:16 -0800117 switch (parm->eaction) {
118 case TCA_EGRESS_MIRROR:
119 case TCA_EGRESS_REDIR:
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300120 case TCA_INGRESS_REDIR:
121 case TCA_INGRESS_MIRROR:
Changli Gaob76965e2009-11-17 04:15:16 -0800122 break;
123 default:
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400124 if (exists)
Chris Mi65a206c2017-08-30 02:31:59 -0400125 tcf_idr_release(*a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300126 else
127 tcf_idr_cleanup(tn, parm->index);
Alexander Aring1d4760c2018-02-15 10:55:00 -0500128 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
Changli Gaob76965e2009-11-17 04:15:16 -0800129 return -EINVAL;
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (parm->ifindex) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000132 dev = __dev_get_by_index(net, parm->ifindex);
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400133 if (dev == NULL) {
134 if (exists)
Chris Mi65a206c2017-08-30 02:31:59 -0400135 tcf_idr_release(*a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300136 else
137 tcf_idr_cleanup(tn, parm->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -ENODEV;
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400139 }
Shmulik Ladkanidcf80032016-10-13 09:06:42 +0300140 mac_header_xmit = dev_is_mac_header_xmit(dev);
Changli Gaob76965e2009-11-17 04:15:16 -0800141 } else {
142 dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400145 if (!exists) {
Alexander Aring1d4760c2018-02-15 10:55:00 -0500146 if (!dev) {
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300147 tcf_idr_cleanup(tn, parm->index);
Alexander Aring1d4760c2018-02-15 10:55:00 -0500148 NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500150 }
Chris Mi65a206c2017-08-30 02:31:59 -0400151 ret = tcf_idr_create(tn, parm->index, est, a,
152 &act_mirred_ops, bind, true);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300153 if (ret) {
154 tcf_idr_cleanup(tn, parm->index);
WANG Cong86062032014-02-11 17:07:31 -0800155 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 ret = ACT_P_CREATED;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300158 } else if (!ovr) {
Chris Mi65a206c2017-08-30 02:31:59 -0400159 tcf_idr_release(*a, bind);
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300160 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
WANG Conga85a9702016-07-25 16:09:41 -0700162 m = to_mirred(*a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700164 ASSERT_RTNL();
David S. Millere9ce1cd2006-08-21 23:54:55 -0700165 m->tcf_action = parm->action;
166 m->tcfm_eaction = parm->eaction;
Changli Gaob76965e2009-11-17 04:15:16 -0800167 if (dev != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (ret != ACT_P_CREATED)
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700169 dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 dev_hold(dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700171 rcu_assign_pointer(m->tcfm_dev, dev);
Shmulik Ladkani16577922016-10-13 09:06:41 +0300172 m->tcfm_mac_header_xmit = mac_header_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700174
stephen hemminger3b879562010-07-22 18:45:04 +0000175 if (ret == ACT_P_CREATED) {
176 list_add(&m->tcfm_list, &mirred_list);
Chris Mi65a206c2017-08-30 02:31:59 -0400177 tcf_idr_insert(tn, *a);
stephen hemminger3b879562010-07-22 18:45:04 +0000178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return ret;
181}
182
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000183static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
David S. Millere9ce1cd2006-08-21 23:54:55 -0700184 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
WANG Conga85a9702016-07-25 16:09:41 -0700186 struct tcf_mirred *m = to_mirred(a);
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200187 struct sk_buff *skb2 = skb;
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300188 bool m_mac_header_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct net_device *dev;
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300190 int retval, err = 0;
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200191 bool use_reinsert;
192 bool want_ingress;
193 bool is_redirect;
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300194 int m_eaction;
195 int mac_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700197 tcf_lastuse_update(&m->tcf_tm);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700198 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
199
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300200 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
201 m_eaction = READ_ONCE(m->tcfm_eaction);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700202 retval = READ_ONCE(m->tcf_action);
Paolo Abeni7fd4b282018-07-30 14:30:43 +0200203 dev = rcu_dereference_bh(m->tcfm_dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700204 if (unlikely(!dev)) {
205 pr_notice_once("tc mirred: target device is gone\n");
stephen hemminger3b879562010-07-22 18:45:04 +0000206 goto out;
207 }
208
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700209 if (unlikely(!(dev->flags & IFF_UP))) {
Joe Perchese87cc472012-05-13 21:56:26 +0000210 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
211 dev->name);
Changli Gaofeed1f12009-11-17 04:14:00 -0800212 goto out;
213 }
214
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200215 /* we could easily avoid the clone only if called by ingress and clsact;
216 * since we can't easily detect the clsact caller, skip clone only for
217 * ingress - that covers the TC S/W datapath.
218 */
219 is_redirect = tcf_mirred_is_act_redirect(m_eaction);
220 use_reinsert = skb_at_tc_ingress(skb) && is_redirect &&
221 tcf_mirred_can_reinsert(retval);
222 if (!use_reinsert) {
223 skb2 = skb_clone(skb, GFP_ATOMIC);
224 if (!skb2)
225 goto out;
226 }
Changli Gaofeed1f12009-11-17 04:14:00 -0800227
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300228 /* If action's target direction differs than filter's direction,
229 * and devices expect a mac header on xmit, then mac push/pull is
230 * needed.
231 */
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200232 want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
233 if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) {
Willem de Bruijna5135bc2017-01-07 17:06:36 -0500234 if (!skb_at_tc_ingress(skb)) {
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300235 /* caught at egress, act ingress: pull mac */
236 mac_len = skb_network_header(skb) - skb_mac_header(skb);
237 skb_pull_rcsum(skb2, mac_len);
238 } else {
239 /* caught at ingress, act egress: push mac */
WANG Cong82a31b92016-06-30 10:15:22 -0700240 skb_push_rcsum(skb2, skb->mac_len);
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300241 }
Changli Gaofeed1f12009-11-17 04:14:00 -0800242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Eric Dumazet8964be42009-11-20 15:35:04 -0800244 skb2->skb_iif = skb->dev->ifindex;
Changli Gao210d6de2010-06-24 16:25:12 +0000245 skb2->dev = dev;
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200246
247 /* mirror is always swallowed */
248 if (is_redirect) {
249 skb2->tc_redirected = 1;
250 skb2->tc_from_ingress = skb2->tc_at_ingress;
251
252 /* let's the caller reinsert the packet, if possible */
253 if (use_reinsert) {
254 res->ingress = want_ingress;
255 res->qstats = this_cpu_ptr(m->common.cpu_qstats);
256 return TC_ACT_REINSERT;
257 }
258 }
259
260 if (!want_ingress)
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300261 err = dev_queue_xmit(skb2);
262 else
263 err = netif_receive_skb(skb2);
Changli Gaofeed1f12009-11-17 04:14:00 -0800264
Changli Gaofeed1f12009-11-17 04:14:00 -0800265 if (err) {
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700266out:
267 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300268 if (tcf_mirred_is_act_redirect(m_eaction))
Jason Wang16c0b162012-08-15 20:44:27 +0000269 retval = TC_ACT_SHOT;
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700270 }
Changli Gaofeed1f12009-11-17 04:14:00 -0800271
272 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100275static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
276 u64 lastuse)
277{
Paul Blakey5712bf92016-10-19 17:42:39 +0300278 struct tcf_mirred *m = to_mirred(a);
279 struct tcf_t *tm = &m->tcf_tm;
280
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100281 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
Roi Dayan3bb23422017-12-26 07:48:51 +0200282 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100283}
284
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400285static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
286 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700288 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700289 struct tcf_mirred *m = to_mirred(a);
Cong Wang9f8a7392017-12-05 16:17:26 -0800290 struct net_device *dev = rtnl_dereference(m->tcfm_dev);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000291 struct tc_mirred opt = {
292 .index = m->tcf_index,
293 .action = m->tcf_action,
Vlad Buslov036bb442018-07-05 17:24:24 +0300294 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
295 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000296 .eaction = m->tcfm_eaction,
Cong Wang9f8a7392017-12-05 16:17:26 -0800297 .ifindex = dev ? dev->ifindex : 0,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000298 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct tcf_t t;
300
David S. Miller1b34ec42012-03-29 05:11:39 -0400301 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
302 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400303
304 tcf_tm_dump(&t, &m->tcf_tm);
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200305 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400306 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return skb->len;
308
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800309nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700310 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return -1;
312}
313
WANG Congddf97cc2016-02-22 15:57:53 -0800314static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
315 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500316 const struct tc_action_ops *ops,
317 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800318{
319 struct tc_action_net *tn = net_generic(net, mirred_net_id);
320
Alexander Aringb3620142018-02-15 10:54:59 -0500321 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800322}
323
Alexander Aring331a9292018-02-15 10:54:57 -0500324static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index,
325 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800326{
327 struct tc_action_net *tn = net_generic(net, mirred_net_id);
328
Chris Mi65a206c2017-08-30 02:31:59 -0400329 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800330}
331
stephen hemminger3b879562010-07-22 18:45:04 +0000332static int mirred_device_event(struct notifier_block *unused,
333 unsigned long event, void *ptr)
334{
Jiri Pirko351638e2013-05-28 01:30:21 +0000335 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
stephen hemminger3b879562010-07-22 18:45:04 +0000336 struct tcf_mirred *m;
337
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700338 ASSERT_RTNL();
WANG Cong6bd00b82015-10-01 11:37:42 -0700339 if (event == NETDEV_UNREGISTER) {
stephen hemminger3b879562010-07-22 18:45:04 +0000340 list_for_each_entry(m, &mirred_list, tcfm_list) {
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700341 if (rcu_access_pointer(m->tcfm_dev) == dev) {
stephen hemminger3b879562010-07-22 18:45:04 +0000342 dev_put(dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700343 /* Note : no rcu grace period necessary, as
344 * net_device are already rcu protected.
345 */
346 RCU_INIT_POINTER(m->tcfm_dev, NULL);
stephen hemminger3b879562010-07-22 18:45:04 +0000347 }
348 }
WANG Cong6bd00b82015-10-01 11:37:42 -0700349 }
stephen hemminger3b879562010-07-22 18:45:04 +0000350
351 return NOTIFY_DONE;
352}
353
354static struct notifier_block mirred_device_notifier = {
355 .notifier_call = mirred_device_event,
356};
357
Jiri Pirko843e79d2017-10-11 09:41:07 +0200358static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200359{
Jiri Pirko843e79d2017-10-11 09:41:07 +0200360 struct tcf_mirred *m = to_mirred(a);
Vlad Buslov84a75b32018-08-10 20:51:52 +0300361 struct net_device *dev = rtnl_dereference(m->tcfm_dev);
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200362
Vlad Buslov84a75b32018-08-10 20:51:52 +0300363 if (dev)
364 dev_hold(dev);
365
366 return dev;
367}
368
369static void tcf_mirred_put_dev(struct net_device *dev)
370{
371 dev_put(dev);
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200372}
373
Vlad Buslovb4090742018-07-05 17:24:28 +0300374static int tcf_mirred_delete(struct net *net, u32 index)
375{
376 struct tc_action_net *tn = net_generic(net, mirred_net_id);
377
378 return tcf_idr_delete_index(tn, index);
379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381static struct tc_action_ops act_mirred_ops = {
382 .kind = "mirred",
383 .type = TCA_ACT_MIRRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 .owner = THIS_MODULE,
385 .act = tcf_mirred,
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100386 .stats_update = tcf_stats_update,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 .dump = tcf_mirred_dump,
WANG Cong86062032014-02-11 17:07:31 -0800388 .cleanup = tcf_mirred_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 .init = tcf_mirred_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800390 .walk = tcf_mirred_walker,
391 .lookup = tcf_mirred_search,
WANG Conga85a9702016-07-25 16:09:41 -0700392 .size = sizeof(struct tcf_mirred),
Jiri Pirko843e79d2017-10-11 09:41:07 +0200393 .get_dev = tcf_mirred_get_dev,
Vlad Buslov84a75b32018-08-10 20:51:52 +0300394 .put_dev = tcf_mirred_put_dev,
Vlad Buslovb4090742018-07-05 17:24:28 +0300395 .delete = tcf_mirred_delete,
WANG Congddf97cc2016-02-22 15:57:53 -0800396};
397
398static __net_init int mirred_init_net(struct net *net)
399{
400 struct tc_action_net *tn = net_generic(net, mirred_net_id);
401
Cong Wangc7e460c2017-11-06 13:47:18 -0800402 return tc_action_net_init(tn, &act_mirred_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800403}
404
Cong Wang039af9c2017-12-11 15:35:03 -0800405static void __net_exit mirred_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800406{
Cong Wang039af9c2017-12-11 15:35:03 -0800407 tc_action_net_exit(net_list, mirred_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800408}
409
410static struct pernet_operations mirred_net_ops = {
411 .init = mirred_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800412 .exit_batch = mirred_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800413 .id = &mirred_net_id,
414 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415};
416
417MODULE_AUTHOR("Jamal Hadi Salim(2002)");
418MODULE_DESCRIPTION("Device Mirror/redirect actions");
419MODULE_LICENSE("GPL");
420
David S. Millere9ce1cd2006-08-21 23:54:55 -0700421static int __init mirred_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
stephen hemminger3b879562010-07-22 18:45:04 +0000423 int err = register_netdevice_notifier(&mirred_device_notifier);
424 if (err)
425 return err;
426
stephen hemminger6ff9c362010-05-12 06:37:05 +0000427 pr_info("Mirror/redirect action on\n");
WANG Congddf97cc2016-02-22 15:57:53 -0800428 return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
David S. Millere9ce1cd2006-08-21 23:54:55 -0700431static void __exit mirred_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
WANG Congddf97cc2016-02-22 15:57:53 -0800433 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
WANG Cong568a1532013-12-20 00:08:51 -0800434 unregister_netdevice_notifier(&mirred_device_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437module_init(mirred_init_module);
438module_exit(mirred_cleanup_module);