blob: 0c9e014e30b494ca19363f244d5aa8290645fd12 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jan Engelhardtf72e25a2008-01-14 23:42:47 -08003 * xt_iprange - Netfilter module to match IP address ranges
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Jozsef Kadlecsikfe03d472019-06-10 13:00:24 +02005 * (C) 2003 Jozsef Kadlecsik <kadlec@netfilter.org>
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -08006 * (C) CC Computer Consultants GmbH, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/skbuff.h>
11#include <linux/ip.h>
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080012#include <linux/ipv6.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080013#include <linux/netfilter/x_tables.h>
Jan Engelhardt5da621f2008-02-07 17:57:11 -080014#include <linux/netfilter/xt_iprange.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080016static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020017iprange_mt4(const struct sk_buff *skb, struct xt_action_param *par)
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080018{
Jan Engelhardtf7108a202008-10-08 11:35:18 +020019 const struct xt_iprange_mtinfo *info = par->matchinfo;
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080020 const struct iphdr *iph = ip_hdr(skb);
21 bool m;
22
23 if (info->flags & IPRANGE_SRC) {
24 m = ntohl(iph->saddr) < ntohl(info->src_min.ip);
25 m |= ntohl(iph->saddr) > ntohl(info->src_max.ip);
Alexey Dobriyan6def1eb2008-10-20 03:32:21 -070026 m ^= !!(info->flags & IPRANGE_SRC_INV);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080027 if (m) {
Harvey Harrison14d5e832008-10-31 00:54:29 -070028 pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n",
29 &iph->saddr,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080030 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
Thomas Jacob705ca142011-01-27 10:56:32 +010031 &info->src_min.ip,
Harvey Harrison14d5e832008-10-31 00:54:29 -070032 &info->src_max.ip);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080033 return false;
34 }
35 }
36 if (info->flags & IPRANGE_DST) {
37 m = ntohl(iph->daddr) < ntohl(info->dst_min.ip);
38 m |= ntohl(iph->daddr) > ntohl(info->dst_max.ip);
Alexey Dobriyan6def1eb2008-10-20 03:32:21 -070039 m ^= !!(info->flags & IPRANGE_DST_INV);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080040 if (m) {
Harvey Harrison14d5e832008-10-31 00:54:29 -070041 pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n",
42 &iph->daddr,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080043 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
Harvey Harrison14d5e832008-10-31 00:54:29 -070044 &info->dst_min.ip,
45 &info->dst_max.ip);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080046 return false;
47 }
48 }
49 return true;
50}
51
52static inline int
Thomas Jacob08b51942011-01-24 21:35:36 +010053iprange_ipv6_lt(const struct in6_addr *a, const struct in6_addr *b)
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080054{
55 unsigned int i;
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080056
57 for (i = 0; i < 4; ++i) {
Thomas Jacob08b51942011-01-24 21:35:36 +010058 if (a->s6_addr32[i] != b->s6_addr32[i])
59 return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080060 }
61
62 return 0;
63}
64
65static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020066iprange_mt6(const struct sk_buff *skb, struct xt_action_param *par)
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080067{
Jan Engelhardtf7108a202008-10-08 11:35:18 +020068 const struct xt_iprange_mtinfo *info = par->matchinfo;
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080069 const struct ipv6hdr *iph = ipv6_hdr(skb);
70 bool m;
71
72 if (info->flags & IPRANGE_SRC) {
Thomas Jacob08b51942011-01-24 21:35:36 +010073 m = iprange_ipv6_lt(&iph->saddr, &info->src_min.in6);
74 m |= iprange_ipv6_lt(&info->src_max.in6, &iph->saddr);
Alexey Dobriyan6def1eb2008-10-20 03:32:21 -070075 m ^= !!(info->flags & IPRANGE_SRC_INV);
Thomas Jacob6a4ddef2011-01-28 19:33:13 +010076 if (m) {
77 pr_debug("src IP %pI6 NOT in range %s%pI6-%pI6\n",
78 &iph->saddr,
79 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
80 &info->src_min.in6,
81 &info->src_max.in6);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080082 return false;
Thomas Jacob6a4ddef2011-01-28 19:33:13 +010083 }
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080084 }
85 if (info->flags & IPRANGE_DST) {
Thomas Jacob08b51942011-01-24 21:35:36 +010086 m = iprange_ipv6_lt(&iph->daddr, &info->dst_min.in6);
87 m |= iprange_ipv6_lt(&info->dst_max.in6, &iph->daddr);
Alexey Dobriyan6def1eb2008-10-20 03:32:21 -070088 m ^= !!(info->flags & IPRANGE_DST_INV);
Thomas Jacob6a4ddef2011-01-28 19:33:13 +010089 if (m) {
90 pr_debug("dst IP %pI6 NOT in range %s%pI6-%pI6\n",
91 &iph->daddr,
92 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
93 &info->dst_min.in6,
94 &info->dst_max.in6);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080095 return false;
Thomas Jacob6a4ddef2011-01-28 19:33:13 +010096 }
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080097 }
98 return true;
99}
100
101static struct xt_match iprange_mt_reg[] __read_mostly = {
102 {
103 .name = "iprange",
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800104 .revision = 1,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200105 .family = NFPROTO_IPV4,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800106 .match = iprange_mt4,
107 .matchsize = sizeof(struct xt_iprange_mtinfo),
108 .me = THIS_MODULE,
109 },
110 {
111 .name = "iprange",
112 .revision = 1,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200113 .family = NFPROTO_IPV6,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800114 .match = iprange_mt6,
115 .matchsize = sizeof(struct xt_iprange_mtinfo),
116 .me = THIS_MODULE,
117 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800120static int __init iprange_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800122 return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800125static void __exit iprange_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800127 xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800130module_init(iprange_mt_init);
131module_exit(iprange_mt_exit);
Jan Engelhardtf72e25a2008-01-14 23:42:47 -0800132MODULE_LICENSE("GPL");
Jozsef Kadlecsikfe03d472019-06-10 13:00:24 +0200133MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
Jan Engelhardt36d4084d2009-06-12 18:58:19 +0200134MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
Jan Engelhardtf72e25a2008-01-14 23:42:47 -0800135MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");
Phil Oester01b7a312008-05-13 23:27:48 -0700136MODULE_ALIAS("ipt_iprange");
137MODULE_ALIAS("ip6t_iprange");