blob: e928802966bce46d02658102acaee9f717d0f3a6 [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
WANG Congddf97cc2016-02-22 15:57:53 -080032static int skbedit_net_id;
33
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000034static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
Alexander Duyckca9b0e22008-09-12 16:30:20 -070035 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 Dumazetbfe0d022011-01-09 08:30:54 +000041 bstats_update(&d->tcf_bstats, skb);
Alexander Duyckca9b0e22008-09-12 16:30:20 -070042
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);
jamal1c55d622009-10-15 03:09:18 +000048 if (d->flags & SKBEDIT_F_MARK)
49 skb->mark = d->mark;
Alexander Duyckca9b0e22008-09-12 16:30:20 -070050
51 spin_unlock(&d->tcf_lock);
52 return d->tcf_action;
53}
54
55static 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) },
jamal1c55d622009-10-15 03:09:18 +000059 [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
Alexander Duyckca9b0e22008-09-12 16:30:20 -070060};
61
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000062static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
63 struct nlattr *est, struct tc_action *a,
64 int ovr, int bind)
Alexander Duyckca9b0e22008-09-12 16:30:20 -070065{
WANG Congddf97cc2016-02-22 15:57:53 -080066 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
Alexander Duyckca9b0e22008-09-12 16:30:20 -070067 struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
68 struct tc_skbedit *parm;
69 struct tcf_skbedit *d;
jamal1c55d622009-10-15 03:09:18 +000070 u32 flags = 0, *priority = NULL, *mark = NULL;
Alexander Duyckca9b0e22008-09-12 16:30:20 -070071 u16 *queue_mapping = NULL;
Jamal Hadi Salim5e1567a2016-05-10 16:49:30 -040072 int ret = 0, err, exists = 0;
Alexander Duyckca9b0e22008-09-12 16:30:20 -070073
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 }
jamal1c55d622009-10-15 03:09:18 +000093
94 if (tb[TCA_SKBEDIT_MARK] != NULL) {
95 flags |= SKBEDIT_F_MARK;
96 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
97 }
98
Alexander Duyckca9b0e22008-09-12 16:30:20 -070099 parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
100
Jamal Hadi Salim5e1567a2016-05-10 16:49:30 -0400101 exists = tcf_hash_check(tn, parm->index, a, bind);
102 if (exists && bind)
103 return 0;
104
105 if (!flags) {
106 tcf_hash_release(a, bind);
107 return -EINVAL;
108 }
109
110 if (!exists) {
WANG Congddf97cc2016-02-22 15:57:53 -0800111 ret = tcf_hash_create(tn, parm->index, est, a,
112 sizeof(*d), bind, false);
WANG Cong86062032014-02-11 17:07:31 -0800113 if (ret)
114 return ret;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700115
WANG Cong86062032014-02-11 17:07:31 -0800116 d = to_skbedit(a);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700117 ret = ACT_P_CREATED;
118 } else {
WANG Cong86062032014-02-11 17:07:31 -0800119 d = to_skbedit(a);
WANG Cong86062032014-02-11 17:07:31 -0800120 tcf_hash_release(a, bind);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -0500121 if (!ovr)
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700122 return -EEXIST;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700123 }
124
125 spin_lock_bh(&d->tcf_lock);
126
127 d->flags = flags;
128 if (flags & SKBEDIT_F_PRIORITY)
129 d->priority = *priority;
130 if (flags & SKBEDIT_F_QUEUE_MAPPING)
131 d->queue_mapping = *queue_mapping;
jamal1c55d622009-10-15 03:09:18 +0000132 if (flags & SKBEDIT_F_MARK)
133 d->mark = *mark;
134
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700135 d->tcf_action = parm->action;
136
137 spin_unlock_bh(&d->tcf_lock);
138
139 if (ret == ACT_P_CREATED)
WANG Congddf97cc2016-02-22 15:57:53 -0800140 tcf_hash_insert(tn, a);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700141 return ret;
142}
143
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000144static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
145 int bind, int ref)
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700146{
147 unsigned char *b = skb_tail_pointer(skb);
148 struct tcf_skbedit *d = a->priv;
Eric Dumazet1c40be12010-08-16 20:04:22 +0000149 struct tc_skbedit opt = {
150 .index = d->tcf_index,
151 .refcnt = d->tcf_refcnt - ref,
152 .bindcnt = d->tcf_bindcnt - bind,
153 .action = d->tcf_action,
154 };
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700155 struct tcf_t t;
156
David S. Miller1b34ec42012-03-29 05:11:39 -0400157 if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
158 goto nla_put_failure;
159 if ((d->flags & SKBEDIT_F_PRIORITY) &&
160 nla_put(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority),
161 &d->priority))
162 goto nla_put_failure;
163 if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
164 nla_put(skb, TCA_SKBEDIT_QUEUE_MAPPING,
165 sizeof(d->queue_mapping), &d->queue_mapping))
166 goto nla_put_failure;
167 if ((d->flags & SKBEDIT_F_MARK) &&
168 nla_put(skb, TCA_SKBEDIT_MARK, sizeof(d->mark),
169 &d->mark))
170 goto nla_put_failure;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700171 t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install);
172 t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse);
173 t.expires = jiffies_to_clock_t(d->tcf_tm.expires);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200174 if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400175 goto nla_put_failure;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700176 return skb->len;
177
178nla_put_failure:
179 nlmsg_trim(skb, b);
180 return -1;
181}
182
WANG Congddf97cc2016-02-22 15:57:53 -0800183static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
184 struct netlink_callback *cb, int type,
185 struct tc_action *a)
186{
187 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
188
189 return tcf_generic_walker(tn, skb, cb, type, a);
190}
191
192static int tcf_skbedit_search(struct net *net, struct tc_action *a, u32 index)
193{
194 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
195
196 return tcf_hash_search(tn, a, index);
197}
198
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700199static struct tc_action_ops act_skbedit_ops = {
200 .kind = "skbedit",
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700201 .type = TCA_ACT_SKBEDIT,
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700202 .owner = THIS_MODULE,
203 .act = tcf_skbedit,
204 .dump = tcf_skbedit_dump,
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700205 .init = tcf_skbedit_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800206 .walk = tcf_skbedit_walker,
207 .lookup = tcf_skbedit_search,
208};
209
210static __net_init int skbedit_init_net(struct net *net)
211{
212 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
213
214 return tc_action_net_init(tn, &act_skbedit_ops, SKBEDIT_TAB_MASK);
215}
216
217static void __net_exit skbedit_exit_net(struct net *net)
218{
219 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
220
221 tc_action_net_exit(tn);
222}
223
224static struct pernet_operations skbedit_net_ops = {
225 .init = skbedit_init_net,
226 .exit = skbedit_exit_net,
227 .id = &skbedit_net_id,
228 .size = sizeof(struct tc_action_net),
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700229};
230
231MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
232MODULE_DESCRIPTION("SKB Editing");
233MODULE_LICENSE("GPL");
234
235static int __init skbedit_init_module(void)
236{
WANG Congddf97cc2016-02-22 15:57:53 -0800237 return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700238}
239
240static void __exit skbedit_cleanup_module(void)
241{
WANG Congddf97cc2016-02-22 15:57:53 -0800242 tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700243}
244
245module_init(skbedit_init_module);
246module_exit(skbedit_cleanup_module);