fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / net / ipv4 / netfilter / ipt_addrtype.c
1 /*
2  *  iptables module to match inet_addr_type() of an ip.
3  *
4  *  Copyright (c) 2004 Patrick McHardy <kaber@trash.net>
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
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/netdevice.h>
15 #include <linux/ip.h>
16 #include <net/route.h>
17
18 #include <linux/netfilter_ipv4/ipt_addrtype.h>
19 #include <linux/netfilter_ipv4/ip_tables.h>
20
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
23 MODULE_DESCRIPTION("iptables addrtype match");
24
25 static inline int match_type(__be32 addr, u_int16_t mask)
26 {
27         return !!(mask & (1 << inet_addr_type(addr)));
28 }
29
30 static int match(const struct sk_buff *skb,
31                  const struct net_device *in, const struct net_device *out,
32                  const struct xt_match *match, const void *matchinfo,
33                  int offset, unsigned int protoff, int *hotdrop)
34 {
35         const struct ipt_addrtype_info *info = matchinfo;
36         const struct iphdr *iph = skb->nh.iph;
37         int ret = 1;
38
39         if (info->source)
40                 ret &= match_type(iph->saddr, info->source)^info->invert_source;
41         if (info->dest)
42                 ret &= match_type(iph->daddr, info->dest)^info->invert_dest;
43         
44         return ret;
45 }
46
47 static struct ipt_match addrtype_match = {
48         .name           = "addrtype",
49         .match          = match,
50         .matchsize      = sizeof(struct ipt_addrtype_info),
51         .me             = THIS_MODULE
52 };
53
54 static int __init ipt_addrtype_init(void)
55 {
56         return ipt_register_match(&addrtype_match);
57 }
58
59 static void __exit ipt_addrtype_fini(void)
60 {
61         ipt_unregister_match(&addrtype_match);
62 }
63
64 module_init(ipt_addrtype_init);
65 module_exit(ipt_addrtype_fini);