blob: eeb335f031025a274a319a381f0f7c9b2a3f4097 [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>
28#include <linux/tc_act/tc_mirred.h>
29#include <net/tc_act/tc_mirred.h>
30
stephen hemminger3b879562010-07-22 18:45:04 +000031static LIST_HEAD(mirred_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Shmulik Ladkani53592b32016-10-13 09:06:44 +030033static bool tcf_mirred_is_act_redirect(int action)
34{
35 return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
36}
37
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050038static bool tcf_mirred_act_wants_ingress(int action)
Shmulik Ladkani53592b32016-10-13 09:06:44 +030039{
40 switch (action) {
41 case TCA_EGRESS_REDIR:
42 case TCA_EGRESS_MIRROR:
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050043 return false;
Shmulik Ladkani53592b32016-10-13 09:06:44 +030044 case TCA_INGRESS_REDIR:
45 case TCA_INGRESS_MIRROR:
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050046 return true;
Shmulik Ladkani53592b32016-10-13 09:06:44 +030047 default:
48 BUG();
49 }
50}
51
Cong Wang9a63b252017-12-05 12:53:07 -080052static void tcf_mirred_release(struct tc_action *a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
WANG Cong86062032014-02-11 17:07:31 -080054 struct tcf_mirred *m = to_mirred(a);
WANG Congdc327f82016-05-16 15:11:18 -070055 struct net_device *dev;
Eric Dumazet2ee22a92015-07-06 05:18:09 -070056
WANG Conga5b5c952014-02-11 17:07:32 -080057 list_del(&m->tcfm_list);
Cong Wang4bee00e2017-12-05 16:17:27 -080058 dev = rtnl_dereference(m->tcfm_dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -070059 if (dev)
60 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Patrick McHardy53b2bf32008-01-23 20:36:30 -080063static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
64 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
65};
66
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030067static unsigned int mirred_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070068static struct tc_action_ops act_mirred_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080069
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000070static int tcf_mirred_init(struct net *net, struct nlattr *nla,
Vlad Buslov789871b2018-07-05 17:24:25 +030071 struct nlattr *est, struct tc_action **a,
72 int ovr, int bind, bool rtnl_held,
73 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
WANG Congddf97cc2016-02-22 15:57:53 -080075 struct tc_action_net *tn = net_generic(net, mirred_net_id);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080076 struct nlattr *tb[TCA_MIRRED_MAX + 1];
Shmulik Ladkani16577922016-10-13 09:06:41 +030077 bool mac_header_xmit = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct tc_mirred *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -070079 struct tcf_mirred *m;
Changli Gaob76965e2009-11-17 04:15:16 -080080 struct net_device *dev;
WANG Congb2313072016-06-13 13:46:28 -070081 bool exists = false;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030082 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Alexander Aring1d4760c2018-02-15 10:55:00 -050084 if (!nla) {
85 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -050087 }
88 ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, extack);
Changli Gaob76965e2009-11-17 04:15:16 -080089 if (ret < 0)
90 return ret;
Alexander Aring1d4760c2018-02-15 10:55:00 -050091 if (!tb[TCA_MIRRED_PARMS]) {
92 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -050094 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -080095 parm = nla_data(tb[TCA_MIRRED_PARMS]);
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -040096
Vlad Buslov0190c1d2018-07-05 17:24:32 +030097 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
98 if (err < 0)
99 return err;
100 exists = err;
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400101 if (exists && bind)
102 return 0;
103
Changli Gaob76965e2009-11-17 04:15:16 -0800104 switch (parm->eaction) {
105 case TCA_EGRESS_MIRROR:
106 case TCA_EGRESS_REDIR:
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300107 case TCA_INGRESS_REDIR:
108 case TCA_INGRESS_MIRROR:
Changli Gaob76965e2009-11-17 04:15:16 -0800109 break;
110 default:
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400111 if (exists)
Chris Mi65a206c2017-08-30 02:31:59 -0400112 tcf_idr_release(*a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300113 else
114 tcf_idr_cleanup(tn, parm->index);
Alexander Aring1d4760c2018-02-15 10:55:00 -0500115 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
Changli Gaob76965e2009-11-17 04:15:16 -0800116 return -EINVAL;
117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (parm->ifindex) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000119 dev = __dev_get_by_index(net, parm->ifindex);
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400120 if (dev == NULL) {
121 if (exists)
Chris Mi65a206c2017-08-30 02:31:59 -0400122 tcf_idr_release(*a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300123 else
124 tcf_idr_cleanup(tn, parm->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return -ENODEV;
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400126 }
Shmulik Ladkanidcf80032016-10-13 09:06:42 +0300127 mac_header_xmit = dev_is_mac_header_xmit(dev);
Changli Gaob76965e2009-11-17 04:15:16 -0800128 } else {
129 dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400132 if (!exists) {
Alexander Aring1d4760c2018-02-15 10:55:00 -0500133 if (!dev) {
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300134 tcf_idr_cleanup(tn, parm->index);
Alexander Aring1d4760c2018-02-15 10:55:00 -0500135 NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500137 }
Chris Mi65a206c2017-08-30 02:31:59 -0400138 ret = tcf_idr_create(tn, parm->index, est, a,
139 &act_mirred_ops, bind, true);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300140 if (ret) {
141 tcf_idr_cleanup(tn, parm->index);
WANG Cong86062032014-02-11 17:07:31 -0800142 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 ret = ACT_P_CREATED;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300145 } else if (!ovr) {
Chris Mi65a206c2017-08-30 02:31:59 -0400146 tcf_idr_release(*a, bind);
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300147 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
WANG Conga85a9702016-07-25 16:09:41 -0700149 m = to_mirred(*a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700151 ASSERT_RTNL();
David S. Millere9ce1cd2006-08-21 23:54:55 -0700152 m->tcf_action = parm->action;
153 m->tcfm_eaction = parm->eaction;
Changli Gaob76965e2009-11-17 04:15:16 -0800154 if (dev != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (ret != ACT_P_CREATED)
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700156 dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 dev_hold(dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700158 rcu_assign_pointer(m->tcfm_dev, dev);
Shmulik Ladkani16577922016-10-13 09:06:41 +0300159 m->tcfm_mac_header_xmit = mac_header_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700161
stephen hemminger3b879562010-07-22 18:45:04 +0000162 if (ret == ACT_P_CREATED) {
163 list_add(&m->tcfm_list, &mirred_list);
Chris Mi65a206c2017-08-30 02:31:59 -0400164 tcf_idr_insert(tn, *a);
stephen hemminger3b879562010-07-22 18:45:04 +0000165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return ret;
168}
169
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000170static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
David S. Millere9ce1cd2006-08-21 23:54:55 -0700171 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
WANG Conga85a9702016-07-25 16:09:41 -0700173 struct tcf_mirred *m = to_mirred(a);
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300174 bool m_mac_header_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct net_device *dev;
Changli Gaofeed1f12009-11-17 04:14:00 -0800176 struct sk_buff *skb2;
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300177 int retval, err = 0;
178 int m_eaction;
179 int mac_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700181 tcf_lastuse_update(&m->tcf_tm);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700182 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
183
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300184 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
185 m_eaction = READ_ONCE(m->tcfm_eaction);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700186 retval = READ_ONCE(m->tcf_action);
Paolo Abeni7fd4b282018-07-30 14:30:43 +0200187 dev = rcu_dereference_bh(m->tcfm_dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700188 if (unlikely(!dev)) {
189 pr_notice_once("tc mirred: target device is gone\n");
stephen hemminger3b879562010-07-22 18:45:04 +0000190 goto out;
191 }
192
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700193 if (unlikely(!(dev->flags & IFF_UP))) {
Joe Perchese87cc472012-05-13 21:56:26 +0000194 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
195 dev->name);
Changli Gaofeed1f12009-11-17 04:14:00 -0800196 goto out;
197 }
198
Florian Westphale578d9c2015-05-11 19:50:41 +0200199 skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700200 if (!skb2)
Changli Gaofeed1f12009-11-17 04:14:00 -0800201 goto out;
202
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300203 /* If action's target direction differs than filter's direction,
204 * and devices expect a mac header on xmit, then mac push/pull is
205 * needed.
206 */
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -0500207 if (skb_at_tc_ingress(skb) != tcf_mirred_act_wants_ingress(m_eaction) &&
Willem de Bruijna5135bc2017-01-07 17:06:36 -0500208 m_mac_header_xmit) {
209 if (!skb_at_tc_ingress(skb)) {
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300210 /* caught at egress, act ingress: pull mac */
211 mac_len = skb_network_header(skb) - skb_mac_header(skb);
212 skb_pull_rcsum(skb2, mac_len);
213 } else {
214 /* caught at ingress, act egress: push mac */
WANG Cong82a31b92016-06-30 10:15:22 -0700215 skb_push_rcsum(skb2, skb->mac_len);
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300216 }
Changli Gaofeed1f12009-11-17 04:14:00 -0800217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* mirror is always swallowed */
Willem de Bruijnbc31c902017-01-07 17:06:38 -0500220 if (tcf_mirred_is_act_redirect(m_eaction)) {
221 skb2->tc_redirected = 1;
222 skb2->tc_from_ingress = skb2->tc_at_ingress;
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Eric Dumazet8964be42009-11-20 15:35:04 -0800225 skb2->skb_iif = skb->dev->ifindex;
Changli Gao210d6de2010-06-24 16:25:12 +0000226 skb2->dev = dev;
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -0500227 if (!tcf_mirred_act_wants_ingress(m_eaction))
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300228 err = dev_queue_xmit(skb2);
229 else
230 err = netif_receive_skb(skb2);
Changli Gaofeed1f12009-11-17 04:14:00 -0800231
Changli Gaofeed1f12009-11-17 04:14:00 -0800232 if (err) {
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700233out:
234 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300235 if (tcf_mirred_is_act_redirect(m_eaction))
Jason Wang16c0b162012-08-15 20:44:27 +0000236 retval = TC_ACT_SHOT;
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700237 }
Changli Gaofeed1f12009-11-17 04:14:00 -0800238
239 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100242static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
243 u64 lastuse)
244{
Paul Blakey5712bf92016-10-19 17:42:39 +0300245 struct tcf_mirred *m = to_mirred(a);
246 struct tcf_t *tm = &m->tcf_tm;
247
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100248 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
Roi Dayan3bb23422017-12-26 07:48:51 +0200249 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100250}
251
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400252static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
253 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700255 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700256 struct tcf_mirred *m = to_mirred(a);
Cong Wang9f8a7392017-12-05 16:17:26 -0800257 struct net_device *dev = rtnl_dereference(m->tcfm_dev);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000258 struct tc_mirred opt = {
259 .index = m->tcf_index,
260 .action = m->tcf_action,
Vlad Buslov036bb442018-07-05 17:24:24 +0300261 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
262 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000263 .eaction = m->tcfm_eaction,
Cong Wang9f8a7392017-12-05 16:17:26 -0800264 .ifindex = dev ? dev->ifindex : 0,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000265 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct tcf_t t;
267
David S. Miller1b34ec42012-03-29 05:11:39 -0400268 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
269 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400270
271 tcf_tm_dump(&t, &m->tcf_tm);
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200272 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400273 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return skb->len;
275
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800276nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700277 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -1;
279}
280
WANG Congddf97cc2016-02-22 15:57:53 -0800281static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
282 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500283 const struct tc_action_ops *ops,
284 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800285{
286 struct tc_action_net *tn = net_generic(net, mirred_net_id);
287
Alexander Aringb3620142018-02-15 10:54:59 -0500288 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800289}
290
Alexander Aring331a9292018-02-15 10:54:57 -0500291static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index,
292 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800293{
294 struct tc_action_net *tn = net_generic(net, mirred_net_id);
295
Chris Mi65a206c2017-08-30 02:31:59 -0400296 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800297}
298
stephen hemminger3b879562010-07-22 18:45:04 +0000299static int mirred_device_event(struct notifier_block *unused,
300 unsigned long event, void *ptr)
301{
Jiri Pirko351638e2013-05-28 01:30:21 +0000302 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
stephen hemminger3b879562010-07-22 18:45:04 +0000303 struct tcf_mirred *m;
304
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700305 ASSERT_RTNL();
WANG Cong6bd00b82015-10-01 11:37:42 -0700306 if (event == NETDEV_UNREGISTER) {
stephen hemminger3b879562010-07-22 18:45:04 +0000307 list_for_each_entry(m, &mirred_list, tcfm_list) {
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700308 if (rcu_access_pointer(m->tcfm_dev) == dev) {
stephen hemminger3b879562010-07-22 18:45:04 +0000309 dev_put(dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700310 /* Note : no rcu grace period necessary, as
311 * net_device are already rcu protected.
312 */
313 RCU_INIT_POINTER(m->tcfm_dev, NULL);
stephen hemminger3b879562010-07-22 18:45:04 +0000314 }
315 }
WANG Cong6bd00b82015-10-01 11:37:42 -0700316 }
stephen hemminger3b879562010-07-22 18:45:04 +0000317
318 return NOTIFY_DONE;
319}
320
321static struct notifier_block mirred_device_notifier = {
322 .notifier_call = mirred_device_event,
323};
324
Jiri Pirko843e79d2017-10-11 09:41:07 +0200325static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200326{
Jiri Pirko843e79d2017-10-11 09:41:07 +0200327 struct tcf_mirred *m = to_mirred(a);
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200328
Cong Wang9f8a7392017-12-05 16:17:26 -0800329 return rtnl_dereference(m->tcfm_dev);
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200330}
331
Vlad Buslovb4090742018-07-05 17:24:28 +0300332static int tcf_mirred_delete(struct net *net, u32 index)
333{
334 struct tc_action_net *tn = net_generic(net, mirred_net_id);
335
336 return tcf_idr_delete_index(tn, index);
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static struct tc_action_ops act_mirred_ops = {
340 .kind = "mirred",
341 .type = TCA_ACT_MIRRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 .owner = THIS_MODULE,
343 .act = tcf_mirred,
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100344 .stats_update = tcf_stats_update,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 .dump = tcf_mirred_dump,
WANG Cong86062032014-02-11 17:07:31 -0800346 .cleanup = tcf_mirred_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 .init = tcf_mirred_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800348 .walk = tcf_mirred_walker,
349 .lookup = tcf_mirred_search,
WANG Conga85a9702016-07-25 16:09:41 -0700350 .size = sizeof(struct tcf_mirred),
Jiri Pirko843e79d2017-10-11 09:41:07 +0200351 .get_dev = tcf_mirred_get_dev,
Vlad Buslovb4090742018-07-05 17:24:28 +0300352 .delete = tcf_mirred_delete,
WANG Congddf97cc2016-02-22 15:57:53 -0800353};
354
355static __net_init int mirred_init_net(struct net *net)
356{
357 struct tc_action_net *tn = net_generic(net, mirred_net_id);
358
Cong Wangc7e460c2017-11-06 13:47:18 -0800359 return tc_action_net_init(tn, &act_mirred_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800360}
361
Cong Wang039af9c2017-12-11 15:35:03 -0800362static void __net_exit mirred_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800363{
Cong Wang039af9c2017-12-11 15:35:03 -0800364 tc_action_net_exit(net_list, mirred_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800365}
366
367static struct pernet_operations mirred_net_ops = {
368 .init = mirred_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800369 .exit_batch = mirred_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800370 .id = &mirred_net_id,
371 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372};
373
374MODULE_AUTHOR("Jamal Hadi Salim(2002)");
375MODULE_DESCRIPTION("Device Mirror/redirect actions");
376MODULE_LICENSE("GPL");
377
David S. Millere9ce1cd2006-08-21 23:54:55 -0700378static int __init mirred_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
stephen hemminger3b879562010-07-22 18:45:04 +0000380 int err = register_netdevice_notifier(&mirred_device_notifier);
381 if (err)
382 return err;
383
stephen hemminger6ff9c362010-05-12 06:37:05 +0000384 pr_info("Mirror/redirect action on\n");
WANG Congddf97cc2016-02-22 15:57:53 -0800385 return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
David S. Millere9ce1cd2006-08-21 23:54:55 -0700388static void __exit mirred_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
WANG Congddf97cc2016-02-22 15:57:53 -0800390 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
WANG Cong568a1532013-12-20 00:08:51 -0800391 unregister_netdevice_notifier(&mirred_device_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394module_init(mirred_init_module);
395module_exit(mirred_cleanup_module);