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/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/netdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/proc_fs.h>
33 #include <linux/kmod.h>
35 #include <net/pkt_sched.h>
36 #include <linux/tc_act/tc_ipt.h>
37 #include <net/tc_act/tc_ipt.h>
39 #include <linux/netfilter_ipv4/ip_tables.h>
41 /* use generic hash table */
42 #define MY_TAB_SIZE 16
43 #define MY_TAB_MASK 15
46 static struct tcf_ipt *tcf_ipt_ht[MY_TAB_SIZE];
47 /* ipt hash table lock */
48 static DEFINE_RWLOCK(ipt_lock);
50 /* ovewrride the defaults */
51 #define tcf_st tcf_ipt
52 #define tcf_t_lock ipt_lock
53 #define tcf_ht tcf_ipt_ht
55 #define CONFIG_NET_ACT_INIT
56 #include <net/pkt_act.h>
59 ipt_init_target(struct ipt_entry_target *t, char *table, unsigned int hook)
61 struct ipt_target *target;
64 target = xt_find_target(AF_INET, t->u.user.name, t->u.user.revision);
68 DPRINTK("ipt_init_target: found %s\n", target->name);
69 t->u.kernel.target = target;
71 ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),
76 if (t->u.kernel.target->checkentry
77 && !t->u.kernel.target->checkentry(table, NULL,
78 t->u.kernel.target, t->data,
79 t->u.target_size - sizeof(*t),
81 DPRINTK("ipt_init_target: check failed for `%s'.\n",
82 t->u.kernel.target->name);
83 module_put(t->u.kernel.target->me);
91 ipt_destroy_target(struct ipt_entry_target *t)
93 if (t->u.kernel.target->destroy)
94 t->u.kernel.target->destroy(t->u.kernel.target, t->data,
95 t->u.target_size - sizeof(*t));
96 module_put(t->u.kernel.target->me);
100 tcf_ipt_release(struct tcf_ipt *p, int bind)
107 if (p->bindcnt <= 0 && p->refcnt <= 0) {
108 ipt_destroy_target(p->t);
119 tcf_ipt_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a,
122 struct rtattr *tb[TCA_IPT_MAX];
124 struct ipt_entry_target *td, *t;
130 if (rta == NULL || rtattr_parse_nested(tb, TCA_IPT_MAX, rta) < 0)
133 if (tb[TCA_IPT_HOOK-1] == NULL ||
134 RTA_PAYLOAD(tb[TCA_IPT_HOOK-1]) < sizeof(u32))
136 if (tb[TCA_IPT_TARG-1] == NULL ||
137 RTA_PAYLOAD(tb[TCA_IPT_TARG-1]) < sizeof(*t))
139 td = (struct ipt_entry_target *)RTA_DATA(tb[TCA_IPT_TARG-1]);
140 if (RTA_PAYLOAD(tb[TCA_IPT_TARG-1]) < td->u.target_size)
143 if (tb[TCA_IPT_INDEX-1] != NULL &&
144 RTA_PAYLOAD(tb[TCA_IPT_INDEX-1]) >= sizeof(u32))
145 index = *(u32 *)RTA_DATA(tb[TCA_IPT_INDEX-1]);
147 p = tcf_hash_check(index, a, ovr, bind);
149 p = tcf_hash_create(index, est, a, sizeof(*p), ovr, bind);
155 tcf_ipt_release(p, bind);
160 hook = *(u32 *)RTA_DATA(tb[TCA_IPT_HOOK-1]);
163 tname = kmalloc(IFNAMSIZ, GFP_KERNEL);
166 if (tb[TCA_IPT_TABLE - 1] == NULL ||
167 rtattr_strlcpy(tname, tb[TCA_IPT_TABLE-1], IFNAMSIZ) >= IFNAMSIZ)
168 strcpy(tname, "mangle");
170 t = kmalloc(td->u.target_size, GFP_KERNEL);
173 memcpy(t, td, td->u.target_size);
175 if ((err = ipt_init_target(t, tname, hook)) < 0)
178 spin_lock_bh(&p->lock);
179 if (ret != ACT_P_CREATED) {
180 ipt_destroy_target(p->t);
187 spin_unlock_bh(&p->lock);
188 if (ret == ACT_P_CREATED)
202 tcf_ipt_cleanup(struct tc_action *a, int bind)
204 struct tcf_ipt *p = PRIV(a, ipt);
205 return tcf_ipt_release(p, bind);
209 tcf_ipt(struct sk_buff *skb, struct tc_action *a, struct tcf_result *res)
211 int ret = 0, result = 0;
212 struct tcf_ipt *p = PRIV(a, ipt);
214 if (skb_cloned(skb)) {
215 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
216 return TC_ACT_UNSPEC;
221 p->tm.lastuse = jiffies;
222 p->bstats.bytes += skb->len;
225 /* yes, we have to worry about both in and out dev
226 worry later - danger - this API seems to have changed
227 from earlier kernels */
229 /* iptables targets take a double skb pointer in case the skb
230 * needs to be replaced. We don't own the skb, so this must not
231 * happen. The pskb_expand_head above should make sure of this */
232 ret = p->t->u.kernel.target->target(&skb, skb->dev, NULL, p->hook,
233 p->t->u.kernel.target, p->t->data,
240 result = TC_ACT_SHOT;
244 result = TC_ACT_PIPE;
248 printk("Bogus netfilter code %d assume ACCEPT\n", ret);
249 result = TC_POLICE_OK;
252 spin_unlock(&p->lock);
258 tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
260 struct ipt_entry_target *t;
263 unsigned char *b = skb->tail;
264 struct tcf_ipt *p = PRIV(a, ipt);
266 /* for simple targets kernel size == user size
267 ** user name = target name
268 ** for foolproof you need to not assume this
271 t = kmalloc(p->t->u.user.target_size, GFP_ATOMIC);
275 c.bindcnt = p->bindcnt - bind;
276 c.refcnt = p->refcnt - ref;
277 memcpy(t, p->t, p->t->u.user.target_size);
278 strcpy(t->u.user.name, p->t->u.kernel.target->name);
280 DPRINTK("\ttcf_ipt_dump tablename %s length %d\n", p->tname,
282 DPRINTK("\tdump target name %s size %d size user %d "
283 "data[0] %x data[1] %x\n", p->t->u.kernel.target->name,
284 p->t->u.target_size, p->t->u.user.target_size,
285 p->t->data[0], p->t->data[1]);
286 RTA_PUT(skb, TCA_IPT_TARG, p->t->u.user.target_size, t);
287 RTA_PUT(skb, TCA_IPT_INDEX, 4, &p->index);
288 RTA_PUT(skb, TCA_IPT_HOOK, 4, &p->hook);
289 RTA_PUT(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c);
290 RTA_PUT(skb, TCA_IPT_TABLE, IFNAMSIZ, p->tname);
291 tm.install = jiffies_to_clock_t(jiffies - p->tm.install);
292 tm.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
293 tm.expires = jiffies_to_clock_t(p->tm.expires);
294 RTA_PUT(skb, TCA_IPT_TM, sizeof (tm), &tm);
299 skb_trim(skb, b - skb->data);
304 static struct tc_action_ops act_ipt_ops = {
307 .capab = TCA_CAP_NONE,
308 .owner = THIS_MODULE,
310 .dump = tcf_ipt_dump,
311 .cleanup = tcf_ipt_cleanup,
312 .lookup = tcf_hash_search,
313 .init = tcf_ipt_init,
314 .walk = tcf_generic_walker
317 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
318 MODULE_DESCRIPTION("Iptables target actions");
319 MODULE_LICENSE("GPL");
322 ipt_init_module(void)
324 return tcf_register_action(&act_ipt_ops);
328 ipt_cleanup_module(void)
330 tcf_unregister_action(&act_ipt_ops);
333 module_init(ipt_init_module);
334 module_exit(ipt_cleanup_module);