1 --- a/Documentation/Configure.help
2 +++ b/Documentation/Configure.help
3 @@ -2986,6 +2986,14 @@ CONFIG_IP_NF_MATCH_TOS
4 If you want to compile it as a module, say M here and read
5 <file:Documentation/modules.txt>. If unsure, say `N'.
8 +CONFIG_IP_NF_MATCH_IPRANGE
9 + This option makes possible to match IP addresses against
12 + If you want to compile it as a module, say M here and read
13 + <file:Documentation/modules.txt>. If unsure, say `N'.
15 Condition variable match support
16 CONFIG_IP_NF_MATCH_CONDITION
17 This option allows you to match firewall rules against condition
19 +++ b/include/linux/netfilter_ipv4/ipt_iprange.h
21 +#ifndef _IPT_IPRANGE_H
22 +#define _IPT_IPRANGE_H
24 +#define IPRANGE_SRC 0x01 /* Match source IP address */
25 +#define IPRANGE_DST 0x02 /* Match destination IP address */
26 +#define IPRANGE_SRC_INV 0x10 /* Negate the condition */
27 +#define IPRANGE_DST_INV 0x20 /* Negate the condition */
30 + /* Inclusive: network order. */
31 + u_int32_t min_ip, max_ip;
34 +struct ipt_iprange_info
36 + struct ipt_iprange src;
37 + struct ipt_iprange dst;
39 + /* Flags from above */
43 +#endif /* _IPT_IPRANGE_H */
44 --- a/net/ipv4/netfilter/Config.in
45 +++ b/net/ipv4/netfilter/Config.in
46 @@ -25,6 +25,7 @@ tristate 'IP tables support (required fo
47 if [ "$CONFIG_IP_NF_IPTABLES" != "n" ]; then
49 dep_tristate ' limit match support' CONFIG_IP_NF_MATCH_LIMIT $CONFIG_IP_NF_IPTABLES
50 + dep_tristate ' IP range match support' CONFIG_IP_NF_MATCH_IPRANGE $CONFIG_IP_NF_IPTABLES
51 dep_tristate ' quota match support' CONFIG_IP_NF_MATCH_QUOTA $CONFIG_IP_NF_IPTABLES
53 dep_tristate ' IP set support' CONFIG_IP_NF_SET $CONFIG_IP_NF_IPTABLES
55 +++ b/net/ipv4/netfilter/ipt_iprange.c
58 + * iptables module to match IP address ranges
59 + * (c) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
61 + * Released under the terms of GNU GPLv2.
64 +#include <linux/module.h>
65 +#include <linux/skbuff.h>
66 +#include <linux/ip.h>
67 +#include <linux/netfilter_ipv4/ip_tables.h>
68 +#include <linux/netfilter_ipv4/ipt_iprange.h>
70 +MODULE_LICENSE("GPL");
71 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
72 +MODULE_DESCRIPTION("iptables arbitrary IP range match module");
75 +#define DEBUGP printk
77 +#define DEBUGP(format, args...)
81 +match(const struct sk_buff *skb,
82 + const struct net_device *in,
83 + const struct net_device *out,
84 + const void *matchinfo,
90 + const struct ipt_iprange_info *info = matchinfo;
91 + const struct iphdr *iph = skb->nh.iph;
94 + if (info->flags & IPRANGE_SRC) {
95 + if (((ntohl(iph->saddr) < ntohl(info->src.min_ip))
96 + || (ntohl(iph->saddr) > ntohl(info->src.max_ip)))
97 + ^ !!(info->flags & IPRANGE_SRC_INV)) {
98 + DEBUGP("src IP %u.%u.%u.%u NOT in range %s"
99 + "%u.%u.%u.%u-%u.%u.%u.%u\n",
100 + NIPQUAD(iph->saddr),
101 + info->flags & IPRANGE_SRC_INV ? "(INV) " : "",
102 + NIPQUAD(info->src.min_ip),
103 + NIPQUAD(info->src.max_ip));
107 + if (info->flags & IPRANGE_DST) {
108 + if (((ntohl(iph->daddr) < ntohl(info->dst.min_ip))
109 + || (ntohl(iph->daddr) > ntohl(info->dst.max_ip)))
110 + ^ !!(info->flags & IPRANGE_DST_INV)) {
111 + DEBUGP("dst IP %u.%u.%u.%u NOT in range %s"
112 + "%u.%u.%u.%u-%u.%u.%u.%u\n",
113 + NIPQUAD(iph->daddr),
114 + info->flags & IPRANGE_DST_INV ? "(INV) " : "",
115 + NIPQUAD(info->dst.min_ip),
116 + NIPQUAD(info->dst.max_ip));
123 +static int check(const char *tablename,
124 + const struct ipt_ip *ip,
126 + unsigned int matchsize,
127 + unsigned int hook_mask)
130 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_iprange_info)))
136 +static struct ipt_match iprange_match =
138 + .list = { NULL, NULL },
141 + .checkentry = &check,
146 +static int __init init(void)
148 + return ipt_register_match(&iprange_match);
151 +static void __exit fini(void)
153 + ipt_unregister_match(&iprange_match);
158 --- a/net/ipv4/netfilter/Makefile
159 +++ b/net/ipv4/netfilter/Makefile
160 @@ -81,6 +81,7 @@ obj-$(CONFIG_IP_NF_NAT) += iptable_nat.o
162 obj-$(CONFIG_IP_NF_MATCH_HELPER) += ipt_helper.o
163 obj-$(CONFIG_IP_NF_MATCH_LIMIT) += ipt_limit.o
164 +obj-$(CONFIG_IP_NF_MATCH_IPRANGE) += ipt_iprange.o
165 obj-$(CONFIG_IP_NF_MATCH_QUOTA) += ipt_quota.o
166 obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o
167 obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o