Commit | Line | Data |
---|---|---|
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> | |
e7e3728b QF |
26 | #include <net/ip.h> |
27 | #include <net/ipv6.h> | |
28 | #include <net/dsfield.h> | |
ca9b0e27 AD |
29 | |
30 | #include <linux/tc_act/tc_skbedit.h> | |
31 | #include <net/tc_act/tc_skbedit.h> | |
32 | ||
c7d03a00 | 33 | static unsigned int skbedit_net_id; |
a85a970a | 34 | static struct tc_action_ops act_skbedit_ops; |
ddf97ccd | 35 | |
45da1dac JHS |
36 | static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a, |
37 | struct tcf_result *res) | |
ca9b0e27 | 38 | { |
a85a970a | 39 | struct tcf_skbedit *d = to_skbedit(a); |
c749cdda DC |
40 | struct tcf_skbedit_params *params; |
41 | int action; | |
ca9b0e27 | 42 | |
9c4a4e48 | 43 | tcf_lastuse_update(&d->tcf_tm); |
6f3dfb0d | 44 | bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb); |
ca9b0e27 | 45 | |
7fd4b288 | 46 | params = rcu_dereference_bh(d->params); |
c749cdda DC |
47 | action = READ_ONCE(d->tcf_action); |
48 | ||
49 | if (params->flags & SKBEDIT_F_PRIORITY) | |
50 | skb->priority = params->priority; | |
51 | if (params->flags & SKBEDIT_F_INHERITDSFIELD) { | |
e7e3728b QF |
52 | int wlen = skb_network_offset(skb); |
53 | ||
54 | switch (tc_skb_protocol(skb)) { | |
55 | case htons(ETH_P_IP): | |
56 | wlen += sizeof(struct iphdr); | |
57 | if (!pskb_may_pull(skb, wlen)) | |
58 | goto err; | |
59 | skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2; | |
60 | break; | |
61 | ||
62 | case htons(ETH_P_IPV6): | |
63 | wlen += sizeof(struct ipv6hdr); | |
64 | if (!pskb_may_pull(skb, wlen)) | |
65 | goto err; | |
66 | skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2; | |
67 | break; | |
68 | } | |
69 | } | |
c749cdda DC |
70 | if (params->flags & SKBEDIT_F_QUEUE_MAPPING && |
71 | skb->dev->real_num_tx_queues > params->queue_mapping) | |
72 | skb_set_queue_mapping(skb, params->queue_mapping); | |
73 | if (params->flags & SKBEDIT_F_MARK) { | |
74 | skb->mark &= ~params->mask; | |
75 | skb->mark |= params->mark & params->mask; | |
4fe77d82 | 76 | } |
c749cdda DC |
77 | if (params->flags & SKBEDIT_F_PTYPE) |
78 | skb->pkt_type = params->ptype; | |
c749cdda | 79 | return action; |
7fd4b288 | 80 | |
e7e3728b | 81 | err: |
6f3dfb0d | 82 | qstats_drop_inc(this_cpu_ptr(d->common.cpu_qstats)); |
7fd4b288 | 83 | return TC_ACT_SHOT; |
ca9b0e27 AD |
84 | } |
85 | ||
86 | static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { | |
87 | [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) }, | |
88 | [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) }, | |
89 | [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) }, | |
1c55d62e | 90 | [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) }, |
ff202ee1 | 91 | [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) }, |
4fe77d82 | 92 | [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) }, |
e7e3728b | 93 | [TCA_SKBEDIT_FLAGS] = { .len = sizeof(u64) }, |
ca9b0e27 AD |
94 | }; |
95 | ||
c1b52739 | 96 | static int tcf_skbedit_init(struct net *net, struct nlattr *nla, |
a85a970a | 97 | struct nlattr *est, struct tc_action **a, |
789871bb | 98 | int ovr, int bind, bool rtnl_held, |
85d0966f | 99 | struct tcf_proto *tp, |
789871bb | 100 | struct netlink_ext_ack *extack) |
ca9b0e27 | 101 | { |
ddf97ccd | 102 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
6d7a8df6 | 103 | struct tcf_skbedit_params *params_new; |
ca9b0e27 AD |
104 | struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; |
105 | struct tc_skbedit *parm; | |
106 | struct tcf_skbedit *d; | |
4fe77d82 | 107 | u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL; |
ff202ee1 | 108 | u16 *queue_mapping = NULL, *ptype = NULL; |
b2313077 WC |
109 | bool exists = false; |
110 | int ret = 0, err; | |
ca9b0e27 AD |
111 | |
112 | if (nla == NULL) | |
113 | return -EINVAL; | |
114 | ||
fceb6435 | 115 | err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL); |
ca9b0e27 AD |
116 | if (err < 0) |
117 | return err; | |
118 | ||
119 | if (tb[TCA_SKBEDIT_PARMS] == NULL) | |
120 | return -EINVAL; | |
121 | ||
122 | if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { | |
123 | flags |= SKBEDIT_F_PRIORITY; | |
124 | priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]); | |
125 | } | |
126 | ||
127 | if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { | |
128 | flags |= SKBEDIT_F_QUEUE_MAPPING; | |
129 | queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]); | |
130 | } | |
1c55d62e | 131 | |
ff202ee1 JHS |
132 | if (tb[TCA_SKBEDIT_PTYPE] != NULL) { |
133 | ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]); | |
134 | if (!skb_pkt_type_ok(*ptype)) | |
135 | return -EINVAL; | |
136 | flags |= SKBEDIT_F_PTYPE; | |
137 | } | |
138 | ||
1c55d62e | 139 | if (tb[TCA_SKBEDIT_MARK] != NULL) { |
140 | flags |= SKBEDIT_F_MARK; | |
141 | mark = nla_data(tb[TCA_SKBEDIT_MARK]); | |
142 | } | |
143 | ||
4fe77d82 AQ |
144 | if (tb[TCA_SKBEDIT_MASK] != NULL) { |
145 | flags |= SKBEDIT_F_MASK; | |
146 | mask = nla_data(tb[TCA_SKBEDIT_MASK]); | |
147 | } | |
148 | ||
e7e3728b QF |
149 | if (tb[TCA_SKBEDIT_FLAGS] != NULL) { |
150 | u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]); | |
151 | ||
152 | if (*pure_flags & SKBEDIT_F_INHERITDSFIELD) | |
153 | flags |= SKBEDIT_F_INHERITDSFIELD; | |
154 | } | |
155 | ||
ca9b0e27 AD |
156 | parm = nla_data(tb[TCA_SKBEDIT_PARMS]); |
157 | ||
0190c1d4 VB |
158 | err = tcf_idr_check_alloc(tn, &parm->index, a, bind); |
159 | if (err < 0) | |
160 | return err; | |
161 | exists = err; | |
5e1567ae JHS |
162 | if (exists && bind) |
163 | return 0; | |
164 | ||
165 | if (!flags) { | |
af5d0184 RM |
166 | if (exists) |
167 | tcf_idr_release(*a, bind); | |
0190c1d4 VB |
168 | else |
169 | tcf_idr_cleanup(tn, parm->index); | |
5e1567ae JHS |
170 | return -EINVAL; |
171 | } | |
172 | ||
173 | if (!exists) { | |
65a206c0 | 174 | ret = tcf_idr_create(tn, parm->index, est, a, |
6f3dfb0d | 175 | &act_skbedit_ops, bind, true); |
0190c1d4 VB |
176 | if (ret) { |
177 | tcf_idr_cleanup(tn, parm->index); | |
86062033 | 178 | return ret; |
0190c1d4 | 179 | } |
ca9b0e27 | 180 | |
a85a970a | 181 | d = to_skbedit(*a); |
ca9b0e27 AD |
182 | ret = ACT_P_CREATED; |
183 | } else { | |
a85a970a | 184 | d = to_skbedit(*a); |
4e8ddd7f VB |
185 | if (!ovr) { |
186 | tcf_idr_release(*a, bind); | |
ca9b0e27 | 187 | return -EEXIST; |
4e8ddd7f | 188 | } |
ca9b0e27 AD |
189 | } |
190 | ||
c749cdda DC |
191 | params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); |
192 | if (unlikely(!params_new)) { | |
6191da98 | 193 | tcf_idr_release(*a, bind); |
c749cdda DC |
194 | return -ENOMEM; |
195 | } | |
196 | ||
197 | params_new->flags = flags; | |
ca9b0e27 | 198 | if (flags & SKBEDIT_F_PRIORITY) |
c749cdda | 199 | params_new->priority = *priority; |
ca9b0e27 | 200 | if (flags & SKBEDIT_F_QUEUE_MAPPING) |
c749cdda | 201 | params_new->queue_mapping = *queue_mapping; |
1c55d62e | 202 | if (flags & SKBEDIT_F_MARK) |
c749cdda | 203 | params_new->mark = *mark; |
ff202ee1 | 204 | if (flags & SKBEDIT_F_PTYPE) |
c749cdda | 205 | params_new->ptype = *ptype; |
4fe77d82 | 206 | /* default behaviour is to use all the bits */ |
c749cdda | 207 | params_new->mask = 0xffffffff; |
4fe77d82 | 208 | if (flags & SKBEDIT_F_MASK) |
c749cdda | 209 | params_new->mask = *mask; |
1c55d62e | 210 | |
6d7a8df6 | 211 | spin_lock_bh(&d->tcf_lock); |
ca9b0e27 | 212 | d->tcf_action = parm->action; |
6d7a8df6 VB |
213 | rcu_swap_protected(d->params, params_new, |
214 | lockdep_is_held(&d->tcf_lock)); | |
215 | spin_unlock_bh(&d->tcf_lock); | |
216 | if (params_new) | |
217 | kfree_rcu(params_new, rcu); | |
ca9b0e27 AD |
218 | |
219 | if (ret == ACT_P_CREATED) | |
65a206c0 | 220 | tcf_idr_insert(tn, *a); |
ca9b0e27 AD |
221 | return ret; |
222 | } | |
223 | ||
cc7ec456 ED |
224 | static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, |
225 | int bind, int ref) | |
ca9b0e27 AD |
226 | { |
227 | unsigned char *b = skb_tail_pointer(skb); | |
a85a970a | 228 | struct tcf_skbedit *d = to_skbedit(a); |
c749cdda | 229 | struct tcf_skbedit_params *params; |
1c40be12 ED |
230 | struct tc_skbedit opt = { |
231 | .index = d->tcf_index, | |
036bb443 VB |
232 | .refcnt = refcount_read(&d->tcf_refcnt) - ref, |
233 | .bindcnt = atomic_read(&d->tcf_bindcnt) - bind, | |
1c40be12 | 234 | }; |
e7e3728b | 235 | u64 pure_flags = 0; |
c749cdda DC |
236 | struct tcf_t t; |
237 | ||
6d7a8df6 VB |
238 | spin_lock_bh(&d->tcf_lock); |
239 | params = rcu_dereference_protected(d->params, | |
240 | lockdep_is_held(&d->tcf_lock)); | |
241 | opt.action = d->tcf_action; | |
ca9b0e27 | 242 | |
1b34ec43 DM |
243 | if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) |
244 | goto nla_put_failure; | |
c749cdda DC |
245 | if ((params->flags & SKBEDIT_F_PRIORITY) && |
246 | nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority)) | |
1b34ec43 | 247 | goto nla_put_failure; |
c749cdda DC |
248 | if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) && |
249 | nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping)) | |
1b34ec43 | 250 | goto nla_put_failure; |
c749cdda DC |
251 | if ((params->flags & SKBEDIT_F_MARK) && |
252 | nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark)) | |
1b34ec43 | 253 | goto nla_put_failure; |
c749cdda DC |
254 | if ((params->flags & SKBEDIT_F_PTYPE) && |
255 | nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype)) | |
ff202ee1 | 256 | goto nla_put_failure; |
c749cdda DC |
257 | if ((params->flags & SKBEDIT_F_MASK) && |
258 | nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask)) | |
4fe77d82 | 259 | goto nla_put_failure; |
c749cdda | 260 | if (params->flags & SKBEDIT_F_INHERITDSFIELD) |
e7e3728b QF |
261 | pure_flags |= SKBEDIT_F_INHERITDSFIELD; |
262 | if (pure_flags != 0 && | |
263 | nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags)) | |
264 | goto nla_put_failure; | |
48d8ee16 JHS |
265 | |
266 | tcf_tm_dump(&t, &d->tcf_tm); | |
9854518e | 267 | if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD)) |
1b34ec43 | 268 | goto nla_put_failure; |
6d7a8df6 VB |
269 | spin_unlock_bh(&d->tcf_lock); |
270 | ||
ca9b0e27 AD |
271 | return skb->len; |
272 | ||
273 | nla_put_failure: | |
6d7a8df6 | 274 | spin_unlock_bh(&d->tcf_lock); |
ca9b0e27 AD |
275 | nlmsg_trim(skb, b); |
276 | return -1; | |
277 | } | |
278 | ||
c749cdda DC |
279 | static void tcf_skbedit_cleanup(struct tc_action *a) |
280 | { | |
281 | struct tcf_skbedit *d = to_skbedit(a); | |
282 | struct tcf_skbedit_params *params; | |
283 | ||
284 | params = rcu_dereference_protected(d->params, 1); | |
285 | if (params) | |
286 | kfree_rcu(params, rcu); | |
287 | } | |
288 | ||
ddf97ccd WC |
289 | static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb, |
290 | struct netlink_callback *cb, int type, | |
41780105 AA |
291 | const struct tc_action_ops *ops, |
292 | struct netlink_ext_ack *extack) | |
ddf97ccd WC |
293 | { |
294 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
295 | ||
b3620145 | 296 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
ddf97ccd WC |
297 | } |
298 | ||
f061b48c | 299 | static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index) |
ddf97ccd WC |
300 | { |
301 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
302 | ||
65a206c0 | 303 | return tcf_idr_search(tn, a, index); |
ddf97ccd WC |
304 | } |
305 | ||
ca9b0e27 AD |
306 | static struct tc_action_ops act_skbedit_ops = { |
307 | .kind = "skbedit", | |
eddd2cf1 | 308 | .id = TCA_ID_SKBEDIT, |
ca9b0e27 | 309 | .owner = THIS_MODULE, |
45da1dac | 310 | .act = tcf_skbedit_act, |
ca9b0e27 | 311 | .dump = tcf_skbedit_dump, |
ca9b0e27 | 312 | .init = tcf_skbedit_init, |
c749cdda | 313 | .cleanup = tcf_skbedit_cleanup, |
ddf97ccd WC |
314 | .walk = tcf_skbedit_walker, |
315 | .lookup = tcf_skbedit_search, | |
a85a970a | 316 | .size = sizeof(struct tcf_skbedit), |
ddf97ccd WC |
317 | }; |
318 | ||
319 | static __net_init int skbedit_init_net(struct net *net) | |
320 | { | |
321 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
322 | ||
c7e460ce | 323 | return tc_action_net_init(tn, &act_skbedit_ops); |
ddf97ccd WC |
324 | } |
325 | ||
039af9c6 | 326 | static void __net_exit skbedit_exit_net(struct list_head *net_list) |
ddf97ccd | 327 | { |
039af9c6 | 328 | tc_action_net_exit(net_list, skbedit_net_id); |
ddf97ccd WC |
329 | } |
330 | ||
331 | static struct pernet_operations skbedit_net_ops = { | |
332 | .init = skbedit_init_net, | |
039af9c6 | 333 | .exit_batch = skbedit_exit_net, |
ddf97ccd WC |
334 | .id = &skbedit_net_id, |
335 | .size = sizeof(struct tc_action_net), | |
ca9b0e27 AD |
336 | }; |
337 | ||
338 | MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); | |
339 | MODULE_DESCRIPTION("SKB Editing"); | |
340 | MODULE_LICENSE("GPL"); | |
341 | ||
342 | static int __init skbedit_init_module(void) | |
343 | { | |
ddf97ccd | 344 | return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops); |
ca9b0e27 AD |
345 | } |
346 | ||
347 | static void __exit skbedit_cleanup_module(void) | |
348 | { | |
ddf97ccd | 349 | tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops); |
ca9b0e27 AD |
350 | } |
351 | ||
352 | module_init(skbedit_init_module); | |
353 | module_exit(skbedit_cleanup_module); |