2 * net/sched/act_pedit.c Generic packet editor
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Jamal Hadi Salim (2002-4)
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>
27 #define PEDIT_TAB_MASK 15
29 static unsigned int pedit_net_id;
30 static struct tc_action_ops act_pedit_ops;
32 static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
33 [TCA_PEDIT_PARMS] = { .len = sizeof(struct tc_pedit) },
34 [TCA_PEDIT_KEYS_EX] = { .type = NLA_NESTED },
37 static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = {
38 [TCA_PEDIT_KEY_EX_HTYPE] = { .type = NLA_U16 },
39 [TCA_PEDIT_KEY_EX_CMD] = { .type = NLA_U16 },
42 static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
45 struct tcf_pedit_key_ex *keys_ex;
46 struct tcf_pedit_key_ex *k;
47 const struct nlattr *ka;
54 keys_ex = kcalloc(n, sizeof(*k), GFP_KERNEL);
56 return ERR_PTR(-ENOMEM);
60 nla_for_each_nested(ka, nla, rem) {
61 struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
69 if (nla_type(ka) != TCA_PEDIT_KEY_EX) {
74 err = nla_parse_nested(tb, TCA_PEDIT_KEY_EX_MAX, ka,
75 pedit_key_ex_policy, NULL);
79 if (!tb[TCA_PEDIT_KEY_EX_HTYPE] ||
80 !tb[TCA_PEDIT_KEY_EX_CMD]) {
85 k->htype = nla_get_u16(tb[TCA_PEDIT_KEY_EX_HTYPE]);
86 k->cmd = nla_get_u16(tb[TCA_PEDIT_KEY_EX_CMD]);
88 if (k->htype > TCA_PEDIT_HDR_TYPE_MAX ||
89 k->cmd > TCA_PEDIT_CMD_MAX) {
109 static int tcf_pedit_key_ex_dump(struct sk_buff *skb,
110 struct tcf_pedit_key_ex *keys_ex, int n)
112 struct nlattr *keys_start = nla_nest_start(skb, TCA_PEDIT_KEYS_EX);
115 struct nlattr *key_start;
117 key_start = nla_nest_start(skb, TCA_PEDIT_KEY_EX);
119 if (nla_put_u16(skb, TCA_PEDIT_KEY_EX_HTYPE, keys_ex->htype) ||
120 nla_put_u16(skb, TCA_PEDIT_KEY_EX_CMD, keys_ex->cmd)) {
121 nlmsg_trim(skb, keys_start);
125 nla_nest_end(skb, key_start);
130 nla_nest_end(skb, keys_start);
135 static int tcf_pedit_init(struct net *net, struct nlattr *nla,
136 struct nlattr *est, struct tc_action **a,
139 struct tc_action_net *tn = net_generic(net, pedit_net_id);
140 struct nlattr *tb[TCA_PEDIT_MAX + 1];
141 struct nlattr *pattr;
142 struct tc_pedit *parm;
145 struct tc_pedit_key *keys = NULL;
146 struct tcf_pedit_key_ex *keys_ex;
152 err = nla_parse_nested(tb, TCA_PEDIT_MAX, nla, pedit_policy, NULL);
156 pattr = tb[TCA_PEDIT_PARMS];
158 pattr = tb[TCA_PEDIT_PARMS_EX];
162 parm = nla_data(pattr);
163 ksize = parm->nkeys * sizeof(struct tc_pedit_key);
164 if (nla_len(pattr) < sizeof(*parm) + ksize)
167 keys_ex = tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
169 return PTR_ERR(keys_ex);
171 if (!tcf_hash_check(tn, parm->index, a, bind)) {
174 ret = tcf_hash_create(tn, parm->index, est, a,
175 &act_pedit_ops, bind, false);
179 keys = kmalloc(ksize, GFP_KERNEL);
181 tcf_hash_cleanup(*a, est);
189 tcf_hash_release(*a, bind);
193 if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) {
194 keys = kmalloc(ksize, GFP_KERNEL);
202 spin_lock_bh(&p->tcf_lock);
203 p->tcfp_flags = parm->flags;
204 p->tcf_action = parm->action;
208 p->tcfp_nkeys = parm->nkeys;
210 memcpy(p->tcfp_keys, parm->keys, ksize);
212 kfree(p->tcfp_keys_ex);
213 p->tcfp_keys_ex = keys_ex;
215 spin_unlock_bh(&p->tcf_lock);
216 if (ret == ACT_P_CREATED)
217 tcf_hash_insert(tn, *a);
221 static void tcf_pedit_cleanup(struct tc_action *a, int bind)
223 struct tcf_pedit *p = to_pedit(a);
224 struct tc_pedit_key *keys = p->tcfp_keys;
226 kfree(p->tcfp_keys_ex);
229 static bool offset_valid(struct sk_buff *skb, int offset)
231 if (offset > 0 && offset > skb->len)
234 if (offset < 0 && -offset > skb_headroom(skb))
240 static int pedit_skb_hdr_offset(struct sk_buff *skb,
241 enum pedit_header_type htype, int *hoffset)
246 case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
247 if (skb_mac_header_was_set(skb)) {
248 *hoffset = skb_mac_offset(skb);
252 case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
253 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
254 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
255 *hoffset = skb_network_offset(skb);
258 case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
259 case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
260 if (skb_transport_header_was_set(skb)) {
261 *hoffset = skb_transport_offset(skb);
273 static int tcf_pedit(struct sk_buff *skb, const struct tc_action *a,
274 struct tcf_result *res)
276 struct tcf_pedit *p = to_pedit(a);
279 if (skb_unclone(skb, GFP_ATOMIC))
280 return p->tcf_action;
282 spin_lock(&p->tcf_lock);
284 tcf_lastuse_update(&p->tcf_tm);
286 if (p->tcfp_nkeys > 0) {
287 struct tc_pedit_key *tkey = p->tcfp_keys;
288 struct tcf_pedit_key_ex *tkey_ex = p->tcfp_keys_ex;
289 enum pedit_header_type htype = TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
290 enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
292 for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
294 int offset = tkey->off;
300 htype = tkey_ex->htype;
306 rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
308 pr_info("tc filter pedit bad header type specified (0x%x)\n",
316 if (!offset_valid(skb, hoffset + tkey->at)) {
317 pr_info("tc filter pedit 'at' offset %d out of bounds\n",
321 d = skb_header_pointer(skb, hoffset + tkey->at, 1,
325 offset += (*d & tkey->offmask) >> tkey->shift;
329 pr_info("tc filter pedit"
330 " offset must be on 32 bit boundaries\n");
334 if (!offset_valid(skb, hoffset + offset)) {
335 pr_info("tc filter pedit offset %d out of bounds\n",
340 ptr = skb_header_pointer(skb, hoffset + offset, 4, &_data);
343 /* just do it, baby */
345 case TCA_PEDIT_KEY_EX_CMD_SET:
348 case TCA_PEDIT_KEY_EX_CMD_ADD:
349 val = (*ptr + tkey->val) & ~tkey->mask;
352 pr_info("tc filter pedit bad command (%d)\n",
357 *ptr = ((*ptr & tkey->mask) ^ val);
359 skb_store_bits(skb, hoffset + offset, ptr, 4);
364 WARN(1, "pedit BUG: index %d\n", p->tcf_index);
367 p->tcf_qstats.overlimits++;
369 bstats_update(&p->tcf_bstats, skb);
370 spin_unlock(&p->tcf_lock);
371 return p->tcf_action;
374 static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
377 unsigned char *b = skb_tail_pointer(skb);
378 struct tcf_pedit *p = to_pedit(a);
379 struct tc_pedit *opt;
383 s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
385 /* netlink spinlocks held above us - must use ATOMIC */
386 opt = kzalloc(s, GFP_ATOMIC);
390 memcpy(opt->keys, p->tcfp_keys,
391 p->tcfp_nkeys * sizeof(struct tc_pedit_key));
392 opt->index = p->tcf_index;
393 opt->nkeys = p->tcfp_nkeys;
394 opt->flags = p->tcfp_flags;
395 opt->action = p->tcf_action;
396 opt->refcnt = p->tcf_refcnt - ref;
397 opt->bindcnt = p->tcf_bindcnt - bind;
399 if (p->tcfp_keys_ex) {
400 tcf_pedit_key_ex_dump(skb, p->tcfp_keys_ex, p->tcfp_nkeys);
402 if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt))
403 goto nla_put_failure;
405 if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
406 goto nla_put_failure;
409 tcf_tm_dump(&t, &p->tcf_tm);
410 if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
411 goto nla_put_failure;
422 static int tcf_pedit_walker(struct net *net, struct sk_buff *skb,
423 struct netlink_callback *cb, int type,
424 const struct tc_action_ops *ops)
426 struct tc_action_net *tn = net_generic(net, pedit_net_id);
428 return tcf_generic_walker(tn, skb, cb, type, ops);
431 static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index)
433 struct tc_action_net *tn = net_generic(net, pedit_net_id);
435 return tcf_hash_search(tn, a, index);
438 static struct tc_action_ops act_pedit_ops = {
440 .type = TCA_ACT_PEDIT,
441 .owner = THIS_MODULE,
443 .dump = tcf_pedit_dump,
444 .cleanup = tcf_pedit_cleanup,
445 .init = tcf_pedit_init,
446 .walk = tcf_pedit_walker,
447 .lookup = tcf_pedit_search,
448 .size = sizeof(struct tcf_pedit),
451 static __net_init int pedit_init_net(struct net *net)
453 struct tc_action_net *tn = net_generic(net, pedit_net_id);
455 return tc_action_net_init(tn, &act_pedit_ops, PEDIT_TAB_MASK);
458 static void __net_exit pedit_exit_net(struct net *net)
460 struct tc_action_net *tn = net_generic(net, pedit_net_id);
462 tc_action_net_exit(tn);
465 static struct pernet_operations pedit_net_ops = {
466 .init = pedit_init_net,
467 .exit = pedit_exit_net,
469 .size = sizeof(struct tc_action_net),
472 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
473 MODULE_DESCRIPTION("Generic Packet Editor actions");
474 MODULE_LICENSE("GPL");
476 static int __init pedit_init_module(void)
478 return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
481 static void __exit pedit_cleanup_module(void)
483 tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
486 module_init(pedit_init_module);
487 module_exit(pedit_cleanup_module);