]>
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 | */ | |
ff67e4e4 | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4 LT |
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 | ||
c0cd1156 | 19 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
2f5dc631 FW |
20 | #include <net/ipv6.h> |
21 | #include <net/ip6_route.h> | |
22 | #include <net/ip6_fib.h> | |
23 | #endif | |
24 | ||
2a7851bf | 25 | #include <linux/netfilter_ipv6.h> |
de81bbea | 26 | #include <linux/netfilter/xt_addrtype.h> |
6709dbbb | 27 | #include <linux/netfilter/x_tables.h> |
1da177e4 LT |
28 | |
29 | MODULE_LICENSE("GPL"); | |
30 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
de81bbea FW |
31 | MODULE_DESCRIPTION("Xtables: address type match"); |
32 | MODULE_ALIAS("ipt_addrtype"); | |
2f5dc631 FW |
33 | MODULE_ALIAS("ip6t_addrtype"); |
34 | ||
c0cd1156 | 35 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
b7225041 | 36 | static u32 match_lookup_rt6(struct net *net, const struct net_device *dev, |
2a7851bf | 37 | const struct in6_addr *addr, u16 mask) |
2f5dc631 | 38 | { |
b7225041 FW |
39 | const struct nf_afinfo *afinfo; |
40 | struct flowi6 flow; | |
41 | struct rt6_info *rt; | |
2a7851bf | 42 | u32 ret = 0; |
b7225041 | 43 | int route_err; |
2f5dc631 | 44 | |
b7225041 | 45 | memset(&flow, 0, sizeof(flow)); |
4e3fd7a0 | 46 | flow.daddr = *addr; |
b7225041 FW |
47 | if (dev) |
48 | flow.flowi6_oif = dev->ifindex; | |
49 | ||
50 | rcu_read_lock(); | |
51 | ||
52 | afinfo = nf_get_afinfo(NFPROTO_IPV6); | |
2a7851bf FW |
53 | if (afinfo != NULL) { |
54 | const struct nf_ipv6_ops *v6ops; | |
55 | ||
56 | if (dev && (mask & XT_ADDRTYPE_LOCAL)) { | |
57 | v6ops = nf_get_ipv6_ops(); | |
58 | if (v6ops && v6ops->chk_addr(net, addr, dev, true)) | |
59 | ret = XT_ADDRTYPE_LOCAL; | |
60 | } | |
b7225041 | 61 | route_err = afinfo->route(net, (struct dst_entry **)&rt, |
2a7851bf FW |
62 | flowi6_to_flowi(&flow), false); |
63 | } else { | |
b7225041 | 64 | route_err = 1; |
2a7851bf | 65 | } |
b7225041 FW |
66 | rcu_read_unlock(); |
67 | ||
68 | if (route_err) | |
2f5dc631 FW |
69 | return XT_ADDRTYPE_UNREACHABLE; |
70 | ||
71 | if (rt->rt6i_flags & RTF_REJECT) | |
72 | ret = XT_ADDRTYPE_UNREACHABLE; | |
2f5dc631 | 73 | |
2a7851bf | 74 | if (dev == NULL && rt->rt6i_flags & RTF_LOCAL) |
2f5dc631 | 75 | ret |= XT_ADDRTYPE_LOCAL; |
2647a9b0 | 76 | if (ipv6_anycast_destination((struct dst_entry *)rt, addr)) |
2f5dc631 | 77 | ret |= XT_ADDRTYPE_ANYCAST; |
b7225041 | 78 | |
b7225041 | 79 | dst_release(&rt->dst); |
2f5dc631 FW |
80 | return ret; |
81 | } | |
82 | ||
83 | static bool match_type6(struct net *net, const struct net_device *dev, | |
84 | const struct in6_addr *addr, u16 mask) | |
85 | { | |
86 | int addr_type = ipv6_addr_type(addr); | |
87 | ||
88 | if ((mask & XT_ADDRTYPE_MULTICAST) && | |
89 | !(addr_type & IPV6_ADDR_MULTICAST)) | |
90 | return false; | |
91 | if ((mask & XT_ADDRTYPE_UNICAST) && !(addr_type & IPV6_ADDR_UNICAST)) | |
92 | return false; | |
93 | if ((mask & XT_ADDRTYPE_UNSPEC) && addr_type != IPV6_ADDR_ANY) | |
94 | return false; | |
95 | ||
96 | if ((XT_ADDRTYPE_LOCAL | XT_ADDRTYPE_ANYCAST | | |
b7225041 | 97 | XT_ADDRTYPE_UNREACHABLE) & mask) |
2a7851bf | 98 | return !!(mask & match_lookup_rt6(net, dev, addr, mask)); |
2f5dc631 FW |
99 | return true; |
100 | } | |
101 | ||
102 | static bool | |
103 | addrtype_mt6(struct net *net, const struct net_device *dev, | |
104 | const struct sk_buff *skb, const struct xt_addrtype_info_v1 *info) | |
105 | { | |
106 | const struct ipv6hdr *iph = ipv6_hdr(skb); | |
107 | bool ret = true; | |
108 | ||
109 | if (info->source) | |
110 | ret &= match_type6(net, dev, &iph->saddr, info->source) ^ | |
111 | (info->flags & XT_ADDRTYPE_INVERT_SOURCE); | |
112 | if (ret && info->dest) | |
113 | ret &= match_type6(net, dev, &iph->daddr, info->dest) ^ | |
114 | !!(info->flags & XT_ADDRTYPE_INVERT_DEST); | |
115 | return ret; | |
116 | } | |
117 | #endif | |
1da177e4 | 118 | |
d4ec52ba AD |
119 | static inline bool match_type(struct net *net, const struct net_device *dev, |
120 | __be32 addr, u_int16_t mask) | |
1da177e4 | 121 | { |
d4ec52ba | 122 | return !!(mask & (1 << inet_dev_addr_type(net, dev, addr))); |
1da177e4 LT |
123 | } |
124 | ||
d3c5ee6d | 125 | static bool |
62fc8051 | 126 | addrtype_mt_v0(const struct sk_buff *skb, struct xt_action_param *par) |
1da177e4 | 127 | { |
686c9b50 | 128 | struct net *net = par->net; |
de81bbea | 129 | const struct xt_addrtype_info *info = par->matchinfo; |
eddc9ec5 | 130 | const struct iphdr *iph = ip_hdr(skb); |
1d93a9cb | 131 | bool ret = true; |
1da177e4 LT |
132 | |
133 | if (info->source) | |
d4ec52ba | 134 | ret &= match_type(net, NULL, iph->saddr, info->source) ^ |
e2cf5ecb | 135 | info->invert_source; |
1da177e4 | 136 | if (info->dest) |
d4ec52ba | 137 | ret &= match_type(net, NULL, iph->daddr, info->dest) ^ |
e2cf5ecb | 138 | info->invert_dest; |
e905a9ed | 139 | |
1da177e4 LT |
140 | return ret; |
141 | } | |
142 | ||
e2cf5ecb | 143 | static bool |
62fc8051 | 144 | addrtype_mt_v1(const struct sk_buff *skb, struct xt_action_param *par) |
e2cf5ecb | 145 | { |
686c9b50 | 146 | struct net *net = par->net; |
de81bbea | 147 | const struct xt_addrtype_info_v1 *info = par->matchinfo; |
2f5dc631 | 148 | const struct iphdr *iph; |
e2cf5ecb LAT |
149 | const struct net_device *dev = NULL; |
150 | bool ret = true; | |
151 | ||
de81bbea | 152 | if (info->flags & XT_ADDRTYPE_LIMIT_IFACE_IN) |
f7108a20 | 153 | dev = par->in; |
de81bbea | 154 | else if (info->flags & XT_ADDRTYPE_LIMIT_IFACE_OUT) |
f7108a20 | 155 | dev = par->out; |
e2cf5ecb | 156 | |
c0cd1156 | 157 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
2f5dc631 FW |
158 | if (par->family == NFPROTO_IPV6) |
159 | return addrtype_mt6(net, dev, skb, info); | |
160 | #endif | |
161 | iph = ip_hdr(skb); | |
e2cf5ecb | 162 | if (info->source) |
d4ec52ba | 163 | ret &= match_type(net, dev, iph->saddr, info->source) ^ |
de81bbea | 164 | (info->flags & XT_ADDRTYPE_INVERT_SOURCE); |
e2cf5ecb | 165 | if (ret && info->dest) |
d4ec52ba | 166 | ret &= match_type(net, dev, iph->daddr, info->dest) ^ |
de81bbea | 167 | !!(info->flags & XT_ADDRTYPE_INVERT_DEST); |
e2cf5ecb LAT |
168 | return ret; |
169 | } | |
170 | ||
b0f38452 | 171 | static int addrtype_mt_checkentry_v1(const struct xt_mtchk_param *par) |
e2cf5ecb | 172 | { |
de81bbea | 173 | struct xt_addrtype_info_v1 *info = par->matchinfo; |
e2cf5ecb | 174 | |
de81bbea FW |
175 | if (info->flags & XT_ADDRTYPE_LIMIT_IFACE_IN && |
176 | info->flags & XT_ADDRTYPE_LIMIT_IFACE_OUT) { | |
ff67e4e4 JE |
177 | pr_info("both incoming and outgoing " |
178 | "interface limitation cannot be selected\n"); | |
bd414ee6 | 179 | return -EINVAL; |
e2cf5ecb LAT |
180 | } |
181 | ||
9b4fce7a JE |
182 | if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) | |
183 | (1 << NF_INET_LOCAL_IN)) && | |
de81bbea | 184 | info->flags & XT_ADDRTYPE_LIMIT_IFACE_OUT) { |
ff67e4e4 JE |
185 | pr_info("output interface limitation " |
186 | "not valid in PREROUTING and INPUT\n"); | |
bd414ee6 | 187 | return -EINVAL; |
e2cf5ecb LAT |
188 | } |
189 | ||
9b4fce7a JE |
190 | if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) | |
191 | (1 << NF_INET_LOCAL_OUT)) && | |
de81bbea | 192 | info->flags & XT_ADDRTYPE_LIMIT_IFACE_IN) { |
ff67e4e4 JE |
193 | pr_info("input interface limitation " |
194 | "not valid in POSTROUTING and OUTPUT\n"); | |
bd414ee6 | 195 | return -EINVAL; |
e2cf5ecb LAT |
196 | } |
197 | ||
c0cd1156 | 198 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
2f5dc631 FW |
199 | if (par->family == NFPROTO_IPV6) { |
200 | if ((info->source | info->dest) & XT_ADDRTYPE_BLACKHOLE) { | |
201 | pr_err("ipv6 BLACKHOLE matching not supported\n"); | |
202 | return -EINVAL; | |
203 | } | |
204 | if ((info->source | info->dest) >= XT_ADDRTYPE_PROHIBIT) { | |
5774c94a | 205 | pr_err("ipv6 PROHIBIT (THROW, NAT ..) matching not supported\n"); |
2f5dc631 FW |
206 | return -EINVAL; |
207 | } | |
208 | if ((info->source | info->dest) & XT_ADDRTYPE_BROADCAST) { | |
209 | pr_err("ipv6 does not support BROADCAST matching\n"); | |
210 | return -EINVAL; | |
211 | } | |
212 | } | |
213 | #endif | |
bd414ee6 | 214 | return 0; |
e2cf5ecb LAT |
215 | } |
216 | ||
217 | static struct xt_match addrtype_mt_reg[] __read_mostly = { | |
218 | { | |
219 | .name = "addrtype", | |
ee999d8b | 220 | .family = NFPROTO_IPV4, |
e2cf5ecb | 221 | .match = addrtype_mt_v0, |
de81bbea | 222 | .matchsize = sizeof(struct xt_addrtype_info), |
e2cf5ecb LAT |
223 | .me = THIS_MODULE |
224 | }, | |
225 | { | |
226 | .name = "addrtype", | |
2f5dc631 | 227 | .family = NFPROTO_UNSPEC, |
e2cf5ecb LAT |
228 | .revision = 1, |
229 | .match = addrtype_mt_v1, | |
230 | .checkentry = addrtype_mt_checkentry_v1, | |
de81bbea | 231 | .matchsize = sizeof(struct xt_addrtype_info_v1), |
e2cf5ecb LAT |
232 | .me = THIS_MODULE |
233 | } | |
1da177e4 LT |
234 | }; |
235 | ||
d3c5ee6d | 236 | static int __init addrtype_mt_init(void) |
1da177e4 | 237 | { |
e2cf5ecb LAT |
238 | return xt_register_matches(addrtype_mt_reg, |
239 | ARRAY_SIZE(addrtype_mt_reg)); | |
1da177e4 LT |
240 | } |
241 | ||
d3c5ee6d | 242 | static void __exit addrtype_mt_exit(void) |
1da177e4 | 243 | { |
e2cf5ecb | 244 | xt_unregister_matches(addrtype_mt_reg, ARRAY_SIZE(addrtype_mt_reg)); |
1da177e4 LT |
245 | } |
246 | ||
d3c5ee6d JE |
247 | module_init(addrtype_mt_init); |
248 | module_exit(addrtype_mt_exit); |