]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/ipv4/netfilter/ipt_addrtype.c
netfilter: xtables: move extension arguments into compound structure (2/6)
[mirror_ubuntu-artful-kernel.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 * (C) 2007 Laszlo Attila Toth <panther@balabit.hu>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/netdevice.h>
16 #include <linux/ip.h>
17 #include <net/route.h>
18
19 #include <linux/netfilter_ipv4/ipt_addrtype.h>
20 #include <linux/netfilter/x_tables.h>
21
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
24 MODULE_DESCRIPTION("Xtables: address type match for IPv4");
25
26 static inline bool match_type(const struct net_device *dev, __be32 addr,
27 u_int16_t mask)
28 {
29 return !!(mask & (1 << inet_dev_addr_type(&init_net, dev, addr)));
30 }
31
32 static bool
33 addrtype_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par)
34 {
35 const struct ipt_addrtype_info *info = par->matchinfo;
36 const struct iphdr *iph = ip_hdr(skb);
37 bool ret = true;
38
39 if (info->source)
40 ret &= match_type(NULL, iph->saddr, info->source) ^
41 info->invert_source;
42 if (info->dest)
43 ret &= match_type(NULL, iph->daddr, info->dest) ^
44 info->invert_dest;
45
46 return ret;
47 }
48
49 static bool
50 addrtype_mt_v1(const struct sk_buff *skb, const struct xt_match_param *par)
51 {
52 const struct ipt_addrtype_info_v1 *info = par->matchinfo;
53 const struct iphdr *iph = ip_hdr(skb);
54 const struct net_device *dev = NULL;
55 bool ret = true;
56
57 if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN)
58 dev = par->in;
59 else if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT)
60 dev = par->out;
61
62 if (info->source)
63 ret &= match_type(dev, iph->saddr, info->source) ^
64 (info->flags & IPT_ADDRTYPE_INVERT_SOURCE);
65 if (ret && info->dest)
66 ret &= match_type(dev, iph->daddr, info->dest) ^
67 !!(info->flags & IPT_ADDRTYPE_INVERT_DEST);
68 return ret;
69 }
70
71 static bool addrtype_mt_checkentry_v1(const struct xt_mtchk_param *par)
72 {
73 struct ipt_addrtype_info_v1 *info = par->matchinfo;
74
75 if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN &&
76 info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) {
77 printk(KERN_ERR "ipt_addrtype: both incoming and outgoing "
78 "interface limitation cannot be selected\n");
79 return false;
80 }
81
82 if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) |
83 (1 << NF_INET_LOCAL_IN)) &&
84 info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) {
85 printk(KERN_ERR "ipt_addrtype: output interface limitation "
86 "not valid in PRE_ROUTING and INPUT\n");
87 return false;
88 }
89
90 if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) |
91 (1 << NF_INET_LOCAL_OUT)) &&
92 info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN) {
93 printk(KERN_ERR "ipt_addrtype: input interface limitation "
94 "not valid in POST_ROUTING and OUTPUT\n");
95 return false;
96 }
97
98 return true;
99 }
100
101 static struct xt_match addrtype_mt_reg[] __read_mostly = {
102 {
103 .name = "addrtype",
104 .family = NFPROTO_IPV4,
105 .match = addrtype_mt_v0,
106 .matchsize = sizeof(struct ipt_addrtype_info),
107 .me = THIS_MODULE
108 },
109 {
110 .name = "addrtype",
111 .family = NFPROTO_IPV4,
112 .revision = 1,
113 .match = addrtype_mt_v1,
114 .checkentry = addrtype_mt_checkentry_v1,
115 .matchsize = sizeof(struct ipt_addrtype_info_v1),
116 .me = THIS_MODULE
117 }
118 };
119
120 static int __init addrtype_mt_init(void)
121 {
122 return xt_register_matches(addrtype_mt_reg,
123 ARRAY_SIZE(addrtype_mt_reg));
124 }
125
126 static void __exit addrtype_mt_exit(void)
127 {
128 xt_unregister_matches(addrtype_mt_reg, ARRAY_SIZE(addrtype_mt_reg));
129 }
130
131 module_init(addrtype_mt_init);
132 module_exit(addrtype_mt_exit);