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

TOMOYO Linux Cross Reference
Linux/net/ipv6/netfilter/ip6t_rpfilter.c

Version: ~ [ linux-5.15-rc1 ] ~ [ linux-5.14.5 ] ~ [ linux-5.13.18 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.66 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.147 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.206 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.246 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.282 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.283 ] ~ [ 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  * Copyright (c) 2011 Florian Westphal <fw@strlen.de>
  3  *
  4  * This program is free software; you can redistribute it and/or modify
  5  * it under the terms of the GNU General Public License version 2 as
  6  * published by the Free Software Foundation.
  7  */
  8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9 #include <linux/module.h>
 10 #include <linux/skbuff.h>
 11 #include <linux/netdevice.h>
 12 #include <linux/route.h>
 13 #include <net/ip6_fib.h>
 14 #include <net/ip6_route.h>
 15 
 16 #include <linux/netfilter/xt_rpfilter.h>
 17 #include <linux/netfilter/x_tables.h>
 18 
 19 MODULE_LICENSE("GPL");
 20 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
 21 MODULE_DESCRIPTION("Xtables: IPv6 reverse path filter match");
 22 
 23 static bool rpfilter_addr_unicast(const struct in6_addr *addr)
 24 {
 25         int addr_type = ipv6_addr_type(addr);
 26         return addr_type & IPV6_ADDR_UNICAST;
 27 }
 28 
 29 static bool rpfilter_lookup_reverse6(struct net *net, const struct sk_buff *skb,
 30                                      const struct net_device *dev, u8 flags)
 31 {
 32         struct rt6_info *rt;
 33         struct ipv6hdr *iph = ipv6_hdr(skb);
 34         bool ret = false;
 35         struct flowi6 fl6 = {
 36                 .flowi6_iif = LOOPBACK_IFINDEX,
 37                 .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
 38                 .flowi6_proto = iph->nexthdr,
 39                 .daddr = iph->saddr,
 40         };
 41         int lookup_flags;
 42 
 43         if (rpfilter_addr_unicast(&iph->daddr)) {
 44                 memcpy(&fl6.saddr, &iph->daddr, sizeof(struct in6_addr));
 45                 lookup_flags = RT6_LOOKUP_F_HAS_SADDR;
 46         } else {
 47                 lookup_flags = 0;
 48         }
 49 
 50         fl6.flowi6_mark = flags & XT_RPFILTER_VALID_MARK ? skb->mark : 0;
 51         if ((flags & XT_RPFILTER_LOOSE) == 0)
 52                 fl6.flowi6_oif = dev->ifindex;
 53 
 54         rt = (void *)ip6_route_lookup(net, &fl6, skb, lookup_flags);
 55         if (rt->dst.error)
 56                 goto out;
 57 
 58         if (rt->rt6i_flags & (RTF_REJECT|RTF_ANYCAST))
 59                 goto out;
 60 
 61         if (rt->rt6i_flags & RTF_LOCAL) {
 62                 ret = flags & XT_RPFILTER_ACCEPT_LOCAL;
 63                 goto out;
 64         }
 65 
 66         if (rt->rt6i_idev->dev == dev || (flags & XT_RPFILTER_LOOSE))
 67                 ret = true;
 68  out:
 69         ip6_rt_put(rt);
 70         return ret;
 71 }
 72 
 73 static bool
 74 rpfilter_is_loopback(const struct sk_buff *skb, const struct net_device *in)
 75 {
 76         return skb->pkt_type == PACKET_LOOPBACK || in->flags & IFF_LOOPBACK;
 77 }
 78 
 79 static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
 80 {
 81         const struct xt_rpfilter_info *info = par->matchinfo;
 82         int saddrtype;
 83         struct ipv6hdr *iph;
 84         bool invert = info->flags & XT_RPFILTER_INVERT;
 85 
 86         if (rpfilter_is_loopback(skb, xt_in(par)))
 87                 return true ^ invert;
 88 
 89         iph = ipv6_hdr(skb);
 90         saddrtype = ipv6_addr_type(&iph->saddr);
 91         if (unlikely(saddrtype == IPV6_ADDR_ANY))
 92                 return true ^ invert; /* not routable: forward path will drop it */
 93 
 94         return rpfilter_lookup_reverse6(xt_net(par), skb, xt_in(par),
 95                                         info->flags) ^ invert;
 96 }
 97 
 98 static int rpfilter_check(const struct xt_mtchk_param *par)
 99 {
100         const struct xt_rpfilter_info *info = par->matchinfo;
101         unsigned int options = ~XT_RPFILTER_OPTION_MASK;
102 
103         if (info->flags & options) {
104                 pr_info_ratelimited("unknown options\n");
105                 return -EINVAL;
106         }
107 
108         if (strcmp(par->table, "mangle") != 0 &&
109             strcmp(par->table, "raw") != 0) {
110                 pr_info_ratelimited("only valid in \'raw\' or \'mangle\' table, not \'%s\'\n",
111                                     par->table);
112                 return -EINVAL;
113         }
114 
115         return 0;
116 }
117 
118 static struct xt_match rpfilter_mt_reg __read_mostly = {
119         .name           = "rpfilter",
120         .family         = NFPROTO_IPV6,
121         .checkentry     = rpfilter_check,
122         .match          = rpfilter_mt,
123         .matchsize      = sizeof(struct xt_rpfilter_info),
124         .hooks          = (1 << NF_INET_PRE_ROUTING),
125         .me             = THIS_MODULE
126 };
127 
128 static int __init rpfilter_mt_init(void)
129 {
130         return xt_register_match(&rpfilter_mt_reg);
131 }
132 
133 static void __exit rpfilter_mt_exit(void)
134 {
135         xt_unregister_match(&rpfilter_mt_reg);
136 }
137 
138 module_init(rpfilter_mt_init);
139 module_exit(rpfilter_mt_exit);
140 

~ [ 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