net/netfilter/nf_nat_redirect.c

Source file repositories/reference/linux-study-clean/net/netfilter/nf_nat_redirect.c

File Facts

System
Linux kernel
Corpus path
net/netfilter/nf_nat_redirect.c
Extension
.c
Size
3354 bytes
Lines
139
Domain
Networking Core
Bucket
Sockets, Protocols, Packet Path, And Network Policy
Inferred role
Networking Core: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.

Dependency Surface

Detected Declarations

Annotated Snippet

if (indev) {
			const struct in_ifaddr *ifa;

			ifa = rcu_dereference(indev->ifa_list);
			if (ifa)
				newdst.ip = ifa->ifa_local;
		}

		if (!newdst.ip)
			return NF_DROP;
	}

	return nf_nat_redirect(skb, range, &newdst);
}
EXPORT_SYMBOL_GPL(nf_nat_redirect_ipv4);

static const struct in6_addr loopback_addr = IN6ADDR_LOOPBACK_INIT;

static bool nf_nat_redirect_ipv6_usable(const struct inet6_ifaddr *ifa, unsigned int scope)
{
	unsigned int ifa_addr_type = ipv6_addr_type(&ifa->addr);

	if (ifa_addr_type & IPV6_ADDR_MAPPED)
		return false;

	if ((ifa->flags & IFA_F_TENTATIVE) && (!(ifa->flags & IFA_F_OPTIMISTIC)))
		return false;

	if (scope) {
		unsigned int ifa_scope = ifa_addr_type & IPV6_ADDR_SCOPE_MASK;

		if (!(scope & ifa_scope))
			return false;
	}

	return true;
}

unsigned int
nf_nat_redirect_ipv6(struct sk_buff *skb, const struct nf_nat_range2 *range,
		     unsigned int hooknum)
{
	union nf_inet_addr newdst = {};

	if (hooknum == NF_INET_LOCAL_OUT) {
		newdst.in6 = loopback_addr;
	} else {
		unsigned int scope = ipv6_addr_scope(&ipv6_hdr(skb)->daddr);
		struct inet6_dev *idev;
		bool addr = false;

		idev = __in6_dev_get(skb->dev);
		if (idev != NULL) {
			const struct inet6_ifaddr *ifa;

			read_lock_bh(&idev->lock);
			list_for_each_entry(ifa, &idev->addr_list, if_list) {
				if (!nf_nat_redirect_ipv6_usable(ifa, scope))
					continue;

				newdst.in6 = ifa->addr;
				addr = true;
				break;
			}
			read_unlock_bh(&idev->lock);
		}

		if (!addr)
			return NF_DROP;
	}

	return nf_nat_redirect(skb, range, &newdst);
}
EXPORT_SYMBOL_GPL(nf_nat_redirect_ipv6);

Annotation

Implementation Notes