2 * net/sched/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 <asm/uaccess.h>
13 #include <asm/system.h>
14 #include <asm/bitops.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/string.h>
20 #include <linux/socket.h>
21 #include <linux/sockios.h>
23 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <linux/netdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/proc_fs.h>
32 #include <net/pkt_sched.h>
33 #include <linux/tc_act/tc_pedit.h>
34 #include <net/tc_act/tc_pedit.h>
39 /* use generic hash table */
40 #define MY_TAB_SIZE 16
41 #define MY_TAB_MASK 15
43 static struct tcf_pedit *tcf_pedit_ht[MY_TAB_SIZE];
44 static DEFINE_RWLOCK(pedit_lock);
46 #define tcf_st tcf_pedit
47 #define tc_st tc_pedit
48 #define tcf_t_lock pedit_lock
49 #define tcf_ht tcf_pedit_ht
51 #define CONFIG_NET_ACT_INIT 1
52 #include <net/pkt_act.h>
55 tcf_pedit_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a,
58 struct rtattr *tb[TCA_PEDIT_MAX];
59 struct tc_pedit *parm;
62 struct tc_pedit_key *keys = NULL;
65 if (rta == NULL || rtattr_parse_nested(tb, TCA_PEDIT_MAX, rta) < 0)
68 if (tb[TCA_PEDIT_PARMS - 1] == NULL ||
69 RTA_PAYLOAD(tb[TCA_PEDIT_PARMS-1]) < sizeof(*parm))
71 parm = RTA_DATA(tb[TCA_PEDIT_PARMS-1]);
72 ksize = parm->nkeys * sizeof(struct tc_pedit_key);
73 if (RTA_PAYLOAD(tb[TCA_PEDIT_PARMS-1]) < sizeof(*parm) + ksize)
76 p = tcf_hash_check(parm->index, a, ovr, bind);
80 p = tcf_hash_create(parm->index, est, a, sizeof(*p), ovr, bind);
83 keys = kmalloc(ksize, GFP_KERNEL);
91 tcf_hash_release(p, bind);
94 if (p->nkeys && p->nkeys != parm->nkeys) {
95 keys = kmalloc(ksize, GFP_KERNEL);
101 spin_lock_bh(&p->lock);
102 p->flags = parm->flags;
103 p->action = parm->action;
107 p->nkeys = parm->nkeys;
109 memcpy(p->keys, parm->keys, ksize);
110 spin_unlock_bh(&p->lock);
111 if (ret == ACT_P_CREATED)
117 tcf_pedit_cleanup(struct tc_action *a, int bind)
119 struct tcf_pedit *p = PRIV(a, pedit);
122 struct tc_pedit_key *keys = p->keys;
123 if (tcf_hash_release(p, bind)) {
132 tcf_pedit(struct sk_buff *skb, struct tc_action *a, struct tcf_result *res)
134 struct tcf_pedit *p = PRIV(a, pedit);
138 if (!(skb->tc_verd & TC_OK2MUNGE)) {
139 /* should we set skb->cloned? */
140 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
149 p->tm.lastuse = jiffies;
152 struct tc_pedit_key *tkey = p->keys;
154 for (i = p->nkeys; i > 0; i--, tkey++) {
156 int offset = tkey->off;
159 if (skb->len > tkey->at) {
160 char *j = pptr + tkey->at;
161 offset += ((*j & tkey->offmask) >>
169 printk("offset must be on 32 bit boundaries\n");
172 if (skb->len < 0 || (offset > 0 && offset > skb->len)) {
173 printk("offset %d cant exceed pkt length %d\n",
178 ptr = (u32 *)(pptr+offset);
179 /* just do it, baby */
180 *ptr = ((*ptr & tkey->mask) ^ tkey->val);
185 skb->tc_verd = SET_TC_MUNGED(skb->tc_verd);
188 printk("pedit BUG: index %d\n",p->index);
192 p->qstats.overlimits++;
194 p->bstats.bytes += skb->len;
196 spin_unlock(&p->lock);
201 tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,int bind, int ref)
203 unsigned char *b = skb->tail;
204 struct tc_pedit *opt;
205 struct tcf_pedit *p = PRIV(a, pedit);
209 s = sizeof(*opt) + p->nkeys * sizeof(struct tc_pedit_key);
211 /* netlink spinlocks held above us - must use ATOMIC */
212 opt = kzalloc(s, GFP_ATOMIC);
216 memcpy(opt->keys, p->keys, p->nkeys * sizeof(struct tc_pedit_key));
217 opt->index = p->index;
218 opt->nkeys = p->nkeys;
219 opt->flags = p->flags;
220 opt->action = p->action;
221 opt->refcnt = p->refcnt - ref;
222 opt->bindcnt = p->bindcnt - bind;
227 /* Debug - get rid of later */
229 struct tc_pedit_key *key = opt->keys;
231 for (i=0; i<opt->nkeys; i++, key++) {
232 printk( "\n key #%d",i);
233 printk( " at %d: val %08x mask %08x",
234 (unsigned int)key->off,
235 (unsigned int)key->val,
236 (unsigned int)key->mask);
241 RTA_PUT(skb, TCA_PEDIT_PARMS, s, opt);
242 t.install = jiffies_to_clock_t(jiffies - p->tm.install);
243 t.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
244 t.expires = jiffies_to_clock_t(p->tm.expires);
245 RTA_PUT(skb, TCA_PEDIT_TM, sizeof(t), &t);
250 skb_trim(skb, b - skb->data);
256 struct tc_action_ops act_pedit_ops = {
258 .type = TCA_ACT_PEDIT,
259 .capab = TCA_CAP_NONE,
260 .owner = THIS_MODULE,
262 .dump = tcf_pedit_dump,
263 .cleanup = tcf_pedit_cleanup,
264 .lookup = tcf_hash_search,
265 .init = tcf_pedit_init,
266 .walk = tcf_generic_walker
269 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
270 MODULE_DESCRIPTION("Generic Packet Editor actions");
271 MODULE_LICENSE("GPL");
274 pedit_init_module(void)
276 return tcf_register_action(&act_pedit_ops);
280 pedit_cleanup_module(void)
282 tcf_unregister_action(&act_pedit_ops);
285 module_init(pedit_init_module);
286 module_exit(pedit_cleanup_module);