blob: dbea0e0893f371bc8100cfa1954fda8832de31f2 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/netfilter_ipv4/ipt_iprange.h>
17
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070018static bool
Jan Engelhardtf72e25a2008-01-14 23:42:47 -080019iprange_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,
22 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
24 const struct ipt_iprange_info *info = matchinfo;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070025 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27 if (info->flags & IPRANGE_SRC) {
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070028 if ((ntohl(iph->saddr) < ntohl(info->src.min_ip)
29 || ntohl(iph->saddr) > ntohl(info->src.max_ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 ^ !!(info->flags & IPRANGE_SRC_INV)) {
Patrick McHardy0d537782007-07-07 22:39:38 -070031 pr_debug("src IP %u.%u.%u.%u NOT in range %s"
32 "%u.%u.%u.%u-%u.%u.%u.%u\n",
33 NIPQUAD(iph->saddr),
34 info->flags & IPRANGE_SRC_INV ? "(INV) " : "",
35 NIPQUAD(info->src.min_ip),
36 NIPQUAD(info->src.max_ip));
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070037 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 }
39 }
40 if (info->flags & IPRANGE_DST) {
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070041 if ((ntohl(iph->daddr) < ntohl(info->dst.min_ip)
42 || ntohl(iph->daddr) > ntohl(info->dst.max_ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 ^ !!(info->flags & IPRANGE_DST_INV)) {
Patrick McHardy0d537782007-07-07 22:39:38 -070044 pr_debug("dst IP %u.%u.%u.%u NOT in range %s"
45 "%u.%u.%u.%u-%u.%u.%u.%u\n",
46 NIPQUAD(iph->daddr),
47 info->flags & IPRANGE_DST_INV ? "(INV) " : "",
48 NIPQUAD(info->dst.min_ip),
49 NIPQUAD(info->dst.max_ip));
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070050 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
52 }
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070053 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080056static bool
57iprange_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,
60 bool *hotdrop)
61{
62 const struct xt_iprange_mtinfo *info = matchinfo;
63 const struct iphdr *iph = ip_hdr(skb);
64 bool m;
65
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;
70 if (m) {
71 pr_debug("src IP " NIPQUAD_FMT " NOT in range %s"
72 NIPQUAD_FMT "-" NIPQUAD_FMT "\n",
73 NIPQUAD(iph->saddr),
74 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
75 NIPQUAD(info->src_max.ip),
76 NIPQUAD(info->src_max.ip));
77 return false;
78 }
79 }
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;
84 if (m) {
85 pr_debug("dst IP " NIPQUAD_FMT " NOT in range %s"
86 NIPQUAD_FMT "-" NIPQUAD_FMT "\n",
87 NIPQUAD(iph->daddr),
88 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
89 NIPQUAD(info->dst_min.ip),
90 NIPQUAD(info->dst_max.ip));
91 return false;
92 }
93 }
94 return true;
95}
96
97static inline int
98iprange_ipv6_sub(const struct in6_addr *a, const struct in6_addr *b)
99{
100 unsigned int i;
101 int r;
102
103 for (i = 0; i < 4; ++i) {
104 r = a->s6_addr32[i] - b->s6_addr32[i];
105 if (r != 0)
106 return r;
107 }
108
109 return 0;
110}
111
112static bool
113iprange_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,
116 bool *hotdrop)
117{
118 const struct xt_iprange_mtinfo *info = matchinfo;
119 const struct ipv6hdr *iph = ipv6_hdr(skb);
120 bool m;
121
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;
126 if (m)
127 return false;
128 }
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;
133 if (m)
134 return false;
135 }
136 return true;
137}
138
139static struct xt_match iprange_mt_reg[] __read_mostly = {
140 {
141 .name = "iprange",
142 .revision = 0,
143 .family = AF_INET,
144 .match = iprange_mt_v0,
145 .matchsize = sizeof(struct ipt_iprange_info),
146 .me = THIS_MODULE,
147 },
148 {
149 .name = "iprange",
150 .revision = 1,
151 .family = AF_INET6,
152 .match = iprange_mt4,
153 .matchsize = sizeof(struct xt_iprange_mtinfo),
154 .me = THIS_MODULE,
155 },
156 {
157 .name = "iprange",
158 .revision = 1,
159 .family = AF_INET6,
160 .match = iprange_mt6,
161 .matchsize = sizeof(struct xt_iprange_mtinfo),
162 .me = THIS_MODULE,
163 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164};
165
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800166static int __init iprange_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800168 return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800171static void __exit iprange_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800173 xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800176module_init(iprange_mt_init);
177module_exit(iprange_mt_exit);
Jan Engelhardtf72e25a2008-01-14 23:42:47 -0800178MODULE_LICENSE("GPL");
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800179MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>, Jan Engelhardt <jengelh@computergmbh.de>");
Jan Engelhardtf72e25a2008-01-14 23:42:47 -0800180MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");