Merge to Fedora kernel-2.6.18-1.2260_FC5 patched with stable patch-2.6.18.5-vs2.0...
[linux-2.6.git] / net / ipv4 / netfilter / ipt_SET.c
1 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2  *                         Patrick Schaaf <bof@bof.de>
3  *                         Martin Josefsson <gandalf@wlug.westbo.se>
4  * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.  
9  */
10
11 /* ipt_SET.c - netfilter target to manipulate IP sets */
12
13 #include <linux/types.h>
14 #include <linux/ip.h>
15 #include <linux/timer.h>
16 #include <linux/module.h>
17 #include <linux/netfilter.h>
18 #include <linux/netdevice.h>
19 #include <linux/if.h>
20 #include <linux/inetdevice.h>
21 #include <linux/version.h>
22 #include <net/protocol.h>
23 #include <net/checksum.h>
24 #include <linux/netfilter_ipv4.h>
25 #include <linux/netfilter_ipv4/ip_nat_rule.h>
26 #include <linux/netfilter_ipv4/ipt_set.h>
27
28 static unsigned int
29 target(struct sk_buff **pskb,
30        const struct net_device *in,
31        const struct net_device *out,
32        unsigned int hooknum,
33 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
34        const struct xt_target *target,
35 #endif
36 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
37        const void *targinfo,
38        void *userinfo)
39 #else
40        const void *targinfo)
41 #endif
42 {
43         const struct ipt_set_info_target *info = targinfo;
44         
45         if (info->add_set.index != IP_SET_INVALID_ID)
46                 ip_set_addip_kernel(info->add_set.index,
47                                     *pskb,
48                                     info->add_set.flags);
49         if (info->del_set.index != IP_SET_INVALID_ID)
50                 ip_set_delip_kernel(info->del_set.index,
51                                     *pskb,
52                                     info->del_set.flags);
53
54         return IPT_CONTINUE;
55 }
56
57 static int
58 checkentry(const char *tablename,
59 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
60            const void *e,
61 #else
62            const struct ipt_entry *e,
63 #endif
64 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
65            const struct xt_target *target,
66 #endif
67            void *targinfo,
68 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
69            unsigned int targinfosize, 
70 #endif
71            unsigned int hook_mask)
72 {
73         struct ipt_set_info_target *info = 
74                 (struct ipt_set_info_target *) targinfo;
75         ip_set_id_t index;
76
77 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
78         if (targinfosize != IPT_ALIGN(sizeof(*info))) {
79                 DP("bad target info size %u", targinfosize);
80                 return 0;
81         }
82 #endif
83
84         if (info->add_set.index != IP_SET_INVALID_ID) {
85                 index = ip_set_get_byindex(info->add_set.index);
86                 if (index == IP_SET_INVALID_ID) {
87                         ip_set_printk("cannot find add_set index %u as target",
88                                       info->add_set.index);
89                         return 0;       /* error */
90                 }
91         }
92
93         if (info->del_set.index != IP_SET_INVALID_ID) {
94                 index = ip_set_get_byindex(info->del_set.index);
95                 if (index == IP_SET_INVALID_ID) {
96                         ip_set_printk("cannot find del_set index %u as target",
97                                       info->del_set.index);
98                         return 0;       /* error */
99                 }
100         }
101         if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
102             || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
103                 ip_set_printk("That's nasty!");
104                 return 0;       /* error */
105         }
106
107         return 1;
108 }
109
110 static void destroy(
111 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
112                     const struct xt_target *target,
113 #endif
114 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
115                     void *targetinfo, unsigned int targetsize)
116 #else
117                     void *targetinfo)
118 #endif
119 {
120         struct ipt_set_info_target *info = targetinfo;
121
122 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
123         if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
124                 ip_set_printk("invalid targetsize %d", targetsize);
125                 return;
126         }
127 #endif
128         if (info->add_set.index != IP_SET_INVALID_ID)
129                 ip_set_put(info->add_set.index);
130         if (info->del_set.index != IP_SET_INVALID_ID)
131                 ip_set_put(info->del_set.index);
132 }
133
134 static struct ipt_target SET_target = {
135         .name           = "SET",
136         .target         = target,
137 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
138         .targetsize     = sizeof(struct ipt_set_info_target),
139 #endif
140         .checkentry     = checkentry,
141         .destroy        = destroy,
142         .me             = THIS_MODULE
143 };
144
145 MODULE_LICENSE("GPL");
146 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
147 MODULE_DESCRIPTION("iptables IP set target module");
148
149 static int __init ipt_SET_init(void)
150 {
151         return ipt_register_target(&SET_target);
152 }
153
154 static void __exit ipt_SET_fini(void)
155 {
156         ipt_unregister_target(&SET_target);
157 }
158
159 module_init(ipt_SET_init);
160 module_exit(ipt_SET_fini);