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 | |
| 30 | #define SKBEDIT_TAB_MASK 15 |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 31 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 32 | static int skbedit_net_id; |
| 33 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 34 | 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] | 35 | struct tcf_result *res) |
| 36 | { |
| 37 | struct tcf_skbedit *d = a->priv; |
| 38 | |
| 39 | spin_lock(&d->tcf_lock); |
| 40 | d->tcf_tm.lastuse = jiffies; |
Eric Dumazet | bfe0d02 | 2011-01-09 08:30:54 +0000 | [diff] [blame] | 41 | bstats_update(&d->tcf_bstats, skb); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 42 | |
| 43 | if (d->flags & SKBEDIT_F_PRIORITY) |
| 44 | skb->priority = d->priority; |
| 45 | if (d->flags & SKBEDIT_F_QUEUE_MAPPING && |
| 46 | skb->dev->real_num_tx_queues > d->queue_mapping) |
| 47 | skb_set_queue_mapping(skb, d->queue_mapping); |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 48 | if (d->flags & SKBEDIT_F_MARK) |
| 49 | skb->mark = d->mark; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 50 | |
| 51 | spin_unlock(&d->tcf_lock); |
| 52 | return d->tcf_action; |
| 53 | } |
| 54 | |
| 55 | static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { |
| 56 | [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) }, |
| 57 | [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) }, |
| 58 | [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) }, |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 59 | [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) }, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 62 | static int tcf_skbedit_init(struct net *net, struct nlattr *nla, |
| 63 | struct nlattr *est, struct tc_action *a, |
| 64 | int ovr, int bind) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 65 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 66 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 67 | struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; |
| 68 | struct tc_skbedit *parm; |
| 69 | struct tcf_skbedit *d; |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 70 | u32 flags = 0, *priority = NULL, *mark = NULL; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 71 | u16 *queue_mapping = NULL; |
| 72 | int ret = 0, err; |
| 73 | |
| 74 | if (nla == NULL) |
| 75 | return -EINVAL; |
| 76 | |
| 77 | err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy); |
| 78 | if (err < 0) |
| 79 | return err; |
| 80 | |
| 81 | if (tb[TCA_SKBEDIT_PARMS] == NULL) |
| 82 | return -EINVAL; |
| 83 | |
| 84 | if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { |
| 85 | flags |= SKBEDIT_F_PRIORITY; |
| 86 | priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]); |
| 87 | } |
| 88 | |
| 89 | if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { |
| 90 | flags |= SKBEDIT_F_QUEUE_MAPPING; |
| 91 | queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]); |
| 92 | } |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 93 | |
| 94 | if (tb[TCA_SKBEDIT_MARK] != NULL) { |
| 95 | flags |= SKBEDIT_F_MARK; |
| 96 | mark = nla_data(tb[TCA_SKBEDIT_MARK]); |
| 97 | } |
| 98 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 99 | if (!flags) |
| 100 | return -EINVAL; |
| 101 | |
| 102 | parm = nla_data(tb[TCA_SKBEDIT_PARMS]); |
| 103 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 104 | if (!tcf_hash_check(tn, parm->index, a, bind)) { |
| 105 | ret = tcf_hash_create(tn, parm->index, est, a, |
| 106 | sizeof(*d), bind, false); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 107 | if (ret) |
| 108 | return ret; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 109 | |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 110 | d = to_skbedit(a); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 111 | ret = ACT_P_CREATED; |
| 112 | } else { |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 113 | d = to_skbedit(a); |
Jamal Hadi Salim | 1a29321 | 2013-12-23 08:02:11 -0500 | [diff] [blame] | 114 | if (bind) |
| 115 | return 0; |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 116 | tcf_hash_release(a, bind); |
Jamal Hadi Salim | 1a29321 | 2013-12-23 08:02:11 -0500 | [diff] [blame] | 117 | if (!ovr) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 118 | return -EEXIST; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | spin_lock_bh(&d->tcf_lock); |
| 122 | |
| 123 | d->flags = flags; |
| 124 | if (flags & SKBEDIT_F_PRIORITY) |
| 125 | d->priority = *priority; |
| 126 | if (flags & SKBEDIT_F_QUEUE_MAPPING) |
| 127 | d->queue_mapping = *queue_mapping; |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 128 | if (flags & SKBEDIT_F_MARK) |
| 129 | d->mark = *mark; |
| 130 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 131 | d->tcf_action = parm->action; |
| 132 | |
| 133 | spin_unlock_bh(&d->tcf_lock); |
| 134 | |
| 135 | if (ret == ACT_P_CREATED) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 136 | tcf_hash_insert(tn, a); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 137 | return ret; |
| 138 | } |
| 139 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 140 | static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, |
| 141 | int bind, int ref) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 142 | { |
| 143 | unsigned char *b = skb_tail_pointer(skb); |
| 144 | struct tcf_skbedit *d = a->priv; |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 145 | struct tc_skbedit opt = { |
| 146 | .index = d->tcf_index, |
| 147 | .refcnt = d->tcf_refcnt - ref, |
| 148 | .bindcnt = d->tcf_bindcnt - bind, |
| 149 | .action = d->tcf_action, |
| 150 | }; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 151 | struct tcf_t t; |
| 152 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 153 | if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) |
| 154 | goto nla_put_failure; |
| 155 | if ((d->flags & SKBEDIT_F_PRIORITY) && |
| 156 | nla_put(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority), |
| 157 | &d->priority)) |
| 158 | goto nla_put_failure; |
| 159 | if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) && |
| 160 | nla_put(skb, TCA_SKBEDIT_QUEUE_MAPPING, |
| 161 | sizeof(d->queue_mapping), &d->queue_mapping)) |
| 162 | goto nla_put_failure; |
| 163 | if ((d->flags & SKBEDIT_F_MARK) && |
| 164 | nla_put(skb, TCA_SKBEDIT_MARK, sizeof(d->mark), |
| 165 | &d->mark)) |
| 166 | goto nla_put_failure; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 167 | t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install); |
| 168 | t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse); |
| 169 | t.expires = jiffies_to_clock_t(d->tcf_tm.expires); |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 170 | if (nla_put(skb, TCA_SKBEDIT_TM, sizeof(t), &t)) |
| 171 | goto nla_put_failure; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 172 | return skb->len; |
| 173 | |
| 174 | nla_put_failure: |
| 175 | nlmsg_trim(skb, b); |
| 176 | return -1; |
| 177 | } |
| 178 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 179 | static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb, |
| 180 | struct netlink_callback *cb, int type, |
| 181 | struct tc_action *a) |
| 182 | { |
| 183 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 184 | |
| 185 | return tcf_generic_walker(tn, skb, cb, type, a); |
| 186 | } |
| 187 | |
| 188 | static int tcf_skbedit_search(struct net *net, struct tc_action *a, u32 index) |
| 189 | { |
| 190 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 191 | |
| 192 | return tcf_hash_search(tn, a, index); |
| 193 | } |
| 194 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 195 | static struct tc_action_ops act_skbedit_ops = { |
| 196 | .kind = "skbedit", |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 197 | .type = TCA_ACT_SKBEDIT, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 198 | .owner = THIS_MODULE, |
| 199 | .act = tcf_skbedit, |
| 200 | .dump = tcf_skbedit_dump, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 201 | .init = tcf_skbedit_init, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 202 | .walk = tcf_skbedit_walker, |
| 203 | .lookup = tcf_skbedit_search, |
| 204 | }; |
| 205 | |
| 206 | static __net_init int skbedit_init_net(struct net *net) |
| 207 | { |
| 208 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 209 | |
| 210 | return tc_action_net_init(tn, &act_skbedit_ops, SKBEDIT_TAB_MASK); |
| 211 | } |
| 212 | |
| 213 | static void __net_exit skbedit_exit_net(struct net *net) |
| 214 | { |
| 215 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
| 216 | |
| 217 | tc_action_net_exit(tn); |
| 218 | } |
| 219 | |
| 220 | static struct pernet_operations skbedit_net_ops = { |
| 221 | .init = skbedit_init_net, |
| 222 | .exit = skbedit_exit_net, |
| 223 | .id = &skbedit_net_id, |
| 224 | .size = sizeof(struct tc_action_net), |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); |
| 228 | MODULE_DESCRIPTION("SKB Editing"); |
| 229 | MODULE_LICENSE("GPL"); |
| 230 | |
| 231 | static int __init skbedit_init_module(void) |
| 232 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 233 | return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static void __exit skbedit_cleanup_module(void) |
| 237 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 238 | tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | module_init(skbedit_init_module); |
| 242 | module_exit(skbedit_cleanup_module); |