~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/xt_addrtype.c

Version: ~ [ linux-5.14-rc3 ] ~ [ linux-5.13.5 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.53 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.135 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.198 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.240 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.276 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.276 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp