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> |
| 26 | |
| 27 | #include <linux/tc_act/tc_skbedit.h> |
| 28 | #include <net/tc_act/tc_skbedit.h> |
| 29 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 30 | static unsigned int skbedit_net_id; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 31 | static struct tc_action_ops act_skbedit_ops; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 32 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 33 | static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 34 | struct tcf_result *res) |
| 35 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 36 | struct tcf_skbedit *d = to_skbedit(a); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 37 | |
| 38 | spin_lock(&d->tcf_lock); |
Jamal Hadi Salim | 9c4a4e4 | 2016-06-06 06:32:53 -0400 | [diff] [blame] | 39 | tcf_lastuse_update(&d->tcf_tm); |
Eric Dumazet | bfe0d02 | 2011-01-09 08:30:54 +0000 | [diff] [blame] | 40 | bstats_update(&d->tcf_bstats, skb); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 41 | |
| 42 | if (d->flags & SKBEDIT_F_PRIORITY) |
| 43 | skb->priority = d->priority; |
| 44 | if (d->flags & SKBEDIT_F_QUEUE_MAPPING && |
| 45 | skb->dev->real_num_tx_queues > d->queue_mapping) |
| 46 | skb_set_queue_mapping(skb, d->queue_mapping); |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 47 | if (d->flags & SKBEDIT_F_MARK) { |
| 48 | skb->mark &= ~d->mask; |
| 49 | skb->mark |= d->mark & d->mask; |
| 50 | } |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 51 | if (d->flags & SKBEDIT_F_PTYPE) |
| 52 | skb->pkt_type = d->ptype; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 53 | |
| 54 | spin_unlock(&d->tcf_lock); |
| 55 | return d->tcf_action; |
| 56 | } |
| 57 | |
| 58 | static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { |
| 59 | [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) }, |
| 60 | [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) }, |
| 61 | [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) }, |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 62 | [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) }, |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 63 | [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) }, |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 64 | [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) }, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 67 | static int tcf_skbedit_init(struct net *net, struct nlattr *nla, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 68 | struct nlattr *est, struct tc_action **a, |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 69 | int ovr, int bind) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 70 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 71 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 72 | struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; |
| 73 | struct tc_skbedit *parm; |
| 74 | struct tcf_skbedit *d; |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 75 | u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL; |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 76 | u16 *queue_mapping = NULL, *ptype = NULL; |
WANG Cong | b231307 | 2016-06-13 13:46:28 -0700 | [diff] [blame] | 77 | bool exists = false; |
| 78 | int ret = 0, err; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 79 | |
| 80 | if (nla == NULL) |
| 81 | return -EINVAL; |
| 82 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 83 | err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 84 | if (err < 0) |
| 85 | return err; |
| 86 | |
| 87 | if (tb[TCA_SKBEDIT_PARMS] == NULL) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { |
| 91 | flags |= SKBEDIT_F_PRIORITY; |
| 92 | priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]); |
| 93 | } |
| 94 | |
| 95 | if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { |
| 96 | flags |= SKBEDIT_F_QUEUE_MAPPING; |
| 97 | queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]); |
| 98 | } |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 99 | |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 100 | if (tb[TCA_SKBEDIT_PTYPE] != NULL) { |
| 101 | ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]); |
| 102 | if (!skb_pkt_type_ok(*ptype)) |
| 103 | return -EINVAL; |
| 104 | flags |= SKBEDIT_F_PTYPE; |
| 105 | } |
| 106 | |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 107 | if (tb[TCA_SKBEDIT_MARK] != NULL) { |
| 108 | flags |= SKBEDIT_F_MARK; |
| 109 | mark = nla_data(tb[TCA_SKBEDIT_MARK]); |
| 110 | } |
| 111 | |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 112 | if (tb[TCA_SKBEDIT_MASK] != NULL) { |
| 113 | flags |= SKBEDIT_F_MASK; |
| 114 | mask = nla_data(tb[TCA_SKBEDIT_MASK]); |
| 115 | } |
| 116 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 117 | parm = nla_data(tb[TCA_SKBEDIT_PARMS]); |
| 118 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 119 | exists = tcf_idr_check(tn, parm->index, a, bind); |
Jamal Hadi Salim | 5e1567a | 2016-05-10 16:49:30 -0400 | [diff] [blame] | 120 | if (exists && bind) |
| 121 | return 0; |
| 122 | |
| 123 | if (!flags) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 124 | tcf_idr_release(*a, bind); |
Jamal Hadi Salim | 5e1567a | 2016-05-10 16:49:30 -0400 | [diff] [blame] | 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | if (!exists) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 129 | ret = tcf_idr_create(tn, parm->index, est, a, |
| 130 | &act_skbedit_ops, bind, false); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 131 | if (ret) |
| 132 | return ret; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 133 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 134 | d = to_skbedit(*a); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 135 | ret = ACT_P_CREATED; |
| 136 | } else { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 137 | d = to_skbedit(*a); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 138 | tcf_idr_release(*a, bind); |
Jamal Hadi Salim | 1a29321 | 2013-12-23 08:02:11 -0500 | [diff] [blame] | 139 | if (!ovr) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 140 | return -EEXIST; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | spin_lock_bh(&d->tcf_lock); |
| 144 | |
| 145 | d->flags = flags; |
| 146 | if (flags & SKBEDIT_F_PRIORITY) |
| 147 | d->priority = *priority; |
| 148 | if (flags & SKBEDIT_F_QUEUE_MAPPING) |
| 149 | d->queue_mapping = *queue_mapping; |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 150 | if (flags & SKBEDIT_F_MARK) |
| 151 | d->mark = *mark; |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 152 | if (flags & SKBEDIT_F_PTYPE) |
| 153 | d->ptype = *ptype; |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 154 | /* default behaviour is to use all the bits */ |
| 155 | d->mask = 0xffffffff; |
| 156 | if (flags & SKBEDIT_F_MASK) |
| 157 | d->mask = *mask; |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 158 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 159 | d->tcf_action = parm->action; |
| 160 | |
| 161 | spin_unlock_bh(&d->tcf_lock); |
| 162 | |
| 163 | if (ret == ACT_P_CREATED) |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 164 | tcf_idr_insert(tn, *a); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 165 | return ret; |
| 166 | } |
| 167 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 168 | static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, |
| 169 | int bind, int ref) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 170 | { |
| 171 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 172 | struct tcf_skbedit *d = to_skbedit(a); |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 173 | struct tc_skbedit opt = { |
| 174 | .index = d->tcf_index, |
| 175 | .refcnt = d->tcf_refcnt - ref, |
| 176 | .bindcnt = d->tcf_bindcnt - bind, |
| 177 | .action = d->tcf_action, |
| 178 | }; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 179 | struct tcf_t t; |
| 180 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 181 | if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) |
| 182 | goto nla_put_failure; |
| 183 | if ((d->flags & SKBEDIT_F_PRIORITY) && |
Jamal Hadi Salim | 61cc535 | 2016-07-02 06:43:16 -0400 | [diff] [blame] | 184 | nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, d->priority)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 185 | goto nla_put_failure; |
| 186 | if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) && |
Jamal Hadi Salim | 61cc535 | 2016-07-02 06:43:16 -0400 | [diff] [blame] | 187 | nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, d->queue_mapping)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 188 | goto nla_put_failure; |
| 189 | if ((d->flags & SKBEDIT_F_MARK) && |
Jamal Hadi Salim | 61cc535 | 2016-07-02 06:43:16 -0400 | [diff] [blame] | 190 | nla_put_u32(skb, TCA_SKBEDIT_MARK, d->mark)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 191 | goto nla_put_failure; |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 192 | if ((d->flags & SKBEDIT_F_PTYPE) && |
Jamal Hadi Salim | 61cc535 | 2016-07-02 06:43:16 -0400 | [diff] [blame] | 193 | nla_put_u16(skb, TCA_SKBEDIT_PTYPE, d->ptype)) |
Jamal Hadi Salim | ff202ee | 2016-07-02 06:43:15 -0400 | [diff] [blame] | 194 | goto nla_put_failure; |
Antonio Quartulli | 4fe77d8 | 2016-10-24 20:32:57 +0800 | [diff] [blame] | 195 | if ((d->flags & SKBEDIT_F_MASK) && |
| 196 | nla_put_u32(skb, TCA_SKBEDIT_MASK, d->mask)) |
| 197 | goto nla_put_failure; |
Jamal Hadi Salim | 48d8ee1 | 2016-06-06 06:32:55 -0400 | [diff] [blame] | 198 | |
| 199 | tcf_tm_dump(&t, &d->tcf_tm); |
Nicolas Dichtel | 9854518 | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 200 | 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] | 201 | goto nla_put_failure; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 202 | return skb->len; |
| 203 | |
| 204 | nla_put_failure: |
| 205 | nlmsg_trim(skb, b); |
| 206 | return -1; |
| 207 | } |
| 208 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 209 | static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb, |
| 210 | struct netlink_callback *cb, int type, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 211 | const struct tc_action_ops *ops) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 212 | { |
| 213 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 214 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 215 | return tcf_generic_walker(tn, skb, cb, type, ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 216 | } |
| 217 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 218 | 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] | 219 | { |
| 220 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 221 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 222 | return tcf_idr_search(tn, a, index); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 225 | static struct tc_action_ops act_skbedit_ops = { |
| 226 | .kind = "skbedit", |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 227 | .type = TCA_ACT_SKBEDIT, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 228 | .owner = THIS_MODULE, |
| 229 | .act = tcf_skbedit, |
| 230 | .dump = tcf_skbedit_dump, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 231 | .init = tcf_skbedit_init, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 232 | .walk = tcf_skbedit_walker, |
| 233 | .lookup = tcf_skbedit_search, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 234 | .size = sizeof(struct tcf_skbedit), |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | static __net_init int skbedit_init_net(struct net *net) |
| 238 | { |
| 239 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 240 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 241 | return tc_action_net_init(tn, &act_skbedit_ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static void __net_exit skbedit_exit_net(struct net *net) |
| 245 | { |
| 246 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 247 | |
| 248 | tc_action_net_exit(tn); |
| 249 | } |
| 250 | |
| 251 | static struct pernet_operations skbedit_net_ops = { |
| 252 | .init = skbedit_init_net, |
| 253 | .exit = skbedit_exit_net, |
| 254 | .id = &skbedit_net_id, |
| 255 | .size = sizeof(struct tc_action_net), |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); |
| 259 | MODULE_DESCRIPTION("SKB Editing"); |
| 260 | MODULE_LICENSE("GPL"); |
| 261 | |
| 262 | static int __init skbedit_init_module(void) |
| 263 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 264 | return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static void __exit skbedit_cleanup_module(void) |
| 268 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 269 | tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | module_init(skbedit_init_module); |
| 273 | module_exit(skbedit_cleanup_module); |