This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / net / sched / ipt.c
1 /*
2  * net/sched/ipt.c      iptables target interface
3  *
4  *TODO: Add other tables. For now we only support the ipv4 table targets
5  *
6  *              This program is free software; you can redistribute it and/or
7  *              modify it under the terms of the GNU General Public License
8  *              as published by the Free Software Foundation; either version
9  *              2 of the License, or (at your option) any later version.
10  *
11  * Copyright:   Jamal Hadi Salim (2002-4)
12  */
13
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <asm/bitops.h>
17 #include <linux/config.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/mm.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
25 #include <linux/in.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/netdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/proc_fs.h>
34 #include <net/sock.h>
35 #include <net/pkt_sched.h>
36 #include <linux/tc_act/tc_ipt.h>
37 #include <net/tc_act/tc_ipt.h>
38
39 #include <linux/netfilter_ipv4/ip_tables.h>
40
41 /* use generic hash table */
42 #define MY_TAB_SIZE     16
43 #define MY_TAB_MASK     15
44
45 static u32 idx_gen;
46 static struct tcf_ipt *tcf_ipt_ht[MY_TAB_SIZE];
47 /* ipt hash table lock */
48 static rwlock_t ipt_lock = RW_LOCK_UNLOCKED;
49
50 /* ovewrride the defaults */
51 #define tcf_st  tcf_ipt
52 #define tcf_t_lock   ipt_lock
53 #define tcf_ht tcf_ipt_ht
54
55 #include <net/pkt_act.h>
56
57 static inline int
58 init_targ(struct tcf_ipt *p)
59 {
60         struct ipt_target *target;
61         int ret = 0;
62         struct ipt_entry_target *t = p->t;
63         target = __ipt_find_target_lock(t->u.user.name, &ret);
64
65         if (!target) {
66                 printk("init_targ: Failed to find %s\n", t->u.user.name);
67                 return -1;
68         }
69
70         DPRINTK("init_targ: found %s\n", target->name);
71         /* we really need proper ref counting
72          seems to be only needed for modules?? Talk to laforge */
73 /*      if (target->me)
74               __MOD_INC_USE_COUNT(target->me);
75 */
76         t->u.kernel.target = target;
77
78         __ipt_mutex_up();
79
80         if (t->u.kernel.target->checkentry
81             && !t->u.kernel.target->checkentry(p->tname, NULL, t->data,
82                                                t->u.target_size
83                                                - sizeof (*t), p->hook)) {
84 /*              if (t->u.kernel.target->me)
85               __MOD_DEC_USE_COUNT(t->u.kernel.target->me);
86 */
87                 DPRINTK("ip_tables: check failed for `%s'.\n",
88                         t->u.kernel.target->name);
89                 ret = -EINVAL;
90         }
91
92         return ret;
93 }
94
95 static int
96 tcf_ipt_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a, int ovr, int bind)
97 {
98         struct ipt_entry_target *t;
99         unsigned h;
100         struct rtattr *tb[TCA_IPT_MAX];
101         struct tcf_ipt *p;
102         int ret = 0;
103         u32 index = 0;
104         u32 hook = 0;
105
106         if (NULL == a || NULL == rta ||
107             (rtattr_parse(tb, TCA_IPT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)) <
108              0)) {
109                 return -1;
110         }
111
112
113         if (tb[TCA_IPT_INDEX - 1]) {
114                 index = *(u32 *) RTA_DATA(tb[TCA_IPT_INDEX - 1]);
115                 DPRINTK("ipt index %d\n", index);
116         }
117
118         if (index && (p = tcf_hash_lookup(index)) != NULL) {
119                 a->priv = (void *) p;
120                 spin_lock(&p->lock);
121                 if (bind) {
122                         p->bindcnt += 1;
123                         p->refcnt += 1;
124                 }
125                 if (ovr) {
126                         goto override;
127                 }
128                 spin_unlock(&p->lock);
129                 return ret;
130         }
131
132         if (NULL == tb[TCA_IPT_TARG - 1] || NULL == tb[TCA_IPT_HOOK - 1]) {
133                 return -1;
134         }
135
136         p = kmalloc(sizeof (*p), GFP_KERNEL);
137         if (p == NULL)
138                 return -1;
139
140         memset(p, 0, sizeof (*p));
141         p->refcnt = 1;
142         ret = 1;
143         spin_lock_init(&p->lock);
144         p->stats_lock = &p->lock;
145         if (bind)
146                 p->bindcnt = 1;
147
148 override:
149         hook = *(u32 *) RTA_DATA(tb[TCA_IPT_HOOK - 1]);
150
151         t = (struct ipt_entry_target *) RTA_DATA(tb[TCA_IPT_TARG - 1]);
152
153         p->t = kmalloc(t->u.target_size, GFP_KERNEL);
154         if (p->t == NULL) {
155                 if (ovr) {
156                         printk("ipt policy messed up \n");
157                         spin_unlock(&p->lock);
158                         return -1;
159                 }
160                 kfree(p);
161                 return -1;
162         }
163
164         memcpy(p->t, RTA_DATA(tb[TCA_IPT_TARG - 1]), t->u.target_size);
165         DPRINTK(" target NAME %s size %d data[0] %x data[1] %x\n",
166                 t->u.user.name, t->u.target_size, t->data[0], t->data[1]);
167
168         p->tname = kmalloc(IFNAMSIZ, GFP_KERNEL);
169
170         if (p->tname == NULL) {
171                 if (ovr) {
172                         printk("ipt policy messed up 2 \n");
173                         spin_unlock(&p->lock);
174                         return -1;
175                 }
176                 kfree(p->t);
177                 kfree(p);
178                 return -1;
179         } else {
180                 int csize = IFNAMSIZ - 1;
181
182                 memset(p->tname, 0, IFNAMSIZ);
183                 if (tb[TCA_IPT_TABLE - 1]) {
184                         if (strlen((char *) RTA_DATA(tb[TCA_IPT_TABLE - 1])) <
185                             csize)
186                                 csize = strlen(RTA_DATA(tb[TCA_IPT_TABLE - 1]));
187                         strncpy(p->tname, RTA_DATA(tb[TCA_IPT_TABLE - 1]),
188                                 csize);
189                         DPRINTK("table name %s\n", p->tname);
190                 } else {
191                         strncpy(p->tname, "mangle", 1 + strlen("mangle"));
192                 }
193         }
194
195         if (0 > init_targ(p)) {
196                 if (ovr) {
197                         printk("ipt policy messed up 2 \n");
198                         spin_unlock(&p->lock);
199                         return -1;
200                 }
201                 kfree(p->tname);
202                 kfree(p->t);
203                 kfree(p);
204                 return -1;
205         }
206
207         if (ovr) {
208                 spin_unlock(&p->lock);
209                 return -1;
210         }
211
212         p->index = index ? : tcf_hash_new_index();
213
214         p->tm.lastuse = jiffies;
215         /*
216         p->tm.expires = jiffies;
217         */
218         p->tm.install = jiffies;
219 #ifdef CONFIG_NET_ESTIMATOR
220         if (est)
221                 gen_new_estimator(&p->bstats, &p->rate_est, p->stats_lock, est);
222 #endif
223         h = tcf_hash(p->index);
224         write_lock_bh(&ipt_lock);
225         p->next = tcf_ipt_ht[h];
226         tcf_ipt_ht[h] = p;
227         write_unlock_bh(&ipt_lock);
228         a->priv = (void *) p;
229         return ret;
230
231 }
232
233 static int
234 tcf_ipt_cleanup(struct tc_action *a, int bind)
235 {
236         struct tcf_ipt *p;
237         p = PRIV(a,ipt);
238         if (NULL != p)
239                 return tcf_hash_release(p, bind);
240         return 0;
241 }
242
243 static int
244 tcf_ipt(struct sk_buff **pskb, struct tc_action *a)
245 {
246         int ret = 0, result = 0;
247         struct tcf_ipt *p;
248         struct sk_buff *skb = *pskb;
249
250         p = PRIV(a,ipt);
251
252         if (NULL == p || NULL == skb) {
253                 return -1;
254         }
255
256         spin_lock(&p->lock);
257
258         p->tm.lastuse = jiffies;
259         p->bstats.bytes += skb->len;
260         p->bstats.packets++;
261
262         if (skb_cloned(skb) ) {
263                 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
264                         return -1;
265                 }
266         }
267         /* yes, we have to worry about both in and out dev
268          worry later - danger - this API seems to have changed
269          from earlier kernels */
270
271         ret = p->t->u.kernel.target->target(&skb, skb->dev, NULL,
272                                             p->hook, p->t->data, (void *)NULL);
273         switch (ret) {
274         case NF_ACCEPT:
275                 result = TC_ACT_OK;
276                 break;
277         case NF_DROP:
278                 result = TC_ACT_SHOT;
279                 p->qstats.drops++;
280                 break;
281         case IPT_CONTINUE:
282                 result = TC_ACT_PIPE;
283                 break;
284         default:
285                 if (net_ratelimit())
286                         printk("Bogus netfilter code %d assume ACCEPT\n", ret);
287                 result = TC_POLICE_OK;
288                 break;
289         }
290         spin_unlock(&p->lock);
291         return result;
292
293 }
294
295 static int
296 tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
297 {
298         struct ipt_entry_target *t;
299         struct tcf_t tm;
300         struct tc_cnt c;
301         unsigned char *b = skb->tail;
302
303         struct tcf_ipt *p;
304
305         p = PRIV(a,ipt);
306         if (NULL == p) {
307                 printk("BUG: tcf_ipt_dump called with NULL params\n");
308                 goto rtattr_failure;
309         }
310         /* for simple targets kernel size == user size
311         ** user name = target name
312         ** for foolproof you need to not assume this
313         */
314
315         t = kmalloc(p->t->u.user.target_size, GFP_ATOMIC);
316
317         if (NULL == t)
318                 goto rtattr_failure;
319
320         c.bindcnt = p->bindcnt - bind;
321         c.refcnt = p->refcnt - ref;
322         memcpy(t, p->t, p->t->u.user.target_size);
323         strcpy(t->u.user.name, p->t->u.kernel.target->name);
324
325         DPRINTK("\ttcf_ipt_dump tablename %s length %d\n", p->tname,
326                 strlen(p->tname));
327         DPRINTK
328             ("\tdump target name %s size %d size user %d data[0] %x data[1] %x\n",
329              p->t->u.kernel.target->name, p->t->u.target_size, p->t->u.user.target_size,
330              p->t->data[0], p->t->data[1]);
331         RTA_PUT(skb, TCA_IPT_TARG, p->t->u.user.target_size, t);
332         RTA_PUT(skb, TCA_IPT_INDEX, 4, &p->index);
333         RTA_PUT(skb, TCA_IPT_HOOK, 4, &p->hook);
334         RTA_PUT(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c);
335         RTA_PUT(skb, TCA_IPT_TABLE, IFNAMSIZ, p->tname);
336         tm.install = jiffies_to_clock_t(jiffies - p->tm.install);
337         tm.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
338         tm.expires = jiffies_to_clock_t(p->tm.expires);
339         RTA_PUT(skb, TCA_IPT_TM, sizeof (tm), &tm);
340         return skb->len;
341
342       rtattr_failure:
343         skb_trim(skb, b - skb->data);
344         return -1;
345 }
346
347 static struct tc_action_ops act_ipt_ops = {
348         .next           =       NULL,
349         .kind           =       "ipt",
350         .type           =       TCA_ACT_IPT,
351         .capab          =       TCA_CAP_NONE,
352         .owner          =       THIS_MODULE,
353         .act            =       tcf_ipt,
354         .dump           =       tcf_ipt_dump,
355         .cleanup        =       tcf_ipt_cleanup,
356         .lookup         =       tcf_hash_search,
357         .init           =       tcf_ipt_init,
358         .walk           =       tcf_generic_walker
359 };
360
361 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
362 MODULE_DESCRIPTION("Iptables target actions");
363 MODULE_LICENSE("GPL");
364
365 static int __init
366 ipt_init_module(void)
367 {
368         return tcf_register_action(&act_ipt_ops);
369 }
370
371 static void __exit
372 ipt_cleanup_module(void)
373 {
374         tcf_unregister_action(&act_ipt_ops);
375 }
376
377 module_init(ipt_init_module);
378 module_exit(ipt_cleanup_module);