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 | */ |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/skbuff.h> |
| 13 | #include <linux/ip.h> |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 14 | #include <linux/ipv6.h> |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 15 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/netfilter_ipv4/ipt_iprange.h> |
| 17 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 18 | static bool |
Jan Engelhardt | f72e25a | 2008-01-14 23:42:47 -0800 | [diff] [blame] | 19 | iprange_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
| 24 | const struct ipt_iprange_info *info = matchinfo; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 25 | const struct iphdr *iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | if (info->flags & IPRANGE_SRC) { |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 28 | if ((ntohl(iph->saddr) < ntohl(info->src.min_ip) |
| 29 | || ntohl(iph->saddr) > ntohl(info->src.max_ip)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | ^ !!(info->flags & IPRANGE_SRC_INV)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 31 | 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 Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 37 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | if (info->flags & IPRANGE_DST) { |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 41 | if ((ntohl(iph->daddr) < ntohl(info->dst.min_ip) |
| 42 | || ntohl(iph->daddr) > ntohl(info->dst.max_ip)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | ^ !!(info->flags & IPRANGE_DST_INV)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 44 | 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 Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 50 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 53 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 56 | static bool |
| 57 | iprange_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 | |
| 97 | static inline int |
| 98 | iprange_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 | |
| 112 | static bool |
| 113 | iprange_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 | |
| 139 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 166 | static int __init iprange_mt_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 168 | return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 171 | static void __exit iprange_mt_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 173 | xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 176 | module_init(iprange_mt_init); |
| 177 | module_exit(iprange_mt_exit); |
Jan Engelhardt | f72e25a | 2008-01-14 23:42:47 -0800 | [diff] [blame] | 178 | MODULE_LICENSE("GPL"); |
Jan Engelhardt | 1a50c5a | 2008-01-14 23:43:03 -0800 | [diff] [blame] | 179 | MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>, Jan Engelhardt <jengelh@computergmbh.de>"); |
Jan Engelhardt | f72e25a | 2008-01-14 23:42:47 -0800 | [diff] [blame] | 180 | MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching"); |