Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * iptables module to match IP address ranges |
| 3 | * |
| 4 | * (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/ip.h> |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 13 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/netfilter_ipv4/ipt_iprange.h> |
| 15 | |
| 16 | MODULE_LICENSE("GPL"); |
| 17 | MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); |
| 18 | MODULE_DESCRIPTION("iptables arbitrary IP range match module"); |
| 19 | |
| 20 | #if 0 |
| 21 | #define DEBUGP printk |
| 22 | #else |
| 23 | #define DEBUGP(format, args...) |
| 24 | #endif |
| 25 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 26 | static bool |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | match(const struct sk_buff *skb, |
| 28 | const struct net_device *in, |
| 29 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 30 | const struct xt_match *match, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | const void *matchinfo, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 32 | int offset, unsigned int protoff, bool *hotdrop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
| 34 | const struct ipt_iprange_info *info = matchinfo; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 35 | const struct iphdr *iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | if (info->flags & IPRANGE_SRC) { |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 38 | if ((ntohl(iph->saddr) < ntohl(info->src.min_ip) |
| 39 | || ntohl(iph->saddr) > ntohl(info->src.max_ip)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | ^ !!(info->flags & IPRANGE_SRC_INV)) { |
| 41 | DEBUGP("src IP %u.%u.%u.%u NOT in range %s" |
| 42 | "%u.%u.%u.%u-%u.%u.%u.%u\n", |
| 43 | NIPQUAD(iph->saddr), |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 44 | info->flags & IPRANGE_SRC_INV ? "(INV) " : "", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | NIPQUAD(info->src.min_ip), |
| 46 | NIPQUAD(info->src.max_ip)); |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 47 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | if (info->flags & IPRANGE_DST) { |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 51 | if ((ntohl(iph->daddr) < ntohl(info->dst.min_ip) |
| 52 | || ntohl(iph->daddr) > ntohl(info->dst.max_ip)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | ^ !!(info->flags & IPRANGE_DST_INV)) { |
| 54 | DEBUGP("dst IP %u.%u.%u.%u NOT in range %s" |
| 55 | "%u.%u.%u.%u-%u.%u.%u.%u\n", |
| 56 | NIPQUAD(iph->daddr), |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 57 | info->flags & IPRANGE_DST_INV ? "(INV) " : "", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | NIPQUAD(info->dst.min_ip), |
| 59 | NIPQUAD(info->dst.max_ip)); |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 60 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 63 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Patrick McHardy | 9f15c53 | 2007-07-07 22:22:02 -0700 | [diff] [blame] | 66 | static struct xt_match iprange_match __read_mostly = { |
Patrick McHardy | 1d5cd90 | 2006-03-20 18:01:14 -0800 | [diff] [blame] | 67 | .name = "iprange", |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 68 | .family = AF_INET, |
Patrick McHardy | 1d5cd90 | 2006-03-20 18:01:14 -0800 | [diff] [blame] | 69 | .match = match, |
| 70 | .matchsize = sizeof(struct ipt_iprange_info), |
Patrick McHardy | 1d5cd90 | 2006-03-20 18:01:14 -0800 | [diff] [blame] | 71 | .me = THIS_MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 74 | static int __init ipt_iprange_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 76 | return xt_register_match(&iprange_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 79 | static void __exit ipt_iprange_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 81 | xt_unregister_match(&iprange_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 84 | module_init(ipt_iprange_init); |
| 85 | module_exit(ipt_iprange_fini); |