Merge to kernel-2.6.20-1.2949.fc6.vs2.2.0.1
[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 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
137         .family         = AF_INET,
138 #endif
139         .target         = target,
140 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
141         .targetsize     = sizeof(struct ipt_set_info_target),
142 #endif
143         .checkentry     = checkentry,
144         .destroy        = destroy,
145         .me             = THIS_MODULE
146 };
147
148 MODULE_LICENSE("GPL");
149 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
150 MODULE_DESCRIPTION("iptables IP set target module");
151
152 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
153 #define ipt_register_target      xt_register_target
154 #define ipt_unregister_target    xt_unregister_target
155 #endif
156
157 static int __init ipt_SET_init(void)
158 {
159         return ipt_register_target(&SET_target);
160 }
161
162 static void __exit ipt_SET_fini(void)
163 {
164         ipt_unregister_target(&SET_target);
165 }
166
167 module_init(ipt_SET_init);
168 module_exit(ipt_SET_fini);