Merge branch 'dmapool' of git://git.kernel.org/pub/scm/linux/kernel/git/willy/misc
[pandora-kernel.git] / net / netfilter / xt_iprange.c
1 /*
2  *      xt_iprange - Netfilter module to match IP address ranges
3  *
4  *      (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5  *      (C) CC Computer Consultants GmbH, 2008
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License version 2 as
9  *      published by the Free Software Foundation.
10  */
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/ip.h>
14 #include <linux/ipv6.h>
15 #include <linux/netfilter/x_tables.h>
16 #include <linux/netfilter_ipv4/ipt_iprange.h>
17
18 static bool
19 iprange_mt_v0(const struct sk_buff *skb, const struct net_device *in,
20               const struct net_device *out, const struct xt_match *match,
21               const void *matchinfo, int offset, unsigned int protoff,
22               bool *hotdrop)
23 {
24         const struct ipt_iprange_info *info = matchinfo;
25         const struct iphdr *iph = ip_hdr(skb);
26
27         if (info->flags & IPRANGE_SRC) {
28                 if ((ntohl(iph->saddr) < ntohl(info->src.min_ip)
29                           || ntohl(iph->saddr) > ntohl(info->src.max_ip))
30                          ^ !!(info->flags & IPRANGE_SRC_INV)) {
31                         pr_debug("src IP %u.%u.%u.%u NOT in range %s"
32                                  "%u.%u.%u.%u-%u.%u.%u.%u\n",
33                                  NIPQUAD(iph->saddr),
34                                  info->flags & IPRANGE_SRC_INV ? "(INV) " : "",
35                                  NIPQUAD(info->src.min_ip),
36                                  NIPQUAD(info->src.max_ip));
37                         return false;
38                 }
39         }
40         if (info->flags & IPRANGE_DST) {
41                 if ((ntohl(iph->daddr) < ntohl(info->dst.min_ip)
42                           || ntohl(iph->daddr) > ntohl(info->dst.max_ip))
43                          ^ !!(info->flags & IPRANGE_DST_INV)) {
44                         pr_debug("dst IP %u.%u.%u.%u NOT in range %s"
45                                  "%u.%u.%u.%u-%u.%u.%u.%u\n",
46                                  NIPQUAD(iph->daddr),
47                                  info->flags & IPRANGE_DST_INV ? "(INV) " : "",
48                                  NIPQUAD(info->dst.min_ip),
49                                  NIPQUAD(info->dst.max_ip));
50                         return false;
51                 }
52         }
53         return true;
54 }
55
56 static bool
57 iprange_mt4(const struct sk_buff *skb, const struct net_device *in,
58             const struct net_device *out, const struct xt_match *match,
59             const void *matchinfo, int offset, unsigned int protoff,
60             bool *hotdrop)
61 {
62         const struct xt_iprange_mtinfo *info = matchinfo;
63         const struct iphdr *iph = ip_hdr(skb);
64         bool m;
65
66         if (info->flags & IPRANGE_SRC) {
67                 m  = ntohl(iph->saddr) < ntohl(info->src_min.ip);
68                 m |= ntohl(iph->saddr) > ntohl(info->src_max.ip);
69                 m ^= info->flags & IPRANGE_SRC_INV;
70                 if (m) {
71                         pr_debug("src IP " NIPQUAD_FMT " NOT in range %s"
72                                  NIPQUAD_FMT "-" NIPQUAD_FMT "\n",
73                                  NIPQUAD(iph->saddr),
74                                  (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
75                                  NIPQUAD(info->src_max.ip),
76                                  NIPQUAD(info->src_max.ip));
77                         return false;
78                 }
79         }
80         if (info->flags & IPRANGE_DST) {
81                 m  = ntohl(iph->daddr) < ntohl(info->dst_min.ip);
82                 m |= ntohl(iph->daddr) > ntohl(info->dst_max.ip);
83                 m ^= info->flags & IPRANGE_DST_INV;
84                 if (m) {
85                         pr_debug("dst IP " NIPQUAD_FMT " NOT in range %s"
86                                  NIPQUAD_FMT "-" NIPQUAD_FMT "\n",
87                                  NIPQUAD(iph->daddr),
88                                  (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
89                                  NIPQUAD(info->dst_min.ip),
90                                  NIPQUAD(info->dst_max.ip));
91                         return false;
92                 }
93         }
94         return true;
95 }
96
97 static inline int
98 iprange_ipv6_sub(const struct in6_addr *a, const struct in6_addr *b)
99 {
100         unsigned int i;
101         int r;
102
103         for (i = 0; i < 4; ++i) {
104                 r = (__force u32)a->s6_addr32[i] - (__force u32)b->s6_addr32[i];
105                 if (r != 0)
106                         return r;
107         }
108
109         return 0;
110 }
111
112 static bool
113 iprange_mt6(const struct sk_buff *skb, const struct net_device *in,
114             const struct net_device *out, const struct xt_match *match,
115             const void *matchinfo, int offset, unsigned int protoff,
116             bool *hotdrop)
117 {
118         const struct xt_iprange_mtinfo *info = matchinfo;
119         const struct ipv6hdr *iph = ipv6_hdr(skb);
120         bool m;
121
122         if (info->flags & IPRANGE_SRC) {
123                 m  = iprange_ipv6_sub(&iph->saddr, &info->src_min.in6) < 0;
124                 m |= iprange_ipv6_sub(&iph->saddr, &info->src_max.in6) > 0;
125                 m ^= info->flags & IPRANGE_SRC_INV;
126                 if (m)
127                         return false;
128         }
129         if (info->flags & IPRANGE_DST) {
130                 m  = iprange_ipv6_sub(&iph->daddr, &info->dst_min.in6) < 0;
131                 m |= iprange_ipv6_sub(&iph->daddr, &info->dst_max.in6) > 0;
132                 m ^= info->flags & IPRANGE_DST_INV;
133                 if (m)
134                         return false;
135         }
136         return true;
137 }
138
139 static struct xt_match iprange_mt_reg[] __read_mostly = {
140         {
141                 .name      = "iprange",
142                 .revision  = 0,
143                 .family    = AF_INET,
144                 .match     = iprange_mt_v0,
145                 .matchsize = sizeof(struct ipt_iprange_info),
146                 .me        = THIS_MODULE,
147         },
148         {
149                 .name      = "iprange",
150                 .revision  = 1,
151                 .family    = AF_INET6,
152                 .match     = iprange_mt4,
153                 .matchsize = sizeof(struct xt_iprange_mtinfo),
154                 .me        = THIS_MODULE,
155         },
156         {
157                 .name      = "iprange",
158                 .revision  = 1,
159                 .family    = AF_INET6,
160                 .match     = iprange_mt6,
161                 .matchsize = sizeof(struct xt_iprange_mtinfo),
162                 .me        = THIS_MODULE,
163         },
164 };
165
166 static int __init iprange_mt_init(void)
167 {
168         return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
169 }
170
171 static void __exit iprange_mt_exit(void)
172 {
173         xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
174 }
175
176 module_init(iprange_mt_init);
177 module_exit(iprange_mt_exit);
178 MODULE_LICENSE("GPL");
179 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>, Jan Engelhardt <jengelh@computergmbh.de>");
180 MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");