[NETFILTER]: Convert ip_tables matches/targets to centralized error checking
[pandora-kernel.git] / net / ipv4 / netfilter / ipt_iprange.c
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>
13 #include <linux/netfilter_ipv4/ip_tables.h>
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
26 static int
27 match(const struct sk_buff *skb,
28       const struct net_device *in,
29       const struct net_device *out,
30       const void *matchinfo,
31       int offset, unsigned int protoff, int *hotdrop)
32 {
33         const struct ipt_iprange_info *info = matchinfo;
34         const struct iphdr *iph = skb->nh.iph;
35
36         if (info->flags & IPRANGE_SRC) {
37                 if (((ntohl(iph->saddr) < ntohl(info->src.min_ip))
38                           || (ntohl(iph->saddr) > ntohl(info->src.max_ip)))
39                          ^ !!(info->flags & IPRANGE_SRC_INV)) {
40                         DEBUGP("src IP %u.%u.%u.%u NOT in range %s"
41                                "%u.%u.%u.%u-%u.%u.%u.%u\n",
42                                 NIPQUAD(iph->saddr),
43                                 info->flags & IPRANGE_SRC_INV ? "(INV) " : "",
44                                 NIPQUAD(info->src.min_ip),
45                                 NIPQUAD(info->src.max_ip));
46                         return 0;
47                 }
48         }
49         if (info->flags & IPRANGE_DST) {
50                 if (((ntohl(iph->daddr) < ntohl(info->dst.min_ip))
51                           || (ntohl(iph->daddr) > ntohl(info->dst.max_ip)))
52                          ^ !!(info->flags & IPRANGE_DST_INV)) {
53                         DEBUGP("dst IP %u.%u.%u.%u NOT in range %s"
54                                "%u.%u.%u.%u-%u.%u.%u.%u\n",
55                                 NIPQUAD(iph->daddr),
56                                 info->flags & IPRANGE_DST_INV ? "(INV) " : "",
57                                 NIPQUAD(info->dst.min_ip),
58                                 NIPQUAD(info->dst.max_ip));
59                         return 0;
60                 }
61         }
62         return 1;
63 }
64
65 static struct ipt_match iprange_match = {
66         .name           = "iprange",
67         .match          = match,
68         .matchsize      = sizeof(struct ipt_iprange_info),
69         .destroy        = NULL,
70         .me             = THIS_MODULE
71 };
72
73 static int __init init(void)
74 {
75         return ipt_register_match(&iprange_match);
76 }
77
78 static void __exit fini(void)
79 {
80         ipt_unregister_match(&iprange_match);
81 }
82
83 module_init(init);
84 module_exit(fini);