File Index
Files and directories
Sign in
FILE

ipt_rpfilter.c

File preview with syntax highlighting.

linux-6.19.14/net/ipv4/netfilter/ipt_rpfilter.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (c) 2011 Florian Westphal <fw@strlen.de>
  4 *
  5 * based on fib_frontend.c; Author: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6 */
  7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8#include <linux/module.h>
  9#include <linux/skbuff.h>
 10#include <linux/netdevice.h>
 11#include <linux/ip.h>
 12#include <net/flow.h>
 13#include <net/ip.h>
 14#include <net/ip_fib.h>
 15#include <net/route.h>
 16
 17#include <linux/netfilter/xt_rpfilter.h>
 18#include <linux/netfilter/x_tables.h>
 19
 20MODULE_LICENSE("GPL");
 21MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
 22MODULE_DESCRIPTION("iptables: ipv4 reverse path filter match");
 23
 24/* don't try to find route from mcast/bcast/zeronet */
 25static __be32 rpfilter_get_saddr(__be32 addr)
 26{
 27	if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr) ||
 28	    ipv4_is_zeronet(addr))
 29		return 0;
 30	return addr;
 31}
 32
 33static bool rpfilter_lookup_reverse(struct net *net, struct flowi4 *fl4,
 34				const struct net_device *dev, u8 flags)
 35{
 36	struct fib_result res;
 37
 38	if (fib_lookup(net, fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE))
 39		return false;
 40
 41	if (res.type != RTN_UNICAST) {
 42		if (res.type != RTN_LOCAL || !(flags & XT_RPFILTER_ACCEPT_LOCAL))
 43			return false;
 44	}
 45	return fib_info_nh_uses_dev(res.fi, dev) || flags & XT_RPFILTER_LOOSE;
 46}
 47
 48static bool
 49rpfilter_is_loopback(const struct sk_buff *skb, const struct net_device *in)
 50{
 51	return skb->pkt_type == PACKET_LOOPBACK || in->flags & IFF_LOOPBACK;
 52}
 53
 54static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
 55{
 56	const struct xt_rpfilter_info *info;
 57	const struct iphdr *iph;
 58	struct flowi4 flow;
 59	bool invert;
 60
 61	info = par->matchinfo;
 62	invert = info->flags & XT_RPFILTER_INVERT;
 63
 64	if (rpfilter_is_loopback(skb, xt_in(par)))
 65		return true ^ invert;
 66
 67	iph = ip_hdr(skb);
 68	if (ipv4_is_zeronet(iph->saddr)) {
 69		if (ipv4_is_lbcast(iph->daddr) ||
 70		    ipv4_is_local_multicast(iph->daddr))
 71			return true ^ invert;
 72	}
 73
 74	memset(&flow, 0, sizeof(flow));
 75	flow.flowi4_iif = LOOPBACK_IFINDEX;
 76	flow.daddr = iph->saddr;
 77	flow.saddr = rpfilter_get_saddr(iph->daddr);
 78	flow.flowi4_mark = info->flags & XT_RPFILTER_VALID_MARK ? skb->mark : 0;
 79	flow.flowi4_dscp = ip4h_dscp(iph);
 80	flow.flowi4_scope = RT_SCOPE_UNIVERSE;
 81	flow.flowi4_l3mdev = l3mdev_master_ifindex_rcu(xt_in(par));
 82	flow.flowi4_uid = sock_net_uid(xt_net(par), NULL);
 83
 84	return rpfilter_lookup_reverse(xt_net(par), &flow, xt_in(par), info->flags) ^ invert;
 85}
 86
 87static int rpfilter_check(const struct xt_mtchk_param *par)
 88{
 89	const struct xt_rpfilter_info *info = par->matchinfo;
 90	unsigned int options = ~XT_RPFILTER_OPTION_MASK;
 91	if (info->flags & options) {
 92		pr_info_ratelimited("unknown options\n");
 93		return -EINVAL;
 94	}
 95
 96	if (strcmp(par->table, "mangle") != 0 &&
 97	    strcmp(par->table, "raw") != 0) {
 98		pr_info_ratelimited("only valid in \'raw\' or \'mangle\' table, not \'%s\'\n",
 99				    par->table);
100		return -EINVAL;
101	}
102
103	return 0;
104}
105
106static struct xt_match rpfilter_mt_reg __read_mostly = {
107	.name		= "rpfilter",
108	.family		= NFPROTO_IPV4,
109	.checkentry	= rpfilter_check,
110	.match		= rpfilter_mt,
111	.matchsize	= sizeof(struct xt_rpfilter_info),
112	.hooks		= (1 << NF_INET_PRE_ROUTING),
113	.me		= THIS_MODULE
114};
115
116static int __init rpfilter_mt_init(void)
117{
118	return xt_register_match(&rpfilter_mt_reg);
119}
120
121static void __exit rpfilter_mt_exit(void)
122{
123	xt_unregister_match(&rpfilter_mt_reg);
124}
125
126module_init(rpfilter_mt_init);
127module_exit(rpfilter_mt_exit);
~/ Read-only file preview.