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