Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jiri Pirko | 0c6965d | 2014-11-05 20:51:51 +0100 | [diff] [blame] | 2 | * net/sched/act_mirred.c packet mirroring and redirect actions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/types.h> |
| 16 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/skbuff.h> |
| 20 | #include <linux/rtnetlink.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/gfp.h> |
Daniel Borkmann | c491680 | 2016-11-26 01:28:06 +0100 | [diff] [blame] | 24 | #include <linux/if_arp.h> |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 25 | #include <net/net_namespace.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 26 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <net/pkt_sched.h> |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 28 | #include <net/pkt_cls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/tc_act/tc_mirred.h> |
| 30 | #include <net/tc_act/tc_mirred.h> |
| 31 | |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 32 | static LIST_HEAD(mirred_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 34 | static bool tcf_mirred_is_act_redirect(int action) |
| 35 | { |
| 36 | return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR; |
| 37 | } |
| 38 | |
Willem de Bruijn | 8dc07fd | 2017-01-07 17:06:37 -0500 | [diff] [blame] | 39 | static bool tcf_mirred_act_wants_ingress(int action) |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 40 | { |
| 41 | switch (action) { |
| 42 | case TCA_EGRESS_REDIR: |
| 43 | case TCA_EGRESS_MIRROR: |
Willem de Bruijn | 8dc07fd | 2017-01-07 17:06:37 -0500 | [diff] [blame] | 44 | return false; |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 45 | case TCA_INGRESS_REDIR: |
| 46 | case TCA_INGRESS_MIRROR: |
Willem de Bruijn | 8dc07fd | 2017-01-07 17:06:37 -0500 | [diff] [blame] | 47 | return true; |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 48 | default: |
| 49 | BUG(); |
| 50 | } |
| 51 | } |
| 52 | |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 53 | static 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 Wang | 9a63b25 | 2017-12-05 12:53:07 -0800 | [diff] [blame] | 65 | static void tcf_mirred_release(struct tc_action *a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 67 | struct tcf_mirred *m = to_mirred(a); |
WANG Cong | dc327f8 | 2016-05-16 15:11:18 -0700 | [diff] [blame] | 68 | struct net_device *dev; |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 69 | |
WANG Cong | a5b5c95 | 2014-02-11 17:07:32 -0800 | [diff] [blame] | 70 | list_del(&m->tcfm_list); |
Cong Wang | 4bee00e | 2017-12-05 16:17:27 -0800 | [diff] [blame] | 71 | dev = rtnl_dereference(m->tcfm_dev); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 72 | if (dev) |
| 73 | dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Patrick McHardy | 53b2bf3 | 2008-01-23 20:36:30 -0800 | [diff] [blame] | 76 | static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = { |
| 77 | [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) }, |
| 78 | }; |
| 79 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 80 | static unsigned int mirred_net_id; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 81 | static struct tc_action_ops act_mirred_ops; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 82 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 83 | static int tcf_mirred_init(struct net *net, struct nlattr *nla, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 84 | struct nlattr *est, struct tc_action **a, |
| 85 | int ovr, int bind, bool rtnl_held, |
| 86 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 88 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 89 | struct nlattr *tb[TCA_MIRRED_MAX + 1]; |
Shmulik Ladkani | 1657792 | 2016-10-13 09:06:41 +0300 | [diff] [blame] | 90 | bool mac_header_xmit = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | struct tc_mirred *parm; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 92 | struct tcf_mirred *m; |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 93 | struct net_device *dev; |
WANG Cong | b231307 | 2016-06-13 13:46:28 -0700 | [diff] [blame] | 94 | bool exists = false; |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 95 | int ret, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 97 | if (!nla) { |
| 98 | NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return -EINVAL; |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 100 | } |
| 101 | ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, extack); |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 102 | if (ret < 0) |
| 103 | return ret; |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 104 | if (!tb[TCA_MIRRED_PARMS]) { |
| 105 | NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return -EINVAL; |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 107 | } |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 108 | parm = nla_data(tb[TCA_MIRRED_PARMS]); |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 109 | |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 110 | err = tcf_idr_check_alloc(tn, &parm->index, a, bind); |
| 111 | if (err < 0) |
| 112 | return err; |
| 113 | exists = err; |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 114 | if (exists && bind) |
| 115 | return 0; |
| 116 | |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 117 | switch (parm->eaction) { |
| 118 | case TCA_EGRESS_MIRROR: |
| 119 | case TCA_EGRESS_REDIR: |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 120 | case TCA_INGRESS_REDIR: |
| 121 | case TCA_INGRESS_MIRROR: |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 122 | break; |
| 123 | default: |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 124 | if (exists) |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 125 | tcf_idr_release(*a, bind); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 126 | else |
| 127 | tcf_idr_cleanup(tn, parm->index); |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 128 | NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option"); |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 129 | return -EINVAL; |
| 130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if (parm->ifindex) { |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 132 | dev = __dev_get_by_index(net, parm->ifindex); |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 133 | if (dev == NULL) { |
| 134 | if (exists) |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 135 | tcf_idr_release(*a, bind); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 136 | else |
| 137 | tcf_idr_cleanup(tn, parm->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | return -ENODEV; |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 139 | } |
Shmulik Ladkani | dcf8003 | 2016-10-13 09:06:42 +0300 | [diff] [blame] | 140 | mac_header_xmit = dev_is_mac_header_xmit(dev); |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 141 | } else { |
| 142 | dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 145 | if (!exists) { |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 146 | if (!dev) { |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 147 | tcf_idr_cleanup(tn, parm->index); |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 148 | NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | return -EINVAL; |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 150 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 151 | ret = tcf_idr_create(tn, parm->index, est, a, |
| 152 | &act_mirred_ops, bind, true); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 153 | if (ret) { |
| 154 | tcf_idr_cleanup(tn, parm->index); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 155 | return ret; |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | ret = ACT_P_CREATED; |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 158 | } else if (!ovr) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 159 | tcf_idr_release(*a, bind); |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 160 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 162 | m = to_mirred(*a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 164 | ASSERT_RTNL(); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 165 | m->tcf_action = parm->action; |
| 166 | m->tcfm_eaction = parm->eaction; |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 167 | if (dev != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | if (ret != ACT_P_CREATED) |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 169 | dev_put(rcu_dereference_protected(m->tcfm_dev, 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | dev_hold(dev); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 171 | rcu_assign_pointer(m->tcfm_dev, dev); |
Shmulik Ladkani | 1657792 | 2016-10-13 09:06:41 +0300 | [diff] [blame] | 172 | m->tcfm_mac_header_xmit = mac_header_xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 174 | |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 175 | if (ret == ACT_P_CREATED) { |
| 176 | list_add(&m->tcfm_list, &mirred_list); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 177 | tcf_idr_insert(tn, *a); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return ret; |
| 181 | } |
| 182 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 183 | static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a, |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 184 | struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 186 | struct tcf_mirred *m = to_mirred(a); |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 187 | struct sk_buff *skb2 = skb; |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 188 | bool m_mac_header_xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | struct net_device *dev; |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 190 | int retval, err = 0; |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 191 | bool use_reinsert; |
| 192 | bool want_ingress; |
| 193 | bool is_redirect; |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 194 | int m_eaction; |
| 195 | int mac_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 197 | tcf_lastuse_update(&m->tcf_tm); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 198 | bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb); |
| 199 | |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 200 | m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit); |
| 201 | m_eaction = READ_ONCE(m->tcfm_eaction); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 202 | retval = READ_ONCE(m->tcf_action); |
Paolo Abeni | 7fd4b28 | 2018-07-30 14:30:43 +0200 | [diff] [blame] | 203 | dev = rcu_dereference_bh(m->tcfm_dev); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 204 | if (unlikely(!dev)) { |
| 205 | pr_notice_once("tc mirred: target device is gone\n"); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 206 | goto out; |
| 207 | } |
| 208 | |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 209 | if (unlikely(!(dev->flags & IFF_UP))) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 210 | net_notice_ratelimited("tc mirred to Houston: device %s is down\n", |
| 211 | dev->name); |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 212 | goto out; |
| 213 | } |
| 214 | |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 215 | /* 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 Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 227 | |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 228 | /* 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 Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 232 | want_ingress = tcf_mirred_act_wants_ingress(m_eaction); |
| 233 | if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) { |
Willem de Bruijn | a5135bc | 2017-01-07 17:06:36 -0500 | [diff] [blame] | 234 | if (!skb_at_tc_ingress(skb)) { |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 235 | /* 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 Cong | 82a31b9 | 2016-06-30 10:15:22 -0700 | [diff] [blame] | 240 | skb_push_rcsum(skb2, skb->mac_len); |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 241 | } |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Eric Dumazet | 8964be4 | 2009-11-20 15:35:04 -0800 | [diff] [blame] | 244 | skb2->skb_iif = skb->dev->ifindex; |
Changli Gao | 210d6de | 2010-06-24 16:25:12 +0000 | [diff] [blame] | 245 | skb2->dev = dev; |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 246 | |
| 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 Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 261 | err = dev_queue_xmit(skb2); |
| 262 | else |
| 263 | err = netif_receive_skb(skb2); |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 264 | |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 265 | if (err) { |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 266 | out: |
| 267 | qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats)); |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 268 | if (tcf_mirred_is_act_redirect(m_eaction)) |
Jason Wang | 16c0b16 | 2012-08-15 20:44:27 +0000 | [diff] [blame] | 269 | retval = TC_ACT_SHOT; |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 270 | } |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 271 | |
| 272 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Jakub Kicinski | 9798e6f | 2016-09-21 11:44:05 +0100 | [diff] [blame] | 275 | static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets, |
| 276 | u64 lastuse) |
| 277 | { |
Paul Blakey | 5712bf9 | 2016-10-19 17:42:39 +0300 | [diff] [blame] | 278 | struct tcf_mirred *m = to_mirred(a); |
| 279 | struct tcf_t *tm = &m->tcf_tm; |
| 280 | |
Jakub Kicinski | 9798e6f | 2016-09-21 11:44:05 +0100 | [diff] [blame] | 281 | _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets); |
Roi Dayan | 3bb2342 | 2017-12-26 07:48:51 +0200 | [diff] [blame] | 282 | tm->lastuse = max_t(u64, tm->lastuse, lastuse); |
Jakub Kicinski | 9798e6f | 2016-09-21 11:44:05 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 285 | static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, |
| 286 | int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 288 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 289 | struct tcf_mirred *m = to_mirred(a); |
Cong Wang | 9f8a739 | 2017-12-05 16:17:26 -0800 | [diff] [blame] | 290 | struct net_device *dev = rtnl_dereference(m->tcfm_dev); |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 291 | struct tc_mirred opt = { |
| 292 | .index = m->tcf_index, |
| 293 | .action = m->tcf_action, |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 294 | .refcnt = refcount_read(&m->tcf_refcnt) - ref, |
| 295 | .bindcnt = atomic_read(&m->tcf_bindcnt) - bind, |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 296 | .eaction = m->tcfm_eaction, |
Cong Wang | 9f8a739 | 2017-12-05 16:17:26 -0800 | [diff] [blame] | 297 | .ifindex = dev ? dev->ifindex : 0, |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 298 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | struct tcf_t t; |
| 300 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 301 | if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt)) |
| 302 | goto nla_put_failure; |
Jamal Hadi Salim | 48d8ee1 | 2016-06-06 06:32:55 -0400 | [diff] [blame] | 303 | |
| 304 | tcf_tm_dump(&t, &m->tcf_tm); |
Nicolas Dichtel | 9854518e | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 305 | if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 306 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | return skb->len; |
| 308 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 309 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 310 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | return -1; |
| 312 | } |
| 313 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 314 | static int tcf_mirred_walker(struct net *net, struct sk_buff *skb, |
| 315 | struct netlink_callback *cb, int type, |
Alexander Aring | 4178010 | 2018-02-15 10:54:58 -0500 | [diff] [blame] | 316 | const struct tc_action_ops *ops, |
| 317 | struct netlink_ext_ack *extack) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 318 | { |
| 319 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
| 320 | |
Alexander Aring | b362014 | 2018-02-15 10:54:59 -0500 | [diff] [blame] | 321 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Alexander Aring | 331a929 | 2018-02-15 10:54:57 -0500 | [diff] [blame] | 324 | static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index, |
| 325 | struct netlink_ext_ack *extack) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 326 | { |
| 327 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
| 328 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 329 | return tcf_idr_search(tn, a, index); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 330 | } |
| 331 | |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 332 | static int mirred_device_event(struct notifier_block *unused, |
| 333 | unsigned long event, void *ptr) |
| 334 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 335 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 336 | struct tcf_mirred *m; |
| 337 | |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 338 | ASSERT_RTNL(); |
WANG Cong | 6bd00b8 | 2015-10-01 11:37:42 -0700 | [diff] [blame] | 339 | if (event == NETDEV_UNREGISTER) { |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 340 | list_for_each_entry(m, &mirred_list, tcfm_list) { |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 341 | if (rcu_access_pointer(m->tcfm_dev) == dev) { |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 342 | dev_put(dev); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 343 | /* Note : no rcu grace period necessary, as |
| 344 | * net_device are already rcu protected. |
| 345 | */ |
| 346 | RCU_INIT_POINTER(m->tcfm_dev, NULL); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
WANG Cong | 6bd00b8 | 2015-10-01 11:37:42 -0700 | [diff] [blame] | 349 | } |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 350 | |
| 351 | return NOTIFY_DONE; |
| 352 | } |
| 353 | |
| 354 | static struct notifier_block mirred_device_notifier = { |
| 355 | .notifier_call = mirred_device_event, |
| 356 | }; |
| 357 | |
Jiri Pirko | 843e79d | 2017-10-11 09:41:07 +0200 | [diff] [blame] | 358 | static struct net_device *tcf_mirred_get_dev(const struct tc_action *a) |
Hadar Hen Zion | 255cb30 | 2016-12-01 14:06:36 +0200 | [diff] [blame] | 359 | { |
Jiri Pirko | 843e79d | 2017-10-11 09:41:07 +0200 | [diff] [blame] | 360 | struct tcf_mirred *m = to_mirred(a); |
Vlad Buslov | 84a75b3 | 2018-08-10 20:51:52 +0300 | [diff] [blame^] | 361 | struct net_device *dev = rtnl_dereference(m->tcfm_dev); |
Hadar Hen Zion | 255cb30 | 2016-12-01 14:06:36 +0200 | [diff] [blame] | 362 | |
Vlad Buslov | 84a75b3 | 2018-08-10 20:51:52 +0300 | [diff] [blame^] | 363 | if (dev) |
| 364 | dev_hold(dev); |
| 365 | |
| 366 | return dev; |
| 367 | } |
| 368 | |
| 369 | static void tcf_mirred_put_dev(struct net_device *dev) |
| 370 | { |
| 371 | dev_put(dev); |
Hadar Hen Zion | 255cb30 | 2016-12-01 14:06:36 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Vlad Buslov | b409074 | 2018-07-05 17:24:28 +0300 | [diff] [blame] | 374 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | static struct tc_action_ops act_mirred_ops = { |
| 382 | .kind = "mirred", |
| 383 | .type = TCA_ACT_MIRRED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | .owner = THIS_MODULE, |
| 385 | .act = tcf_mirred, |
Jakub Kicinski | 9798e6f | 2016-09-21 11:44:05 +0100 | [diff] [blame] | 386 | .stats_update = tcf_stats_update, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | .dump = tcf_mirred_dump, |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 388 | .cleanup = tcf_mirred_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | .init = tcf_mirred_init, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 390 | .walk = tcf_mirred_walker, |
| 391 | .lookup = tcf_mirred_search, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 392 | .size = sizeof(struct tcf_mirred), |
Jiri Pirko | 843e79d | 2017-10-11 09:41:07 +0200 | [diff] [blame] | 393 | .get_dev = tcf_mirred_get_dev, |
Vlad Buslov | 84a75b3 | 2018-08-10 20:51:52 +0300 | [diff] [blame^] | 394 | .put_dev = tcf_mirred_put_dev, |
Vlad Buslov | b409074 | 2018-07-05 17:24:28 +0300 | [diff] [blame] | 395 | .delete = tcf_mirred_delete, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 396 | }; |
| 397 | |
| 398 | static __net_init int mirred_init_net(struct net *net) |
| 399 | { |
| 400 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
| 401 | |
Cong Wang | c7e460c | 2017-11-06 13:47:18 -0800 | [diff] [blame] | 402 | return tc_action_net_init(tn, &act_mirred_ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 405 | static void __net_exit mirred_exit_net(struct list_head *net_list) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 406 | { |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 407 | tc_action_net_exit(net_list, mirred_net_id); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static struct pernet_operations mirred_net_ops = { |
| 411 | .init = mirred_init_net, |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 412 | .exit_batch = mirred_exit_net, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 413 | .id = &mirred_net_id, |
| 414 | .size = sizeof(struct tc_action_net), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | MODULE_AUTHOR("Jamal Hadi Salim(2002)"); |
| 418 | MODULE_DESCRIPTION("Device Mirror/redirect actions"); |
| 419 | MODULE_LICENSE("GPL"); |
| 420 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 421 | static int __init mirred_init_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | { |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 423 | int err = register_netdevice_notifier(&mirred_device_notifier); |
| 424 | if (err) |
| 425 | return err; |
| 426 | |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 427 | pr_info("Mirror/redirect action on\n"); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 428 | return tcf_register_action(&act_mirred_ops, &mirred_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } |
| 430 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 431 | static void __exit mirred_cleanup_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 433 | tcf_unregister_action(&act_mirred_ops, &mirred_net_ops); |
WANG Cong | 568a153 | 2013-12-20 00:08:51 -0800 | [diff] [blame] | 434 | unregister_netdevice_notifier(&mirred_device_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | module_init(mirred_init_module); |
| 438 | module_exit(mirred_cleanup_module); |