]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netfilter/xt_iprange.c
net: replace NIPQUAD() in net/netfilter/
[net-next-2.6.git] / net / netfilter / xt_iprange.c
CommitLineData
1da177e4 1/*
f72e25a8 2 * xt_iprange - Netfilter module to match IP address ranges
1da177e4 3 *
f72e25a8 4 * (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1a50c5a1 5 * (C) CC Computer Consultants GmbH, 2008
1da177e4 6 *
f72e25a8
JE
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.
1da177e4
LT
10 */
11#include <linux/module.h>
12#include <linux/skbuff.h>
13#include <linux/ip.h>
1a50c5a1 14#include <linux/ipv6.h>
6709dbbb 15#include <linux/netfilter/x_tables.h>
5da621f1 16#include <linux/netfilter/xt_iprange.h>
1da177e4
LT
17#include <linux/netfilter_ipv4/ipt_iprange.h>
18
1d93a9cb 19static bool
f7108a20 20iprange_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par)
1da177e4 21{
f7108a20 22 const struct ipt_iprange_info *info = par->matchinfo;
eddc9ec5 23 const struct iphdr *iph = ip_hdr(skb);
1da177e4
LT
24
25 if (info->flags & IPRANGE_SRC) {
7c4e36bc
JE
26 if ((ntohl(iph->saddr) < ntohl(info->src.min_ip)
27 || ntohl(iph->saddr) > ntohl(info->src.max_ip))
1da177e4 28 ^ !!(info->flags & IPRANGE_SRC_INV)) {
14d5e834
HH
29 pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n",
30 &iph->saddr,
0d53778e 31 info->flags & IPRANGE_SRC_INV ? "(INV) " : "",
14d5e834
HH
32 &info->src.min_ip,
33 &info->src.max_ip);
1d93a9cb 34 return false;
1da177e4
LT
35 }
36 }
37 if (info->flags & IPRANGE_DST) {
7c4e36bc
JE
38 if ((ntohl(iph->daddr) < ntohl(info->dst.min_ip)
39 || ntohl(iph->daddr) > ntohl(info->dst.max_ip))
1da177e4 40 ^ !!(info->flags & IPRANGE_DST_INV)) {
14d5e834
HH
41 pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n",
42 &iph->daddr,
0d53778e 43 info->flags & IPRANGE_DST_INV ? "(INV) " : "",
14d5e834
HH
44 &info->dst.min_ip,
45 &info->dst.max_ip);
1d93a9cb 46 return false;
1da177e4
LT
47 }
48 }
1d93a9cb 49 return true;
1da177e4
LT
50}
51
1a50c5a1 52static bool
f7108a20 53iprange_mt4(const struct sk_buff *skb, const struct xt_match_param *par)
1a50c5a1 54{
f7108a20 55 const struct xt_iprange_mtinfo *info = par->matchinfo;
1a50c5a1
JE
56 const struct iphdr *iph = ip_hdr(skb);
57 bool m;
58
59 if (info->flags & IPRANGE_SRC) {
60 m = ntohl(iph->saddr) < ntohl(info->src_min.ip);
61 m |= ntohl(iph->saddr) > ntohl(info->src_max.ip);
6def1eb4 62 m ^= !!(info->flags & IPRANGE_SRC_INV);
1a50c5a1 63 if (m) {
14d5e834
HH
64 pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n",
65 &iph->saddr,
1a50c5a1 66 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
14d5e834
HH
67 &info->src_max.ip,
68 &info->src_max.ip);
1a50c5a1
JE
69 return false;
70 }
71 }
72 if (info->flags & IPRANGE_DST) {
73 m = ntohl(iph->daddr) < ntohl(info->dst_min.ip);
74 m |= ntohl(iph->daddr) > ntohl(info->dst_max.ip);
6def1eb4 75 m ^= !!(info->flags & IPRANGE_DST_INV);
1a50c5a1 76 if (m) {
14d5e834
HH
77 pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n",
78 &iph->daddr,
1a50c5a1 79 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
14d5e834
HH
80 &info->dst_min.ip,
81 &info->dst_max.ip);
1a50c5a1
JE
82 return false;
83 }
84 }
85 return true;
86}
87
88static inline int
89iprange_ipv6_sub(const struct in6_addr *a, const struct in6_addr *b)
90{
91 unsigned int i;
92 int r;
93
94 for (i = 0; i < 4; ++i) {
27ecb1ff 95 r = ntohl(a->s6_addr32[i]) - ntohl(b->s6_addr32[i]);
1a50c5a1
JE
96 if (r != 0)
97 return r;
98 }
99
100 return 0;
101}
102
103static bool
f7108a20 104iprange_mt6(const struct sk_buff *skb, const struct xt_match_param *par)
1a50c5a1 105{
f7108a20 106 const struct xt_iprange_mtinfo *info = par->matchinfo;
1a50c5a1
JE
107 const struct ipv6hdr *iph = ipv6_hdr(skb);
108 bool m;
109
110 if (info->flags & IPRANGE_SRC) {
111 m = iprange_ipv6_sub(&iph->saddr, &info->src_min.in6) < 0;
112 m |= iprange_ipv6_sub(&iph->saddr, &info->src_max.in6) > 0;
6def1eb4 113 m ^= !!(info->flags & IPRANGE_SRC_INV);
1a50c5a1
JE
114 if (m)
115 return false;
116 }
117 if (info->flags & IPRANGE_DST) {
118 m = iprange_ipv6_sub(&iph->daddr, &info->dst_min.in6) < 0;
119 m |= iprange_ipv6_sub(&iph->daddr, &info->dst_max.in6) > 0;
6def1eb4 120 m ^= !!(info->flags & IPRANGE_DST_INV);
1a50c5a1
JE
121 if (m)
122 return false;
123 }
124 return true;
125}
126
127static struct xt_match iprange_mt_reg[] __read_mostly = {
128 {
129 .name = "iprange",
130 .revision = 0,
ee999d8b 131 .family = NFPROTO_IPV4,
1a50c5a1
JE
132 .match = iprange_mt_v0,
133 .matchsize = sizeof(struct ipt_iprange_info),
134 .me = THIS_MODULE,
135 },
136 {
137 .name = "iprange",
138 .revision = 1,
ee999d8b 139 .family = NFPROTO_IPV4,
1a50c5a1
JE
140 .match = iprange_mt4,
141 .matchsize = sizeof(struct xt_iprange_mtinfo),
142 .me = THIS_MODULE,
143 },
144 {
145 .name = "iprange",
146 .revision = 1,
ee999d8b 147 .family = NFPROTO_IPV6,
1a50c5a1
JE
148 .match = iprange_mt6,
149 .matchsize = sizeof(struct xt_iprange_mtinfo),
150 .me = THIS_MODULE,
151 },
1da177e4
LT
152};
153
d3c5ee6d 154static int __init iprange_mt_init(void)
1da177e4 155{
1a50c5a1 156 return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
1da177e4
LT
157}
158
d3c5ee6d 159static void __exit iprange_mt_exit(void)
1da177e4 160{
1a50c5a1 161 xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
1da177e4
LT
162}
163
d3c5ee6d
JE
164module_init(iprange_mt_init);
165module_exit(iprange_mt_exit);
f72e25a8 166MODULE_LICENSE("GPL");
1a50c5a1 167MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>, Jan Engelhardt <jengelh@computergmbh.de>");
f72e25a8 168MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");
01b7a314
PO
169MODULE_ALIAS("ipt_iprange");
170MODULE_ALIAS("ip6t_iprange");