netfilter: xt_iprange: add IPv6 match debug print code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / xt_iprange.c
blobd3eb5ed1892fa4c0318cce266421c336a887797e
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/ip.h>
15 #include <linux/ipv6.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter/xt_iprange.h>
19 static bool
20 iprange_mt4(const struct sk_buff *skb, struct xt_action_param *par)
22 const struct xt_iprange_mtinfo *info = par->matchinfo;
23 const struct iphdr *iph = ip_hdr(skb);
24 bool m;
26 if (info->flags & IPRANGE_SRC) {
27 m = ntohl(iph->saddr) < ntohl(info->src_min.ip);
28 m |= ntohl(iph->saddr) > ntohl(info->src_max.ip);
29 m ^= !!(info->flags & IPRANGE_SRC_INV);
30 if (m) {
31 pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n",
32 &iph->saddr,
33 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
34 &info->src_min.ip,
35 &info->src_max.ip);
36 return false;
39 if (info->flags & IPRANGE_DST) {
40 m = ntohl(iph->daddr) < ntohl(info->dst_min.ip);
41 m |= ntohl(iph->daddr) > ntohl(info->dst_max.ip);
42 m ^= !!(info->flags & IPRANGE_DST_INV);
43 if (m) {
44 pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n",
45 &iph->daddr,
46 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
47 &info->dst_min.ip,
48 &info->dst_max.ip);
49 return false;
52 return true;
55 static inline int
56 iprange_ipv6_sub(const struct in6_addr *a, const struct in6_addr *b)
58 unsigned int i;
59 int r;
61 for (i = 0; i < 4; ++i) {
62 r = ntohl(a->s6_addr32[i]) - ntohl(b->s6_addr32[i]);
63 if (r != 0)
64 return r;
67 return 0;
70 static bool
71 iprange_mt6(const struct sk_buff *skb, struct xt_action_param *par)
73 const struct xt_iprange_mtinfo *info = par->matchinfo;
74 const struct ipv6hdr *iph = ipv6_hdr(skb);
75 bool m;
77 if (info->flags & IPRANGE_SRC) {
78 m = iprange_ipv6_sub(&iph->saddr, &info->src_min.in6) < 0;
79 m |= iprange_ipv6_sub(&iph->saddr, &info->src_max.in6) > 0;
80 m ^= !!(info->flags & IPRANGE_SRC_INV);
81 if (m) {
82 pr_debug("src IP %pI6 NOT in range %s%pI6-%pI6\n",
83 &iph->saddr,
84 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
85 &info->src_min.in6,
86 &info->src_max.in6);
87 return false;
90 if (info->flags & IPRANGE_DST) {
91 m = iprange_ipv6_sub(&iph->daddr, &info->dst_min.in6) < 0;
92 m |= iprange_ipv6_sub(&iph->daddr, &info->dst_max.in6) > 0;
93 m ^= !!(info->flags & IPRANGE_DST_INV);
94 if (m) {
95 pr_debug("dst IP %pI6 NOT in range %s%pI6-%pI6\n",
96 &iph->daddr,
97 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
98 &info->dst_min.in6,
99 &info->dst_max.in6);
100 return false;
103 return true;
106 static struct xt_match iprange_mt_reg[] __read_mostly = {
108 .name = "iprange",
109 .revision = 1,
110 .family = NFPROTO_IPV4,
111 .match = iprange_mt4,
112 .matchsize = sizeof(struct xt_iprange_mtinfo),
113 .me = THIS_MODULE,
116 .name = "iprange",
117 .revision = 1,
118 .family = NFPROTO_IPV6,
119 .match = iprange_mt6,
120 .matchsize = sizeof(struct xt_iprange_mtinfo),
121 .me = THIS_MODULE,
125 static int __init iprange_mt_init(void)
127 return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
130 static void __exit iprange_mt_exit(void)
132 xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
135 module_init(iprange_mt_init);
136 module_exit(iprange_mt_exit);
137 MODULE_LICENSE("GPL");
138 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
139 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
140 MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");
141 MODULE_ALIAS("ipt_iprange");
142 MODULE_ALIAS("ip6t_iprange");