Get ifindex in BPF_SK_LOOKUP prog type
Subsystems (9)
v2 cover Get ifindex in BPF_SK_LOOKUP prog type
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.
v2:
- Fix inaccurate comment (Alexei)
- Add more details to commit messages (John)
Mark Pashmfouroush (2):
bpf: Add ifindex to bpf_sk_lookup
selftests/bpf: Add tests for accessing ifindex in bpf_sk_lookup
include/linux/filter.h | 7 ++-- include/uapi/linux/bpf.h | 1 + net/core/filter.c | 7 ++++ net/ipv4/inet_hashtables.c | 8 ++--- net/ipv4/udp.c | 8 ++--- net/ipv6/inet6_hashtables.c | 8 ++--- net/ipv6/udp.c | 8 ++--- tools/include/uapi/linux/bpf.h | 1 + .../selftests/bpf/prog_tests/sk_lookup.c | 31 ++++++++++++++++++ .../selftests/bpf/progs/test_sk_lookup.c | 8 +++++ .../selftests/bpf/verifier/ctx_sk_lookup.c | 32 +++++++++++++++++++ 11 files changed, 101 insertions(+), 18 deletions(-)
Signature · 1 line
2.31.1
PATCH v2 1/2 bpf: Add ifindex to bpf_sk_lookup
It may be helpful to have access to the ifindex during bpf socket lookup. An example may be to scope certain socket lookup logic to specific interfaces, i.e. an interface may be made exempt from custom lookup code.
Add the ifindex of the arriving connection to the bpf_sk_lookup API.
include/linux/filter.h +5 -2
@@ -1374,6 +1374,7 @@ struct bpf_sk_lookup_kern {const struct in6_addr *daddr; } v6;struct sock *selected_sk; +u32 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, + .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, + .ifindex = ifindex, };u32 act;
include/uapi/linux/bpf.h +1 -0
@@ -6296,6 +6296,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 ifindex;/* The arriving interface. Determined by inet_iif. */ };/*
net/core/filter.c +7 -0
@@ -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, 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 , ifindex): + *insn++ =BPF_LDX_MEM (BPF_W , si->dst_reg, si->src_reg, + bpf_target_off(struct bpf_sk_lookup_kern, + ifindex,4 , target_size)); +break ; }return insn - insn_buf;
net/ipv4/inet_hashtables.c +4 -4
@@ -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; }
net/ipv4/udp.c +4 -4
@@ -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;
net/ipv6/inet6_hashtables.c +4 -4
@@ -165,7 +165,7 @@ static inline struct sock *inet6_lookup_run_bpf(struct net *net,const struct in6_addr *saddr, const __be16 sport, const struct in6_addr *daddr, - const u16 hnum) + const u16 hnum, const int dif) { struct sock *sk, *reuse_sk;bool no_reuseport;
@@ -173,8 +173,8 @@ static inline struct sock *inet6_lookup_run_bpf(struct net *net,if (hashinfo != &tcp_hashinfo)return NULL ;/* only TCP is supported */ - no_reuseport =bpf_sk_lookup_run_v6 (net,IPPROTO_TCP , - saddr, sport, daddr, hnum, &sk); + no_reuseport =bpf_sk_lookup_run_v6 (net,IPPROTO_TCP , saddr, sport, + daddr, hnum, dif, &sk);if (no_reuseport ||IS_ERR_OR_NULL (sk))return sk;
@@ -198,7 +198,7 @@ struct sock *inet6_lookup_listener(struct net *net,/* Lookup redirect from BPF */ if (static_branch_unlikely (&bpf_sk_lookup_enabled)) { result =inet6_lookup_run_bpf (net, hashinfo, skb, doff, - saddr, sport, daddr, hnum); + saddr, sport, daddr, hnum, dif);if (result) goto done; }
net/ipv6/udp.c +4 -4
@@ -195,7 +195,7 @@ static inline struct sock *udp6_lookup_run_bpf(struct net *net,const struct in6_addr *saddr, __be16 sport, const struct in6_addr *daddr, - u16 hnum) + u16 hnum, const int dif) { struct sock *sk, *reuse_sk;bool no_reuseport;
@@ -203,8 +203,8 @@ static inline struct sock *udp6_lookup_run_bpf(struct net *net,if (udptable != &udp_table)return NULL ;/* only UDP is supported */ - no_reuseport =bpf_sk_lookup_run_v6 (net,IPPROTO_UDP , - saddr, sport, daddr, hnum, &sk); + no_reuseport =bpf_sk_lookup_run_v6 (net,IPPROTO_UDP , saddr, sport, + daddr, hnum, dif, &sk);if (no_reuseport ||IS_ERR_OR_NULL (sk))return sk;
@@ -240,7 +240,7 @@ struct sock *__udp6_lib_lookup(struct net *net,/* Lookup redirect from BPF */ if (static_branch_unlikely (&bpf_sk_lookup_enabled)) { sk =udp6_lookup_run_bpf (net, udptable, skb, - saddr, sport, daddr, hnum); + saddr, sport, daddr, hnum, dif);if (sk) { result = sk; goto done;
tools/include/uapi/linux/bpf.h +1 -0
@@ -6296,6 +6296,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 ifindex;/* The arriving interface. Determined by inet_iif. */ };/*
Signature · 1 line
2.31.1
Re: [PATCH bpf-next v2 1/2] bpf: Add ifindex to bpf_sk_lookup
Quoted 14 lines · Mark Pashmfouroush
It may be helpful to have access to the ifindex during bpf socket lookup. An example may be to scope certain socket lookup logic to specific interfaces, i.e. an interface may be made exempt from custom lookup code.
Add the ifindex of the arriving connection to the
bpf_sk_lookupAPI.Signed-off-by Mark Pashmfouroush <[email protected]>include/linux/filter.h +0
-0
include/linux/filter.h struct bpf_sk_lookup_kern { · line 1377
Quoted 4 lines
include/linux/filter.h +1
-0@@ -1374,6 +1374,7 @@ struct bpf_sk_lookup_kern {const struct in6_addr *daddr; } v6;struct sock *selected_sk; +u32 ifindex;
In struct __sk_buff, we have two ifindex related fields:
__u32 ingress_ifindex;
__u32 ifindex;Does newly-added ifindex corresponds to skb->ingress_ifindex or skb->ifindex? From comments:
include/linux/filter.h struct bpf_sk_lookup_kern { · line 1378
Quoted 1 line
include/linux/filter.h +1
-0+__u32 ifindex;/* The arriving interface. Determined by inet_iif. */
looks like it corresponds to ingress? Should be use the name ingress_ifindex to be consistent with __sk_buff?
Quoted 47 lines
include/linux/filter.h +4
-2bool 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, + .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, + .ifindex = ifindex, };u32 act;include/uapi/linux/bpf.h +1
-0@@ -6296,6 +6296,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 ifindex;/* The arriving interface. Determined by inet_iif. */ };
[...]
Re: [PATCH bpf-next v2 1/2] bpf: Add ifindex to bpf_sk_lookup
Quoted 22 lines · Yonghong Song' via kernel-team+notifications
Quoted 11 lines · Mark Pashmfouroush
It may be helpful to have access to the ifindex during bpf socket lookup. An example may be to scope certain socket lookup logic to specific interfaces, i.e. an interface may be made exempt from custom lookup code.
Add the ifindex of the arriving connection to thebpf_sk_lookupAPI.Signed-off-by Mark Pashmfouroush <[email protected]>include/linux/filter.h +0
-0include/linux/filter.h struct bpf_sk_lookup_kern { · line 1377
Quoted 4 lines
include/linux/filter.h +1
-0@@ -1374,6 +1374,7 @@ struct bpf_sk_lookup_kern {const struct in6_addr *daddr; } v6;struct sock *selected_sk; +u32 ifindex;In struct
__sk_buff, we have two ifindex related fields:__u32 ingress_ifindex; __u32 ifindex;
Quoted 7 lines
Does newly-added ifindex corresponds to
skb->ingress_ifindexorskb->ifindex? From comments:include/linux/filter.h struct bpf_sk_lookup_kern { · line 1378
Quoted 1 line
include/linux/filter.h +1
-0+__u32 ifindex;/* The arriving interface. Determined by inet_iif. */ looks like it corresponds to ingress? Should be use the name
ingress_ifindexto be consistent with__sk_buff?
On ingress these two (skb->skb_iif and skb->dev-ifindex) are the same, if I read the code correctly [1].
That said, I agree that ingress_ifindex would be less ambiguous (iif -> ingress interface, can't get that wrong).
Also, as Yonghong points out __sk_buff and xdp_md context objects already use this identifier for the same bit of information, so it will be less of surprise.
[1] https://elixir.bootlin.com/linux/latest/source/net/core/dev.c#L5258
[...]
PATCH v2 2/2 selftests/bpf: Add tests for accessing ifindex in bpf_sk_lookup
In reply to Mark PashmfouroushA new field was added to the bpf_sk_lookup data that users can access. Add tests that validate that the new ifindex field contains the right data.
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +31 -0
@@ -937,6 +937,37 @@ static void test_drop_on_lookup(struct test_sk_lookup *skel) .connect_to = { EXT_IP6, EXT_PORT }, .listen_at = { EXT_IP6, INT_PORT }, }, + /* The program will drop on success, meaning that the ifindex + * was 1. + */ + { + .desc ="TCP IPv4 drop on valid ifindex" , + .lookup_prog = skel->progs.check_ifindex, + .sotype = SOCK_STREAM, + .connect_to = { EXT_IP4, EXT_PORT }, + .listen_at = { EXT_IP4, EXT_PORT }, + }, + { + .desc ="TCP IPv6 drop on valid ifindex" , + .lookup_prog = skel->progs.check_ifindex, + .sotype = SOCK_STREAM, + .connect_to = { EXT_IP6, EXT_PORT }, + .listen_at = { EXT_IP6, EXT_PORT }, + }, + { + .desc ="UDP IPv4 drop on valid ifindex" , + .lookup_prog = skel->progs.check_ifindex, + .sotype = SOCK_DGRAM, + .connect_to = { EXT_IP4, EXT_PORT }, + .listen_at = { EXT_IP4, EXT_PORT }, + }, + { + .desc ="UDP IPv6 drop on valid ifindex" , + .lookup_prog = skel->progs.check_ifindex, + .sotype = SOCK_DGRAM, + .connect_to = { EXT_IP6, EXT_PORT }, + .listen_at = { EXT_IP6, EXT_PORT }, + }, };const struct test *t;
tools/testing/selftests/bpf/progs/test_sk_lookup.c +8 -0
@@ -84,6 +84,14 @@ int lookup_drop(struct bpf_sk_lookup *ctx)return SK_DROP ; } +SEC("sk_lookup") +int check_ifindex (struct bpf_sk_lookup *ctx) +{ +if (ctx->ifindex ==1 ) +return SK_DROP ; +return SK_PASS ; +} + SEC("sk_reuseport")int reuseport_pass (struct sk_reuseport_md *ctx) {
tools/testing/selftests/bpf/verifier/ctx_sk_lookup.c +32 -0
@@ -229,6 +229,24 @@BPF_LDX_MEM (BPF_W ,BPF_REG_0 ,BPF_REG_1 , offsetof(struct bpf_sk_lookup , local_port)), +/* 1-byte read from ifindex field */ +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_sk_lookup , ifindex)), +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_sk_lookup , ifindex) +1 ), +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_sk_lookup , ifindex) +2 ), +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_sk_lookup , ifindex) +3 ), +/* 2-byte read from ifindex field */ +BPF_LDX_MEM (BPF_H ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_sk_lookup , ifindex)), +BPF_LDX_MEM (BPF_H ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_sk_lookup , ifindex) +2 ), +/* 4-byte read from ifindex field */ +BPF_LDX_MEM (BPF_W ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_sk_lookup , ifindex)), +/* 8-byte read from sk field */ BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 , offsetof(struct bpf_sk_lookup, sk)),
@@ -351,6 +369,20 @@ .expected_attach_type = BPF_SK_LOOKUP, .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS, }, +{ + "invalid 8-byte read from bpf_sk_lookup ifindex field", + .insns = { +BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_sk_lookup , ifindex)), +BPF_MOV32_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .errstr ="invalid bpf_context access" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_SK_LOOKUP, + .expected_attach_type = BPF_SK_LOOKUP, + .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS, +}, /* invalid 1,2,4-byte reads from 8-byte fields in bpf_sk_lookup */ { "invalid 4-byte read from bpf_sk_lookup sk field",
Signature · 1 line
2.31.1