Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008, Intel Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
Jeff Kirsher | c057b19 | 2013-12-06 09:13:44 -0800 | [diff] [blame] | 14 | * this program; if not, see <http://www.gnu.org/licenses/>. |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 15 | * |
| 16 | * Author: Alexander Duyck <alexander.h.duyck@intel.com> |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/skbuff.h> |
| 23 | #include <linux/rtnetlink.h> |
| 24 | #include <net/netlink.h> |
| 25 | #include <net/pkt_sched.h> |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 26 | #include <net/ip.h> |
| 27 | #include <net/ipv6.h> |
| 28 | #include <net/dsfield.h> |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 29 | #include <net/pkt_cls.h> |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 30 | |
| 31 | #include <linux/tc_act/tc_skbedit.h> |
| 32 | #include <net/tc_act/tc_skbedit.h> |
| 33 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 34 | static unsigned int skbedit_net_id; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 35 | static struct tc_action_ops act_skbedit_ops; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 36 | |
Jamal Hadi Salim | 45da1da | 2018-08-12 09:34:58 -0400 | [diff] [blame] | 37 | static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a, |
| 38 | struct tcf_result *res) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 39 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 40 | struct tcf_skbedit *d = to_skbedit(a); |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 41 | struct tcf_skbedit_params *params; |
| 42 | int action; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 43 | |
Jamal Hadi Salim | 9c4a4e4 | 2016-06-06 06:32:53 -0400 | [diff] [blame] | 44 | tcf_lastuse_update(&d->tcf_tm); |
Davide Caratti | 6f3dfb0 | 2018-07-11 16:04:49 +0200 | [diff] [blame] | 45 | bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 46 | |
Paolo Abeni | 7fd4b28 | 2018-07-30 14:30:43 +0200 | [diff] [blame] | 47 | params = rcu_dereference_bh(d->params); |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 48 | action = READ_ONCE(d->tcf_action); |
| 49 | |
| 50 | if (params->flags & SKBEDIT_F_PRIORITY) |
| 51 | skb->priority = params->priority; |
| 52 | if (params->flags & SKBEDIT_F_INHERITDSFIELD) { |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 53 | int wlen = skb_network_offset(skb); |
| 54 | |
| 55 | switch (tc_skb_protocol(skb)) { |
| 56 | case htons(ETH_P_IP): |
| 57 | wlen += sizeof(struct iphdr); |
| 58 | if (!pskb_may_pull(skb, wlen)) |
| 59 | goto err; |
| 60 | skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2; |
| 61 | break; |
| 62 | |
| 63 | case htons(ETH_P_IPV6): |
| 64 | wlen += sizeof(struct ipv6hdr); |
| 65 | if (!pskb_may_pull(skb, wlen)) |
| 66 | goto err; |
| 67 | skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2; |
| 68 | break; |
| 69 | } |
| 70 | } |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 71 | if (params->flags & SKBEDIT_F_QUEUE_MAPPING && |
| 72 | skb->dev->real_num_tx_queues > params->queue_mapping) |
| 73 | skb_set_queue_mapping(skb, params->queue_mapping); |
| 74 | if (params->flags & SKBEDIT_F_MARK) { |
| 75 | skb->mark &= ~params->mask; |
| 76 | skb->mark |= params->mark & params->mask; |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 77 | } |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 78 | if (params->flags & SKBEDIT_F_PTYPE) |
| 79 | skb->pkt_type = params->ptype; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 80 | return action; |
Paolo Abeni | 7fd4b28 | 2018-07-30 14:30:43 +0200 | [diff] [blame] | 81 | |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 82 | err: |
Davide Caratti | 6f3dfb0 | 2018-07-11 16:04:49 +0200 | [diff] [blame] | 83 | qstats_drop_inc(this_cpu_ptr(d->common.cpu_qstats)); |
Paolo Abeni | 7fd4b28 | 2018-07-30 14:30:43 +0200 | [diff] [blame] | 84 | return TC_ACT_SHOT; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { |
| 88 | [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) }, |
| 89 | [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) }, |
| 90 | [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) }, |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 91 | [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) }, |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 92 | [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) }, |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 93 | [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) }, |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 94 | [TCA_SKBEDIT_FLAGS] = { .len = sizeof(u64) }, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 97 | static int tcf_skbedit_init(struct net *net, struct nlattr *nla, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 98 | struct nlattr *est, struct tc_action **a, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 99 | int ovr, int bind, bool rtnl_held, |
Davide Caratti | 85d0966 | 2019-03-20 14:59:59 +0100 | [diff] [blame] | 100 | struct tcf_proto *tp, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 101 | struct netlink_ext_ack *extack) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 102 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 103 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
Vlad Buslov | 6d7a8df | 2018-09-03 10:07:15 +0300 | [diff] [blame] | 104 | struct tcf_skbedit_params *params_new; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 105 | struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 106 | struct tcf_chain *goto_ch = NULL; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 107 | struct tc_skbedit *parm; |
| 108 | struct tcf_skbedit *d; |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 109 | u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL; |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 110 | u16 *queue_mapping = NULL, *ptype = NULL; |
WANG Cong | b231307 | 2016-06-13 13:46:28 -0700 | [diff] [blame] | 111 | bool exists = false; |
| 112 | int ret = 0, err; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 113 | |
| 114 | if (nla == NULL) |
| 115 | return -EINVAL; |
| 116 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame^] | 117 | err = nla_parse_nested_deprecated(tb, TCA_SKBEDIT_MAX, nla, |
| 118 | skbedit_policy, NULL); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 119 | if (err < 0) |
| 120 | return err; |
| 121 | |
| 122 | if (tb[TCA_SKBEDIT_PARMS] == NULL) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { |
| 126 | flags |= SKBEDIT_F_PRIORITY; |
| 127 | priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]); |
| 128 | } |
| 129 | |
| 130 | if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { |
| 131 | flags |= SKBEDIT_F_QUEUE_MAPPING; |
| 132 | queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]); |
| 133 | } |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 134 | |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 135 | if (tb[TCA_SKBEDIT_PTYPE] != NULL) { |
| 136 | ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]); |
| 137 | if (!skb_pkt_type_ok(*ptype)) |
| 138 | return -EINVAL; |
| 139 | flags |= SKBEDIT_F_PTYPE; |
| 140 | } |
| 141 | |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 142 | if (tb[TCA_SKBEDIT_MARK] != NULL) { |
| 143 | flags |= SKBEDIT_F_MARK; |
| 144 | mark = nla_data(tb[TCA_SKBEDIT_MARK]); |
| 145 | } |
| 146 | |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 147 | if (tb[TCA_SKBEDIT_MASK] != NULL) { |
| 148 | flags |= SKBEDIT_F_MASK; |
| 149 | mask = nla_data(tb[TCA_SKBEDIT_MASK]); |
| 150 | } |
| 151 | |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 152 | if (tb[TCA_SKBEDIT_FLAGS] != NULL) { |
| 153 | u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]); |
| 154 | |
| 155 | if (*pure_flags & SKBEDIT_F_INHERITDSFIELD) |
| 156 | flags |= SKBEDIT_F_INHERITDSFIELD; |
| 157 | } |
| 158 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 159 | parm = nla_data(tb[TCA_SKBEDIT_PARMS]); |
| 160 | |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 161 | err = tcf_idr_check_alloc(tn, &parm->index, a, bind); |
| 162 | if (err < 0) |
| 163 | return err; |
| 164 | exists = err; |
Jamal Hadi Salim | 5e1567a | 2016-05-10 16:49:30 -0400 | [diff] [blame] | 165 | if (exists && bind) |
| 166 | return 0; |
| 167 | |
| 168 | if (!flags) { |
Roman Mashak | af5d018 | 2018-05-11 10:55:09 -0400 | [diff] [blame] | 169 | if (exists) |
| 170 | tcf_idr_release(*a, bind); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 171 | else |
| 172 | tcf_idr_cleanup(tn, parm->index); |
Jamal Hadi Salim | 5e1567a | 2016-05-10 16:49:30 -0400 | [diff] [blame] | 173 | return -EINVAL; |
| 174 | } |
| 175 | |
| 176 | if (!exists) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 177 | ret = tcf_idr_create(tn, parm->index, est, a, |
Davide Caratti | 6f3dfb0 | 2018-07-11 16:04:49 +0200 | [diff] [blame] | 178 | &act_skbedit_ops, bind, true); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 179 | if (ret) { |
| 180 | tcf_idr_cleanup(tn, parm->index); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 181 | return ret; |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 182 | } |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 183 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 184 | d = to_skbedit(*a); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 185 | ret = ACT_P_CREATED; |
| 186 | } else { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 187 | d = to_skbedit(*a); |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 188 | if (!ovr) { |
| 189 | tcf_idr_release(*a, bind); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 190 | return -EEXIST; |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 191 | } |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 192 | } |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 193 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
| 194 | if (err < 0) |
| 195 | goto release_idr; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 196 | |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 197 | params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); |
| 198 | if (unlikely(!params_new)) { |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 199 | err = -ENOMEM; |
| 200 | goto put_chain; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | params_new->flags = flags; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 204 | if (flags & SKBEDIT_F_PRIORITY) |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 205 | params_new->priority = *priority; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 206 | if (flags & SKBEDIT_F_QUEUE_MAPPING) |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 207 | params_new->queue_mapping = *queue_mapping; |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 208 | if (flags & SKBEDIT_F_MARK) |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 209 | params_new->mark = *mark; |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 210 | if (flags & SKBEDIT_F_PTYPE) |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 211 | params_new->ptype = *ptype; |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 212 | /* default behaviour is to use all the bits */ |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 213 | params_new->mask = 0xffffffff; |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 214 | if (flags & SKBEDIT_F_MASK) |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 215 | params_new->mask = *mask; |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 216 | |
Vlad Buslov | 6d7a8df | 2018-09-03 10:07:15 +0300 | [diff] [blame] | 217 | spin_lock_bh(&d->tcf_lock); |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 218 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
Vlad Buslov | 6d7a8df | 2018-09-03 10:07:15 +0300 | [diff] [blame] | 219 | rcu_swap_protected(d->params, params_new, |
| 220 | lockdep_is_held(&d->tcf_lock)); |
| 221 | spin_unlock_bh(&d->tcf_lock); |
| 222 | if (params_new) |
| 223 | kfree_rcu(params_new, rcu); |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 224 | if (goto_ch) |
| 225 | tcf_chain_put_by_act(goto_ch); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 226 | |
| 227 | if (ret == ACT_P_CREATED) |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 228 | tcf_idr_insert(tn, *a); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 229 | return ret; |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 230 | put_chain: |
| 231 | if (goto_ch) |
| 232 | tcf_chain_put_by_act(goto_ch); |
| 233 | release_idr: |
| 234 | tcf_idr_release(*a, bind); |
| 235 | return err; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 238 | static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, |
| 239 | int bind, int ref) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 240 | { |
| 241 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 242 | struct tcf_skbedit *d = to_skbedit(a); |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 243 | struct tcf_skbedit_params *params; |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 244 | struct tc_skbedit opt = { |
| 245 | .index = d->tcf_index, |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 246 | .refcnt = refcount_read(&d->tcf_refcnt) - ref, |
| 247 | .bindcnt = atomic_read(&d->tcf_bindcnt) - bind, |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 248 | }; |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 249 | u64 pure_flags = 0; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 250 | struct tcf_t t; |
| 251 | |
Vlad Buslov | 6d7a8df | 2018-09-03 10:07:15 +0300 | [diff] [blame] | 252 | spin_lock_bh(&d->tcf_lock); |
| 253 | params = rcu_dereference_protected(d->params, |
| 254 | lockdep_is_held(&d->tcf_lock)); |
| 255 | opt.action = d->tcf_action; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 256 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 257 | if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) |
| 258 | goto nla_put_failure; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 259 | if ((params->flags & SKBEDIT_F_PRIORITY) && |
| 260 | nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 261 | goto nla_put_failure; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 262 | if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) && |
| 263 | nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 264 | goto nla_put_failure; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 265 | if ((params->flags & SKBEDIT_F_MARK) && |
| 266 | nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 267 | goto nla_put_failure; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 268 | if ((params->flags & SKBEDIT_F_PTYPE) && |
| 269 | nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype)) |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 270 | goto nla_put_failure; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 271 | if ((params->flags & SKBEDIT_F_MASK) && |
| 272 | nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask)) |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 273 | goto nla_put_failure; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 274 | if (params->flags & SKBEDIT_F_INHERITDSFIELD) |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 275 | pure_flags |= SKBEDIT_F_INHERITDSFIELD; |
| 276 | if (pure_flags != 0 && |
| 277 | nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags)) |
| 278 | goto nla_put_failure; |
Jamal Hadi Salim | 48d8ee1 | 2016-06-06 06:32:55 -0400 | [diff] [blame] | 279 | |
| 280 | tcf_tm_dump(&t, &d->tcf_tm); |
Nicolas Dichtel | 9854518 | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 281 | if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 282 | goto nla_put_failure; |
Vlad Buslov | 6d7a8df | 2018-09-03 10:07:15 +0300 | [diff] [blame] | 283 | spin_unlock_bh(&d->tcf_lock); |
| 284 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 285 | return skb->len; |
| 286 | |
| 287 | nla_put_failure: |
Vlad Buslov | 6d7a8df | 2018-09-03 10:07:15 +0300 | [diff] [blame] | 288 | spin_unlock_bh(&d->tcf_lock); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 289 | nlmsg_trim(skb, b); |
| 290 | return -1; |
| 291 | } |
| 292 | |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 293 | static void tcf_skbedit_cleanup(struct tc_action *a) |
| 294 | { |
| 295 | struct tcf_skbedit *d = to_skbedit(a); |
| 296 | struct tcf_skbedit_params *params; |
| 297 | |
| 298 | params = rcu_dereference_protected(d->params, 1); |
| 299 | if (params) |
| 300 | kfree_rcu(params, rcu); |
| 301 | } |
| 302 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 303 | static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb, |
| 304 | struct netlink_callback *cb, int type, |
Alexander Aring | 4178010 | 2018-02-15 10:54:58 -0500 | [diff] [blame] | 305 | const struct tc_action_ops *ops, |
| 306 | struct netlink_ext_ack *extack) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 307 | { |
| 308 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 309 | |
Alexander Aring | b362014 | 2018-02-15 10:54:59 -0500 | [diff] [blame] | 310 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Cong Wang | f061b48 | 2018-08-29 10:15:35 -0700 | [diff] [blame] | 313 | static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 314 | { |
| 315 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 316 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 317 | return tcf_idr_search(tn, a, index); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 320 | static struct tc_action_ops act_skbedit_ops = { |
| 321 | .kind = "skbedit", |
Eli Cohen | eddd2cf | 2019-02-10 14:25:00 +0200 | [diff] [blame] | 322 | .id = TCA_ID_SKBEDIT, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 323 | .owner = THIS_MODULE, |
Jamal Hadi Salim | 45da1da | 2018-08-12 09:34:58 -0400 | [diff] [blame] | 324 | .act = tcf_skbedit_act, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 325 | .dump = tcf_skbedit_dump, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 326 | .init = tcf_skbedit_init, |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 327 | .cleanup = tcf_skbedit_cleanup, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 328 | .walk = tcf_skbedit_walker, |
| 329 | .lookup = tcf_skbedit_search, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 330 | .size = sizeof(struct tcf_skbedit), |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | static __net_init int skbedit_init_net(struct net *net) |
| 334 | { |
| 335 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 336 | |
Cong Wang | c7e460c | 2017-11-06 13:47:18 -0800 | [diff] [blame] | 337 | return tc_action_net_init(tn, &act_skbedit_ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 340 | static void __net_exit skbedit_exit_net(struct list_head *net_list) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 341 | { |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 342 | tc_action_net_exit(net_list, skbedit_net_id); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static struct pernet_operations skbedit_net_ops = { |
| 346 | .init = skbedit_init_net, |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 347 | .exit_batch = skbedit_exit_net, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 348 | .id = &skbedit_net_id, |
| 349 | .size = sizeof(struct tc_action_net), |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 350 | }; |
| 351 | |
| 352 | MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); |
| 353 | MODULE_DESCRIPTION("SKB Editing"); |
| 354 | MODULE_LICENSE("GPL"); |
| 355 | |
| 356 | static int __init skbedit_init_module(void) |
| 357 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 358 | return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static void __exit skbedit_cleanup_module(void) |
| 362 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 363 | tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | module_init(skbedit_init_module); |
| 367 | module_exit(skbedit_cleanup_module); |