Commit 04f8cb6d authored by Alexei Starovoitov's avatar Alexei Starovoitov
Browse files

Merge branch 'Get ingress_ifindex in BPF_SK_LOOKUP prog type'



Mark Pashmfouroush says:

====================

BPF_SK_LOOKUP users may want to have access to the ifindex of the skb
which triggered the socket lookup. This may be useful for selectively
applying programmable socket lookup logic to packets that arrive on a
specific interface, or excluding packets from an interface.

v3:
- Rename ifindex field to ingress_ifindex for consistency. (Yonghong)

v2:
- Fix inaccurate comment (Alexei)
- Add more details to commit messages (John)
====================

Revieview-by: default avatarLorenz Bauer <lmb@cloudflare.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 1a8b597d 8b4fd2bf
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -1374,6 +1374,7 @@ struct bpf_sk_lookup_kern {
		const struct in6_addr *daddr;
	} v6;
	struct sock	*selected_sk;
	u32		ingress_ifindex;
	bool		no_reuseport;
};

@@ -1436,7 +1437,7 @@ extern struct static_key_false bpf_sk_lookup_enabled;
static inline bool bpf_sk_lookup_run_v4(struct net *net, int protocol,
					const __be32 saddr, const __be16 sport,
					const __be32 daddr, const u16 dport,
					struct sock **psk)
					const int ifindex, struct sock **psk)
{
	struct bpf_prog_array *run_array;
	struct sock *selected_sk = NULL;
@@ -1452,6 +1453,7 @@ static inline bool bpf_sk_lookup_run_v4(struct net *net, int protocol,
			.v4.daddr	= daddr,
			.sport		= sport,
			.dport		= dport,
			.ingress_ifindex	= ifindex,
		};
		u32 act;

@@ -1474,7 +1476,7 @@ static inline bool bpf_sk_lookup_run_v6(struct net *net, int protocol,
					const __be16 sport,
					const struct in6_addr *daddr,
					const u16 dport,
					struct sock **psk)
					const int ifindex, struct sock **psk)
{
	struct bpf_prog_array *run_array;
	struct sock *selected_sk = NULL;
@@ -1490,6 +1492,7 @@ static inline bool bpf_sk_lookup_run_v6(struct net *net, int protocol,
			.v6.daddr	= daddr,
			.sport		= sport,
			.dport		= dport,
			.ingress_ifindex	= ifindex,
		};
		u32 act;

+1 −0
Original line number Diff line number Diff line
@@ -6316,6 +6316,7 @@ struct bpf_sk_lookup {
	__u32 local_ip4;	/* Network byte order */
	__u32 local_ip6[4];	/* Network byte order */
	__u32 local_port;	/* Host byte order */
	__u32 ingress_ifindex;		/* The arriving interface. Determined by inet_iif. */
};

/*
+7 −0
Original line number Diff line number Diff line
@@ -10491,6 +10491,7 @@ static bool sk_lookup_is_valid_access(int off, int size,
	case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
	case bpf_ctx_range(struct bpf_sk_lookup, remote_port):
	case bpf_ctx_range(struct bpf_sk_lookup, local_port):
	case bpf_ctx_range(struct bpf_sk_lookup, ingress_ifindex):
		bpf_ctx_record_field_size(info, sizeof(__u32));
		return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32));

@@ -10580,6 +10581,12 @@ static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type,
				      bpf_target_off(struct bpf_sk_lookup_kern,
						     dport, 2, target_size));
		break;

	case offsetof(struct bpf_sk_lookup, ingress_ifindex):
		*insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
				      bpf_target_off(struct bpf_sk_lookup_kern,
						     ingress_ifindex, 4, target_size));
		break;
	}

	return insn - insn_buf;
+4 −4
Original line number Diff line number Diff line
@@ -307,7 +307,7 @@ static inline struct sock *inet_lookup_run_bpf(struct net *net,
					       struct inet_hashinfo *hashinfo,
					       struct sk_buff *skb, int doff,
					       __be32 saddr, __be16 sport,
					       __be32 daddr, u16 hnum)
					       __be32 daddr, u16 hnum, const int dif)
{
	struct sock *sk, *reuse_sk;
	bool no_reuseport;
@@ -315,8 +315,8 @@ static inline struct sock *inet_lookup_run_bpf(struct net *net,
	if (hashinfo != &tcp_hashinfo)
		return NULL; /* only TCP is supported */

	no_reuseport = bpf_sk_lookup_run_v4(net, IPPROTO_TCP,
					    saddr, sport, daddr, hnum, &sk);
	no_reuseport = bpf_sk_lookup_run_v4(net, IPPROTO_TCP, saddr, sport,
					    daddr, hnum, dif, &sk);
	if (no_reuseport || IS_ERR_OR_NULL(sk))
		return sk;

@@ -340,7 +340,7 @@ struct sock *__inet_lookup_listener(struct net *net,
	/* Lookup redirect from BPF */
	if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
		result = inet_lookup_run_bpf(net, hashinfo, skb, doff,
					     saddr, sport, daddr, hnum);
					     saddr, sport, daddr, hnum, dif);
		if (result)
			goto done;
	}
+4 −4
Original line number Diff line number Diff line
@@ -460,7 +460,7 @@ static struct sock *udp4_lookup_run_bpf(struct net *net,
					struct udp_table *udptable,
					struct sk_buff *skb,
					__be32 saddr, __be16 sport,
					__be32 daddr, u16 hnum)
					__be32 daddr, u16 hnum, const int dif)
{
	struct sock *sk, *reuse_sk;
	bool no_reuseport;
@@ -468,8 +468,8 @@ static struct sock *udp4_lookup_run_bpf(struct net *net,
	if (udptable != &udp_table)
		return NULL; /* only UDP is supported */

	no_reuseport = bpf_sk_lookup_run_v4(net, IPPROTO_UDP,
					    saddr, sport, daddr, hnum, &sk);
	no_reuseport = bpf_sk_lookup_run_v4(net, IPPROTO_UDP, saddr, sport,
					    daddr, hnum, dif, &sk);
	if (no_reuseport || IS_ERR_OR_NULL(sk))
		return sk;

@@ -505,7 +505,7 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
	/* Lookup redirect from BPF */
	if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
		sk = udp4_lookup_run_bpf(net, udptable, skb,
					 saddr, sport, daddr, hnum);
					 saddr, sport, daddr, hnum, dif);
		if (sk) {
			result = sk;
			goto done;
Loading