Linux 2.6.17.7
[linux/fpc-iii.git] / net / sched / act_ipt.c
blob37640c6fc014c4c8a53a01609cbc78a51cbc7de6
1 /*
2 * net/sched/ipt.c iptables target interface
4 *TODO: Add other tables. For now we only support the ipv4 table targets
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.
11 * Copyright: Jamal Hadi Salim (2002-4)
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 <linux/kmod.h>
35 #include <net/sock.h>
36 #include <net/pkt_sched.h>
37 #include <linux/tc_act/tc_ipt.h>
38 #include <net/tc_act/tc_ipt.h>
40 #include <linux/netfilter_ipv4/ip_tables.h>
42 /* use generic hash table */
43 #define MY_TAB_SIZE 16
44 #define MY_TAB_MASK 15
46 static u32 idx_gen;
47 static struct tcf_ipt *tcf_ipt_ht[MY_TAB_SIZE];
48 /* ipt hash table lock */
49 static DEFINE_RWLOCK(ipt_lock);
51 /* ovewrride the defaults */
52 #define tcf_st tcf_ipt
53 #define tcf_t_lock ipt_lock
54 #define tcf_ht tcf_ipt_ht
56 #define CONFIG_NET_ACT_INIT
57 #include <net/pkt_act.h>
59 static int
60 ipt_init_target(struct ipt_entry_target *t, char *table, unsigned int hook)
62 struct ipt_target *target;
63 int ret = 0;
65 target = xt_find_target(AF_INET, t->u.user.name, t->u.user.revision);
66 if (!target)
67 return -ENOENT;
69 DPRINTK("ipt_init_target: found %s\n", target->name);
70 t->u.kernel.target = target;
72 ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),
73 table, hook, 0, 0);
74 if (ret)
75 return ret;
77 if (t->u.kernel.target->checkentry
78 && !t->u.kernel.target->checkentry(table, NULL,
79 t->u.kernel.target, t->data,
80 t->u.target_size - sizeof(*t),
81 hook)) {
82 DPRINTK("ipt_init_target: check failed for `%s'.\n",
83 t->u.kernel.target->name);
84 module_put(t->u.kernel.target->me);
85 ret = -EINVAL;
88 return ret;
91 static void
92 ipt_destroy_target(struct ipt_entry_target *t)
94 if (t->u.kernel.target->destroy)
95 t->u.kernel.target->destroy(t->u.kernel.target, t->data,
96 t->u.target_size - sizeof(*t));
97 module_put(t->u.kernel.target->me);
100 static int
101 tcf_ipt_release(struct tcf_ipt *p, int bind)
103 int ret = 0;
104 if (p) {
105 if (bind)
106 p->bindcnt--;
107 p->refcnt--;
108 if (p->bindcnt <= 0 && p->refcnt <= 0) {
109 ipt_destroy_target(p->t);
110 kfree(p->tname);
111 kfree(p->t);
112 tcf_hash_destroy(p);
113 ret = ACT_P_DELETED;
116 return ret;
119 static int
120 tcf_ipt_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a,
121 int ovr, int bind)
123 struct rtattr *tb[TCA_IPT_MAX];
124 struct tcf_ipt *p;
125 struct ipt_entry_target *td, *t;
126 char *tname;
127 int ret = 0, err;
128 u32 hook = 0;
129 u32 index = 0;
131 if (rta == NULL || rtattr_parse_nested(tb, TCA_IPT_MAX, rta) < 0)
132 return -EINVAL;
134 if (tb[TCA_IPT_HOOK-1] == NULL ||
135 RTA_PAYLOAD(tb[TCA_IPT_HOOK-1]) < sizeof(u32))
136 return -EINVAL;
137 if (tb[TCA_IPT_TARG-1] == NULL ||
138 RTA_PAYLOAD(tb[TCA_IPT_TARG-1]) < sizeof(*t))
139 return -EINVAL;
140 td = (struct ipt_entry_target *)RTA_DATA(tb[TCA_IPT_TARG-1]);
141 if (RTA_PAYLOAD(tb[TCA_IPT_TARG-1]) < td->u.target_size)
142 return -EINVAL;
144 if (tb[TCA_IPT_INDEX-1] != NULL &&
145 RTA_PAYLOAD(tb[TCA_IPT_INDEX-1]) >= sizeof(u32))
146 index = *(u32 *)RTA_DATA(tb[TCA_IPT_INDEX-1]);
148 p = tcf_hash_check(index, a, ovr, bind);
149 if (p == NULL) {
150 p = tcf_hash_create(index, est, a, sizeof(*p), ovr, bind);
151 if (p == NULL)
152 return -ENOMEM;
153 ret = ACT_P_CREATED;
154 } else {
155 if (!ovr) {
156 tcf_ipt_release(p, bind);
157 return -EEXIST;
161 hook = *(u32 *)RTA_DATA(tb[TCA_IPT_HOOK-1]);
163 err = -ENOMEM;
164 tname = kmalloc(IFNAMSIZ, GFP_KERNEL);
165 if (tname == NULL)
166 goto err1;
167 if (tb[TCA_IPT_TABLE - 1] == NULL ||
168 rtattr_strlcpy(tname, tb[TCA_IPT_TABLE-1], IFNAMSIZ) >= IFNAMSIZ)
169 strcpy(tname, "mangle");
171 t = kmalloc(td->u.target_size, GFP_KERNEL);
172 if (t == NULL)
173 goto err2;
174 memcpy(t, td, td->u.target_size);
176 if ((err = ipt_init_target(t, tname, hook)) < 0)
177 goto err3;
179 spin_lock_bh(&p->lock);
180 if (ret != ACT_P_CREATED) {
181 ipt_destroy_target(p->t);
182 kfree(p->tname);
183 kfree(p->t);
185 p->tname = tname;
186 p->t = t;
187 p->hook = hook;
188 spin_unlock_bh(&p->lock);
189 if (ret == ACT_P_CREATED)
190 tcf_hash_insert(p);
191 return ret;
193 err3:
194 kfree(t);
195 err2:
196 kfree(tname);
197 err1:
198 kfree(p);
199 return err;
202 static int
203 tcf_ipt_cleanup(struct tc_action *a, int bind)
205 struct tcf_ipt *p = PRIV(a, ipt);
206 return tcf_ipt_release(p, bind);
209 static int
210 tcf_ipt(struct sk_buff *skb, struct tc_action *a, struct tcf_result *res)
212 int ret = 0, result = 0;
213 struct tcf_ipt *p = PRIV(a, ipt);
215 if (skb_cloned(skb)) {
216 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
217 return TC_ACT_UNSPEC;
220 spin_lock(&p->lock);
222 p->tm.lastuse = jiffies;
223 p->bstats.bytes += skb->len;
224 p->bstats.packets++;
226 /* yes, we have to worry about both in and out dev
227 worry later - danger - this API seems to have changed
228 from earlier kernels */
230 /* iptables targets take a double skb pointer in case the skb
231 * needs to be replaced. We don't own the skb, so this must not
232 * happen. The pskb_expand_head above should make sure of this */
233 ret = p->t->u.kernel.target->target(&skb, skb->dev, NULL, p->hook,
234 p->t->u.kernel.target, p->t->data,
235 NULL);
236 switch (ret) {
237 case NF_ACCEPT:
238 result = TC_ACT_OK;
239 break;
240 case NF_DROP:
241 result = TC_ACT_SHOT;
242 p->qstats.drops++;
243 break;
244 case IPT_CONTINUE:
245 result = TC_ACT_PIPE;
246 break;
247 default:
248 if (net_ratelimit())
249 printk("Bogus netfilter code %d assume ACCEPT\n", ret);
250 result = TC_POLICE_OK;
251 break;
253 spin_unlock(&p->lock);
254 return result;
258 static int
259 tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
261 struct ipt_entry_target *t;
262 struct tcf_t tm;
263 struct tc_cnt c;
264 unsigned char *b = skb->tail;
265 struct tcf_ipt *p = PRIV(a, ipt);
267 /* for simple targets kernel size == user size
268 ** user name = target name
269 ** for foolproof you need to not assume this
272 t = kmalloc(p->t->u.user.target_size, GFP_ATOMIC);
273 if (t == NULL)
274 goto rtattr_failure;
276 c.bindcnt = p->bindcnt - bind;
277 c.refcnt = p->refcnt - ref;
278 memcpy(t, p->t, p->t->u.user.target_size);
279 strcpy(t->u.user.name, p->t->u.kernel.target->name);
281 DPRINTK("\ttcf_ipt_dump tablename %s length %d\n", p->tname,
282 strlen(p->tname));
283 DPRINTK("\tdump target name %s size %d size user %d "
284 "data[0] %x data[1] %x\n", p->t->u.kernel.target->name,
285 p->t->u.target_size, p->t->u.user.target_size,
286 p->t->data[0], p->t->data[1]);
287 RTA_PUT(skb, TCA_IPT_TARG, p->t->u.user.target_size, t);
288 RTA_PUT(skb, TCA_IPT_INDEX, 4, &p->index);
289 RTA_PUT(skb, TCA_IPT_HOOK, 4, &p->hook);
290 RTA_PUT(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c);
291 RTA_PUT(skb, TCA_IPT_TABLE, IFNAMSIZ, p->tname);
292 tm.install = jiffies_to_clock_t(jiffies - p->tm.install);
293 tm.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
294 tm.expires = jiffies_to_clock_t(p->tm.expires);
295 RTA_PUT(skb, TCA_IPT_TM, sizeof (tm), &tm);
296 kfree(t);
297 return skb->len;
299 rtattr_failure:
300 skb_trim(skb, b - skb->data);
301 kfree(t);
302 return -1;
305 static struct tc_action_ops act_ipt_ops = {
306 .kind = "ipt",
307 .type = TCA_ACT_IPT,
308 .capab = TCA_CAP_NONE,
309 .owner = THIS_MODULE,
310 .act = tcf_ipt,
311 .dump = tcf_ipt_dump,
312 .cleanup = tcf_ipt_cleanup,
313 .lookup = tcf_hash_search,
314 .init = tcf_ipt_init,
315 .walk = tcf_generic_walker
318 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
319 MODULE_DESCRIPTION("Iptables target actions");
320 MODULE_LICENSE("GPL");
322 static int __init
323 ipt_init_module(void)
325 return tcf_register_action(&act_ipt_ops);
328 static void __exit
329 ipt_cleanup_module(void)
331 tcf_unregister_action(&act_ipt_ops);
334 module_init(ipt_init_module);
335 module_exit(ipt_cleanup_module);