blob: 6138d1d71900b561f50578bf22110902bb488bf4 [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
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030030static unsigned int skbedit_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070031static struct tc_action_ops act_skbedit_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080032
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000033static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
Alexander Duyckca9b0e22008-09-12 16:30:20 -070034 struct tcf_result *res)
35{
WANG Conga85a9702016-07-25 16:09:41 -070036 struct tcf_skbedit *d = to_skbedit(a);
Alexander Duyckca9b0e22008-09-12 16:30:20 -070037
38 spin_lock(&d->tcf_lock);
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -040039 tcf_lastuse_update(&d->tcf_tm);
Eric Dumazetbfe0d022011-01-09 08:30:54 +000040 bstats_update(&d->tcf_bstats, skb);
Alexander Duyckca9b0e22008-09-12 16:30:20 -070041
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 Quartulli4fe77d82016-10-24 20:32:57 +080047 if (d->flags & SKBEDIT_F_MARK) {
48 skb->mark &= ~d->mask;
49 skb->mark |= d->mark & d->mask;
50 }
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -040051 if (d->flags & SKBEDIT_F_PTYPE)
52 skb->pkt_type = d->ptype;
Alexander Duyckca9b0e22008-09-12 16:30:20 -070053
54 spin_unlock(&d->tcf_lock);
55 return d->tcf_action;
56}
57
58static 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) },
jamal1c55d622009-10-15 03:09:18 +000062 [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -040063 [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) },
Antonio Quartulli4fe77d82016-10-24 20:32:57 +080064 [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) },
Alexander Duyckca9b0e22008-09-12 16:30:20 -070065};
66
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000067static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070068 struct nlattr *est, struct tc_action **a,
Alexander Aring589dad62018-02-15 10:54:56 -050069 int ovr, int bind, struct netlink_ext_ack *extack)
Alexander Duyckca9b0e22008-09-12 16:30:20 -070070{
WANG Congddf97cc2016-02-22 15:57:53 -080071 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
Alexander Duyckca9b0e22008-09-12 16:30:20 -070072 struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
73 struct tc_skbedit *parm;
74 struct tcf_skbedit *d;
Antonio Quartulli4fe77d82016-10-24 20:32:57 +080075 u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL;
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -040076 u16 *queue_mapping = NULL, *ptype = NULL;
WANG Congb2313072016-06-13 13:46:28 -070077 bool exists = false;
78 int ret = 0, err;
Alexander Duyckca9b0e22008-09-12 16:30:20 -070079
80 if (nla == NULL)
81 return -EINVAL;
82
Johannes Bergfceb6432017-04-12 14:34:07 +020083 err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL);
Alexander Duyckca9b0e22008-09-12 16:30:20 -070084 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 }
jamal1c55d622009-10-15 03:09:18 +000099
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -0400100 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
jamal1c55d622009-10-15 03:09:18 +0000107 if (tb[TCA_SKBEDIT_MARK] != NULL) {
108 flags |= SKBEDIT_F_MARK;
109 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
110 }
111
Antonio Quartulli4fe77d82016-10-24 20:32:57 +0800112 if (tb[TCA_SKBEDIT_MASK] != NULL) {
113 flags |= SKBEDIT_F_MASK;
114 mask = nla_data(tb[TCA_SKBEDIT_MASK]);
115 }
116
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700117 parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
118
Chris Mi65a206c2017-08-30 02:31:59 -0400119 exists = tcf_idr_check(tn, parm->index, a, bind);
Jamal Hadi Salim5e1567a2016-05-10 16:49:30 -0400120 if (exists && bind)
121 return 0;
122
123 if (!flags) {
Roman Mashakaf5d0182018-05-11 10:55:09 -0400124 if (exists)
125 tcf_idr_release(*a, bind);
Jamal Hadi Salim5e1567a2016-05-10 16:49:30 -0400126 return -EINVAL;
127 }
128
129 if (!exists) {
Chris Mi65a206c2017-08-30 02:31:59 -0400130 ret = tcf_idr_create(tn, parm->index, est, a,
131 &act_skbedit_ops, bind, false);
WANG Cong86062032014-02-11 17:07:31 -0800132 if (ret)
133 return ret;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700134
WANG Conga85a9702016-07-25 16:09:41 -0700135 d = to_skbedit(*a);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700136 ret = ACT_P_CREATED;
137 } else {
WANG Conga85a9702016-07-25 16:09:41 -0700138 d = to_skbedit(*a);
Chris Mi65a206c2017-08-30 02:31:59 -0400139 tcf_idr_release(*a, bind);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -0500140 if (!ovr)
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700141 return -EEXIST;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700142 }
143
144 spin_lock_bh(&d->tcf_lock);
145
146 d->flags = flags;
147 if (flags & SKBEDIT_F_PRIORITY)
148 d->priority = *priority;
149 if (flags & SKBEDIT_F_QUEUE_MAPPING)
150 d->queue_mapping = *queue_mapping;
jamal1c55d622009-10-15 03:09:18 +0000151 if (flags & SKBEDIT_F_MARK)
152 d->mark = *mark;
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -0400153 if (flags & SKBEDIT_F_PTYPE)
154 d->ptype = *ptype;
Antonio Quartulli4fe77d82016-10-24 20:32:57 +0800155 /* default behaviour is to use all the bits */
156 d->mask = 0xffffffff;
157 if (flags & SKBEDIT_F_MASK)
158 d->mask = *mask;
jamal1c55d622009-10-15 03:09:18 +0000159
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700160 d->tcf_action = parm->action;
161
162 spin_unlock_bh(&d->tcf_lock);
163
164 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -0400165 tcf_idr_insert(tn, *a);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700166 return ret;
167}
168
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000169static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
170 int bind, int ref)
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700171{
172 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700173 struct tcf_skbedit *d = to_skbedit(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000174 struct tc_skbedit opt = {
175 .index = d->tcf_index,
176 .refcnt = d->tcf_refcnt - ref,
177 .bindcnt = d->tcf_bindcnt - bind,
178 .action = d->tcf_action,
179 };
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700180 struct tcf_t t;
181
David S. Miller1b34ec42012-03-29 05:11:39 -0400182 if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
183 goto nla_put_failure;
184 if ((d->flags & SKBEDIT_F_PRIORITY) &&
Jamal Hadi Salim61cc5352016-07-02 06:43:16 -0400185 nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, d->priority))
David S. Miller1b34ec42012-03-29 05:11:39 -0400186 goto nla_put_failure;
187 if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
Jamal Hadi Salim61cc5352016-07-02 06:43:16 -0400188 nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, d->queue_mapping))
David S. Miller1b34ec42012-03-29 05:11:39 -0400189 goto nla_put_failure;
190 if ((d->flags & SKBEDIT_F_MARK) &&
Jamal Hadi Salim61cc5352016-07-02 06:43:16 -0400191 nla_put_u32(skb, TCA_SKBEDIT_MARK, d->mark))
David S. Miller1b34ec42012-03-29 05:11:39 -0400192 goto nla_put_failure;
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -0400193 if ((d->flags & SKBEDIT_F_PTYPE) &&
Jamal Hadi Salim61cc5352016-07-02 06:43:16 -0400194 nla_put_u16(skb, TCA_SKBEDIT_PTYPE, d->ptype))
Jamal Hadi Salimff202ee2016-07-02 06:43:15 -0400195 goto nla_put_failure;
Antonio Quartulli4fe77d82016-10-24 20:32:57 +0800196 if ((d->flags & SKBEDIT_F_MASK) &&
197 nla_put_u32(skb, TCA_SKBEDIT_MASK, d->mask))
198 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400199
200 tcf_tm_dump(&t, &d->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200201 if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400202 goto nla_put_failure;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700203 return skb->len;
204
205nla_put_failure:
206 nlmsg_trim(skb, b);
207 return -1;
208}
209
WANG Congddf97cc2016-02-22 15:57:53 -0800210static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
211 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500212 const struct tc_action_ops *ops,
213 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800214{
215 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
216
Alexander Aringb3620142018-02-15 10:54:59 -0500217 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800218}
219
Alexander Aring331a9292018-02-15 10:54:57 -0500220static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index,
221 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800222{
223 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
224
Chris Mi65a206c2017-08-30 02:31:59 -0400225 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800226}
227
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700228static struct tc_action_ops act_skbedit_ops = {
229 .kind = "skbedit",
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700230 .type = TCA_ACT_SKBEDIT,
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700231 .owner = THIS_MODULE,
232 .act = tcf_skbedit,
233 .dump = tcf_skbedit_dump,
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700234 .init = tcf_skbedit_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800235 .walk = tcf_skbedit_walker,
236 .lookup = tcf_skbedit_search,
WANG Conga85a9702016-07-25 16:09:41 -0700237 .size = sizeof(struct tcf_skbedit),
WANG Congddf97cc2016-02-22 15:57:53 -0800238};
239
240static __net_init int skbedit_init_net(struct net *net)
241{
242 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
243
Cong Wangc7e460c2017-11-06 13:47:18 -0800244 return tc_action_net_init(tn, &act_skbedit_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800245}
246
Cong Wang039af9c2017-12-11 15:35:03 -0800247static void __net_exit skbedit_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800248{
Cong Wang039af9c2017-12-11 15:35:03 -0800249 tc_action_net_exit(net_list, skbedit_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800250}
251
252static struct pernet_operations skbedit_net_ops = {
253 .init = skbedit_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800254 .exit_batch = skbedit_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800255 .id = &skbedit_net_id,
256 .size = sizeof(struct tc_action_net),
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700257};
258
259MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
260MODULE_DESCRIPTION("SKB Editing");
261MODULE_LICENSE("GPL");
262
263static int __init skbedit_init_module(void)
264{
WANG Congddf97cc2016-02-22 15:57:53 -0800265 return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700266}
267
268static void __exit skbedit_cleanup_module(void)
269{
WANG Congddf97cc2016-02-22 15:57:53 -0800270 tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700271}
272
273module_init(skbedit_init_module);
274module_exit(skbedit_cleanup_module);