[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / net / sched / pedit.c
blob678be6a645fbf41548d2c120ccd278ed0e03042a
1 /*
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/config.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/mm.h>
21 #include <linux/socket.h>
22 #include <linux/sockios.h>
23 #include <linux/in.h>
24 #include <linux/errno.h>
25 #include <linux/interrupt.h>
26 #include <linux/netdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/proc_fs.h>
32 #include <net/sock.h>
33 #include <net/pkt_sched.h>
34 #include <linux/tc_act/tc_pedit.h>
35 #include <net/tc_act/tc_pedit.h>
38 #define PEDIT_DEB 1
40 /* use generic hash table */
41 #define MY_TAB_SIZE 16
42 #define MY_TAB_MASK 15
43 static u32 idx_gen;
44 static struct tcf_pedit *tcf_pedit_ht[MY_TAB_SIZE];
45 static DEFINE_RWLOCK(pedit_lock);
47 #define tcf_st tcf_pedit
48 #define tc_st tc_pedit
49 #define tcf_t_lock pedit_lock
50 #define tcf_ht tcf_pedit_ht
52 #define CONFIG_NET_ACT_INIT 1
53 #include <net/pkt_act.h>
55 static int
56 tcf_pedit_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a,
57 int ovr, int bind)
59 struct rtattr *tb[TCA_PEDIT_MAX];
60 struct tc_pedit *parm;
61 int ret = 0;
62 struct tcf_pedit *p;
63 struct tc_pedit_key *keys = NULL;
64 int ksize;
66 if (rta == NULL || rtattr_parse_nested(tb, TCA_PEDIT_MAX, rta) < 0)
67 return -EINVAL;
69 if (tb[TCA_PEDIT_PARMS - 1] == NULL ||
70 RTA_PAYLOAD(tb[TCA_PEDIT_PARMS-1]) < sizeof(*parm))
71 return -EINVAL;
72 parm = RTA_DATA(tb[TCA_PEDIT_PARMS-1]);
73 ksize = parm->nkeys * sizeof(struct tc_pedit_key);
74 if (RTA_PAYLOAD(tb[TCA_PEDIT_PARMS-1]) < sizeof(*parm) + ksize)
75 return -EINVAL;
77 p = tcf_hash_check(parm->index, a, ovr, bind);
78 if (p == NULL) {
79 if (!parm->nkeys)
80 return -EINVAL;
81 p = tcf_hash_create(parm->index, est, a, sizeof(*p), ovr, bind);
82 if (p == NULL)
83 return -ENOMEM;
84 keys = kmalloc(ksize, GFP_KERNEL);
85 if (keys == NULL) {
86 kfree(p);
87 return -ENOMEM;
89 ret = ACT_P_CREATED;
90 } else {
91 if (!ovr) {
92 tcf_hash_release(p, bind);
93 return -EEXIST;
95 if (p->nkeys && p->nkeys != parm->nkeys) {
96 keys = kmalloc(ksize, GFP_KERNEL);
97 if (keys == NULL)
98 return -ENOMEM;
102 spin_lock_bh(&p->lock);
103 p->flags = parm->flags;
104 p->action = parm->action;
105 if (keys) {
106 kfree(p->keys);
107 p->keys = keys;
108 p->nkeys = parm->nkeys;
110 memcpy(p->keys, parm->keys, ksize);
111 spin_unlock_bh(&p->lock);
112 if (ret == ACT_P_CREATED)
113 tcf_hash_insert(p);
114 return ret;
117 static int
118 tcf_pedit_cleanup(struct tc_action *a, int bind)
120 struct tcf_pedit *p = PRIV(a, pedit);
122 if (p != NULL) {
123 struct tc_pedit_key *keys = p->keys;
124 if (tcf_hash_release(p, bind)) {
125 kfree(keys);
126 return 1;
129 return 0;
132 static int
133 tcf_pedit(struct sk_buff **pskb, struct tc_action *a)
135 struct tcf_pedit *p = PRIV(a, pedit);
136 struct sk_buff *skb = *pskb;
137 int i, munged = 0;
138 u8 *pptr;
140 if (!(skb->tc_verd & TC_OK2MUNGE)) {
141 /* should we set skb->cloned? */
142 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
143 return p->action;
147 pptr = skb->nh.raw;
149 spin_lock(&p->lock);
151 p->tm.lastuse = jiffies;
153 if (p->nkeys > 0) {
154 struct tc_pedit_key *tkey = p->keys;
156 for (i = p->nkeys; i > 0; i--, tkey++) {
157 u32 *ptr;
158 int offset = tkey->off;
160 if (tkey->offmask) {
161 if (skb->len > tkey->at) {
162 char *j = pptr + tkey->at;
163 offset += ((*j & tkey->offmask) >>
164 tkey->shift);
165 } else {
166 goto bad;
170 if (offset % 4) {
171 printk("offset must be on 32 bit boundaries\n");
172 goto bad;
174 if (skb->len < 0 || (offset > 0 && offset > skb->len)) {
175 printk("offset %d cant exceed pkt length %d\n",
176 offset, skb->len);
177 goto bad;
180 ptr = (u32 *)(pptr+offset);
181 /* just do it, baby */
182 *ptr = ((*ptr & tkey->mask) ^ tkey->val);
183 munged++;
186 if (munged)
187 skb->tc_verd = SET_TC_MUNGED(skb->tc_verd);
188 goto done;
189 } else {
190 printk("pedit BUG: index %d\n",p->index);
193 bad:
194 p->qstats.overlimits++;
195 done:
196 p->bstats.bytes += skb->len;
197 p->bstats.packets++;
198 spin_unlock(&p->lock);
199 return p->action;
202 static int
203 tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,int bind, int ref)
205 unsigned char *b = skb->tail;
206 struct tc_pedit *opt;
207 struct tcf_pedit *p = PRIV(a, pedit);
208 struct tcf_t t;
209 int s;
211 s = sizeof(*opt) + p->nkeys * sizeof(struct tc_pedit_key);
213 /* netlink spinlocks held above us - must use ATOMIC */
214 opt = kmalloc(s, GFP_ATOMIC);
215 if (opt == NULL)
216 return -ENOBUFS;
217 memset(opt, 0, s);
219 memcpy(opt->keys, p->keys, p->nkeys * sizeof(struct tc_pedit_key));
220 opt->index = p->index;
221 opt->nkeys = p->nkeys;
222 opt->flags = p->flags;
223 opt->action = p->action;
224 opt->refcnt = p->refcnt - ref;
225 opt->bindcnt = p->bindcnt - bind;
228 #ifdef PEDIT_DEB
230 /* Debug - get rid of later */
231 int i;
232 struct tc_pedit_key *key = opt->keys;
234 for (i=0; i<opt->nkeys; i++, key++) {
235 printk( "\n key #%d",i);
236 printk( " at %d: val %08x mask %08x",
237 (unsigned int)key->off,
238 (unsigned int)key->val,
239 (unsigned int)key->mask);
242 #endif
244 RTA_PUT(skb, TCA_PEDIT_PARMS, s, opt);
245 t.install = jiffies_to_clock_t(jiffies - p->tm.install);
246 t.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
247 t.expires = jiffies_to_clock_t(p->tm.expires);
248 RTA_PUT(skb, TCA_PEDIT_TM, sizeof(t), &t);
249 return skb->len;
251 rtattr_failure:
252 skb_trim(skb, b - skb->data);
253 return -1;
256 static
257 struct tc_action_ops act_pedit_ops = {
258 .kind = "pedit",
259 .type = TCA_ACT_PEDIT,
260 .capab = TCA_CAP_NONE,
261 .owner = THIS_MODULE,
262 .act = tcf_pedit,
263 .dump = tcf_pedit_dump,
264 .cleanup = tcf_pedit_cleanup,
265 .lookup = tcf_hash_search,
266 .init = tcf_pedit_init,
267 .walk = tcf_generic_walker
270 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
271 MODULE_DESCRIPTION("Generic Packet Editor actions");
272 MODULE_LICENSE("GPL");
274 static int __init
275 pedit_init_module(void)
277 return tcf_register_action(&act_pedit_ops);
280 static void __exit
281 pedit_cleanup_module(void)
283 tcf_unregister_action(&act_pedit_ops);
286 module_init(pedit_init_module);
287 module_exit(pedit_cleanup_module);