This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / net / sched / pedit.c
1 /*
2  * net/sched/pedit.c    Generic packet editor
3  *
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.
8  *
9  * Authors:     Jamal Hadi Salim (2002-4)
10  */
11
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>
36
37
38 #define PEDIT_DEB 1
39
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 rwlock_t pedit_lock = RW_LOCK_UNLOCKED;
46
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
51
52 #define CONFIG_NET_ACT_INIT 1
53 #include <net/pkt_act.h>
54
55
56 static int
57 tcf_pedit_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a,int ovr, int bind)
58 {
59         struct rtattr *tb[TCA_PEDIT_MAX];
60         struct tc_pedit *parm;
61         int size = 0;
62         int ret = 0;
63         struct tcf_pedit *p = NULL;
64
65         if (rtattr_parse(tb, TCA_PEDIT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)) < 0)
66                 return -1;
67
68         if (NULL == a || NULL == tb[TCA_PEDIT_PARMS - 1]) {
69                 printk("BUG: tcf_pedit_init called with NULL params\n");
70                 return -1;
71         }
72
73         parm = RTA_DATA(tb[TCA_PEDIT_PARMS - 1]);
74
75         p = tcf_hash_check(parm, a, ovr, bind);
76
77         if (NULL == p) { /* new */
78
79                 if (!parm->nkeys)
80                         return -1;
81
82                 size = sizeof (*p)+ (parm->nkeys*sizeof(struct tc_pedit_key));
83
84                 p = tcf_hash_create(parm,est,a,size,ovr,bind);
85
86                 if (NULL == p)
87                         return -1;
88                 ret = 1;
89                 goto override;
90         } 
91
92         if (ovr) {
93 override:
94                 p->flags = parm->flags;
95                 p->nkeys = parm->nkeys;
96                 p->action = parm->action;
97                 memcpy(p->keys,parm->keys,parm->nkeys*(sizeof(struct tc_pedit_key)));
98         }
99
100         return ret;
101 }
102
103 static int
104 tcf_pedit_cleanup(struct tc_action *a, int bind)
105 {
106         struct tcf_pedit *p;
107         p = PRIV(a,pedit);
108         if (NULL != p)
109                 return  tcf_hash_release(p, bind);
110         return 0;
111 }
112
113 /*
114 **
115 */
116 static int
117 tcf_pedit(struct sk_buff **pskb, struct tc_action *a)
118 {
119         struct tcf_pedit *p;
120         struct sk_buff *skb = *pskb;
121         int i, munged = 0;
122         u8 *pptr;
123
124         p = PRIV(a,pedit);
125
126         if (NULL == p) {
127                 printk("BUG: tcf_pedit called with NULL params\n");
128                 return -1; /* change to something symbolic */
129         }
130
131         if (!(skb->tc_verd & TC_OK2MUNGE)) {
132                 /* should we set skb->cloned? */
133                 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
134                         return p->action;
135                 }
136         }
137
138         pptr = skb->nh.raw;
139
140         spin_lock(&p->lock);
141
142         p->tm.lastuse = jiffies;
143
144         if (0 < p->nkeys) {
145                 struct tc_pedit_key *tkey = p->keys;
146
147                 for (i = p->nkeys; i > 0; i--, tkey++) {
148                         u32 *ptr ;
149
150                         int offset = tkey->off;
151                         if (tkey->offmask) {
152                                 if (skb->len > tkey->at) {
153                                          char *j = pptr+tkey->at;
154                                          offset +=((*j&tkey->offmask)>>tkey->shift);
155                                 } else {
156                                         goto bad;
157                                 }
158                         }
159
160                         if (offset % 4) {
161                                 printk("offset must be on 32 bit boundaries\n");
162                                 goto bad;
163                         }
164
165                         if (skb->len < 0 || (offset > 0 && offset > skb->len)) {
166                                 printk("offset %d cant exceed pkt length %d\n",
167                                                 offset, skb->len);
168                                 goto bad;
169                         }
170
171
172                         ptr = (u32 *)(pptr+offset);
173                         /* just do it, baby */
174                         *ptr = ((*ptr & tkey->mask) ^ tkey->val);
175                         munged++;
176                 }
177                 
178                 if (munged)
179                         skb->tc_verd = SET_TC_MUNGED(skb->tc_verd);
180                 goto done;
181         } else {
182                 printk("pedit BUG: index %d\n",p->index);
183         }
184
185 bad:
186         p->qstats.overlimits++;
187 done:
188         p->bstats.bytes += skb->len;
189         p->bstats.packets++;
190         spin_unlock(&p->lock);
191         return p->action;
192 }
193
194 static int
195 tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,int bind, int ref)
196 {
197         unsigned char *b = skb->tail;
198         struct tc_pedit *opt;
199         struct tcf_pedit *p;
200         struct tcf_t t;
201         int s; 
202                 
203
204         p = PRIV(a,pedit);
205
206         if (NULL == p) {
207                 printk("BUG: tcf_pedit_dump called with NULL params\n");
208                 goto rtattr_failure;
209         }
210
211         s = sizeof (*opt)+(p->nkeys*sizeof(struct tc_pedit_key));
212
213         /* netlink spinlocks held above us - must use ATOMIC
214          * */
215         opt = kmalloc(s, GFP_ATOMIC);
216         if (opt == NULL)
217                 return -ENOBUFS;
218
219         memset(opt, 0, s);
220
221         memcpy(opt->keys,p->keys,p->nkeys*(sizeof(struct tc_pedit_key)));
222         opt->index = p->index;
223         opt->nkeys = p->nkeys;
224         opt->flags = p->flags;
225         opt->action = p->action;
226         opt->refcnt = p->refcnt - ref;
227         opt->bindcnt = p->bindcnt - bind;
228
229
230 #ifdef PEDIT_DEB
231         {                
232                 /* Debug - get rid of later */
233                 int i;
234                 struct tc_pedit_key *key = opt->keys;
235
236                 for (i=0; i<opt->nkeys; i++, key++) {
237                         printk( "\n key #%d",i);
238                         printk( "  at %d: val %08x mask %08x",
239                         (unsigned int)key->off,
240                         (unsigned int)key->val,
241                         (unsigned int)key->mask);
242                                                                                                 }
243                                                                                         }
244 #endif
245
246         RTA_PUT(skb, TCA_PEDIT_PARMS, s, opt);
247         t.install = jiffies_to_clock_t(jiffies - p->tm.install);
248         t.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
249         t.expires = jiffies_to_clock_t(p->tm.expires);
250         RTA_PUT(skb, TCA_PEDIT_TM, sizeof (t), &t);
251         return skb->len;
252
253 rtattr_failure:
254         skb_trim(skb, b - skb->data);
255         return -1;
256 }
257
258 static
259 struct tc_action_ops act_pedit_ops = {
260         .kind           =       "pedit",
261         .type           =       TCA_ACT_PEDIT,
262         .capab          =       TCA_CAP_NONE,
263         .owner          =       THIS_MODULE,
264         .act            =       tcf_pedit,
265         .dump           =       tcf_pedit_dump,
266         .cleanup        =       tcf_pedit_cleanup,
267         .lookup         =       tcf_hash_search,
268         .init           =       tcf_pedit_init,
269         .walk           =       tcf_generic_walker
270 };
271
272 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
273 MODULE_DESCRIPTION("Generic Packet Editor actions");
274 MODULE_LICENSE("GPL");
275
276 static int __init
277 pedit_init_module(void)
278 {
279         return tcf_register_action(&act_pedit_ops);
280 }
281
282 static void __exit
283 pedit_cleanup_module(void)
284 {
285         tcf_unregister_action(&act_pedit_ops);
286 }
287
288 module_init(pedit_init_module);
289 module_exit(pedit_cleanup_module);
290