blob: ffc96387d5565fa43286739d82902a37e7a9aa83 [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
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080018static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +020019iprange_mt4(const struct sk_buff *skb, const struct xt_match_param *par)
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080020{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020021 const struct xt_iprange_mtinfo *info = par->matchinfo;
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080022 const struct iphdr *iph = ip_hdr(skb);
23 bool m;
24
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);
Alexey Dobriyan6def1eb2008-10-20 03:32:21 -070028 m ^= !!(info->flags & IPRANGE_SRC_INV);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080029 if (m) {
Harvey Harrison14d5e832008-10-31 00:54:29 -070030 pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n",
31 &iph->saddr,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080032 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
Harvey Harrison14d5e832008-10-31 00:54:29 -070033 &info->src_max.ip,
34 &info->src_max.ip);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080035 return false;
36 }
37 }
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);
Alexey Dobriyan6def1eb2008-10-20 03:32:21 -070041 m ^= !!(info->flags & IPRANGE_DST_INV);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080042 if (m) {
Harvey Harrison14d5e832008-10-31 00:54:29 -070043 pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n",
44 &iph->daddr,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080045 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
Harvey Harrison14d5e832008-10-31 00:54:29 -070046 &info->dst_min.ip,
47 &info->dst_max.ip);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080048 return false;
49 }
50 }
51 return true;
52}
53
54static inline int
55iprange_ipv6_sub(const struct in6_addr *a, const struct in6_addr *b)
56{
57 unsigned int i;
58 int r;
59
60 for (i = 0; i < 4; ++i) {
Jan Engelhardt27ecb1f2008-02-19 17:20:06 -080061 r = ntohl(a->s6_addr32[i]) - ntohl(b->s6_addr32[i]);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080062 if (r != 0)
63 return r;
64 }
65
66 return 0;
67}
68
69static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +020070iprange_mt6(const struct sk_buff *skb, const struct xt_match_param *par)
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080071{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020072 const struct xt_iprange_mtinfo *info = par->matchinfo;
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080073 const struct ipv6hdr *iph = ipv6_hdr(skb);
74 bool m;
75
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;
Alexey Dobriyan6def1eb2008-10-20 03:32:21 -070079 m ^= !!(info->flags & IPRANGE_SRC_INV);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080080 if (m)
81 return false;
82 }
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;
Alexey Dobriyan6def1eb2008-10-20 03:32:21 -070086 m ^= !!(info->flags & IPRANGE_DST_INV);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080087 if (m)
88 return false;
89 }
90 return true;
91}
92
93static struct xt_match iprange_mt_reg[] __read_mostly = {
94 {
95 .name = "iprange",
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080096 .revision = 1,
Jan Engelhardtee999d82008-10-08 11:35:01 +020097 .family = NFPROTO_IPV4,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080098 .match = iprange_mt4,
99 .matchsize = sizeof(struct xt_iprange_mtinfo),
100 .me = THIS_MODULE,
101 },
102 {
103 .name = "iprange",
104 .revision = 1,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200105 .family = NFPROTO_IPV6,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800106 .match = iprange_mt6,
107 .matchsize = sizeof(struct xt_iprange_mtinfo),
108 .me = THIS_MODULE,
109 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800112static int __init iprange_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800114 return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800117static void __exit iprange_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800119 xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800122module_init(iprange_mt_init);
123module_exit(iprange_mt_exit);
Jan Engelhardtf72e25a2008-01-14 23:42:47 -0800124MODULE_LICENSE("GPL");
Jan Engelhardt36d4084d2009-06-12 18:58:19 +0200125MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
126MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
Jan Engelhardtf72e25a2008-01-14 23:42:47 -0800127MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");
Phil Oester01b7a312008-05-13 23:27:48 -0700128MODULE_ALIAS("ipt_iprange");
129MODULE_ALIAS("ip6t_iprange");