Add iprange (#1799)
[openwrt.git] / target / linux / generic-2.4 / patches / 620-netfilter_iprange.patch
1 diff -ruaN linux-2.4.34.orig/Documentation/Configure.help linux-2.4.34/Documentation/Configure.help
2 --- linux-2.4.34.orig/Documentation/Configure.help      2007-06-01 12:17:16.000000000 +0100
3 +++ linux-2.4.34/Documentation/Configure.help   2007-06-01 12:20:20.000000000 +0100
4 @@ -2986,6 +2986,14 @@
5    If you want to compile it as a module, say M here and read
6    <file:Documentation/modules.txt>.  If unsure, say `N'.
7  
8 +iprange match support
9 +CONFIG_IP_NF_MATCH_IPRANGE
10 +  This option makes possible to match IP addresses against
11 +  IP address ranges.
12 +
13 +  If you want to compile it as a module, say M here and read
14 +  <file:Documentation/modules.txt>.  If unsure, say `N'.
15 +
16  Condition variable match support
17  CONFIG_IP_NF_MATCH_CONDITION
18    This option allows you to match firewall rules against condition
19 diff -ruaN linux-2.4.34.orig/include/linux/netfilter_ipv4/ipt_iprange.h linux-2.4.34/include/linux/netfilter_ipv4/ipt_iprange.h
20 --- linux-2.4.34.orig/include/linux/netfilter_ipv4/ipt_iprange.h        1970-01-01 01:00:00.000000000 +0100
21 +++ linux-2.4.34/include/linux/netfilter_ipv4/ipt_iprange.h     2007-06-01 12:20:20.000000000 +0100
22 @@ -0,0 +1,23 @@
23 +#ifndef _IPT_IPRANGE_H
24 +#define _IPT_IPRANGE_H
25 +
26 +#define IPRANGE_SRC            0x01    /* Match source IP address */
27 +#define IPRANGE_DST            0x02    /* Match destination IP address */
28 +#define IPRANGE_SRC_INV                0x10    /* Negate the condition */
29 +#define IPRANGE_DST_INV                0x20    /* Negate the condition */
30 +
31 +struct ipt_iprange {
32 +       /* Inclusive: network order. */
33 +       u_int32_t min_ip, max_ip;
34 +};
35 +
36 +struct ipt_iprange_info
37 +{
38 +       struct ipt_iprange src;
39 +       struct ipt_iprange dst;
40 +
41 +       /* Flags from above */
42 +       u_int8_t flags;
43 +};
44 +
45 +#endif /* _IPT_IPRANGE_H */
46 diff -ruaN linux-2.4.34.orig/net/ipv4/netfilter/Config.in linux-2.4.34/net/ipv4/netfilter/Config.in
47 --- linux-2.4.34.orig/net/ipv4/netfilter/Config.in      2007-06-01 12:17:17.000000000 +0100
48 +++ linux-2.4.34/net/ipv4/netfilter/Config.in   2007-06-01 12:20:20.000000000 +0100
49 @@ -27,6 +27,7 @@
50  if [ "$CONFIG_IP_NF_IPTABLES" != "n" ]; then
51  # The simple matches.
52    dep_tristate '  limit match support' CONFIG_IP_NF_MATCH_LIMIT $CONFIG_IP_NF_IPTABLES
53 +  dep_tristate '  IP range match support' CONFIG_IP_NF_MATCH_IPRANGE $CONFIG_IP_NF_IPTABLES
54    dep_tristate '  quota match support' CONFIG_IP_NF_MATCH_QUOTA $CONFIG_IP_NF_IPTABLES
55  
56    dep_tristate '  IP set support' CONFIG_IP_NF_SET $CONFIG_IP_NF_IPTABLES
57 diff -ruaN linux-2.4.34.orig/net/ipv4/netfilter/ipt_iprange.c linux-2.4.34/net/ipv4/netfilter/ipt_iprange.c
58 --- linux-2.4.34.orig/net/ipv4/netfilter/ipt_iprange.c  1970-01-01 01:00:00.000000000 +0100
59 +++ linux-2.4.34/net/ipv4/netfilter/ipt_iprange.c       2007-06-01 12:20:20.000000000 +0100
60 @@ -0,0 +1,101 @@
61 +/*
62 + * iptables module to match IP address ranges
63 + *   (c) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
64 + *
65 + * Released under the terms of GNU GPLv2.
66 + *
67 + */
68 +#include <linux/module.h>
69 +#include <linux/skbuff.h>
70 +#include <linux/ip.h>
71 +#include <linux/netfilter_ipv4/ip_tables.h>
72 +#include <linux/netfilter_ipv4/ipt_iprange.h>
73 +
74 +MODULE_LICENSE("GPL");
75 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
76 +MODULE_DESCRIPTION("iptables arbitrary IP range match module");
77 +
78 +#if 0
79 +#define DEBUGP printk
80 +#else
81 +#define DEBUGP(format, args...)
82 +#endif
83 +
84 +static int
85 +match(const struct sk_buff *skb,
86 +      const struct net_device *in,
87 +      const struct net_device *out,
88 +      const void *matchinfo,
89 +      int offset,
90 +      const void *hdr,
91 +      u_int16_t datalen,
92 +      int *hotdrop)
93 +{
94 +       const struct ipt_iprange_info *info = matchinfo;
95 +       const struct iphdr *iph = skb->nh.iph;
96 +       
97 +
98 +       if (info->flags & IPRANGE_SRC) {
99 +               if (((ntohl(iph->saddr) < ntohl(info->src.min_ip))
100 +                         || (ntohl(iph->saddr) > ntohl(info->src.max_ip)))
101 +                        ^ !!(info->flags & IPRANGE_SRC_INV)) {
102 +                       DEBUGP("src IP %u.%u.%u.%u NOT in range %s"
103 +                              "%u.%u.%u.%u-%u.%u.%u.%u\n",
104 +                               NIPQUAD(iph->saddr),
105 +                               info->flags & IPRANGE_SRC_INV ? "(INV) " : "",
106 +                               NIPQUAD(info->src.min_ip),
107 +                               NIPQUAD(info->src.max_ip));
108 +                       return 0;
109 +               }
110 +       }
111 +       if (info->flags & IPRANGE_DST) {
112 +               if (((ntohl(iph->daddr) < ntohl(info->dst.min_ip))
113 +                         || (ntohl(iph->daddr) > ntohl(info->dst.max_ip)))
114 +                        ^ !!(info->flags & IPRANGE_DST_INV)) {
115 +                       DEBUGP("dst IP %u.%u.%u.%u NOT in range %s"
116 +                              "%u.%u.%u.%u-%u.%u.%u.%u\n",
117 +                               NIPQUAD(iph->daddr),
118 +                               info->flags & IPRANGE_DST_INV ? "(INV) " : "",
119 +                               NIPQUAD(info->dst.min_ip),
120 +                               NIPQUAD(info->dst.max_ip));
121 +                       return 0;
122 +               }
123 +       }
124 +       return 1;
125 +}
126 +
127 +static int check(const char *tablename,
128 +                const struct ipt_ip *ip,
129 +                void *matchinfo,
130 +                unsigned int matchsize,
131 +                unsigned int hook_mask)
132 +{
133 +       /* verify size */
134 +       if (matchsize != IPT_ALIGN(sizeof(struct ipt_iprange_info)))
135 +               return 0;
136 +
137 +       return 1;
138 +}
139 +
140 +static struct ipt_match iprange_match = 
141 +{ 
142 +       .list = { NULL, NULL }, 
143 +       .name = "iprange", 
144 +       .match = &match, 
145 +       .checkentry = &check, 
146 +       .destroy = NULL, 
147 +       .me = THIS_MODULE
148 +};
149 +
150 +static int __init init(void)
151 +{
152 +       return ipt_register_match(&iprange_match);
153 +}
154 +
155 +static void __exit fini(void)
156 +{
157 +       ipt_unregister_match(&iprange_match);
158 +}
159 +
160 +module_init(init);
161 +module_exit(fini);
162 diff -ruaN linux-2.4.34.orig/net/ipv4/netfilter/Makefile linux-2.4.34/net/ipv4/netfilter/Makefile
163 --- linux-2.4.34.orig/net/ipv4/netfilter/Makefile       2007-06-01 12:17:17.000000000 +0100
164 +++ linux-2.4.34/net/ipv4/netfilter/Makefile    2007-06-01 12:20:20.000000000 +0100
165 @@ -90,6 +90,7 @@
166  # matches
167  obj-$(CONFIG_IP_NF_MATCH_HELPER) += ipt_helper.o
168  obj-$(CONFIG_IP_NF_MATCH_LIMIT) += ipt_limit.o
169 +obj-$(CONFIG_IP_NF_MATCH_IPRANGE) += ipt_iprange.o
170  obj-$(CONFIG_IP_NF_MATCH_QUOTA) += ipt_quota.o
171  obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o
172  obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o