]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/ipv4/netfilter/ipt_addrtype.c
[NETFILTER]: Convert ip_tables matches/targets to centralized error checking
[mv-sheeva.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(u_int32_t 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, const struct net_device *in,
31                  const struct net_device *out, const void *matchinfo,
32                  int offset, unsigned int protoff, int *hotdrop)
33 {
34         const struct ipt_addrtype_info *info = matchinfo;
35         const struct iphdr *iph = skb->nh.iph;
36         int ret = 1;
37
38         if (info->source)
39                 ret &= match_type(iph->saddr, info->source)^info->invert_source;
40         if (info->dest)
41                 ret &= match_type(iph->daddr, info->dest)^info->invert_dest;
42         
43         return ret;
44 }
45
46 static struct ipt_match addrtype_match = {
47         .name           = "addrtype",
48         .match          = match,
49         .matchsize      = sizeof(struct ipt_addrtype_info),
50         .me             = THIS_MODULE
51 };
52
53 static int __init init(void)
54 {
55         return ipt_register_match(&addrtype_match);
56 }
57
58 static void __exit fini(void)
59 {
60         ipt_unregister_match(&addrtype_match);
61 }
62
63 module_init(init);
64 module_exit(fini);