Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * iptables module to match inet_addr_type() of an ip. |
| 3 | * |
| 4 | * Copyright (c) 2004 Patrick McHardy <kaber@trash.net> |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 5 | * (C) 2007 Laszlo Attila Toth <panther@balabit.hu> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 19 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
Florian Westphal | 2f5dc63 | 2011-03-15 20:17:44 +0100 | [diff] [blame] | 20 | #include <net/ipv6.h> |
| 21 | #include <net/ip6_route.h> |
| 22 | #include <net/ip6_fib.h> |
| 23 | #endif |
| 24 | |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 25 | #include <linux/netfilter/xt_addrtype.h> |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 26 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 30 | MODULE_DESCRIPTION("Xtables: address type match"); |
| 31 | MODULE_ALIAS("ipt_addrtype"); |
Florian Westphal | 2f5dc63 | 2011-03-15 20:17:44 +0100 | [diff] [blame] | 32 | MODULE_ALIAS("ip6t_addrtype"); |
| 33 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 34 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
Florian Westphal | b722504 | 2011-04-04 17:01:43 +0200 | [diff] [blame] | 35 | static u32 match_lookup_rt6(struct net *net, const struct net_device *dev, |
| 36 | const struct in6_addr *addr) |
Florian Westphal | 2f5dc63 | 2011-03-15 20:17:44 +0100 | [diff] [blame] | 37 | { |
Florian Westphal | b722504 | 2011-04-04 17:01:43 +0200 | [diff] [blame] | 38 | const struct nf_afinfo *afinfo; |
| 39 | struct flowi6 flow; |
| 40 | struct rt6_info *rt; |
Florian Westphal | 2f5dc63 | 2011-03-15 20:17:44 +0100 | [diff] [blame] | 41 | u32 ret; |
Florian Westphal | b722504 | 2011-04-04 17:01:43 +0200 | [diff] [blame] | 42 | int route_err; |
Florian Westphal | 2f5dc63 | 2011-03-15 20:17:44 +0100 | [diff] [blame] | 43 | |
Florian Westphal | b722504 | 2011-04-04 17:01:43 +0200 | [diff] [blame] | 44 | memset(&flow, 0, sizeof(flow)); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 45 | flow.daddr = *addr; |
Florian Westphal | b722504 | 2011-04-04 17:01:43 +0200 | [diff] [blame] | 46 | 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 Westphal | 2f5dc63 | 2011-03-15 20:17:44 +0100 | [diff] [blame] | 61 | 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 Westphal | b722504 | 2011-04-04 17:01:43 +0200 | [diff] [blame] | 72 | |
| 73 | |
| 74 | dst_release(&rt->dst); |
Florian Westphal | 2f5dc63 | 2011-03-15 20:17:44 +0100 | [diff] [blame] | 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | static 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 Westphal | b722504 | 2011-04-04 17:01:43 +0200 | [diff] [blame] | 92 | XT_ADDRTYPE_UNREACHABLE) & mask) |
| 93 | return !!(mask & match_lookup_rt6(net, dev, addr)); |
Florian Westphal | 2f5dc63 | 2011-03-15 20:17:44 +0100 | [diff] [blame] | 94 | return true; |
| 95 | } |
| 96 | |
| 97 | static bool |
| 98 | addrtype_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 114 | static inline bool match_type(struct net *net, const struct net_device *dev, |
| 115 | __be32 addr, u_int16_t mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 117 | return !!(mask & (1 << inet_dev_addr_type(net, dev, addr))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 120 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 121 | addrtype_mt_v0(const struct sk_buff *skb, struct xt_action_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 123 | struct net *net = dev_net(par->in ? par->in : par->out); |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 124 | const struct xt_addrtype_info *info = par->matchinfo; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 125 | const struct iphdr *iph = ip_hdr(skb); |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 126 | bool ret = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | if (info->source) |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 129 | ret &= match_type(net, NULL, iph->saddr, info->source) ^ |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 130 | info->invert_source; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if (info->dest) |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 132 | ret &= match_type(net, NULL, iph->daddr, info->dest) ^ |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 133 | info->invert_dest; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return ret; |
| 136 | } |
| 137 | |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 138 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 139 | addrtype_mt_v1(const struct sk_buff *skb, struct xt_action_param *par) |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 140 | { |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 141 | struct net *net = dev_net(par->in ? par->in : par->out); |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 142 | const struct xt_addrtype_info_v1 *info = par->matchinfo; |
Florian Westphal | 2f5dc63 | 2011-03-15 20:17:44 +0100 | [diff] [blame] | 143 | const struct iphdr *iph; |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 144 | const struct net_device *dev = NULL; |
| 145 | bool ret = true; |
| 146 | |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 147 | if (info->flags & XT_ADDRTYPE_LIMIT_IFACE_IN) |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 148 | dev = par->in; |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 149 | else if (info->flags & XT_ADDRTYPE_LIMIT_IFACE_OUT) |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 150 | dev = par->out; |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 151 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 152 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
Florian Westphal | 2f5dc63 | 2011-03-15 20:17:44 +0100 | [diff] [blame] | 153 | if (par->family == NFPROTO_IPV6) |
| 154 | return addrtype_mt6(net, dev, skb, info); |
| 155 | #endif |
| 156 | iph = ip_hdr(skb); |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 157 | if (info->source) |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 158 | ret &= match_type(net, dev, iph->saddr, info->source) ^ |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 159 | (info->flags & XT_ADDRTYPE_INVERT_SOURCE); |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 160 | if (ret && info->dest) |
Alexey Dobriyan | d4ec52b | 2008-11-04 14:21:48 +0100 | [diff] [blame] | 161 | ret &= match_type(net, dev, iph->daddr, info->dest) ^ |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 162 | !!(info->flags & XT_ADDRTYPE_INVERT_DEST); |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 163 | return ret; |
| 164 | } |
| 165 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 166 | static int addrtype_mt_checkentry_v1(const struct xt_mtchk_param *par) |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 167 | { |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 168 | struct xt_addrtype_info_v1 *info = par->matchinfo; |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 169 | |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 170 | if (info->flags & XT_ADDRTYPE_LIMIT_IFACE_IN && |
| 171 | info->flags & XT_ADDRTYPE_LIMIT_IFACE_OUT) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 172 | pr_info("both incoming and outgoing " |
| 173 | "interface limitation cannot be selected\n"); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 174 | return -EINVAL; |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 177 | if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) | |
| 178 | (1 << NF_INET_LOCAL_IN)) && |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 179 | info->flags & XT_ADDRTYPE_LIMIT_IFACE_OUT) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 180 | pr_info("output interface limitation " |
| 181 | "not valid in PREROUTING and INPUT\n"); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 182 | return -EINVAL; |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 185 | if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) | |
| 186 | (1 << NF_INET_LOCAL_OUT)) && |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 187 | info->flags & XT_ADDRTYPE_LIMIT_IFACE_IN) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 188 | pr_info("input interface limitation " |
| 189 | "not valid in POSTROUTING and OUTPUT\n"); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 190 | return -EINVAL; |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 193 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
Florian Westphal | 2f5dc63 | 2011-03-15 20:17:44 +0100 | [diff] [blame] | 194 | 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 Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 209 | return 0; |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static struct xt_match addrtype_mt_reg[] __read_mostly = { |
| 213 | { |
| 214 | .name = "addrtype", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 215 | .family = NFPROTO_IPV4, |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 216 | .match = addrtype_mt_v0, |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 217 | .matchsize = sizeof(struct xt_addrtype_info), |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 218 | .me = THIS_MODULE |
| 219 | }, |
| 220 | { |
| 221 | .name = "addrtype", |
Florian Westphal | 2f5dc63 | 2011-03-15 20:17:44 +0100 | [diff] [blame] | 222 | .family = NFPROTO_UNSPEC, |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 223 | .revision = 1, |
| 224 | .match = addrtype_mt_v1, |
| 225 | .checkentry = addrtype_mt_checkentry_v1, |
Florian Westphal | de81bbe | 2011-03-15 20:16:20 +0100 | [diff] [blame] | 226 | .matchsize = sizeof(struct xt_addrtype_info_v1), |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 227 | .me = THIS_MODULE |
| 228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | }; |
| 230 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 231 | static int __init addrtype_mt_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 233 | return xt_register_matches(addrtype_mt_reg, |
| 234 | ARRAY_SIZE(addrtype_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 237 | static void __exit addrtype_mt_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
Laszlo Attila Toth | e2cf5ec | 2007-12-04 23:30:18 -0800 | [diff] [blame] | 239 | xt_unregister_matches(addrtype_mt_reg, ARRAY_SIZE(addrtype_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 242 | module_init(addrtype_mt_init); |
| 243 | module_exit(addrtype_mt_exit); |