blob: 11d6091991a4ec569f6797023b75b241c3aa7575 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * iptables module to match inet_addr_type() of an ip.
3 *
4 * Copyright (c) 2004 Patrick McHardy <kaber@trash.net>
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -08005 * (C) 2007 Laszlo Attila Toth <panther@balabit.hu>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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
Igor Maravićc0cd1152011-12-12 02:58:24 +000019#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
Florian Westphal2f5dc632011-03-15 20:17:44 +010020#include <net/ipv6.h>
21#include <net/ip6_route.h>
22#include <net/ip6_fib.h>
23#endif
24
Florian Westphal2a7851b2013-05-17 03:56:10 +000025#include <linux/netfilter_ipv6.h>
Florian Westphalde81bbe2011-03-15 20:16:20 +010026#include <linux/netfilter/xt_addrtype.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080027#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29MODULE_LICENSE("GPL");
30MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
Florian Westphalde81bbe2011-03-15 20:16:20 +010031MODULE_DESCRIPTION("Xtables: address type match");
32MODULE_ALIAS("ipt_addrtype");
Florian Westphal2f5dc632011-03-15 20:17:44 +010033MODULE_ALIAS("ip6t_addrtype");
34
Igor Maravićc0cd1152011-12-12 02:58:24 +000035#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
Florian Westphalb7225042011-04-04 17:01:43 +020036static u32 match_lookup_rt6(struct net *net, const struct net_device *dev,
Florian Westphal2a7851b2013-05-17 03:56:10 +000037 const struct in6_addr *addr, u16 mask)
Florian Westphal2f5dc632011-03-15 20:17:44 +010038{
Florian Westphalb7225042011-04-04 17:01:43 +020039 const struct nf_afinfo *afinfo;
40 struct flowi6 flow;
41 struct rt6_info *rt;
Florian Westphal2a7851b2013-05-17 03:56:10 +000042 u32 ret = 0;
Florian Westphalb7225042011-04-04 17:01:43 +020043 int route_err;
Florian Westphal2f5dc632011-03-15 20:17:44 +010044
Florian Westphalb7225042011-04-04 17:01:43 +020045 memset(&flow, 0, sizeof(flow));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +000046 flow.daddr = *addr;
Florian Westphalb7225042011-04-04 17:01:43 +020047 if (dev)
48 flow.flowi6_oif = dev->ifindex;
49
50 rcu_read_lock();
51
52 afinfo = nf_get_afinfo(NFPROTO_IPV6);
Florian Westphal2a7851b2013-05-17 03:56:10 +000053 if (afinfo != NULL) {
54 const struct nf_ipv6_ops *v6ops;
Florian Westphalb7225042011-04-04 17:01:43 +020055
Florian Westphal2a7851b2013-05-17 03:56:10 +000056 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 }
61 route_err = afinfo->route(net, (struct dst_entry **)&rt,
62 flowi6_to_flowi(&flow), false);
63 } else {
64 route_err = 1;
65 }
Florian Westphalb7225042011-04-04 17:01:43 +020066 rcu_read_unlock();
67
68 if (route_err)
Florian Westphal2f5dc632011-03-15 20:17:44 +010069 return XT_ADDRTYPE_UNREACHABLE;
70
71 if (rt->rt6i_flags & RTF_REJECT)
72 ret = XT_ADDRTYPE_UNREACHABLE;
Florian Westphal2f5dc632011-03-15 20:17:44 +010073
Florian Westphal2a7851b2013-05-17 03:56:10 +000074 if (dev == NULL && rt->rt6i_flags & RTF_LOCAL)
Florian Westphal2f5dc632011-03-15 20:17:44 +010075 ret |= XT_ADDRTYPE_LOCAL;
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -070076 if (ipv6_anycast_destination((struct dst_entry *)rt, addr))
Florian Westphal2f5dc632011-03-15 20:17:44 +010077 ret |= XT_ADDRTYPE_ANYCAST;
Florian Westphalb7225042011-04-04 17:01:43 +020078
Florian Westphalb7225042011-04-04 17:01:43 +020079 dst_release(&rt->dst);
Florian Westphal2f5dc632011-03-15 20:17:44 +010080 return ret;
81}
82
83static 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 |
Florian Westphalb7225042011-04-04 17:01:43 +020097 XT_ADDRTYPE_UNREACHABLE) & mask)
Florian Westphal2a7851b2013-05-17 03:56:10 +000098 return !!(mask & match_lookup_rt6(net, dev, addr, mask));
Florian Westphal2f5dc632011-03-15 20:17:44 +010099 return true;
100}
101
102static bool
103addrtype_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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100119static inline bool match_type(struct net *net, const struct net_device *dev,
120 __be32 addr, u_int16_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100122 return !!(mask & (1 << inet_dev_addr_type(net, dev, addr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800125static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +0200126addrtype_mt_v0(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500128 struct net *net = par->net;
Florian Westphalde81bbe2011-03-15 20:16:20 +0100129 const struct xt_addrtype_info *info = par->matchinfo;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700130 const struct iphdr *iph = ip_hdr(skb);
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700131 bool ret = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 if (info->source)
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100134 ret &= match_type(net, NULL, iph->saddr, info->source) ^
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800135 info->invert_source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (info->dest)
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100137 ret &= match_type(net, NULL, iph->daddr, info->dest) ^
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800138 info->invert_dest;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return ret;
141}
142
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800143static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +0200144addrtype_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800145{
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500146 struct net *net = par->net;
Florian Westphalde81bbe2011-03-15 20:16:20 +0100147 const struct xt_addrtype_info_v1 *info = par->matchinfo;
Florian Westphal2f5dc632011-03-15 20:17:44 +0100148 const struct iphdr *iph;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800149 const struct net_device *dev = NULL;
150 bool ret = true;
151
Florian Westphalde81bbe2011-03-15 20:16:20 +0100152 if (info->flags & XT_ADDRTYPE_LIMIT_IFACE_IN)
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200153 dev = par->in;
Florian Westphalde81bbe2011-03-15 20:16:20 +0100154 else if (info->flags & XT_ADDRTYPE_LIMIT_IFACE_OUT)
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200155 dev = par->out;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800156
Igor Maravićc0cd1152011-12-12 02:58:24 +0000157#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
Florian Westphal2f5dc632011-03-15 20:17:44 +0100158 if (par->family == NFPROTO_IPV6)
159 return addrtype_mt6(net, dev, skb, info);
160#endif
161 iph = ip_hdr(skb);
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800162 if (info->source)
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100163 ret &= match_type(net, dev, iph->saddr, info->source) ^
Florian Westphalde81bbe2011-03-15 20:16:20 +0100164 (info->flags & XT_ADDRTYPE_INVERT_SOURCE);
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800165 if (ret && info->dest)
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100166 ret &= match_type(net, dev, iph->daddr, info->dest) ^
Florian Westphalde81bbe2011-03-15 20:16:20 +0100167 !!(info->flags & XT_ADDRTYPE_INVERT_DEST);
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800168 return ret;
169}
170
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100171static int addrtype_mt_checkentry_v1(const struct xt_mtchk_param *par)
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800172{
Florian Westphalde81bbe2011-03-15 20:16:20 +0100173 struct xt_addrtype_info_v1 *info = par->matchinfo;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800174
Florian Westphalde81bbe2011-03-15 20:16:20 +0100175 if (info->flags & XT_ADDRTYPE_LIMIT_IFACE_IN &&
176 info->flags & XT_ADDRTYPE_LIMIT_IFACE_OUT) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100177 pr_info("both incoming and outgoing "
178 "interface limitation cannot be selected\n");
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100179 return -EINVAL;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800180 }
181
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200182 if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) |
183 (1 << NF_INET_LOCAL_IN)) &&
Florian Westphalde81bbe2011-03-15 20:16:20 +0100184 info->flags & XT_ADDRTYPE_LIMIT_IFACE_OUT) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100185 pr_info("output interface limitation "
186 "not valid in PREROUTING and INPUT\n");
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100187 return -EINVAL;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800188 }
189
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200190 if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) |
191 (1 << NF_INET_LOCAL_OUT)) &&
Florian Westphalde81bbe2011-03-15 20:16:20 +0100192 info->flags & XT_ADDRTYPE_LIMIT_IFACE_IN) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100193 pr_info("input interface limitation "
194 "not valid in POSTROUTING and OUTPUT\n");
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100195 return -EINVAL;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800196 }
197
Igor Maravićc0cd1152011-12-12 02:58:24 +0000198#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
Florian Westphal2f5dc632011-03-15 20:17:44 +0100199 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) {
Phil Oester5774c942013-07-11 12:06:58 -0700205 pr_err("ipv6 PROHIBIT (THROW, NAT ..) matching not supported\n");
Florian Westphal2f5dc632011-03-15 20:17:44 +0100206 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
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100214 return 0;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800215}
216
217static struct xt_match addrtype_mt_reg[] __read_mostly = {
218 {
219 .name = "addrtype",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200220 .family = NFPROTO_IPV4,
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800221 .match = addrtype_mt_v0,
Florian Westphalde81bbe2011-03-15 20:16:20 +0100222 .matchsize = sizeof(struct xt_addrtype_info),
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800223 .me = THIS_MODULE
224 },
225 {
226 .name = "addrtype",
Florian Westphal2f5dc632011-03-15 20:17:44 +0100227 .family = NFPROTO_UNSPEC,
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800228 .revision = 1,
229 .match = addrtype_mt_v1,
230 .checkentry = addrtype_mt_checkentry_v1,
Florian Westphalde81bbe2011-03-15 20:16:20 +0100231 .matchsize = sizeof(struct xt_addrtype_info_v1),
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800232 .me = THIS_MODULE
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800236static int __init addrtype_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800238 return xt_register_matches(addrtype_mt_reg,
239 ARRAY_SIZE(addrtype_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800242static void __exit addrtype_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800244 xt_unregister_matches(addrtype_mt_reg, ARRAY_SIZE(addrtype_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800247module_init(addrtype_mt_init);
248module_exit(addrtype_mt_exit);