blob: 0106dc955a69900ba8593bb9b8ee517c71562983 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Engelhardt6709dbb2007-02-07 15:11:19 -080013#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/netfilter_ipv4/ipt_iprange.h>
15
16MODULE_LICENSE("GPL");
17MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
18MODULE_DESCRIPTION("iptables arbitrary IP range match module");
19
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070020static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070021match(const struct sk_buff *skb,
22 const struct net_device *in,
23 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080024 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 const void *matchinfo,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070026 int offset, unsigned int protoff, bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 const struct ipt_iprange_info *info = matchinfo;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070029 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31 if (info->flags & IPRANGE_SRC) {
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070032 if ((ntohl(iph->saddr) < ntohl(info->src.min_ip)
33 || ntohl(iph->saddr) > ntohl(info->src.max_ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 ^ !!(info->flags & IPRANGE_SRC_INV)) {
Patrick McHardy0d537782007-07-07 22:39:38 -070035 pr_debug("src IP %u.%u.%u.%u NOT in range %s"
36 "%u.%u.%u.%u-%u.%u.%u.%u\n",
37 NIPQUAD(iph->saddr),
38 info->flags & IPRANGE_SRC_INV ? "(INV) " : "",
39 NIPQUAD(info->src.min_ip),
40 NIPQUAD(info->src.max_ip));
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070041 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 }
43 }
44 if (info->flags & IPRANGE_DST) {
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070045 if ((ntohl(iph->daddr) < ntohl(info->dst.min_ip)
46 || ntohl(iph->daddr) > ntohl(info->dst.max_ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 ^ !!(info->flags & IPRANGE_DST_INV)) {
Patrick McHardy0d537782007-07-07 22:39:38 -070048 pr_debug("dst IP %u.%u.%u.%u NOT in range %s"
49 "%u.%u.%u.%u-%u.%u.%u.%u\n",
50 NIPQUAD(iph->daddr),
51 info->flags & IPRANGE_DST_INV ? "(INV) " : "",
52 NIPQUAD(info->dst.min_ip),
53 NIPQUAD(info->dst.max_ip));
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070054 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
56 }
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070057 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Patrick McHardy9f15c532007-07-07 22:22:02 -070060static struct xt_match iprange_match __read_mostly = {
Patrick McHardy1d5cd902006-03-20 18:01:14 -080061 .name = "iprange",
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080062 .family = AF_INET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -080063 .match = match,
64 .matchsize = sizeof(struct ipt_iprange_info),
Patrick McHardy1d5cd902006-03-20 18:01:14 -080065 .me = THIS_MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
Andrew Morton65b4b4e2006-03-28 16:37:06 -080068static int __init ipt_iprange_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080070 return xt_register_match(&iprange_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Andrew Morton65b4b4e2006-03-28 16:37:06 -080073static void __exit ipt_iprange_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080075 xt_unregister_match(&iprange_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Andrew Morton65b4b4e2006-03-28 16:37:06 -080078module_init(ipt_iprange_init);
79module_exit(ipt_iprange_fini);