Thomas Gleixner | 9952f69 | 2019-05-28 10:10:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2008, Intel Corporation. |
| 4 | * |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 5 | * Author: Alexander Duyck <alexander.h.duyck@intel.com> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/rtnetlink.h> |
| 13 | #include <net/netlink.h> |
| 14 | #include <net/pkt_sched.h> |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 15 | #include <net/ip.h> |
| 16 | #include <net/ipv6.h> |
| 17 | #include <net/dsfield.h> |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 18 | #include <net/pkt_cls.h> |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 19 | |
| 20 | #include <linux/tc_act/tc_skbedit.h> |
| 21 | #include <net/tc_act/tc_skbedit.h> |
| 22 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 23 | static unsigned int skbedit_net_id; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 24 | static struct tc_action_ops act_skbedit_ops; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 25 | |
Jamal Hadi Salim | 45da1da | 2018-08-12 09:34:58 -0400 | [diff] [blame] | 26 | static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a, |
| 27 | struct tcf_result *res) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 28 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 29 | struct tcf_skbedit *d = to_skbedit(a); |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 30 | struct tcf_skbedit_params *params; |
| 31 | int action; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 32 | |
Jamal Hadi Salim | 9c4a4e4 | 2016-06-06 06:32:53 -0400 | [diff] [blame] | 33 | tcf_lastuse_update(&d->tcf_tm); |
Davide Caratti | 6f3dfb0 | 2018-07-11 16:04:49 +0200 | [diff] [blame] | 34 | bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 35 | |
Paolo Abeni | 7fd4b28 | 2018-07-30 14:30:43 +0200 | [diff] [blame] | 36 | params = rcu_dereference_bh(d->params); |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 37 | action = READ_ONCE(d->tcf_action); |
| 38 | |
| 39 | if (params->flags & SKBEDIT_F_PRIORITY) |
| 40 | skb->priority = params->priority; |
| 41 | if (params->flags & SKBEDIT_F_INHERITDSFIELD) { |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 42 | int wlen = skb_network_offset(skb); |
| 43 | |
Toke Høiland-Jørgensen | d7bf2eb | 2020-07-03 22:26:43 +0200 | [diff] [blame] | 44 | switch (skb_protocol(skb, true)) { |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 45 | case htons(ETH_P_IP): |
| 46 | wlen += sizeof(struct iphdr); |
| 47 | if (!pskb_may_pull(skb, wlen)) |
| 48 | goto err; |
| 49 | skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2; |
| 50 | break; |
| 51 | |
| 52 | case htons(ETH_P_IPV6): |
| 53 | wlen += sizeof(struct ipv6hdr); |
| 54 | if (!pskb_may_pull(skb, wlen)) |
| 55 | goto err; |
| 56 | skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2; |
| 57 | break; |
| 58 | } |
| 59 | } |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 60 | if (params->flags & SKBEDIT_F_QUEUE_MAPPING && |
| 61 | skb->dev->real_num_tx_queues > params->queue_mapping) |
| 62 | skb_set_queue_mapping(skb, params->queue_mapping); |
| 63 | if (params->flags & SKBEDIT_F_MARK) { |
| 64 | skb->mark &= ~params->mask; |
| 65 | skb->mark |= params->mark & params->mask; |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 66 | } |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 67 | if (params->flags & SKBEDIT_F_PTYPE) |
| 68 | skb->pkt_type = params->ptype; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 69 | return action; |
Paolo Abeni | 7fd4b28 | 2018-07-30 14:30:43 +0200 | [diff] [blame] | 70 | |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 71 | err: |
Davide Caratti | 6f3dfb0 | 2018-07-11 16:04:49 +0200 | [diff] [blame] | 72 | qstats_drop_inc(this_cpu_ptr(d->common.cpu_qstats)); |
Paolo Abeni | 7fd4b28 | 2018-07-30 14:30:43 +0200 | [diff] [blame] | 73 | return TC_ACT_SHOT; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Petr Machata | 837cb17 | 2020-03-26 22:45:55 +0200 | [diff] [blame] | 76 | static void tcf_skbedit_stats_update(struct tc_action *a, u64 bytes, |
Po Liu | 4b61d3e | 2020-06-19 14:01:07 +0800 | [diff] [blame] | 77 | u64 packets, u64 drops, |
| 78 | u64 lastuse, bool hw) |
Petr Machata | 837cb17 | 2020-03-26 22:45:55 +0200 | [diff] [blame] | 79 | { |
| 80 | struct tcf_skbedit *d = to_skbedit(a); |
| 81 | struct tcf_t *tm = &d->tcf_tm; |
| 82 | |
Po Liu | 4b61d3e | 2020-06-19 14:01:07 +0800 | [diff] [blame] | 83 | tcf_action_update_stats(a, bytes, packets, drops, hw); |
Petr Machata | 837cb17 | 2020-03-26 22:45:55 +0200 | [diff] [blame] | 84 | tm->lastuse = max_t(u64, tm->lastuse, lastuse); |
| 85 | } |
| 86 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 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, |
Vlad Buslov | abbb0d3 | 2019-10-30 16:09:05 +0200 | [diff] [blame] | 100 | struct tcf_proto *tp, u32 act_flags, |
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; |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 113 | u32 index; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 114 | |
| 115 | if (nla == NULL) |
| 116 | return -EINVAL; |
| 117 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 118 | err = nla_parse_nested_deprecated(tb, TCA_SKBEDIT_MAX, nla, |
| 119 | skbedit_policy, NULL); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 120 | if (err < 0) |
| 121 | return err; |
| 122 | |
| 123 | if (tb[TCA_SKBEDIT_PARMS] == NULL) |
| 124 | return -EINVAL; |
| 125 | |
| 126 | if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { |
| 127 | flags |= SKBEDIT_F_PRIORITY; |
| 128 | priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]); |
| 129 | } |
| 130 | |
| 131 | if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { |
| 132 | flags |= SKBEDIT_F_QUEUE_MAPPING; |
| 133 | queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]); |
| 134 | } |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 135 | |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 136 | if (tb[TCA_SKBEDIT_PTYPE] != NULL) { |
| 137 | ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]); |
| 138 | if (!skb_pkt_type_ok(*ptype)) |
| 139 | return -EINVAL; |
| 140 | flags |= SKBEDIT_F_PTYPE; |
| 141 | } |
| 142 | |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 143 | if (tb[TCA_SKBEDIT_MARK] != NULL) { |
| 144 | flags |= SKBEDIT_F_MARK; |
| 145 | mark = nla_data(tb[TCA_SKBEDIT_MARK]); |
| 146 | } |
| 147 | |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 148 | if (tb[TCA_SKBEDIT_MASK] != NULL) { |
| 149 | flags |= SKBEDIT_F_MASK; |
| 150 | mask = nla_data(tb[TCA_SKBEDIT_MASK]); |
| 151 | } |
| 152 | |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 153 | if (tb[TCA_SKBEDIT_FLAGS] != NULL) { |
| 154 | u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]); |
| 155 | |
| 156 | if (*pure_flags & SKBEDIT_F_INHERITDSFIELD) |
| 157 | flags |= SKBEDIT_F_INHERITDSFIELD; |
| 158 | } |
| 159 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 160 | parm = nla_data(tb[TCA_SKBEDIT_PARMS]); |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 161 | index = parm->index; |
| 162 | err = tcf_idr_check_alloc(tn, &index, a, bind); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 163 | if (err < 0) |
| 164 | return err; |
| 165 | exists = err; |
Jamal Hadi Salim | 5e1567a | 2016-05-10 16:49:30 -0400 | [diff] [blame] | 166 | if (exists && bind) |
| 167 | return 0; |
| 168 | |
| 169 | if (!flags) { |
Roman Mashak | af5d018 | 2018-05-11 10:55:09 -0400 | [diff] [blame] | 170 | if (exists) |
| 171 | tcf_idr_release(*a, bind); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 172 | else |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 173 | tcf_idr_cleanup(tn, index); |
Jamal Hadi Salim | 5e1567a | 2016-05-10 16:49:30 -0400 | [diff] [blame] | 174 | return -EINVAL; |
| 175 | } |
| 176 | |
| 177 | if (!exists) { |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 178 | ret = tcf_idr_create(tn, index, est, a, |
Vlad Buslov | e382267 | 2019-10-30 16:09:06 +0200 | [diff] [blame] | 179 | &act_skbedit_ops, bind, true, 0); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 180 | if (ret) { |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 181 | tcf_idr_cleanup(tn, index); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 182 | return ret; |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 183 | } |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 184 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 185 | d = to_skbedit(*a); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 186 | ret = ACT_P_CREATED; |
| 187 | } else { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 188 | d = to_skbedit(*a); |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 189 | if (!ovr) { |
| 190 | tcf_idr_release(*a, bind); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 191 | return -EEXIST; |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 192 | } |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 193 | } |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 194 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
| 195 | if (err < 0) |
| 196 | goto release_idr; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 197 | |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 198 | params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); |
| 199 | if (unlikely(!params_new)) { |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 200 | err = -ENOMEM; |
| 201 | goto put_chain; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | params_new->flags = flags; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 205 | if (flags & SKBEDIT_F_PRIORITY) |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 206 | params_new->priority = *priority; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 207 | if (flags & SKBEDIT_F_QUEUE_MAPPING) |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 208 | params_new->queue_mapping = *queue_mapping; |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 209 | if (flags & SKBEDIT_F_MARK) |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 210 | params_new->mark = *mark; |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 211 | if (flags & SKBEDIT_F_PTYPE) |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 212 | params_new->ptype = *ptype; |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 213 | /* default behaviour is to use all the bits */ |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 214 | params_new->mask = 0xffffffff; |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 215 | if (flags & SKBEDIT_F_MASK) |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 216 | params_new->mask = *mask; |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 217 | |
Vlad Buslov | 6d7a8df | 2018-09-03 10:07:15 +0300 | [diff] [blame] | 218 | spin_lock_bh(&d->tcf_lock); |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 219 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
Paul E. McKenney | 445d374 | 2019-09-23 16:09:18 -0700 | [diff] [blame] | 220 | params_new = rcu_replace_pointer(d->params, params_new, |
| 221 | lockdep_is_held(&d->tcf_lock)); |
Vlad Buslov | 6d7a8df | 2018-09-03 10:07:15 +0300 | [diff] [blame] | 222 | spin_unlock_bh(&d->tcf_lock); |
| 223 | if (params_new) |
| 224 | kfree_rcu(params_new, rcu); |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 225 | if (goto_ch) |
| 226 | tcf_chain_put_by_act(goto_ch); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 227 | |
| 228 | if (ret == ACT_P_CREATED) |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 229 | tcf_idr_insert(tn, *a); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 230 | return ret; |
Davide Caratti | ec7727b | 2019-03-20 15:00:11 +0100 | [diff] [blame] | 231 | put_chain: |
| 232 | if (goto_ch) |
| 233 | tcf_chain_put_by_act(goto_ch); |
| 234 | release_idr: |
| 235 | tcf_idr_release(*a, bind); |
| 236 | return err; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 239 | static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, |
| 240 | int bind, int ref) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 241 | { |
| 242 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 243 | struct tcf_skbedit *d = to_skbedit(a); |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 244 | struct tcf_skbedit_params *params; |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 245 | struct tc_skbedit opt = { |
| 246 | .index = d->tcf_index, |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 247 | .refcnt = refcount_read(&d->tcf_refcnt) - ref, |
| 248 | .bindcnt = atomic_read(&d->tcf_bindcnt) - bind, |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 249 | }; |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 250 | u64 pure_flags = 0; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 251 | struct tcf_t t; |
| 252 | |
Vlad Buslov | 6d7a8df | 2018-09-03 10:07:15 +0300 | [diff] [blame] | 253 | spin_lock_bh(&d->tcf_lock); |
| 254 | params = rcu_dereference_protected(d->params, |
| 255 | lockdep_is_held(&d->tcf_lock)); |
| 256 | opt.action = d->tcf_action; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 257 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 258 | if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) |
| 259 | goto nla_put_failure; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 260 | if ((params->flags & SKBEDIT_F_PRIORITY) && |
| 261 | nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 262 | goto nla_put_failure; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 263 | if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) && |
| 264 | nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 265 | goto nla_put_failure; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 266 | if ((params->flags & SKBEDIT_F_MARK) && |
| 267 | nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 268 | goto nla_put_failure; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 269 | if ((params->flags & SKBEDIT_F_PTYPE) && |
| 270 | nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype)) |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 271 | goto nla_put_failure; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 272 | if ((params->flags & SKBEDIT_F_MASK) && |
| 273 | nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask)) |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 274 | goto nla_put_failure; |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 275 | if (params->flags & SKBEDIT_F_INHERITDSFIELD) |
Qiaobin Fu | e7e3728 | 2018-07-01 15:16:27 -0400 | [diff] [blame] | 276 | pure_flags |= SKBEDIT_F_INHERITDSFIELD; |
| 277 | if (pure_flags != 0 && |
| 278 | nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags)) |
| 279 | goto nla_put_failure; |
Jamal Hadi Salim | 48d8ee1 | 2016-06-06 06:32:55 -0400 | [diff] [blame] | 280 | |
| 281 | tcf_tm_dump(&t, &d->tcf_tm); |
Nicolas Dichtel | 9854518 | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 282 | 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] | 283 | goto nla_put_failure; |
Vlad Buslov | 6d7a8df | 2018-09-03 10:07:15 +0300 | [diff] [blame] | 284 | spin_unlock_bh(&d->tcf_lock); |
| 285 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 286 | return skb->len; |
| 287 | |
| 288 | nla_put_failure: |
Vlad Buslov | 6d7a8df | 2018-09-03 10:07:15 +0300 | [diff] [blame] | 289 | spin_unlock_bh(&d->tcf_lock); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 290 | nlmsg_trim(skb, b); |
| 291 | return -1; |
| 292 | } |
| 293 | |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 294 | static void tcf_skbedit_cleanup(struct tc_action *a) |
| 295 | { |
| 296 | struct tcf_skbedit *d = to_skbedit(a); |
| 297 | struct tcf_skbedit_params *params; |
| 298 | |
| 299 | params = rcu_dereference_protected(d->params, 1); |
| 300 | if (params) |
| 301 | kfree_rcu(params, rcu); |
| 302 | } |
| 303 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 304 | static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb, |
| 305 | struct netlink_callback *cb, int type, |
Alexander Aring | 4178010 | 2018-02-15 10:54:58 -0500 | [diff] [blame] | 306 | const struct tc_action_ops *ops, |
| 307 | struct netlink_ext_ack *extack) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 308 | { |
| 309 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 310 | |
Alexander Aring | b362014 | 2018-02-15 10:54:59 -0500 | [diff] [blame] | 311 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Cong Wang | f061b48 | 2018-08-29 10:15:35 -0700 | [diff] [blame] | 314 | 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] | 315 | { |
| 316 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 317 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 318 | return tcf_idr_search(tn, a, index); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Roman Mashak | e1fea32 | 2019-08-07 15:57:28 -0400 | [diff] [blame] | 321 | static size_t tcf_skbedit_get_fill_size(const struct tc_action *act) |
| 322 | { |
| 323 | return nla_total_size(sizeof(struct tc_skbedit)) |
| 324 | + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_PRIORITY */ |
| 325 | + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING */ |
| 326 | + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MARK */ |
| 327 | + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_PTYPE */ |
| 328 | + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MASK */ |
| 329 | + nla_total_size_64bit(sizeof(u64)); /* TCA_SKBEDIT_FLAGS */ |
| 330 | } |
| 331 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 332 | static struct tc_action_ops act_skbedit_ops = { |
| 333 | .kind = "skbedit", |
Eli Cohen | eddd2cf | 2019-02-10 14:25:00 +0200 | [diff] [blame] | 334 | .id = TCA_ID_SKBEDIT, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 335 | .owner = THIS_MODULE, |
Jamal Hadi Salim | 45da1da | 2018-08-12 09:34:58 -0400 | [diff] [blame] | 336 | .act = tcf_skbedit_act, |
Petr Machata | 837cb17 | 2020-03-26 22:45:55 +0200 | [diff] [blame] | 337 | .stats_update = tcf_skbedit_stats_update, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 338 | .dump = tcf_skbedit_dump, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 339 | .init = tcf_skbedit_init, |
Davide Caratti | c749cdd | 2018-07-11 16:04:50 +0200 | [diff] [blame] | 340 | .cleanup = tcf_skbedit_cleanup, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 341 | .walk = tcf_skbedit_walker, |
Roman Mashak | e1fea32 | 2019-08-07 15:57:28 -0400 | [diff] [blame] | 342 | .get_fill_size = tcf_skbedit_get_fill_size, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 343 | .lookup = tcf_skbedit_search, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 344 | .size = sizeof(struct tcf_skbedit), |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | static __net_init int skbedit_init_net(struct net *net) |
| 348 | { |
| 349 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 350 | |
Cong Wang | 981471b | 2019-08-25 10:01:32 -0700 | [diff] [blame] | 351 | return tc_action_net_init(net, tn, &act_skbedit_ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 354 | static void __net_exit skbedit_exit_net(struct list_head *net_list) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 355 | { |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 356 | tc_action_net_exit(net_list, skbedit_net_id); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static struct pernet_operations skbedit_net_ops = { |
| 360 | .init = skbedit_init_net, |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 361 | .exit_batch = skbedit_exit_net, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 362 | .id = &skbedit_net_id, |
| 363 | .size = sizeof(struct tc_action_net), |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); |
| 367 | MODULE_DESCRIPTION("SKB Editing"); |
| 368 | MODULE_LICENSE("GPL"); |
| 369 | |
| 370 | static int __init skbedit_init_module(void) |
| 371 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 372 | return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void __exit skbedit_cleanup_module(void) |
| 376 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 377 | tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | module_init(skbedit_init_module); |
| 381 | module_exit(skbedit_cleanup_module); |