code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.34.14-moxart.git] / net / netfilter / xt_iprange.c
blobffc96387d5565fa43286739d82902a37e7a9aa83
1 /*
2 * xt_iprange - Netfilter module to match IP address ranges
4 * (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5 * (C) CC Computer Consultants GmbH, 2008
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.
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/xt_iprange.h>
18 static bool
19 iprange_mt4(const struct sk_buff *skb, const struct xt_match_param *par)
21 const struct xt_iprange_mtinfo *info = par->matchinfo;
22 const struct iphdr *iph = ip_hdr(skb);
23 bool m;
25 if (info->flags & IPRANGE_SRC) {
26 m = ntohl(iph->saddr) < ntohl(info->src_min.ip);
27 m |= ntohl(iph->saddr) > ntohl(info->src_max.ip);
28 m ^= !!(info->flags & IPRANGE_SRC_INV);
29 if (m) {
30 pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n",
31 &iph->saddr,
32 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
33 &info->src_max.ip,
34 &info->src_max.ip);
35 return false;
38 if (info->flags & IPRANGE_DST) {
39 m = ntohl(iph->daddr) < ntohl(info->dst_min.ip);
40 m |= ntohl(iph->daddr) > ntohl(info->dst_max.ip);
41 m ^= !!(info->flags & IPRANGE_DST_INV);
42 if (m) {
43 pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n",
44 &iph->daddr,
45 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
46 &info->dst_min.ip,
47 &info->dst_max.ip);
48 return false;
51 return true;
54 static inline int
55 iprange_ipv6_sub(const struct in6_addr *a, const struct in6_addr *b)
57 unsigned int i;
58 int r;
60 for (i = 0; i < 4; ++i) {
61 r = ntohl(a->s6_addr32[i]) - ntohl(b->s6_addr32[i]);
62 if (r != 0)
63 return r;
66 return 0;
69 static bool
70 iprange_mt6(const struct sk_buff *skb, const struct xt_match_param *par)
72 const struct xt_iprange_mtinfo *info = par->matchinfo;
73 const struct ipv6hdr *iph = ipv6_hdr(skb);
74 bool m;
76 if (info->flags & IPRANGE_SRC) {
77 m = iprange_ipv6_sub(&iph->saddr, &info->src_min.in6) < 0;
78 m |= iprange_ipv6_sub(&iph->saddr, &info->src_max.in6) > 0;
79 m ^= !!(info->flags & IPRANGE_SRC_INV);
80 if (m)
81 return false;
83 if (info->flags & IPRANGE_DST) {
84 m = iprange_ipv6_sub(&iph->daddr, &info->dst_min.in6) < 0;
85 m |= iprange_ipv6_sub(&iph->daddr, &info->dst_max.in6) > 0;
86 m ^= !!(info->flags & IPRANGE_DST_INV);
87 if (m)
88 return false;
90 return true;
93 static struct xt_match iprange_mt_reg[] __read_mostly = {
95 .name = "iprange",
96 .revision = 1,
97 .family = NFPROTO_IPV4,
98 .match = iprange_mt4,
99 .matchsize = sizeof(struct xt_iprange_mtinfo),
100 .me = THIS_MODULE,
103 .name = "iprange",
104 .revision = 1,
105 .family = NFPROTO_IPV6,
106 .match = iprange_mt6,
107 .matchsize = sizeof(struct xt_iprange_mtinfo),
108 .me = THIS_MODULE,
112 static int __init iprange_mt_init(void)
114 return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
117 static void __exit iprange_mt_exit(void)
119 xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
122 module_init(iprange_mt_init);
123 module_exit(iprange_mt_exit);
124 MODULE_LICENSE("GPL");
125 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
126 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
127 MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");
128 MODULE_ALIAS("ipt_iprange");
129 MODULE_ALIAS("ip6t_iprange");