Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Jan Engelhardt | f72e25a | 2008-01-14 23:42:47 -0800 | [diff] [blame] | 3 | * xt_iprange - Netfilter module to match IP address ranges |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Jozsef Kadlecsik | fe03d47 | 2019-06-10 13:00:24 +0200 | [diff] [blame] | 5 | * (C) 2003 Jozsef Kadlecsik <kadlec@netfilter.org> |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 6 | * (C) CC Computer Consultants GmbH, 2008 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/skbuff.h> |
| 11 | #include <linux/ip.h> |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 12 | #include <linux/ipv6.h> |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 13 | #include <linux/netfilter/x_tables.h> |
Jan Engelhardt | 5da621f | 2008-02-07 17:57:11 -0800 | [diff] [blame] | 14 | #include <linux/netfilter/xt_iprange.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 16 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 17 | iprange_mt4(const struct sk_buff *skb, struct xt_action_param *par) |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 18 | { |
Jan Engelhardt | f7108a20 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 19 | const struct xt_iprange_mtinfo *info = par->matchinfo; |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 20 | 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 Dobriyan | 6def1eb | 2008-10-20 03:32:21 -0700 | [diff] [blame] | 26 | m ^= !!(info->flags & IPRANGE_SRC_INV); |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 27 | if (m) { |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 28 | pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n", |
| 29 | &iph->saddr, |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 30 | (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "", |
Thomas Jacob | 705ca14 | 2011-01-27 10:56:32 +0100 | [diff] [blame] | 31 | &info->src_min.ip, |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 32 | &info->src_max.ip); |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 33 | 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 Dobriyan | 6def1eb | 2008-10-20 03:32:21 -0700 | [diff] [blame] | 39 | m ^= !!(info->flags & IPRANGE_DST_INV); |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 40 | if (m) { |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 41 | pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n", |
| 42 | &iph->daddr, |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 43 | (info->flags & IPRANGE_DST_INV) ? "(INV) " : "", |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 44 | &info->dst_min.ip, |
| 45 | &info->dst_max.ip); |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 46 | return false; |
| 47 | } |
| 48 | } |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | static inline int |
Thomas Jacob | 08b5194 | 2011-01-24 21:35:36 +0100 | [diff] [blame] | 53 | iprange_ipv6_lt(const struct in6_addr *a, const struct in6_addr *b) |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 54 | { |
| 55 | unsigned int i; |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 56 | |
| 57 | for (i = 0; i < 4; ++i) { |
Thomas Jacob | 08b5194 | 2011-01-24 21:35:36 +0100 | [diff] [blame] | 58 | if (a->s6_addr32[i] != b->s6_addr32[i]) |
| 59 | return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]); |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 66 | iprange_mt6(const struct sk_buff *skb, struct xt_action_param *par) |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 67 | { |
Jan Engelhardt | f7108a20 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 68 | const struct xt_iprange_mtinfo *info = par->matchinfo; |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 69 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 70 | bool m; |
| 71 | |
| 72 | if (info->flags & IPRANGE_SRC) { |
Thomas Jacob | 08b5194 | 2011-01-24 21:35:36 +0100 | [diff] [blame] | 73 | m = iprange_ipv6_lt(&iph->saddr, &info->src_min.in6); |
| 74 | m |= iprange_ipv6_lt(&info->src_max.in6, &iph->saddr); |
Alexey Dobriyan | 6def1eb | 2008-10-20 03:32:21 -0700 | [diff] [blame] | 75 | m ^= !!(info->flags & IPRANGE_SRC_INV); |
Thomas Jacob | 6a4ddef | 2011-01-28 19:33:13 +0100 | [diff] [blame] | 76 | 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 Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 82 | return false; |
Thomas Jacob | 6a4ddef | 2011-01-28 19:33:13 +0100 | [diff] [blame] | 83 | } |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 84 | } |
| 85 | if (info->flags & IPRANGE_DST) { |
Thomas Jacob | 08b5194 | 2011-01-24 21:35:36 +0100 | [diff] [blame] | 86 | m = iprange_ipv6_lt(&iph->daddr, &info->dst_min.in6); |
| 87 | m |= iprange_ipv6_lt(&info->dst_max.in6, &iph->daddr); |
Alexey Dobriyan | 6def1eb | 2008-10-20 03:32:21 -0700 | [diff] [blame] | 88 | m ^= !!(info->flags & IPRANGE_DST_INV); |
Thomas Jacob | 6a4ddef | 2011-01-28 19:33:13 +0100 | [diff] [blame] | 89 | 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 Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 95 | return false; |
Thomas Jacob | 6a4ddef | 2011-01-28 19:33:13 +0100 | [diff] [blame] | 96 | } |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | static struct xt_match iprange_mt_reg[] __read_mostly = { |
| 102 | { |
| 103 | .name = "iprange", |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 104 | .revision = 1, |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 105 | .family = NFPROTO_IPV4, |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 106 | .match = iprange_mt4, |
| 107 | .matchsize = sizeof(struct xt_iprange_mtinfo), |
| 108 | .me = THIS_MODULE, |
| 109 | }, |
| 110 | { |
| 111 | .name = "iprange", |
| 112 | .revision = 1, |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 113 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 114 | .match = iprange_mt6, |
| 115 | .matchsize = sizeof(struct xt_iprange_mtinfo), |
| 116 | .me = THIS_MODULE, |
| 117 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 120 | static int __init iprange_mt_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 122 | return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 125 | static void __exit iprange_mt_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 127 | xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 130 | module_init(iprange_mt_init); |
| 131 | module_exit(iprange_mt_exit); |
Jan Engelhardt | f72e25a | 2008-01-14 23:42:47 -0800 | [diff] [blame] | 132 | MODULE_LICENSE("GPL"); |
Jozsef Kadlecsik | fe03d47 | 2019-06-10 13:00:24 +0200 | [diff] [blame] | 133 | MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>"); |
Jan Engelhardt | 36d4084d | 2009-06-12 18:58:19 +0200 | [diff] [blame] | 134 | MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>"); |
Jan Engelhardt | f72e25a | 2008-01-14 23:42:47 -0800 | [diff] [blame] | 135 | MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching"); |
Phil Oester | 01b7a31 | 2008-05-13 23:27:48 -0700 | [diff] [blame] | 136 | MODULE_ALIAS("ipt_iprange"); |
| 137 | MODULE_ALIAS("ip6t_iprange"); |