~ubuntu-branches/ubuntu/precise/xtables-addons/precise

« back to all changes in this revision

Viewing changes to extensions/ipset/ipt_SET.c

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Chifflier
  • Date: 2008-10-27 22:31:20 UTC
  • Revision ID: james.westby@ubuntu.com-20081027223120-njqjsraqjpp7s98t
Tags: upstream-1.5.7
ImportĀ upstreamĀ versionĀ 1.5.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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_tables.h>
 
26
#include "ipt_set.h"
 
27
 
 
28
static unsigned int
 
29
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
 
30
target(struct sk_buff *skb,
 
31
#else
 
32
target(struct sk_buff **pskb,
 
33
#endif
 
34
       const struct net_device *in,
 
35
       const struct net_device *out,
 
36
       unsigned int hooknum,
 
37
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
 
38
       const struct xt_target *target,
 
39
#endif
 
40
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
 
41
       const void *targinfo,
 
42
       void *userinfo)
 
43
#else
 
44
       const void *targinfo)
 
45
#endif
 
46
{
 
47
        const struct ipt_set_info_target *info = targinfo;
 
48
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
 
49
        struct sk_buff *skb = *pskb;
 
50
#endif
 
51
 
 
52
        
 
53
        if (info->add_set.index != IP_SET_INVALID_ID)
 
54
                ip_set_addip_kernel(info->add_set.index,
 
55
                                    skb,
 
56
                                    info->add_set.flags);
 
57
        if (info->del_set.index != IP_SET_INVALID_ID)
 
58
                ip_set_delip_kernel(info->del_set.index,
 
59
                                    skb,
 
60
                                    info->del_set.flags);
 
61
 
 
62
        return IPT_CONTINUE;
 
63
}
 
64
 
 
65
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
 
66
static bool
 
67
#else
 
68
static int
 
69
#endif
 
70
checkentry(const char *tablename,
 
71
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
 
72
           const void *e,
 
73
#else
 
74
           const struct ipt_entry *e,
 
75
#endif
 
76
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
 
77
           const struct xt_target *target,
 
78
#endif
 
79
           void *targinfo,
 
80
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
 
81
           unsigned int targinfosize,
 
82
#endif
 
83
           unsigned int hook_mask)
 
84
{
 
85
        struct ipt_set_info_target *info = targinfo;
 
86
        ip_set_id_t index;
 
87
 
 
88
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
 
89
        if (targinfosize != IPT_ALIGN(sizeof(*info))) {
 
90
                DP("bad target info size %u", targinfosize);
 
91
                return 0;
 
92
        }
 
93
#endif
 
94
 
 
95
        if (info->add_set.index != IP_SET_INVALID_ID) {
 
96
                index = ip_set_get_byindex(info->add_set.index);
 
97
                if (index == IP_SET_INVALID_ID) {
 
98
                        ip_set_printk("cannot find add_set index %u as target",
 
99
                                      info->add_set.index);
 
100
                        return 0;       /* error */
 
101
                }
 
102
        }
 
103
 
 
104
        if (info->del_set.index != IP_SET_INVALID_ID) {
 
105
                index = ip_set_get_byindex(info->del_set.index);
 
106
                if (index == IP_SET_INVALID_ID) {
 
107
                        ip_set_printk("cannot find del_set index %u as target",
 
108
                                      info->del_set.index);
 
109
                        return 0;       /* error */
 
110
                }
 
111
        }
 
112
        if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
 
113
            || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
 
114
                ip_set_printk("That's nasty!");
 
115
                return 0;       /* error */
 
116
        }
 
117
 
 
118
        return 1;
 
119
}
 
120
 
 
121
static void destroy(
 
122
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
 
123
                    const struct xt_target *target,
 
124
#endif
 
125
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
 
126
                    void *targetinfo, unsigned int targetsize)
 
127
#else
 
128
                    void *targetinfo)
 
129
#endif
 
130
{
 
131
        struct ipt_set_info_target *info = targetinfo;
 
132
 
 
133
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
 
134
        if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
 
135
                ip_set_printk("invalid targetsize %d", targetsize);
 
136
                return;
 
137
        }
 
138
#endif
 
139
        if (info->add_set.index != IP_SET_INVALID_ID)
 
140
                ip_set_put(info->add_set.index);
 
141
        if (info->del_set.index != IP_SET_INVALID_ID)
 
142
                ip_set_put(info->del_set.index);
 
143
}
 
144
 
 
145
static struct ipt_target SET_target = {
 
146
        .name           = "SET",
 
147
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
 
148
        .family         = AF_INET,
 
149
#endif
 
150
        .target         = target,
 
151
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
 
152
        .targetsize     = sizeof(struct ipt_set_info_target),
 
153
#endif
 
154
        .checkentry     = checkentry,
 
155
        .destroy        = destroy,
 
156
        .me             = THIS_MODULE
 
157
};
 
158
 
 
159
MODULE_LICENSE("GPL");
 
160
MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
 
161
MODULE_DESCRIPTION("iptables IP set target module");
 
162
 
 
163
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
 
164
#define ipt_register_target      xt_register_target
 
165
#define ipt_unregister_target    xt_unregister_target
 
166
#endif
 
167
 
 
168
static int __init ipt_SET_init(void)
 
169
{
 
170
        return ipt_register_target(&SET_target);
 
171
}
 
172
 
 
173
static void __exit ipt_SET_fini(void)
 
174
{
 
175
        ipt_unregister_target(&SET_target);
 
176
}
 
177
 
 
178
module_init(ipt_SET_init);
 
179
module_exit(ipt_SET_fini);