Merge branch 'master' into upstream-fixes
[deliverable/linux.git] / net / sched / act_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/types.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/socket.h>
21 #include <linux/sockios.h>
22 #include <linux/in.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>
31 #include <net/sock.h>
32 #include <net/pkt_sched.h>
33 #include <linux/tc_act/tc_pedit.h>
34 #include <net/tc_act/tc_pedit.h>
35
36
37 #define PEDIT_DEB 1
38
39 /* use generic hash table */
40 #define MY_TAB_SIZE 16
41 #define MY_TAB_MASK 15
42 static u32 idx_gen;
43 static struct tcf_pedit *tcf_pedit_ht[MY_TAB_SIZE];
44 static DEFINE_RWLOCK(pedit_lock);
45
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
50
51 #define CONFIG_NET_ACT_INIT 1
52 #include <net/pkt_act.h>
53
54 static int
55 tcf_pedit_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a,
56 int ovr, int bind)
57 {
58 struct rtattr *tb[TCA_PEDIT_MAX];
59 struct tc_pedit *parm;
60 int ret = 0;
61 struct tcf_pedit *p;
62 struct tc_pedit_key *keys = NULL;
63 int ksize;
64
65 if (rta == NULL || rtattr_parse_nested(tb, TCA_PEDIT_MAX, rta) < 0)
66 return -EINVAL;
67
68 if (tb[TCA_PEDIT_PARMS - 1] == NULL ||
69 RTA_PAYLOAD(tb[TCA_PEDIT_PARMS-1]) < sizeof(*parm))
70 return -EINVAL;
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)
74 return -EINVAL;
75
76 p = tcf_hash_check(parm->index, a, ovr, bind);
77 if (p == NULL) {
78 if (!parm->nkeys)
79 return -EINVAL;
80 p = tcf_hash_create(parm->index, est, a, sizeof(*p), ovr, bind);
81 if (p == NULL)
82 return -ENOMEM;
83 keys = kmalloc(ksize, GFP_KERNEL);
84 if (keys == NULL) {
85 kfree(p);
86 return -ENOMEM;
87 }
88 ret = ACT_P_CREATED;
89 } else {
90 if (!ovr) {
91 tcf_hash_release(p, bind);
92 return -EEXIST;
93 }
94 if (p->nkeys && p->nkeys != parm->nkeys) {
95 keys = kmalloc(ksize, GFP_KERNEL);
96 if (keys == NULL)
97 return -ENOMEM;
98 }
99 }
100
101 spin_lock_bh(&p->lock);
102 p->flags = parm->flags;
103 p->action = parm->action;
104 if (keys) {
105 kfree(p->keys);
106 p->keys = keys;
107 p->nkeys = parm->nkeys;
108 }
109 memcpy(p->keys, parm->keys, ksize);
110 spin_unlock_bh(&p->lock);
111 if (ret == ACT_P_CREATED)
112 tcf_hash_insert(p);
113 return ret;
114 }
115
116 static int
117 tcf_pedit_cleanup(struct tc_action *a, int bind)
118 {
119 struct tcf_pedit *p = PRIV(a, pedit);
120
121 if (p != NULL) {
122 struct tc_pedit_key *keys = p->keys;
123 if (tcf_hash_release(p, bind)) {
124 kfree(keys);
125 return 1;
126 }
127 }
128 return 0;
129 }
130
131 static int
132 tcf_pedit(struct sk_buff *skb, struct tc_action *a, struct tcf_result *res)
133 {
134 struct tcf_pedit *p = PRIV(a, pedit);
135 int i, munged = 0;
136 u8 *pptr;
137
138 if (!(skb->tc_verd & TC_OK2MUNGE)) {
139 /* should we set skb->cloned? */
140 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
141 return p->action;
142 }
143 }
144
145 pptr = skb->nh.raw;
146
147 spin_lock(&p->lock);
148
149 p->tm.lastuse = jiffies;
150
151 if (p->nkeys > 0) {
152 struct tc_pedit_key *tkey = p->keys;
153
154 for (i = p->nkeys; i > 0; i--, tkey++) {
155 u32 *ptr;
156 int offset = tkey->off;
157
158 if (tkey->offmask) {
159 if (skb->len > tkey->at) {
160 char *j = pptr + tkey->at;
161 offset += ((*j & tkey->offmask) >>
162 tkey->shift);
163 } else {
164 goto bad;
165 }
166 }
167
168 if (offset % 4) {
169 printk("offset must be on 32 bit boundaries\n");
170 goto bad;
171 }
172 if (skb->len < 0 || (offset > 0 && offset > skb->len)) {
173 printk("offset %d cant exceed pkt length %d\n",
174 offset, skb->len);
175 goto bad;
176 }
177
178 ptr = (u32 *)(pptr+offset);
179 /* just do it, baby */
180 *ptr = ((*ptr & tkey->mask) ^ tkey->val);
181 munged++;
182 }
183
184 if (munged)
185 skb->tc_verd = SET_TC_MUNGED(skb->tc_verd);
186 goto done;
187 } else {
188 printk("pedit BUG: index %d\n",p->index);
189 }
190
191 bad:
192 p->qstats.overlimits++;
193 done:
194 p->bstats.bytes += skb->len;
195 p->bstats.packets++;
196 spin_unlock(&p->lock);
197 return p->action;
198 }
199
200 static int
201 tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,int bind, int ref)
202 {
203 unsigned char *b = skb->tail;
204 struct tc_pedit *opt;
205 struct tcf_pedit *p = PRIV(a, pedit);
206 struct tcf_t t;
207 int s;
208
209 s = sizeof(*opt) + p->nkeys * sizeof(struct tc_pedit_key);
210
211 /* netlink spinlocks held above us - must use ATOMIC */
212 opt = kzalloc(s, GFP_ATOMIC);
213 if (opt == NULL)
214 return -ENOBUFS;
215
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;
223
224
225 #ifdef PEDIT_DEB
226 {
227 /* Debug - get rid of later */
228 int i;
229 struct tc_pedit_key *key = opt->keys;
230
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);
237 }
238 }
239 #endif
240
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);
246 kfree(opt);
247 return skb->len;
248
249 rtattr_failure:
250 skb_trim(skb, b - skb->data);
251 kfree(opt);
252 return -1;
253 }
254
255 static
256 struct tc_action_ops act_pedit_ops = {
257 .kind = "pedit",
258 .type = TCA_ACT_PEDIT,
259 .capab = TCA_CAP_NONE,
260 .owner = THIS_MODULE,
261 .act = tcf_pedit,
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
267 };
268
269 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
270 MODULE_DESCRIPTION("Generic Packet Editor actions");
271 MODULE_LICENSE("GPL");
272
273 static int __init
274 pedit_init_module(void)
275 {
276 return tcf_register_action(&act_pedit_ops);
277 }
278
279 static void __exit
280 pedit_cleanup_module(void)
281 {
282 tcf_unregister_action(&act_pedit_ops);
283 }
284
285 module_init(pedit_init_module);
286 module_exit(pedit_cleanup_module);
287
This page took 0.138291 seconds and 6 git commands to generate.