blob: 6b3e65d7de0c2e81240618e4500ee894819df833 [file] [log] [blame]
Alexander Duyckca9b0e22008-09-12 16:30:20 -07001/*
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 Kirsherc057b192013-12-06 09:13:44 -080014 * this program; if not, see <http://www.gnu.org/licenses/>.
Alexander Duyckca9b0e22008-09-12 16:30:20 -070015 *
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 Duyckca9b0e22008-09-12 16:30:20 -070031
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030032static unsigned int skbedit_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070033static struct tc_action_ops act_skbedit_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080034
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000035static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
Alexander Duyckca9b0e22008-09-12 16:30:20 -070036 struct tcf_result *res)
37{
WANG Conga85a9702016-07-25 16:09:41 -070038 struct tcf_skbedit *d = to_skbedit(a);
Alexander Duyckca9b0e22008-09-12 16:30:20 -070039
40 spin_lock(&d->tcf_lock);
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -040041 tcf_lastuse_update(&d->tcf_tm);
Eric Dumazetbfe0d022011-01-09 08:30:54 +000042 bstats_update(&d->tcf_bstats, skb);
Alexander Duyckca9b0e22008-09-12 16:30:20 -070043
44 if (d->flags & SKBEDIT_F_PRIORITY)
45 skb->priority = d->priority;
46 if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
47 skb->dev->real_num_tx_queues > d->queue_mapping)
48 skb_set_queue_mapping(skb, d->queue_mapping);
Antonio Quartulli4fe77d82016-10-24 20:32:57 +080049 if (d->flags & SKBEDIT_F_MARK) {
50 skb->mark &= ~d->mask;
51 skb->mark |= d->mark & d->mask;
52 }
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -040053 if (d->flags & SKBEDIT_F_PTYPE)
54 skb->pkt_type = d->ptype;
Alexander Duyckca9b0e22008-09-12 16:30:20 -070055
56 spin_unlock(&d->tcf_lock);
57 return d->tcf_action;
58}
59
60static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
61 [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
62 [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
63 [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
jamal1c55d622009-10-15 03:09:18 +000064 [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -040065 [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) },
Antonio Quartulli4fe77d82016-10-24 20:32:57 +080066 [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) },
Alexander Duyckca9b0e22008-09-12 16:30:20 -070067};
68
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000069static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070070 struct nlattr *est, struct tc_action **a,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000071 int ovr, int bind)
Alexander Duyckca9b0e22008-09-12 16:30:20 -070072{
WANG Congddf97cc2016-02-22 15:57:53 -080073 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
Alexander Duyckca9b0e22008-09-12 16:30:20 -070074 struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
75 struct tc_skbedit *parm;
76 struct tcf_skbedit *d;
Antonio Quartulli4fe77d82016-10-24 20:32:57 +080077 u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL;
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -040078 u16 *queue_mapping = NULL, *ptype = NULL;
WANG Congb2313072016-06-13 13:46:28 -070079 bool exists = false;
80 int ret = 0, err;
Alexander Duyckca9b0e22008-09-12 16:30:20 -070081
82 if (nla == NULL)
83 return -EINVAL;
84
Johannes Bergfceb6432017-04-12 14:34:07 +020085 err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL);
Alexander Duyckca9b0e22008-09-12 16:30:20 -070086 if (err < 0)
87 return err;
88
89 if (tb[TCA_SKBEDIT_PARMS] == NULL)
90 return -EINVAL;
91
92 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
93 flags |= SKBEDIT_F_PRIORITY;
94 priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
95 }
96
97 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
98 flags |= SKBEDIT_F_QUEUE_MAPPING;
99 queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
100 }
jamal1c55d622009-10-15 03:09:18 +0000101
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -0400102 if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
103 ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
104 if (!skb_pkt_type_ok(*ptype))
105 return -EINVAL;
106 flags |= SKBEDIT_F_PTYPE;
107 }
108
jamal1c55d622009-10-15 03:09:18 +0000109 if (tb[TCA_SKBEDIT_MARK] != NULL) {
110 flags |= SKBEDIT_F_MARK;
111 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
112 }
113
Antonio Quartulli4fe77d82016-10-24 20:32:57 +0800114 if (tb[TCA_SKBEDIT_MASK] != NULL) {
115 flags |= SKBEDIT_F_MASK;
116 mask = nla_data(tb[TCA_SKBEDIT_MASK]);
117 }
118
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700119 parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
120
Jamal Hadi Salim5e1567a2016-05-10 16:49:30 -0400121 exists = tcf_hash_check(tn, parm->index, a, bind);
122 if (exists && bind)
123 return 0;
124
125 if (!flags) {
WANG Conga85a9702016-07-25 16:09:41 -0700126 tcf_hash_release(*a, bind);
Jamal Hadi Salim5e1567a2016-05-10 16:49:30 -0400127 return -EINVAL;
128 }
129
130 if (!exists) {
WANG Congddf97cc2016-02-22 15:57:53 -0800131 ret = tcf_hash_create(tn, parm->index, est, a,
WANG Conga85a9702016-07-25 16:09:41 -0700132 &act_skbedit_ops, bind, false);
WANG Cong86062032014-02-11 17:07:31 -0800133 if (ret)
134 return ret;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700135
WANG Conga85a9702016-07-25 16:09:41 -0700136 d = to_skbedit(*a);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700137 ret = ACT_P_CREATED;
138 } else {
WANG Conga85a9702016-07-25 16:09:41 -0700139 d = to_skbedit(*a);
140 tcf_hash_release(*a, bind);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -0500141 if (!ovr)
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700142 return -EEXIST;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700143 }
144
145 spin_lock_bh(&d->tcf_lock);
146
147 d->flags = flags;
148 if (flags & SKBEDIT_F_PRIORITY)
149 d->priority = *priority;
150 if (flags & SKBEDIT_F_QUEUE_MAPPING)
151 d->queue_mapping = *queue_mapping;
jamal1c55d622009-10-15 03:09:18 +0000152 if (flags & SKBEDIT_F_MARK)
153 d->mark = *mark;
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -0400154 if (flags & SKBEDIT_F_PTYPE)
155 d->ptype = *ptype;
Antonio Quartulli4fe77d82016-10-24 20:32:57 +0800156 /* default behaviour is to use all the bits */
157 d->mask = 0xffffffff;
158 if (flags & SKBEDIT_F_MASK)
159 d->mask = *mask;
jamal1c55d622009-10-15 03:09:18 +0000160
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700161 d->tcf_action = parm->action;
162
163 spin_unlock_bh(&d->tcf_lock);
164
165 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -0700166 tcf_hash_insert(tn, *a);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700167 return ret;
168}
169
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000170static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
171 int bind, int ref)
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700172{
173 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700174 struct tcf_skbedit *d = to_skbedit(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000175 struct tc_skbedit opt = {
176 .index = d->tcf_index,
177 .refcnt = d->tcf_refcnt - ref,
178 .bindcnt = d->tcf_bindcnt - bind,
179 .action = d->tcf_action,
180 };
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700181 struct tcf_t t;
182
David S. Miller1b34ec42012-03-29 05:11:39 -0400183 if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
184 goto nla_put_failure;
185 if ((d->flags & SKBEDIT_F_PRIORITY) &&
Jamal Hadi Salim61cc5352016-07-02 06:43:16 -0400186 nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, d->priority))
David S. Miller1b34ec42012-03-29 05:11:39 -0400187 goto nla_put_failure;
188 if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
Jamal Hadi Salim61cc5352016-07-02 06:43:16 -0400189 nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, d->queue_mapping))
David S. Miller1b34ec42012-03-29 05:11:39 -0400190 goto nla_put_failure;
191 if ((d->flags & SKBEDIT_F_MARK) &&
Jamal Hadi Salim61cc5352016-07-02 06:43:16 -0400192 nla_put_u32(skb, TCA_SKBEDIT_MARK, d->mark))
David S. Miller1b34ec42012-03-29 05:11:39 -0400193 goto nla_put_failure;
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -0400194 if ((d->flags & SKBEDIT_F_PTYPE) &&
Jamal Hadi Salim61cc5352016-07-02 06:43:16 -0400195 nla_put_u16(skb, TCA_SKBEDIT_PTYPE, d->ptype))
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -0400196 goto nla_put_failure;
Antonio Quartulli4fe77d82016-10-24 20:32:57 +0800197 if ((d->flags & SKBEDIT_F_MASK) &&
198 nla_put_u32(skb, TCA_SKBEDIT_MASK, d->mask))
199 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400200
201 tcf_tm_dump(&t, &d->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200202 if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400203 goto nla_put_failure;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700204 return skb->len;
205
206nla_put_failure:
207 nlmsg_trim(skb, b);
208 return -1;
209}
210
WANG Congddf97cc2016-02-22 15:57:53 -0800211static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
212 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700213 const struct tc_action_ops *ops)
WANG Congddf97cc2016-02-22 15:57:53 -0800214{
215 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
216
WANG Conga85a9702016-07-25 16:09:41 -0700217 return tcf_generic_walker(tn, skb, cb, type, ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800218}
219
WANG Conga85a9702016-07-25 16:09:41 -0700220static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800221{
222 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
223
224 return tcf_hash_search(tn, a, index);
225}
226
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700227static struct tc_action_ops act_skbedit_ops = {
228 .kind = "skbedit",
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700229 .type = TCA_ACT_SKBEDIT,
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700230 .owner = THIS_MODULE,
231 .act = tcf_skbedit,
232 .dump = tcf_skbedit_dump,
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700233 .init = tcf_skbedit_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800234 .walk = tcf_skbedit_walker,
235 .lookup = tcf_skbedit_search,
WANG Conga85a9702016-07-25 16:09:41 -0700236 .size = sizeof(struct tcf_skbedit),
WANG Congddf97cc2016-02-22 15:57:53 -0800237};
238
239static __net_init int skbedit_init_net(struct net *net)
240{
241 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
242
243 return tc_action_net_init(tn, &act_skbedit_ops, SKBEDIT_TAB_MASK);
244}
245
246static void __net_exit skbedit_exit_net(struct net *net)
247{
248 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
249
250 tc_action_net_exit(tn);
251}
252
253static struct pernet_operations skbedit_net_ops = {
254 .init = skbedit_init_net,
255 .exit = skbedit_exit_net,
256 .id = &skbedit_net_id,
257 .size = sizeof(struct tc_action_net),
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700258};
259
260MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
261MODULE_DESCRIPTION("SKB Editing");
262MODULE_LICENSE("GPL");
263
264static int __init skbedit_init_module(void)
265{
WANG Congddf97cc2016-02-22 15:57:53 -0800266 return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700267}
268
269static void __exit skbedit_cleanup_module(void)
270{
WANG Congddf97cc2016-02-22 15:57:53 -0800271 tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700272}
273
274module_init(skbedit_init_module);
275module_exit(skbedit_cleanup_module);