blob: 6a3a033a680835b47164d3f2e157d8a24e56665f [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
20#if 0
21#define DEBUGP printk
22#else
23#define DEBUGP(format, args...)
24#endif
25
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070026static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070027match(const struct sk_buff *skb,
28 const struct net_device *in,
29 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080030 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 const void *matchinfo,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070032 int offset, unsigned int protoff, bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 const struct ipt_iprange_info *info = matchinfo;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070035 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37 if (info->flags & IPRANGE_SRC) {
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070038 if ((ntohl(iph->saddr) < ntohl(info->src.min_ip)
39 || ntohl(iph->saddr) > ntohl(info->src.max_ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 ^ !!(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 Hideakie905a9e2007-02-09 23:24:47 +090044 info->flags & IPRANGE_SRC_INV ? "(INV) " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 NIPQUAD(info->src.min_ip),
46 NIPQUAD(info->src.max_ip));
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070047 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 }
49 }
50 if (info->flags & IPRANGE_DST) {
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070051 if ((ntohl(iph->daddr) < ntohl(info->dst.min_ip)
52 || ntohl(iph->daddr) > ntohl(info->dst.max_ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 ^ !!(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 Hideakie905a9e2007-02-09 23:24:47 +090057 info->flags & IPRANGE_DST_INV ? "(INV) " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 NIPQUAD(info->dst.min_ip),
59 NIPQUAD(info->dst.max_ip));
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070060 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
62 }
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070063 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Patrick McHardy9f15c532007-07-07 22:22:02 -070066static struct xt_match iprange_match __read_mostly = {
Patrick McHardy1d5cd902006-03-20 18:01:14 -080067 .name = "iprange",
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080068 .family = AF_INET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -080069 .match = match,
70 .matchsize = sizeof(struct ipt_iprange_info),
Patrick McHardy1d5cd902006-03-20 18:01:14 -080071 .me = THIS_MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Andrew Morton65b4b4e2006-03-28 16:37:06 -080074static int __init ipt_iprange_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080076 return xt_register_match(&iprange_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Andrew Morton65b4b4e2006-03-28 16:37:06 -080079static void __exit ipt_iprange_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080081 xt_unregister_match(&iprange_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Andrew Morton65b4b4e2006-03-28 16:37:06 -080084module_init(ipt_iprange_init);
85module_exit(ipt_iprange_fini);