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> |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 24 | #include <net/net_namespace.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 25 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <net/pkt_sched.h> |
| 27 | #include <linux/tc_act/tc_mirred.h> |
| 28 | #include <net/tc_act/tc_mirred.h> |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/if_arp.h> |
| 31 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 32 | #define MIRRED_TAB_MASK 7 |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 33 | static LIST_HEAD(mirred_list); |
WANG Cong | 6bd00b8 | 2015-10-01 11:37:42 -0700 | [diff] [blame] | 34 | static DEFINE_SPINLOCK(mirred_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame^] | 36 | static bool tcf_mirred_is_act_redirect(int action) |
| 37 | { |
| 38 | return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR; |
| 39 | } |
| 40 | |
| 41 | static u32 tcf_mirred_act_direction(int action) |
| 42 | { |
| 43 | switch (action) { |
| 44 | case TCA_EGRESS_REDIR: |
| 45 | case TCA_EGRESS_MIRROR: |
| 46 | return AT_EGRESS; |
| 47 | case TCA_INGRESS_REDIR: |
| 48 | case TCA_INGRESS_MIRROR: |
| 49 | return AT_INGRESS; |
| 50 | default: |
| 51 | BUG(); |
| 52 | } |
| 53 | } |
| 54 | |
WANG Cong | a5b5c95 | 2014-02-11 17:07:32 -0800 | [diff] [blame] | 55 | static void tcf_mirred_release(struct tc_action *a, int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 57 | struct tcf_mirred *m = to_mirred(a); |
WANG Cong | dc327f8 | 2016-05-16 15:11:18 -0700 | [diff] [blame] | 58 | struct net_device *dev; |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 59 | |
WANG Cong | 6bd00b8 | 2015-10-01 11:37:42 -0700 | [diff] [blame] | 60 | /* We could be called either in a RCU callback or with RTNL lock held. */ |
| 61 | spin_lock_bh(&mirred_list_lock); |
WANG Cong | a5b5c95 | 2014-02-11 17:07:32 -0800 | [diff] [blame] | 62 | list_del(&m->tcfm_list); |
WANG Cong | dc327f8 | 2016-05-16 15:11:18 -0700 | [diff] [blame] | 63 | dev = rcu_dereference_protected(m->tcfm_dev, 1); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 64 | if (dev) |
| 65 | dev_put(dev); |
WANG Cong | dc327f8 | 2016-05-16 15:11:18 -0700 | [diff] [blame] | 66 | spin_unlock_bh(&mirred_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Patrick McHardy | 53b2bf3 | 2008-01-23 20:36:30 -0800 | [diff] [blame] | 69 | static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = { |
| 70 | [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) }, |
| 71 | }; |
| 72 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 73 | static int mirred_net_id; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 74 | static struct tc_action_ops act_mirred_ops; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 75 | |
Shmulik Ladkani | dcf8003 | 2016-10-13 09:06:42 +0300 | [diff] [blame] | 76 | static bool dev_is_mac_header_xmit(const struct net_device *dev) |
| 77 | { |
| 78 | switch (dev->type) { |
| 79 | case ARPHRD_TUNNEL: |
| 80 | case ARPHRD_TUNNEL6: |
| 81 | case ARPHRD_SIT: |
| 82 | case ARPHRD_IPGRE: |
| 83 | case ARPHRD_VOID: |
| 84 | case ARPHRD_NONE: |
| 85 | return false; |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 90 | static int tcf_mirred_init(struct net *net, struct nlattr *nla, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 91 | struct nlattr *est, struct tc_action **a, int ovr, |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 92 | int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 94 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 95 | struct nlattr *tb[TCA_MIRRED_MAX + 1]; |
Shmulik Ladkani | 1657792 | 2016-10-13 09:06:41 +0300 | [diff] [blame] | 96 | bool mac_header_xmit = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | struct tc_mirred *parm; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 98 | struct tcf_mirred *m; |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 99 | struct net_device *dev; |
WANG Cong | b231307 | 2016-06-13 13:46:28 -0700 | [diff] [blame] | 100 | bool exists = false; |
Shmulik Ladkani | 1657792 | 2016-10-13 09:06:41 +0300 | [diff] [blame] | 101 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 103 | if (nla == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return -EINVAL; |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 105 | ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy); |
| 106 | if (ret < 0) |
| 107 | return ret; |
Patrick McHardy | 53b2bf3 | 2008-01-23 20:36:30 -0800 | [diff] [blame] | 108 | if (tb[TCA_MIRRED_PARMS] == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | return -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 110 | parm = nla_data(tb[TCA_MIRRED_PARMS]); |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 111 | |
| 112 | exists = tcf_hash_check(tn, parm->index, a, bind); |
| 113 | if (exists && bind) |
| 114 | return 0; |
| 115 | |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 116 | switch (parm->eaction) { |
| 117 | case TCA_EGRESS_MIRROR: |
| 118 | case TCA_EGRESS_REDIR: |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame^] | 119 | case TCA_INGRESS_REDIR: |
| 120 | case TCA_INGRESS_MIRROR: |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 121 | break; |
| 122 | default: |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 123 | if (exists) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 124 | tcf_hash_release(*a, bind); |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 125 | return -EINVAL; |
| 126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | if (parm->ifindex) { |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 128 | dev = __dev_get_by_index(net, parm->ifindex); |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 129 | if (dev == NULL) { |
| 130 | if (exists) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 131 | tcf_hash_release(*a, bind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | return -ENODEV; |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 133 | } |
Shmulik Ladkani | dcf8003 | 2016-10-13 09:06:42 +0300 | [diff] [blame] | 134 | mac_header_xmit = dev_is_mac_header_xmit(dev); |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 135 | } else { |
| 136 | dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 139 | if (!exists) { |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 140 | if (dev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return -EINVAL; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 142 | ret = tcf_hash_create(tn, parm->index, est, a, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 143 | &act_mirred_ops, bind, true); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 144 | if (ret) |
| 145 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | ret = ACT_P_CREATED; |
| 147 | } else { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 148 | tcf_hash_release(*a, bind); |
WANG Cong | 215c90a | 2015-10-01 11:37:43 -0700 | [diff] [blame] | 149 | if (!ovr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 152 | m = to_mirred(*a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 154 | ASSERT_RTNL(); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 155 | m->tcf_action = parm->action; |
| 156 | m->tcfm_eaction = parm->eaction; |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 157 | if (dev != NULL) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 158 | m->tcfm_ifindex = parm->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | if (ret != ACT_P_CREATED) |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 160 | dev_put(rcu_dereference_protected(m->tcfm_dev, 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | dev_hold(dev); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 162 | rcu_assign_pointer(m->tcfm_dev, dev); |
Shmulik Ladkani | 1657792 | 2016-10-13 09:06:41 +0300 | [diff] [blame] | 163 | m->tcfm_mac_header_xmit = mac_header_xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 165 | |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 166 | if (ret == ACT_P_CREATED) { |
WANG Cong | 6bd00b8 | 2015-10-01 11:37:42 -0700 | [diff] [blame] | 167 | spin_lock_bh(&mirred_list_lock); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 168 | list_add(&m->tcfm_list, &mirred_list); |
WANG Cong | 6bd00b8 | 2015-10-01 11:37:42 -0700 | [diff] [blame] | 169 | spin_unlock_bh(&mirred_list_lock); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 170 | tcf_hash_insert(tn, *a); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | return ret; |
| 174 | } |
| 175 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 176 | 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] | 177 | struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 179 | struct tcf_mirred *m = to_mirred(a); |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame^] | 180 | bool m_mac_header_xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | struct net_device *dev; |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 182 | struct sk_buff *skb2; |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame^] | 183 | int retval, err = 0; |
| 184 | int m_eaction; |
| 185 | int mac_len; |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 186 | u32 at; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 188 | tcf_lastuse_update(&m->tcf_tm); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 189 | bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb); |
| 190 | |
| 191 | rcu_read_lock(); |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame^] | 192 | m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit); |
| 193 | m_eaction = READ_ONCE(m->tcfm_eaction); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 194 | retval = READ_ONCE(m->tcf_action); |
| 195 | dev = rcu_dereference(m->tcfm_dev); |
| 196 | if (unlikely(!dev)) { |
| 197 | pr_notice_once("tc mirred: target device is gone\n"); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 198 | goto out; |
| 199 | } |
| 200 | |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 201 | if (unlikely(!(dev->flags & IFF_UP))) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 202 | net_notice_ratelimited("tc mirred to Houston: device %s is down\n", |
| 203 | dev->name); |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 204 | goto out; |
| 205 | } |
| 206 | |
Changli Gao | 210d6de | 2010-06-24 16:25:12 +0000 | [diff] [blame] | 207 | at = G_TC_AT(skb->tc_verd); |
Florian Westphal | e578d9c | 2015-05-11 19:50:41 +0200 | [diff] [blame] | 208 | skb2 = skb_clone(skb, GFP_ATOMIC); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 209 | if (!skb2) |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 210 | goto out; |
| 211 | |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame^] | 212 | /* If action's target direction differs than filter's direction, |
| 213 | * and devices expect a mac header on xmit, then mac push/pull is |
| 214 | * needed. |
| 215 | */ |
| 216 | if (at != tcf_mirred_act_direction(m_eaction) && m_mac_header_xmit) { |
| 217 | if (at & AT_EGRESS) { |
| 218 | /* caught at egress, act ingress: pull mac */ |
| 219 | mac_len = skb_network_header(skb) - skb_mac_header(skb); |
| 220 | skb_pull_rcsum(skb2, mac_len); |
| 221 | } else { |
| 222 | /* caught at ingress, act egress: push mac */ |
WANG Cong | 82a31b9 | 2016-06-30 10:15:22 -0700 | [diff] [blame] | 223 | skb_push_rcsum(skb2, skb->mac_len); |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame^] | 224 | } |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | /* mirror is always swallowed */ |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame^] | 228 | if (tcf_mirred_is_act_redirect(m_eaction)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at); |
| 230 | |
Eric Dumazet | 8964be4 | 2009-11-20 15:35:04 -0800 | [diff] [blame] | 231 | skb2->skb_iif = skb->dev->ifindex; |
Changli Gao | 210d6de | 2010-06-24 16:25:12 +0000 | [diff] [blame] | 232 | skb2->dev = dev; |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame^] | 233 | if (tcf_mirred_act_direction(m_eaction) & AT_EGRESS) |
| 234 | err = dev_queue_xmit(skb2); |
| 235 | else |
| 236 | err = netif_receive_skb(skb2); |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 237 | |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 238 | if (err) { |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 239 | out: |
| 240 | qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats)); |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame^] | 241 | if (tcf_mirred_is_act_redirect(m_eaction)) |
Jason Wang | 16c0b16 | 2012-08-15 20:44:27 +0000 | [diff] [blame] | 242 | retval = TC_ACT_SHOT; |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 243 | } |
| 244 | rcu_read_unlock(); |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 245 | |
| 246 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Jakub Kicinski | 9798e6f | 2016-09-21 11:44:05 +0100 | [diff] [blame] | 249 | static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets, |
| 250 | u64 lastuse) |
| 251 | { |
| 252 | tcf_lastuse_update(&a->tcfa_tm); |
| 253 | _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets); |
| 254 | } |
| 255 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 256 | static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, |
| 257 | int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 259 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 260 | struct tcf_mirred *m = to_mirred(a); |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 261 | struct tc_mirred opt = { |
| 262 | .index = m->tcf_index, |
| 263 | .action = m->tcf_action, |
| 264 | .refcnt = m->tcf_refcnt - ref, |
| 265 | .bindcnt = m->tcf_bindcnt - bind, |
| 266 | .eaction = m->tcfm_eaction, |
| 267 | .ifindex = m->tcfm_ifindex, |
| 268 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | struct tcf_t t; |
| 270 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 271 | if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt)) |
| 272 | goto nla_put_failure; |
Jamal Hadi Salim | 48d8ee1 | 2016-06-06 06:32:55 -0400 | [diff] [blame] | 273 | |
| 274 | tcf_tm_dump(&t, &m->tcf_tm); |
Nicolas Dichtel | 9854518 | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 275 | 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] | 276 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | return skb->len; |
| 278 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 279 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 280 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return -1; |
| 282 | } |
| 283 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 284 | static int tcf_mirred_walker(struct net *net, struct sk_buff *skb, |
| 285 | struct netlink_callback *cb, int type, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 286 | const struct tc_action_ops *ops) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 287 | { |
| 288 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
| 289 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 290 | return tcf_generic_walker(tn, skb, cb, type, ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 291 | } |
| 292 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 293 | static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 294 | { |
| 295 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
| 296 | |
| 297 | return tcf_hash_search(tn, a, index); |
| 298 | } |
| 299 | |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 300 | static int mirred_device_event(struct notifier_block *unused, |
| 301 | unsigned long event, void *ptr) |
| 302 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 303 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 304 | struct tcf_mirred *m; |
| 305 | |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 306 | ASSERT_RTNL(); |
WANG Cong | 6bd00b8 | 2015-10-01 11:37:42 -0700 | [diff] [blame] | 307 | if (event == NETDEV_UNREGISTER) { |
| 308 | spin_lock_bh(&mirred_list_lock); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 309 | list_for_each_entry(m, &mirred_list, tcfm_list) { |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 310 | if (rcu_access_pointer(m->tcfm_dev) == dev) { |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 311 | dev_put(dev); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 312 | /* Note : no rcu grace period necessary, as |
| 313 | * net_device are already rcu protected. |
| 314 | */ |
| 315 | RCU_INIT_POINTER(m->tcfm_dev, NULL); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
WANG Cong | 6bd00b8 | 2015-10-01 11:37:42 -0700 | [diff] [blame] | 318 | spin_unlock_bh(&mirred_list_lock); |
| 319 | } |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 320 | |
| 321 | return NOTIFY_DONE; |
| 322 | } |
| 323 | |
| 324 | static struct notifier_block mirred_device_notifier = { |
| 325 | .notifier_call = mirred_device_event, |
| 326 | }; |
| 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | static struct tc_action_ops act_mirred_ops = { |
| 329 | .kind = "mirred", |
| 330 | .type = TCA_ACT_MIRRED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | .owner = THIS_MODULE, |
| 332 | .act = tcf_mirred, |
Jakub Kicinski | 9798e6f | 2016-09-21 11:44:05 +0100 | [diff] [blame] | 333 | .stats_update = tcf_stats_update, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | .dump = tcf_mirred_dump, |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 335 | .cleanup = tcf_mirred_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | .init = tcf_mirred_init, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 337 | .walk = tcf_mirred_walker, |
| 338 | .lookup = tcf_mirred_search, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 339 | .size = sizeof(struct tcf_mirred), |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 340 | }; |
| 341 | |
| 342 | static __net_init int mirred_init_net(struct net *net) |
| 343 | { |
| 344 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
| 345 | |
| 346 | return tc_action_net_init(tn, &act_mirred_ops, MIRRED_TAB_MASK); |
| 347 | } |
| 348 | |
| 349 | static void __net_exit mirred_exit_net(struct net *net) |
| 350 | { |
| 351 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
| 352 | |
| 353 | tc_action_net_exit(tn); |
| 354 | } |
| 355 | |
| 356 | static struct pernet_operations mirred_net_ops = { |
| 357 | .init = mirred_init_net, |
| 358 | .exit = mirred_exit_net, |
| 359 | .id = &mirred_net_id, |
| 360 | .size = sizeof(struct tc_action_net), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | MODULE_AUTHOR("Jamal Hadi Salim(2002)"); |
| 364 | MODULE_DESCRIPTION("Device Mirror/redirect actions"); |
| 365 | MODULE_LICENSE("GPL"); |
| 366 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 367 | static int __init mirred_init_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 369 | int err = register_netdevice_notifier(&mirred_device_notifier); |
| 370 | if (err) |
| 371 | return err; |
| 372 | |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 373 | pr_info("Mirror/redirect action on\n"); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 374 | return tcf_register_action(&act_mirred_ops, &mirred_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
| 376 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 377 | static void __exit mirred_cleanup_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 379 | tcf_unregister_action(&act_mirred_ops, &mirred_net_ops); |
WANG Cong | 568a153 | 2013-12-20 00:08:51 -0800 | [diff] [blame] | 380 | unregister_netdevice_notifier(&mirred_device_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | module_init(mirred_init_module); |
| 384 | module_exit(mirred_cleanup_module); |