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>
14 #include <linux/ipv6.h>
15 #include <linux/netfilter/x_tables.h>
16 #include <linux/netfilter_ipv4/ipt_iprange.h>
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,
24 const struct ipt_iprange_info *info = matchinfo;
25 const struct iphdr *iph = ip_hdr(skb);
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",
34 info->flags & IPRANGE_SRC_INV ? "(INV) " : "",
35 NIPQUAD(info->src.min_ip),
36 NIPQUAD(info->src.max_ip));
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",
47 info->flags & IPRANGE_DST_INV ? "(INV) " : "",
48 NIPQUAD(info->dst.min_ip),
49 NIPQUAD(info->dst.max_ip));
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,
62 const struct xt_iprange_mtinfo *info = matchinfo;
63 const struct iphdr *iph = ip_hdr(skb);
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;
71 pr_debug("src IP " NIPQUAD_FMT " NOT in range %s"
72 NIPQUAD_FMT "-" NIPQUAD_FMT "\n",
74 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
75 NIPQUAD(info->src_max.ip),
76 NIPQUAD(info->src_max.ip));
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;
85 pr_debug("dst IP " NIPQUAD_FMT " NOT in range %s"
86 NIPQUAD_FMT "-" NIPQUAD_FMT "\n",
88 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
89 NIPQUAD(info->dst_min.ip),
90 NIPQUAD(info->dst_max.ip));
98 iprange_ipv6_sub(const struct in6_addr *a, const struct in6_addr *b)
103 for (i = 0; i < 4; ++i) {
104 r = (__force u32)a->s6_addr32[i] - (__force u32)b->s6_addr32[i];
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,
118 const struct xt_iprange_mtinfo *info = matchinfo;
119 const struct ipv6hdr *iph = ipv6_hdr(skb);
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;
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;
139 static struct xt_match iprange_mt_reg[] __read_mostly = {
144 .match = iprange_mt_v0,
145 .matchsize = sizeof(struct ipt_iprange_info),
152 .match = iprange_mt4,
153 .matchsize = sizeof(struct xt_iprange_mtinfo),
160 .match = iprange_mt6,
161 .matchsize = sizeof(struct xt_iprange_mtinfo),
166 static int __init iprange_mt_init(void)
168 return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
171 static void __exit iprange_mt_exit(void)
173 xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
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");