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

TOMOYO Linux Cross Reference
Linux/net/ipv4/netfilter/ipt_rpfilter.c

Version: ~ [ linux-5.3-rc5 ] ~ [ linux-5.2.9 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.67 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.139 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.189 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.189 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.72 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ 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  * based on fib_frontend.c; Author: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9  */
 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 11 #include <linux/module.h>
 12 #include <linux/skbuff.h>
 13 #include <linux/netdevice.h>
 14 #include <linux/ip.h>
 15 #include <net/ip.h>
 16 #include <net/ip_fib.h>
 17 #include <net/route.h>
 18 
 19 #include <linux/netfilter/xt_rpfilter.h>
 20 #include <linux/netfilter/x_tables.h>
 21 
 22 MODULE_LICENSE("GPL");
 23 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
 24 MODULE_DESCRIPTION("iptables: ipv4 reverse path filter match");
 25 
 26 /* don't try to find route from mcast/bcast/zeronet */
 27 static __be32 rpfilter_get_saddr(__be32 addr)
 28 {
 29         if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr) ||
 30             ipv4_is_zeronet(addr))
 31                 return 0;
 32         return addr;
 33 }
 34 
 35 static bool rpfilter_lookup_reverse(struct flowi4 *fl4,
 36                                 const struct net_device *dev, u8 flags)
 37 {
 38         struct fib_result res;
 39         bool dev_match;
 40         struct net *net = dev_net(dev);
 41         int ret __maybe_unused;
 42 
 43         if (fib_lookup(net, fl4, &res))
 44                 return false;
 45 
 46         if (res.type != RTN_UNICAST) {
 47                 if (res.type != RTN_LOCAL || !(flags & XT_RPFILTER_ACCEPT_LOCAL))
 48                         return false;
 49         }
 50         dev_match = false;
 51 #ifdef CONFIG_IP_ROUTE_MULTIPATH
 52         for (ret = 0; ret < res.fi->fib_nhs; ret++) {
 53                 struct fib_nh *nh = &res.fi->fib_nh[ret];
 54 
 55                 if (nh->nh_dev == dev) {
 56                         dev_match = true;
 57                         break;
 58                 }
 59         }
 60 #else
 61         if (FIB_RES_DEV(res) == dev)
 62                 dev_match = true;
 63 #endif
 64         if (dev_match || flags & XT_RPFILTER_LOOSE)
 65                 return FIB_RES_NH(res).nh_scope <= RT_SCOPE_HOST;
 66         return dev_match;
 67 }
 68 
 69 static bool rpfilter_is_local(const struct sk_buff *skb)
 70 {
 71         const struct rtable *rt = skb_rtable(skb);
 72         return rt && (rt->rt_flags & RTCF_LOCAL);
 73 }
 74 
 75 static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
 76 {
 77         const struct xt_rpfilter_info *info;
 78         const struct iphdr *iph;
 79         struct flowi4 flow;
 80         bool invert;
 81 
 82         info = par->matchinfo;
 83         invert = info->flags & XT_RPFILTER_INVERT;
 84 
 85         if (rpfilter_is_local(skb))
 86                 return true ^ invert;
 87 
 88         iph = ip_hdr(skb);
 89         if (ipv4_is_multicast(iph->daddr)) {
 90                 if (ipv4_is_zeronet(iph->saddr))
 91                         return ipv4_is_local_multicast(iph->daddr) ^ invert;
 92                 flow.flowi4_iif = 0;
 93         } else {
 94                 flow.flowi4_iif = LOOPBACK_IFINDEX;
 95         }
 96 
 97         flow.daddr = iph->saddr;
 98         flow.saddr = rpfilter_get_saddr(iph->daddr);
 99         flow.flowi4_oif = 0;
100         flow.flowi4_mark = info->flags & XT_RPFILTER_VALID_MARK ? skb->mark : 0;
101         flow.flowi4_tos = RT_TOS(iph->tos);
102         flow.flowi4_scope = RT_SCOPE_UNIVERSE;
103 
104         return rpfilter_lookup_reverse(&flow, par->in, info->flags) ^ invert;
105 }
106 
107 static int rpfilter_check(const struct xt_mtchk_param *par)
108 {
109         const struct xt_rpfilter_info *info = par->matchinfo;
110         unsigned int options = ~XT_RPFILTER_OPTION_MASK;
111         if (info->flags & options) {
112                 pr_info("unknown options encountered");
113                 return -EINVAL;
114         }
115 
116         if (strcmp(par->table, "mangle") != 0 &&
117             strcmp(par->table, "raw") != 0) {
118                 pr_info("match only valid in the \'raw\' "
119                         "or \'mangle\' tables, not \'%s\'.\n", par->table);
120                 return -EINVAL;
121         }
122 
123         return 0;
124 }
125 
126 static struct xt_match rpfilter_mt_reg __read_mostly = {
127         .name           = "rpfilter",
128         .family         = NFPROTO_IPV4,
129         .checkentry     = rpfilter_check,
130         .match          = rpfilter_mt,
131         .matchsize      = sizeof(struct xt_rpfilter_info),
132         .hooks          = (1 << NF_INET_PRE_ROUTING),
133         .me             = THIS_MODULE
134 };
135 
136 static int __init rpfilter_mt_init(void)
137 {
138         return xt_register_match(&rpfilter_mt_reg);
139 }
140 
141 static void __exit rpfilter_mt_exit(void)
142 {
143         xt_unregister_match(&rpfilter_mt_reg);
144 }
145 
146 module_init(rpfilter_mt_init);
147 module_exit(rpfilter_mt_exit);
148 

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