]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sched/act_skbedit.c
net: simplify and make pkt_type_ok() available for other users
[mirror_ubuntu-bionic-kernel.git] / net / sched / act_skbedit.c
CommitLineData
ca9b0e27
AD
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
c057b190 14 * this program; if not, see <http://www.gnu.org/licenses/>.
ca9b0e27
AD
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
ca9b0e27 31
ddf97ccd
WC
32static int skbedit_net_id;
33
dc7f9f6e 34static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
ca9b0e27
AD
35 struct tcf_result *res)
36{
37 struct tcf_skbedit *d = a->priv;
38
39 spin_lock(&d->tcf_lock);
9c4a4e48 40 tcf_lastuse_update(&d->tcf_tm);
bfe0d029 41 bstats_update(&d->tcf_bstats, skb);
ca9b0e27
AD
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);
1c55d62e 48 if (d->flags & SKBEDIT_F_MARK)
49 skb->mark = d->mark;
ca9b0e27
AD
50
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) },
1c55d62e 59 [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
ca9b0e27
AD
60};
61
c1b52739
BL
62static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
63 struct nlattr *est, struct tc_action *a,
64 int ovr, int bind)
ca9b0e27 65{
ddf97ccd 66 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
ca9b0e27
AD
67 struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
68 struct tc_skbedit *parm;
69 struct tcf_skbedit *d;
1c55d62e 70 u32 flags = 0, *priority = NULL, *mark = NULL;
ca9b0e27 71 u16 *queue_mapping = NULL;
b2313077
WC
72 bool exists = false;
73 int ret = 0, err;
ca9b0e27
AD
74
75 if (nla == NULL)
76 return -EINVAL;
77
78 err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy);
79 if (err < 0)
80 return err;
81
82 if (tb[TCA_SKBEDIT_PARMS] == NULL)
83 return -EINVAL;
84
85 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
86 flags |= SKBEDIT_F_PRIORITY;
87 priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
88 }
89
90 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
91 flags |= SKBEDIT_F_QUEUE_MAPPING;
92 queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
93 }
1c55d62e 94
95 if (tb[TCA_SKBEDIT_MARK] != NULL) {
96 flags |= SKBEDIT_F_MARK;
97 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
98 }
99
ca9b0e27
AD
100 parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
101
5e1567ae
JHS
102 exists = tcf_hash_check(tn, parm->index, a, bind);
103 if (exists && bind)
104 return 0;
105
106 if (!flags) {
107 tcf_hash_release(a, bind);
108 return -EINVAL;
109 }
110
111 if (!exists) {
ddf97ccd
WC
112 ret = tcf_hash_create(tn, parm->index, est, a,
113 sizeof(*d), bind, false);
86062033
WC
114 if (ret)
115 return ret;
ca9b0e27 116
86062033 117 d = to_skbedit(a);
ca9b0e27
AD
118 ret = ACT_P_CREATED;
119 } else {
86062033 120 d = to_skbedit(a);
86062033 121 tcf_hash_release(a, bind);
1a29321e 122 if (!ovr)
ca9b0e27 123 return -EEXIST;
ca9b0e27
AD
124 }
125
126 spin_lock_bh(&d->tcf_lock);
127
128 d->flags = flags;
129 if (flags & SKBEDIT_F_PRIORITY)
130 d->priority = *priority;
131 if (flags & SKBEDIT_F_QUEUE_MAPPING)
132 d->queue_mapping = *queue_mapping;
1c55d62e 133 if (flags & SKBEDIT_F_MARK)
134 d->mark = *mark;
135
ca9b0e27
AD
136 d->tcf_action = parm->action;
137
138 spin_unlock_bh(&d->tcf_lock);
139
140 if (ret == ACT_P_CREATED)
ddf97ccd 141 tcf_hash_insert(tn, a);
ca9b0e27
AD
142 return ret;
143}
144
cc7ec456
ED
145static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
146 int bind, int ref)
ca9b0e27
AD
147{
148 unsigned char *b = skb_tail_pointer(skb);
149 struct tcf_skbedit *d = a->priv;
1c40be12
ED
150 struct tc_skbedit opt = {
151 .index = d->tcf_index,
152 .refcnt = d->tcf_refcnt - ref,
153 .bindcnt = d->tcf_bindcnt - bind,
154 .action = d->tcf_action,
155 };
ca9b0e27
AD
156 struct tcf_t t;
157
1b34ec43
DM
158 if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
159 goto nla_put_failure;
160 if ((d->flags & SKBEDIT_F_PRIORITY) &&
161 nla_put(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority),
162 &d->priority))
163 goto nla_put_failure;
164 if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
165 nla_put(skb, TCA_SKBEDIT_QUEUE_MAPPING,
166 sizeof(d->queue_mapping), &d->queue_mapping))
167 goto nla_put_failure;
168 if ((d->flags & SKBEDIT_F_MARK) &&
169 nla_put(skb, TCA_SKBEDIT_MARK, sizeof(d->mark),
170 &d->mark))
171 goto nla_put_failure;
48d8ee16
JHS
172
173 tcf_tm_dump(&t, &d->tcf_tm);
9854518e 174 if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
1b34ec43 175 goto nla_put_failure;
ca9b0e27
AD
176 return skb->len;
177
178nla_put_failure:
179 nlmsg_trim(skb, b);
180 return -1;
181}
182
ddf97ccd
WC
183static 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
ca9b0e27
AD
199static struct tc_action_ops act_skbedit_ops = {
200 .kind = "skbedit",
ca9b0e27 201 .type = TCA_ACT_SKBEDIT,
ca9b0e27
AD
202 .owner = THIS_MODULE,
203 .act = tcf_skbedit,
204 .dump = tcf_skbedit_dump,
ca9b0e27 205 .init = tcf_skbedit_init,
ddf97ccd
WC
206 .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),
ca9b0e27
AD
229};
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{
ddf97ccd 237 return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
ca9b0e27
AD
238}
239
240static void __exit skbedit_cleanup_module(void)
241{
ddf97ccd 242 tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
ca9b0e27
AD
243}
244
245module_init(skbedit_init_module);
246module_exit(skbedit_cleanup_module);