blob: 49c5ff7f6dd67fdbec03b360323aa102ff59e37a [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 Westphalde81bbe2011-03-15 20:16:20 +010025#include <linux/netfilter/xt_addrtype.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080026#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28MODULE_LICENSE("GPL");
29MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
Florian Westphalde81bbe2011-03-15 20:16:20 +010030MODULE_DESCRIPTION("Xtables: address type match");
31MODULE_ALIAS("ipt_addrtype");
Florian Westphal2f5dc632011-03-15 20:17:44 +010032MODULE_ALIAS("ip6t_addrtype");
33
Igor Maravićc0cd1152011-12-12 02:58:24 +000034#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
Florian Westphalb7225042011-04-04 17:01:43 +020035static u32 match_lookup_rt6(struct net *net, const struct net_device *dev,
36 const struct in6_addr *addr)
Florian Westphal2f5dc632011-03-15 20:17:44 +010037{
Florian Westphalb7225042011-04-04 17:01:43 +020038 const struct nf_afinfo *afinfo;
39 struct flowi6 flow;
40 struct rt6_info *rt;
Florian Westphal2f5dc632011-03-15 20:17:44 +010041 u32 ret;
Florian Westphalb7225042011-04-04 17:01:43 +020042 int route_err;
Florian Westphal2f5dc632011-03-15 20:17:44 +010043
Florian Westphalb7225042011-04-04 17:01:43 +020044 memset(&flow, 0, sizeof(flow));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +000045 flow.daddr = *addr;
Florian Westphalb7225042011-04-04 17:01:43 +020046 if (dev)
47 flow.flowi6_oif = dev->ifindex;
48
49 rcu_read_lock();
50
51 afinfo = nf_get_afinfo(NFPROTO_IPV6);
52 if (afinfo != NULL)
53 route_err = afinfo->route(net, (struct dst_entry **)&rt,
54 flowi6_to_flowi(&flow), !!dev);
55 else
56 route_err = 1;
57
58 rcu_read_unlock();
59
60 if (route_err)
Florian Westphal2f5dc632011-03-15 20:17:44 +010061 return XT_ADDRTYPE_UNREACHABLE;
62
63 if (rt->rt6i_flags & RTF_REJECT)
64 ret = XT_ADDRTYPE_UNREACHABLE;
65 else
66 ret = 0;
67
68 if (rt->rt6i_flags & RTF_LOCAL)
69 ret |= XT_ADDRTYPE_LOCAL;
70 if (rt->rt6i_flags & RTF_ANYCAST)
71 ret |= XT_ADDRTYPE_ANYCAST;
Florian Westphalb7225042011-04-04 17:01:43 +020072
73
74 dst_release(&rt->dst);
Florian Westphal2f5dc632011-03-15 20:17:44 +010075 return ret;
76}
77
78static bool match_type6(struct net *net, const struct net_device *dev,
79 const struct in6_addr *addr, u16 mask)
80{
81 int addr_type = ipv6_addr_type(addr);
82
83 if ((mask & XT_ADDRTYPE_MULTICAST) &&
84 !(addr_type & IPV6_ADDR_MULTICAST))
85 return false;
86 if ((mask & XT_ADDRTYPE_UNICAST) && !(addr_type & IPV6_ADDR_UNICAST))
87 return false;
88 if ((mask & XT_ADDRTYPE_UNSPEC) && addr_type != IPV6_ADDR_ANY)
89 return false;
90
91 if ((XT_ADDRTYPE_LOCAL | XT_ADDRTYPE_ANYCAST |
Florian Westphalb7225042011-04-04 17:01:43 +020092 XT_ADDRTYPE_UNREACHABLE) & mask)
93 return !!(mask & match_lookup_rt6(net, dev, addr));
Florian Westphal2f5dc632011-03-15 20:17:44 +010094 return true;
95}
96
97static bool
98addrtype_mt6(struct net *net, const struct net_device *dev,
99 const struct sk_buff *skb, const struct xt_addrtype_info_v1 *info)
100{
101 const struct ipv6hdr *iph = ipv6_hdr(skb);
102 bool ret = true;
103
104 if (info->source)
105 ret &= match_type6(net, dev, &iph->saddr, info->source) ^
106 (info->flags & XT_ADDRTYPE_INVERT_SOURCE);
107 if (ret && info->dest)
108 ret &= match_type6(net, dev, &iph->daddr, info->dest) ^
109 !!(info->flags & XT_ADDRTYPE_INVERT_DEST);
110 return ret;
111}
112#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100114static inline bool match_type(struct net *net, const struct net_device *dev,
115 __be32 addr, u_int16_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100117 return !!(mask & (1 << inet_dev_addr_type(net, dev, addr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800120static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +0200121addrtype_mt_v0(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100123 struct net *net = dev_net(par->in ? par->in : par->out);
Florian Westphalde81bbe2011-03-15 20:16:20 +0100124 const struct xt_addrtype_info *info = par->matchinfo;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700125 const struct iphdr *iph = ip_hdr(skb);
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700126 bool ret = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (info->source)
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100129 ret &= match_type(net, NULL, iph->saddr, info->source) ^
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800130 info->invert_source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (info->dest)
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100132 ret &= match_type(net, NULL, iph->daddr, info->dest) ^
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800133 info->invert_dest;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return ret;
136}
137
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800138static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +0200139addrtype_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800140{
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100141 struct net *net = dev_net(par->in ? par->in : par->out);
Florian Westphalde81bbe2011-03-15 20:16:20 +0100142 const struct xt_addrtype_info_v1 *info = par->matchinfo;
Florian Westphal2f5dc632011-03-15 20:17:44 +0100143 const struct iphdr *iph;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800144 const struct net_device *dev = NULL;
145 bool ret = true;
146
Florian Westphalde81bbe2011-03-15 20:16:20 +0100147 if (info->flags & XT_ADDRTYPE_LIMIT_IFACE_IN)
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200148 dev = par->in;
Florian Westphalde81bbe2011-03-15 20:16:20 +0100149 else if (info->flags & XT_ADDRTYPE_LIMIT_IFACE_OUT)
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200150 dev = par->out;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800151
Igor Maravićc0cd1152011-12-12 02:58:24 +0000152#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
Florian Westphal2f5dc632011-03-15 20:17:44 +0100153 if (par->family == NFPROTO_IPV6)
154 return addrtype_mt6(net, dev, skb, info);
155#endif
156 iph = ip_hdr(skb);
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800157 if (info->source)
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100158 ret &= match_type(net, dev, iph->saddr, info->source) ^
Florian Westphalde81bbe2011-03-15 20:16:20 +0100159 (info->flags & XT_ADDRTYPE_INVERT_SOURCE);
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800160 if (ret && info->dest)
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +0100161 ret &= match_type(net, dev, iph->daddr, info->dest) ^
Florian Westphalde81bbe2011-03-15 20:16:20 +0100162 !!(info->flags & XT_ADDRTYPE_INVERT_DEST);
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800163 return ret;
164}
165
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100166static int addrtype_mt_checkentry_v1(const struct xt_mtchk_param *par)
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800167{
Florian Westphalde81bbe2011-03-15 20:16:20 +0100168 struct xt_addrtype_info_v1 *info = par->matchinfo;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800169
Florian Westphalde81bbe2011-03-15 20:16:20 +0100170 if (info->flags & XT_ADDRTYPE_LIMIT_IFACE_IN &&
171 info->flags & XT_ADDRTYPE_LIMIT_IFACE_OUT) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100172 pr_info("both incoming and outgoing "
173 "interface limitation cannot be selected\n");
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100174 return -EINVAL;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800175 }
176
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200177 if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) |
178 (1 << NF_INET_LOCAL_IN)) &&
Florian Westphalde81bbe2011-03-15 20:16:20 +0100179 info->flags & XT_ADDRTYPE_LIMIT_IFACE_OUT) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100180 pr_info("output interface limitation "
181 "not valid in PREROUTING and INPUT\n");
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100182 return -EINVAL;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800183 }
184
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200185 if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) |
186 (1 << NF_INET_LOCAL_OUT)) &&
Florian Westphalde81bbe2011-03-15 20:16:20 +0100187 info->flags & XT_ADDRTYPE_LIMIT_IFACE_IN) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100188 pr_info("input interface limitation "
189 "not valid in POSTROUTING and OUTPUT\n");
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100190 return -EINVAL;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800191 }
192
Igor Maravićc0cd1152011-12-12 02:58:24 +0000193#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
Florian Westphal2f5dc632011-03-15 20:17:44 +0100194 if (par->family == NFPROTO_IPV6) {
195 if ((info->source | info->dest) & XT_ADDRTYPE_BLACKHOLE) {
196 pr_err("ipv6 BLACKHOLE matching not supported\n");
197 return -EINVAL;
198 }
199 if ((info->source | info->dest) >= XT_ADDRTYPE_PROHIBIT) {
200 pr_err("ipv6 PROHIBT (THROW, NAT ..) matching not supported\n");
201 return -EINVAL;
202 }
203 if ((info->source | info->dest) & XT_ADDRTYPE_BROADCAST) {
204 pr_err("ipv6 does not support BROADCAST matching\n");
205 return -EINVAL;
206 }
207 }
208#endif
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100209 return 0;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800210}
211
212static struct xt_match addrtype_mt_reg[] __read_mostly = {
213 {
214 .name = "addrtype",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200215 .family = NFPROTO_IPV4,
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800216 .match = addrtype_mt_v0,
Florian Westphalde81bbe2011-03-15 20:16:20 +0100217 .matchsize = sizeof(struct xt_addrtype_info),
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800218 .me = THIS_MODULE
219 },
220 {
221 .name = "addrtype",
Florian Westphal2f5dc632011-03-15 20:17:44 +0100222 .family = NFPROTO_UNSPEC,
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800223 .revision = 1,
224 .match = addrtype_mt_v1,
225 .checkentry = addrtype_mt_checkentry_v1,
Florian Westphalde81bbe2011-03-15 20:16:20 +0100226 .matchsize = sizeof(struct xt_addrtype_info_v1),
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800227 .me = THIS_MODULE
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800231static int __init addrtype_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800233 return xt_register_matches(addrtype_mt_reg,
234 ARRAY_SIZE(addrtype_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800237static void __exit addrtype_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800239 xt_unregister_matches(addrtype_mt_reg, ARRAY_SIZE(addrtype_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800242module_init(addrtype_mt_init);
243module_exit(addrtype_mt_exit);