]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * iptables module to match inet_addr_type() of an ip. | |
3 | * | |
4 | * Copyright (c) 2004 Patrick McHardy <kaber@trash.net> | |
e2cf5ecb | 5 | * (C) 2007 Laszlo Attila Toth <panther@balabit.hu> |
1da177e4 LT |
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> | |
6709dbbb | 20 | #include <linux/netfilter/x_tables.h> |
1da177e4 LT |
21 | |
22 | MODULE_LICENSE("GPL"); | |
23 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
2ae15b64 | 24 | MODULE_DESCRIPTION("Xtables: address type match for IPv4"); |
1da177e4 | 25 | |
e2cf5ecb LAT |
26 | static inline bool match_type(const struct net_device *dev, __be32 addr, |
27 | u_int16_t mask) | |
1da177e4 | 28 | { |
6b175b26 | 29 | return !!(mask & (1 << inet_dev_addr_type(&init_net, dev, addr))); |
1da177e4 LT |
30 | } |
31 | ||
d3c5ee6d | 32 | static bool |
f7108a20 | 33 | addrtype_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par) |
1da177e4 | 34 | { |
f7108a20 | 35 | const struct ipt_addrtype_info *info = par->matchinfo; |
eddc9ec5 | 36 | const struct iphdr *iph = ip_hdr(skb); |
1d93a9cb | 37 | bool ret = true; |
1da177e4 LT |
38 | |
39 | if (info->source) | |
e2cf5ecb LAT |
40 | ret &= match_type(NULL, iph->saddr, info->source) ^ |
41 | info->invert_source; | |
1da177e4 | 42 | if (info->dest) |
e2cf5ecb LAT |
43 | ret &= match_type(NULL, iph->daddr, info->dest) ^ |
44 | info->invert_dest; | |
e905a9ed | 45 | |
1da177e4 LT |
46 | return ret; |
47 | } | |
48 | ||
e2cf5ecb | 49 | static bool |
f7108a20 | 50 | addrtype_mt_v1(const struct sk_buff *skb, const struct xt_match_param *par) |
e2cf5ecb | 51 | { |
f7108a20 | 52 | const struct ipt_addrtype_info_v1 *info = par->matchinfo; |
e2cf5ecb LAT |
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) | |
f7108a20 | 58 | dev = par->in; |
e2cf5ecb | 59 | else if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) |
f7108a20 | 60 | dev = par->out; |
e2cf5ecb LAT |
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) ^ | |
46faec98 | 67 | !!(info->flags & IPT_ADDRTYPE_INVERT_DEST); |
e2cf5ecb LAT |
68 | return ret; |
69 | } | |
70 | ||
71 | static bool | |
72 | addrtype_mt_checkentry_v1(const char *tablename, const void *ip_void, | |
73 | const struct xt_match *match, void *matchinfo, | |
74 | unsigned int hook_mask) | |
75 | { | |
76 | struct ipt_addrtype_info_v1 *info = matchinfo; | |
77 | ||
78 | if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN && | |
79 | info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) { | |
80 | printk(KERN_ERR "ipt_addrtype: both incoming and outgoing " | |
81 | "interface limitation cannot be selected\n"); | |
82 | return false; | |
83 | } | |
84 | ||
85 | if (hook_mask & (1 << NF_INET_PRE_ROUTING | 1 << NF_INET_LOCAL_IN) && | |
86 | info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) { | |
87 | printk(KERN_ERR "ipt_addrtype: output interface limitation " | |
88 | "not valid in PRE_ROUTING and INPUT\n"); | |
89 | return false; | |
90 | } | |
91 | ||
92 | if (hook_mask & (1 << NF_INET_POST_ROUTING | 1 << NF_INET_LOCAL_OUT) && | |
93 | info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN) { | |
94 | printk(KERN_ERR "ipt_addrtype: input interface limitation " | |
95 | "not valid in POST_ROUTING and OUTPUT\n"); | |
96 | return false; | |
97 | } | |
98 | ||
99 | return true; | |
100 | } | |
101 | ||
102 | static struct xt_match addrtype_mt_reg[] __read_mostly = { | |
103 | { | |
104 | .name = "addrtype", | |
ee999d8b | 105 | .family = NFPROTO_IPV4, |
e2cf5ecb LAT |
106 | .match = addrtype_mt_v0, |
107 | .matchsize = sizeof(struct ipt_addrtype_info), | |
108 | .me = THIS_MODULE | |
109 | }, | |
110 | { | |
111 | .name = "addrtype", | |
ee999d8b | 112 | .family = NFPROTO_IPV4, |
e2cf5ecb LAT |
113 | .revision = 1, |
114 | .match = addrtype_mt_v1, | |
115 | .checkentry = addrtype_mt_checkentry_v1, | |
116 | .matchsize = sizeof(struct ipt_addrtype_info_v1), | |
117 | .me = THIS_MODULE | |
118 | } | |
1da177e4 LT |
119 | }; |
120 | ||
d3c5ee6d | 121 | static int __init addrtype_mt_init(void) |
1da177e4 | 122 | { |
e2cf5ecb LAT |
123 | return xt_register_matches(addrtype_mt_reg, |
124 | ARRAY_SIZE(addrtype_mt_reg)); | |
1da177e4 LT |
125 | } |
126 | ||
d3c5ee6d | 127 | static void __exit addrtype_mt_exit(void) |
1da177e4 | 128 | { |
e2cf5ecb | 129 | xt_unregister_matches(addrtype_mt_reg, ARRAY_SIZE(addrtype_mt_reg)); |
1da177e4 LT |
130 | } |
131 | ||
d3c5ee6d JE |
132 | module_init(addrtype_mt_init); |
133 | module_exit(addrtype_mt_exit); |