1
2
3
4
5
6
7
8
9
10
11
12#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/string.h>
15#include <linux/errno.h>
16#include <linux/skbuff.h>
17#include <linux/rtnetlink.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <net/netlink.h>
22#include <net/pkt_sched.h>
23#include <linux/tc_act/tc_pedit.h>
24#include <net/tc_act/tc_pedit.h>
25#include <uapi/linux/tc_act/tc_pedit.h>
26
27static int pedit_net_id;
28static struct tc_action_ops act_pedit_ops;
29
30static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
31 [TCA_PEDIT_PARMS] = { .len = sizeof(struct tc_pedit) },
32 [TCA_PEDIT_KEYS_EX] = { .type = NLA_NESTED },
33};
34
35static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = {
36 [TCA_PEDIT_KEY_EX_HTYPE] = { .type = NLA_U16 },
37 [TCA_PEDIT_KEY_EX_CMD] = { .type = NLA_U16 },
38};
39
40static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
41 u8 n)
42{
43 struct tcf_pedit_key_ex *keys_ex;
44 struct tcf_pedit_key_ex *k;
45 const struct nlattr *ka;
46 int err = -EINVAL;
47 int rem;
48
49 if (!nla || !n)
50 return NULL;
51
52 keys_ex = kcalloc(n, sizeof(*k), GFP_KERNEL);
53 if (!keys_ex)
54 return ERR_PTR(-ENOMEM);
55
56 k = keys_ex;
57
58 nla_for_each_nested(ka, nla, rem) {
59 struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
60
61 if (!n) {
62 err = -EINVAL;
63 goto err_out;
64 }
65 n--;
66
67 if (nla_type(ka) != TCA_PEDIT_KEY_EX) {
68 err = -EINVAL;
69 goto err_out;
70 }
71
72 err = nla_parse_nested(tb, TCA_PEDIT_KEY_EX_MAX, ka,
73 pedit_key_ex_policy);
74 if (err)
75 goto err_out;
76
77 if (!tb[TCA_PEDIT_KEY_EX_HTYPE] ||
78 !tb[TCA_PEDIT_KEY_EX_CMD]) {
79 err = -EINVAL;
80 goto err_out;
81 }
82
83 k->htype = nla_get_u16(tb[TCA_PEDIT_KEY_EX_HTYPE]);
84 k->cmd = nla_get_u16(tb[TCA_PEDIT_KEY_EX_CMD]);
85
86 if (k->htype > TCA_PEDIT_HDR_TYPE_MAX ||
87 k->cmd > TCA_PEDIT_CMD_MAX) {
88 err = -EINVAL;
89 goto err_out;
90 }
91
92 k++;
93 }
94
95 if (n) {
96 err = -EINVAL;
97 goto err_out;
98 }
99
100 return keys_ex;
101
102err_out:
103 kfree(keys_ex);
104 return ERR_PTR(err);
105}
106
107static int tcf_pedit_key_ex_dump(struct sk_buff *skb,
108 struct tcf_pedit_key_ex *keys_ex, int n)
109{
110 struct nlattr *keys_start = nla_nest_start(skb, TCA_PEDIT_KEYS_EX);
111
112 if (!keys_start)
113 goto nla_failure;
114 for (; n > 0; n--) {
115 struct nlattr *key_start;
116
117 key_start = nla_nest_start(skb, TCA_PEDIT_KEY_EX);
118 if (!key_start)
119 goto nla_failure;
120
121 if (nla_put_u16(skb, TCA_PEDIT_KEY_EX_HTYPE, keys_ex->htype) ||
122 nla_put_u16(skb, TCA_PEDIT_KEY_EX_CMD, keys_ex->cmd))
123 goto nla_failure;
124
125 nla_nest_end(skb, key_start);
126
127 keys_ex++;
128 }
129
130 nla_nest_end(skb, keys_start);
131
132 return 0;
133nla_failure:
134 nla_nest_cancel(skb, keys_start);
135 return -EINVAL;
136}
137
138static int tcf_pedit_init(struct net *net, struct nlattr *nla,
139 struct nlattr *est, struct tc_action **a,
140 int ovr, int bind)
141{
142 struct tc_action_net *tn = net_generic(net, pedit_net_id);
143 struct nlattr *tb[TCA_PEDIT_MAX + 1];
144 struct nlattr *pattr;
145 struct tc_pedit *parm;
146 int ret = 0, err;
147 struct tcf_pedit *p;
148 struct tc_pedit_key *keys = NULL;
149 struct tcf_pedit_key_ex *keys_ex;
150 int ksize;
151
152 if (nla == NULL)
153 return -EINVAL;
154
155 err = nla_parse_nested(tb, TCA_PEDIT_MAX, nla, pedit_policy);
156 if (err < 0)
157 return err;
158
159 pattr = tb[TCA_PEDIT_PARMS];
160 if (!pattr)
161 pattr = tb[TCA_PEDIT_PARMS_EX];
162 if (!pattr)
163 return -EINVAL;
164
165 parm = nla_data(pattr);
166 ksize = parm->nkeys * sizeof(struct tc_pedit_key);
167 if (nla_len(pattr) < sizeof(*parm) + ksize)
168 return -EINVAL;
169
170 keys_ex = tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
171 if (IS_ERR(keys_ex))
172 return PTR_ERR(keys_ex);
173
174 if (!tcf_idr_check(tn, parm->index, a, bind)) {
175 if (!parm->nkeys) {
176 ret = -EINVAL;
177 goto out_free;
178 }
179 ret = tcf_idr_create(tn, parm->index, est, a,
180 &act_pedit_ops, bind, false);
181 if (ret)
182 goto out_free;
183 p = to_pedit(*a);
184 keys = kmalloc(ksize, GFP_KERNEL);
185 if (keys == NULL) {
186 tcf_idr_release(*a, bind);
187 ret = -ENOMEM;
188 goto out_free;
189 }
190 ret = ACT_P_CREATED;
191 } else {
192 if (bind)
193 goto out_free;
194 if (!ovr) {
195 tcf_idr_release(*a, bind);
196 ret = -EEXIST;
197 goto out_free;
198 }
199 p = to_pedit(*a);
200 if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) {
201 keys = kmalloc(ksize, GFP_KERNEL);
202 if (!keys) {
203 ret = -ENOMEM;
204 goto out_free;
205 }
206 }
207 }
208
209 spin_lock_bh(&p->tcf_lock);
210 p->tcfp_flags = parm->flags;
211 p->tcf_action = parm->action;
212 if (keys) {
213 kfree(p->tcfp_keys);
214 p->tcfp_keys = keys;
215 p->tcfp_nkeys = parm->nkeys;
216 }
217 memcpy(p->tcfp_keys, parm->keys, ksize);
218
219 kfree(p->tcfp_keys_ex);
220 p->tcfp_keys_ex = keys_ex;
221
222 spin_unlock_bh(&p->tcf_lock);
223 if (ret == ACT_P_CREATED)
224 tcf_idr_insert(tn, *a);
225 return ret;
226out_free:
227 kfree(keys_ex);
228 return ret;
229
230}
231
232static void tcf_pedit_cleanup(struct tc_action *a, int bind)
233{
234 struct tcf_pedit *p = to_pedit(a);
235 struct tc_pedit_key *keys = p->tcfp_keys;
236 kfree(keys);
237 kfree(p->tcfp_keys_ex);
238}
239
240static bool offset_valid(struct sk_buff *skb, int offset)
241{
242 if (offset > 0 && offset > skb->len)
243 return false;
244
245 if (offset < 0 && -offset > skb_headroom(skb))
246 return false;
247
248 return true;
249}
250
251static int pedit_skb_hdr_offset(struct sk_buff *skb,
252 enum pedit_header_type htype, int *hoffset)
253{
254 int ret = -EINVAL;
255
256 switch (htype) {
257 case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
258 if (skb_mac_header_was_set(skb)) {
259 *hoffset = skb_mac_offset(skb);
260 ret = 0;
261 }
262 break;
263 case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
264 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
265 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
266 *hoffset = skb_network_offset(skb);
267 ret = 0;
268 break;
269 case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
270 case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
271 if (skb_transport_header_was_set(skb)) {
272 *hoffset = skb_transport_offset(skb);
273 ret = 0;
274 }
275 break;
276 default:
277 ret = -EINVAL;
278 break;
279 };
280
281 return ret;
282}
283
284static int tcf_pedit(struct sk_buff *skb, const struct tc_action *a,
285 struct tcf_result *res)
286{
287 struct tcf_pedit *p = to_pedit(a);
288 int i;
289
290 if (skb_unclone(skb, GFP_ATOMIC))
291 return p->tcf_action;
292
293 spin_lock(&p->tcf_lock);
294
295 tcf_lastuse_update(&p->tcf_tm);
296
297 if (p->tcfp_nkeys > 0) {
298 struct tc_pedit_key *tkey = p->tcfp_keys;
299 struct tcf_pedit_key_ex *tkey_ex = p->tcfp_keys_ex;
300 enum pedit_header_type htype = TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
301 enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
302
303 for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
304 u32 *ptr, _data;
305 int offset = tkey->off;
306 int hoffset;
307 u32 val;
308 int rc;
309
310 if (tkey_ex) {
311 htype = tkey_ex->htype;
312 cmd = tkey_ex->cmd;
313
314 tkey_ex++;
315 }
316
317 rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
318 if (rc) {
319 pr_info("tc filter pedit bad header type specified (0x%x)\n",
320 htype);
321 goto bad;
322 }
323
324 if (tkey->offmask) {
325 char *d, _d;
326
327 if (!offset_valid(skb, hoffset + tkey->at)) {
328 pr_info("tc filter pedit 'at' offset %d out of bounds\n",
329 hoffset + tkey->at);
330 goto bad;
331 }
332 d = skb_header_pointer(skb, hoffset + tkey->at, 1,
333 &_d);
334 if (!d)
335 goto bad;
336 offset += (*d & tkey->offmask) >> tkey->shift;
337 }
338
339 if (offset % 4) {
340 pr_info("tc filter pedit"
341 " offset must be on 32 bit boundaries\n");
342 goto bad;
343 }
344
345 if (!offset_valid(skb, hoffset + offset)) {
346 pr_info("tc filter pedit offset %d out of bounds\n",
347 hoffset + offset);
348 goto bad;
349 }
350
351 ptr = skb_header_pointer(skb, hoffset + offset, 4, &_data);
352 if (!ptr)
353 goto bad;
354
355 switch (cmd) {
356 case TCA_PEDIT_KEY_EX_CMD_SET:
357 val = tkey->val;
358 break;
359 case TCA_PEDIT_KEY_EX_CMD_ADD:
360 val = (*ptr + tkey->val) & ~tkey->mask;
361 break;
362 default:
363 pr_info("tc filter pedit bad command (%d)\n",
364 cmd);
365 goto bad;
366 }
367
368 *ptr = ((*ptr & tkey->mask) ^ val);
369 if (ptr == &_data)
370 skb_store_bits(skb, hoffset + offset, ptr, 4);
371 }
372
373 goto done;
374 } else
375 WARN(1, "pedit BUG: index %d\n", p->tcf_index);
376
377bad:
378 p->tcf_qstats.overlimits++;
379done:
380 bstats_update(&p->tcf_bstats, skb);
381 spin_unlock(&p->tcf_lock);
382 return p->tcf_action;
383}
384
385static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
386 int bind, int ref)
387{
388 unsigned char *b = skb_tail_pointer(skb);
389 struct tcf_pedit *p = to_pedit(a);
390 struct tc_pedit *opt;
391 struct tcf_t t;
392 int s;
393
394 s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
395
396
397 opt = kzalloc(s, GFP_ATOMIC);
398 if (unlikely(!opt))
399 return -ENOBUFS;
400
401 memcpy(opt->keys, p->tcfp_keys,
402 p->tcfp_nkeys * sizeof(struct tc_pedit_key));
403 opt->index = p->tcf_index;
404 opt->nkeys = p->tcfp_nkeys;
405 opt->flags = p->tcfp_flags;
406 opt->action = p->tcf_action;
407 opt->refcnt = p->tcf_refcnt - ref;
408 opt->bindcnt = p->tcf_bindcnt - bind;
409
410 if (p->tcfp_keys_ex) {
411 if (tcf_pedit_key_ex_dump(skb,
412 p->tcfp_keys_ex,
413 p->tcfp_nkeys))
414 goto nla_put_failure;
415
416 if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt))
417 goto nla_put_failure;
418 } else {
419 if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
420 goto nla_put_failure;
421 }
422
423 tcf_tm_dump(&t, &p->tcf_tm);
424 if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
425 goto nla_put_failure;
426
427 kfree(opt);
428 return skb->len;
429
430nla_put_failure:
431 nlmsg_trim(skb, b);
432 kfree(opt);
433 return -1;
434}
435
436static int tcf_pedit_walker(struct net *net, struct sk_buff *skb,
437 struct netlink_callback *cb, int type,
438 const struct tc_action_ops *ops)
439{
440 struct tc_action_net *tn = net_generic(net, pedit_net_id);
441
442 return tcf_generic_walker(tn, skb, cb, type, ops);
443}
444
445static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index)
446{
447 struct tc_action_net *tn = net_generic(net, pedit_net_id);
448
449 return tcf_idr_search(tn, a, index);
450}
451
452static struct tc_action_ops act_pedit_ops = {
453 .kind = "pedit",
454 .type = TCA_ACT_PEDIT,
455 .owner = THIS_MODULE,
456 .act = tcf_pedit,
457 .dump = tcf_pedit_dump,
458 .cleanup = tcf_pedit_cleanup,
459 .init = tcf_pedit_init,
460 .walk = tcf_pedit_walker,
461 .lookup = tcf_pedit_search,
462 .size = sizeof(struct tcf_pedit),
463};
464
465static __net_init int pedit_init_net(struct net *net)
466{
467 struct tc_action_net *tn = net_generic(net, pedit_net_id);
468
469 return tc_action_net_init(tn, &act_pedit_ops);
470}
471
472static void __net_exit pedit_exit_net(struct net *net)
473{
474 struct tc_action_net *tn = net_generic(net, pedit_net_id);
475
476 tc_action_net_exit(tn);
477}
478
479static struct pernet_operations pedit_net_ops = {
480 .init = pedit_init_net,
481 .exit = pedit_exit_net,
482 .id = &pedit_net_id,
483 .size = sizeof(struct tc_action_net),
484};
485
486MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
487MODULE_DESCRIPTION("Generic Packet Editor actions");
488MODULE_LICENSE("GPL");
489
490static int __init pedit_init_module(void)
491{
492 return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
493}
494
495static void __exit pedit_cleanup_module(void)
496{
497 tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
498}
499
500module_init(pedit_init_module);
501module_exit(pedit_cleanup_module);
502
503