blob: c63e9333c7550a04971084b3f688d8fe295919ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jan Engelhardtf72e25a2008-01-14 23:42:47 -08002 * xt_iprange - Netfilter module to match IP address ranges
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Jan Engelhardtf72e25a2008-01-14 23:42:47 -08004 * (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -08005 * (C) CC Computer Consultants GmbH, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Jan Engelhardtf72e25a2008-01-14 23:42:47 -08007 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11#include <linux/module.h>
12#include <linux/skbuff.h>
13#include <linux/ip.h>
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080014#include <linux/ipv6.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080015#include <linux/netfilter/x_tables.h>
Jan Engelhardt5da621f2008-02-07 17:57:11 -080016#include <linux/netfilter/xt_iprange.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/netfilter_ipv4/ipt_iprange.h>
18
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070019static bool
Jan Engelhardtf72e25a2008-01-14 23:42:47 -080020iprange_mt_v0(const struct sk_buff *skb, const struct net_device *in,
21 const struct net_device *out, const struct xt_match *match,
22 const void *matchinfo, int offset, unsigned int protoff,
23 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 const struct ipt_iprange_info *info = matchinfo;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070026 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28 if (info->flags & IPRANGE_SRC) {
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070029 if ((ntohl(iph->saddr) < ntohl(info->src.min_ip)
30 || ntohl(iph->saddr) > ntohl(info->src.max_ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 ^ !!(info->flags & IPRANGE_SRC_INV)) {
Patrick McHardy0d537782007-07-07 22:39:38 -070032 pr_debug("src IP %u.%u.%u.%u NOT in range %s"
33 "%u.%u.%u.%u-%u.%u.%u.%u\n",
34 NIPQUAD(iph->saddr),
35 info->flags & IPRANGE_SRC_INV ? "(INV) " : "",
36 NIPQUAD(info->src.min_ip),
37 NIPQUAD(info->src.max_ip));
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070038 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 }
40 }
41 if (info->flags & IPRANGE_DST) {
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070042 if ((ntohl(iph->daddr) < ntohl(info->dst.min_ip)
43 || ntohl(iph->daddr) > ntohl(info->dst.max_ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 ^ !!(info->flags & IPRANGE_DST_INV)) {
Patrick McHardy0d537782007-07-07 22:39:38 -070045 pr_debug("dst IP %u.%u.%u.%u NOT in range %s"
46 "%u.%u.%u.%u-%u.%u.%u.%u\n",
47 NIPQUAD(iph->daddr),
48 info->flags & IPRANGE_DST_INV ? "(INV) " : "",
49 NIPQUAD(info->dst.min_ip),
50 NIPQUAD(info->dst.max_ip));
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070051 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
53 }
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070054 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080057static bool
58iprange_mt4(const struct sk_buff *skb, const struct net_device *in,
59 const struct net_device *out, const struct xt_match *match,
60 const void *matchinfo, int offset, unsigned int protoff,
61 bool *hotdrop)
62{
63 const struct xt_iprange_mtinfo *info = matchinfo;
64 const struct iphdr *iph = ip_hdr(skb);
65 bool m;
66
67 if (info->flags & IPRANGE_SRC) {
68 m = ntohl(iph->saddr) < ntohl(info->src_min.ip);
69 m |= ntohl(iph->saddr) > ntohl(info->src_max.ip);
70 m ^= info->flags & IPRANGE_SRC_INV;
71 if (m) {
72 pr_debug("src IP " NIPQUAD_FMT " NOT in range %s"
73 NIPQUAD_FMT "-" NIPQUAD_FMT "\n",
74 NIPQUAD(iph->saddr),
75 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
76 NIPQUAD(info->src_max.ip),
77 NIPQUAD(info->src_max.ip));
78 return false;
79 }
80 }
81 if (info->flags & IPRANGE_DST) {
82 m = ntohl(iph->daddr) < ntohl(info->dst_min.ip);
83 m |= ntohl(iph->daddr) > ntohl(info->dst_max.ip);
84 m ^= info->flags & IPRANGE_DST_INV;
85 if (m) {
86 pr_debug("dst IP " NIPQUAD_FMT " NOT in range %s"
87 NIPQUAD_FMT "-" NIPQUAD_FMT "\n",
88 NIPQUAD(iph->daddr),
89 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
90 NIPQUAD(info->dst_min.ip),
91 NIPQUAD(info->dst_max.ip));
92 return false;
93 }
94 }
95 return true;
96}
97
98static inline int
99iprange_ipv6_sub(const struct in6_addr *a, const struct in6_addr *b)
100{
101 unsigned int i;
102 int r;
103
104 for (i = 0; i < 4; ++i) {
Jan Engelhardt27ecb1f2008-02-19 17:20:06 -0800105 r = ntohl(a->s6_addr32[i]) - ntohl(b->s6_addr32[i]);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800106 if (r != 0)
107 return r;
108 }
109
110 return 0;
111}
112
113static bool
114iprange_mt6(const struct sk_buff *skb, const struct net_device *in,
115 const struct net_device *out, const struct xt_match *match,
116 const void *matchinfo, int offset, unsigned int protoff,
117 bool *hotdrop)
118{
119 const struct xt_iprange_mtinfo *info = matchinfo;
120 const struct ipv6hdr *iph = ipv6_hdr(skb);
121 bool m;
122
123 if (info->flags & IPRANGE_SRC) {
124 m = iprange_ipv6_sub(&iph->saddr, &info->src_min.in6) < 0;
125 m |= iprange_ipv6_sub(&iph->saddr, &info->src_max.in6) > 0;
126 m ^= info->flags & IPRANGE_SRC_INV;
127 if (m)
128 return false;
129 }
130 if (info->flags & IPRANGE_DST) {
131 m = iprange_ipv6_sub(&iph->daddr, &info->dst_min.in6) < 0;
132 m |= iprange_ipv6_sub(&iph->daddr, &info->dst_max.in6) > 0;
133 m ^= info->flags & IPRANGE_DST_INV;
134 if (m)
135 return false;
136 }
137 return true;
138}
139
140static struct xt_match iprange_mt_reg[] __read_mostly = {
141 {
142 .name = "iprange",
143 .revision = 0,
144 .family = AF_INET,
145 .match = iprange_mt_v0,
146 .matchsize = sizeof(struct ipt_iprange_info),
147 .me = THIS_MODULE,
148 },
149 {
150 .name = "iprange",
151 .revision = 1,
Patrick McHardyd9d17572008-02-07 17:56:49 -0800152 .family = AF_INET,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800153 .match = iprange_mt4,
154 .matchsize = sizeof(struct xt_iprange_mtinfo),
155 .me = THIS_MODULE,
156 },
157 {
158 .name = "iprange",
159 .revision = 1,
160 .family = AF_INET6,
161 .match = iprange_mt6,
162 .matchsize = sizeof(struct xt_iprange_mtinfo),
163 .me = THIS_MODULE,
164 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800167static int __init iprange_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800169 return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800172static void __exit iprange_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800174 xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800177module_init(iprange_mt_init);
178module_exit(iprange_mt_exit);
Jan Engelhardtf72e25a2008-01-14 23:42:47 -0800179MODULE_LICENSE("GPL");
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800180MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>, Jan Engelhardt <jengelh@computergmbh.de>");
Jan Engelhardtf72e25a2008-01-14 23:42:47 -0800181MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");
Phil Oester01b7a312008-05-13 23:27:48 -0700182MODULE_ALIAS("ipt_iprange");
183MODULE_ALIAS("ip6t_iprange");