Programming socket lookup with BPF
Subsystems (10)
v2 cover Programming socket lookup with BPF
This patch set adds a mechanism for programming mappings between the local addresses and listening/receiving sockets with BPF.
It introduces a new per-netns BPF program type, called inet_lookup, which runs during the socket lookup. The program is allowed to select a listening/receiving socket from a SOCKARRAY map that the packet will be delivered to.
BPF inet_lookup intends to be an alternative for:
* SO_BINDTOPREFIX [1] - a mechanism that provides a way to listen/receive on all local addresses that belong to a network prefix. An alternative to binding to INADDR_ANY that allows applications bound to disjoint network prefixes to share a port. Not generic. Never got upstreamed.
* TPROXY [2] - a powerful mechanism that allows steering packets destined to non-local addresses to a local socket. It also works for local addresses, which is a less restrictive case. Can be used to implement what SO_BINDTOPREFIX does, and more - in particular, all ports can be redirected to a single socket. Socket dispatch happens early in ingress path (PREROUTING hook). Versatile but comes with complexities.
Compared to the above, inet_lookup aims to be a programmatic way to map (address, port) pairs to a socket. It runs after a routing decision for local delivery was made, and hence is limited to local addresses only.
Being part of the socket lookup, has a desired effect that redirection is visible to XDP programs which call bpf_sk_lookup helpers.
When it comes to use cases, we have presented them in RFCv1 [3] cover letter and also at last Netconf [4]. To recap, they are:
1) sharing a port between two services
Services are accepting connections on different (disjoint) IP ranges but same port. Requests going to 192.0.2.0/24 tcp/80 are handled by NGINX, while 198.51.100.0/24 tcp/80 IP range is handled by Apache server. Applications are running as different users, in a flat single-netns setup.
2) receiving traffic on all ports
We have a proxy server that accepts connections to _any_ port [5].
A simple demo program that implements (1) could look like
#define NET1 (IP4(192, 0, 2, 0) >> 8)
#define NET2 (IP4(198, 51, 100, 0) >> 8)
#define MAX_SERVERS 2
struct {
__uint(type, BPF_MAP_TYPE_REUSEPORT_SOCKARRAY);
__uint(max_entries, MAX_SERVERS);
__type(key, __u32);
__type(value, __u64);
} redir_map SEC(".maps");SEC("inet_lookup/demo_two_servers")
int demo_two_http_servers(struct bpf_inet_lookup *ctx)
{
__u32 index = 0;
__u64 flags = 0;
if (ctx->family != AF_INET)
return BPF_OK;
if (ctx->protocol != IPPROTO_TCP)
return BPF_OK;
if (ctx->local_port != 80)
return BPF_OK;
switch (bpf_ntohl(ctx->local_ip4) >> 8) {
case NET1:
index = 0;
break;
case NET2:
index = 1;
break;
default:
return BPF_OK;
}
return bpf_redirect_lookup(ctx, &redir_map, &index, flags);
}Since RFCv1, we've changed the approach from rewriting the lookup key to map-based redirection. This has been suggested at Netconf, and is a recurring pattern in existing BPF program types.
We're posting the 2nd version of RFC patch set to collect further feedback and set context for the presentation and discussions at the upcoming Network Summit at LPC '19 [6].
Patches are also available on GitHub [7].
Thanks,
Jakub
[1] https://www.spinics.net/lists/netdev/msg370789.html
[2] https://www.kernel.org/doc/Documentation/networking/tproxy.txt
[3] https://lore.kernel.org/netdev/[email protected]/
[4] http://vger.kernel.org/netconf2019_files/Programmable%20socket%20lookup.pdf
[5] https://blog.cloudflare.com/how-we-built-spectrum/
[6] https://linuxplumbersconf.org/event/4/contributions/487/
[7] https://github.com/jsitnicki/linux/commits/bpf-inet-lookup
Changes RFCv1 -> RFCv2:
- Make socket lookup redirection map-based. BPF program now uses a dedicated helper and a SOCKARRAY map to select the socket to redirect to. A consequence of this change is that bpf_inet_lookup context is now read-only.
- Look for connected UDP sockets before allowing redirection from BPF. This makes connected UDP socket work as expected in the presence of inet_lookup prog.
- Share the code for BPF_PROG_{ATTACH,DETACH,QUERY} with flow_dissector, the only other per-netns BPF prog type.
Jakub Sitnicki (12):
flow_dissector: Extract attach/detach/query helpers
bpf: Introduce inet_lookup program type for redirecting socket lookup
bpf: Add verifier tests for inet_lookup context access
inet: Store layer 4 protocol in inet_hashinfo
udp: Store layer 4 protocol in udp_table
inet: Run inet_lookup bpf program on socket lookup
inet6: Run inet_lookup bpf program on socket lookup
udp: Run inet_lookup bpf program on socket lookup
udp6: Run inet_lookup bpf program on socket lookup
bpf: Sync linux/bpf.h to tools/
libbpf: Add support for inet_lookup program type
bpf: Test redirecting listening/receiving socket lookup
include/linux/bpf.h | 8 + include/linux/bpf_types.h | 1 + include/linux/filter.h | 18 + include/net/inet6_hashtables.h | 19 + include/net/inet_hashtables.h | 36 + include/net/net_namespace.h | 2 + include/net/udp.h | 10 +- include/uapi/linux/bpf.h | 58 +- kernel/bpf/syscall.c | 10 + kernel/bpf/verifier.c | 7 +- net/core/filter.c | 304 ++++++++ net/core/flow_dissector.c | 65 +- net/dccp/proto.c | 2 +- net/ipv4/inet_hashtables.c | 5 + net/ipv4/tcp_ipv4.c | 2 +- net/ipv4/udp.c | 59 +- net/ipv4/udp_impl.h | 2 +- net/ipv4/udplite.c | 4 +- net/ipv6/inet6_hashtables.c | 5 + net/ipv6/udp.c | 54 +- net/ipv6/udp_impl.h | 2 +- net/ipv6/udplite.c | 2 +- tools/include/uapi/linux/bpf.h | 58 +- tools/lib/bpf/libbpf.c | 4 + tools/lib/bpf/libbpf.h | 2 + tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/libbpf_probes.c | 1 + tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 5 +- tools/testing/selftests/bpf/bpf_helpers.h | 3 + .../selftests/bpf/progs/inet_lookup_progs.c | 78 ++ .../testing/selftests/bpf/test_inet_lookup.c | 522 +++++++++++++ .../testing/selftests/bpf/test_inet_lookup.sh | 35 + .../selftests/bpf/verifier/ctx_inet_lookup.c | 696 ++++++++++++++++++ 34 files changed, 1974 insertions(+), 108 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/inet_lookup_progs.c create mode 100644 tools/testing/selftests/bpf/test_inet_lookup.c create mode 100755 tools/testing/selftests/bpf/test_inet_lookup.sh create mode 100644 tools/testing/selftests/bpf/verifier/ctx_inet_lookup.c
Signature · 1 line
2.20.1
RFC v2 1/12 flow_dissector: Extract attach/detach/query helpers
Move generic parts of callbacks for querying, attaching, and detaching a single BPF program for reuse by other BPF program types.
Subsequent patch makes use of the extracted routines.
--- include/linux/bpf.h | 8 +++++ net/core/filter.c | 73 +++++++++++++++++++++++++++++++++++++++ net/core/flow_dissector.c | 65 ++++++---------------------------- 3 files changed, 92 insertions(+), 54 deletions(-)
include/linux/bpf.h +8 -0
@@ -23,6 +23,7 @@ struct sock;struct seq_file ;struct btf ;struct btf_type ; +struct mutex ;extern struct idr btf_idr;extern spinlock_t btf_idr_lock;
@@ -1145,4 +1146,11 @@ static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type, }#endif /* CONFIG_INET */ +int bpf_prog_query_one (struct bpf_prog __rcu **pprog, +const union bpf_attr *attr, +union bpf_attr __user *uattr); +int bpf_prog_attach_one (struct bpf_prog __rcu **pprog,struct mutex *lock, +struct bpf_prog *prog,u32 flags); +int bpf_prog_detach_one (struct bpf_prog __rcu **pprog,struct mutex *lock); +#endif /* _LINUX_BPF_H */
net/core/filter.c +73 -0
79 lines not on this page · raw message
net/core/flow_dissector.c +11 -54
91 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC v2 2/12 bpf: Introduce inet_lookup program type for redirecting socket lookup
In reply to Jakub SitnickiAdd a new program type for redirecting the listening/bound socket lookup from BPF. The program attaches to a network namespace. It is allowed to select a socket from a SOCKARRAY, which will be used as a result of socket lookup.
This provides a mechanism for programming the mapping between local (address, port) pairs and listening/receiving sockets.
The program receives the 4-tuple, as well as the IP version and L4 protocol, of the packet that triggered the lookup as its context for making a decision.
The netns-attached program is not called anywhere yet. Following patches hook it up to ipv4 and ipv6 stacks.
--- include/linux/bpf_types.h | 1 + include/linux/filter.h | 18 +++ include/net/net_namespace.h | 2 + include/uapi/linux/bpf.h | 58 ++++++++- kernel/bpf/syscall.c | 10 ++ kernel/bpf/verifier.c | 7 +- net/core/filter.c | 231 ++++++++++++++++++++++++++++++++++++ 7 files changed, 325 insertions(+), 2 deletions(-)
include/linux/bpf_types.h +1 -0
@@ -37,6 +37,7 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_LIRC_MODE2, lirc_mode2)#endif #ifdef CONFIG_INET BPF_PROG_TYPE(BPF_PROG_TYPE_SK_REUSEPORT, sk_reuseport) +BPF_PROG_TYPE(BPF_PROG_TYPE_INET_LOOKUP, inet_lookup)#endif BPF_MAP_TYPE (BPF_MAP_TYPE_ARRAY , array_map_ops)
include/linux/filter.h +18 -0
@@ -1229,4 +1229,22 @@ struct bpf_sockopt_kern { s32 retval; }; +struct bpf_inet_lookup_kern { +unsigned short family; +u8 protocol; +__be32 saddr; +struct in6_addr saddr6; +__be16 sport; +__be32 daddr; +struct in6_addr daddr6; +unsigned short hnum; +struct sock *redir_sk; +}; + +int inet_lookup_bpf_prog_attach (const union bpf_attr *attr, +struct bpf_prog *prog); +int inet_lookup_bpf_prog_detach (const union bpf_attr *attr); +int inet_lookup_bpf_prog_query (const union bpf_attr *attr, +union bpf_attr __user *uattr); +#endif /* __LINUX_FILTER_H__ */
include/net/net_namespace.h +2 -0
@@ -171,6 +171,8 @@ struct net {#ifdef CONFIG_XDP_SOCKETS struct netns_xdp xdp;#endif +struct bpf_prog __rcu *inet_lookup_prog; +struct sock *diag_nlsk;atomic_t fnhe_genid; } __randomize_layout;
include/uapi/linux/bpf.h +57 -1
88 lines not on this page · raw message
kernel/bpf/syscall.c +10 -0
@@ -1935,6 +1935,9 @@ static int bpf_prog_attach(const union bpf_attr *attr)case BPF_CGROUP_SETSOCKOPT : ptype =BPF_PROG_TYPE_CGROUP_SOCKOPT ;break ; +case BPF_INET_LOOKUP : + ptype =BPF_PROG_TYPE_INET_LOOKUP ; +break ;default :return -EINVAL ; }
@@ -1959,6 +1962,9 @@ static int bpf_prog_attach(const union bpf_attr *attr)case BPF_PROG_TYPE_FLOW_DISSECTOR : ret =skb_flow_dissector_bpf_prog_attach (attr, prog);break ; +case BPF_PROG_TYPE_INET_LOOKUP : + ret =inet_lookup_bpf_prog_attach (attr, prog); +break ;default : ret =cgroup_bpf_prog_attach (attr, ptype, prog); }
@@ -2022,6 +2028,8 @@ static int bpf_prog_detach(const union bpf_attr *attr)case BPF_CGROUP_SETSOCKOPT : ptype =BPF_PROG_TYPE_CGROUP_SOCKOPT ;break ; +case BPF_INET_LOOKUP : +return inet_lookup_bpf_prog_detach (attr);default :return -EINVAL ; }
@@ -2065,6 +2073,8 @@ static int bpf_prog_query(const union bpf_attr *attr,return lirc_prog_query (attr, uattr);case BPF_FLOW_DISSECTOR :return skb_flow_dissector_prog_query (attr, uattr); +case BPF_INET_LOOKUP : +return inet_lookup_bpf_prog_query (attr, uattr);default :return -EINVAL ; }
kernel/bpf/verifier.c +6 -1
@@ -3494,7 +3494,8 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, goto error;break ;case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY : -if (func_id != BPF_FUNC_sk_select_reuseport) +if (func_id != BPF_FUNC_sk_select_reuseport && + func_id != BPF_FUNC_redirect_lookup) goto error;break ;case BPF_MAP_TYPE_QUEUE :
@@ -3578,6 +3579,10 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,if (map->map_type !=BPF_MAP_TYPE_SK_STORAGE ) goto error;break ; +case BPF_FUNC_redirect_lookup: +if (map->map_type !=BPF_MAP_TYPE_REUSEPORT_SOCKARRAY ) + goto error; +break ;default :break ; }
net/core/filter.c +231 -0
235 lines not on this page · raw message
Signature · 1 line
2.20.1
RFC v2 4/12 inet: Store layer 4 protocol in inet_hashinfo
In reply to Jakub SitnickiMake it possible to identify the protocol of the sockets stored in hashinfo without looking up one.
Subsequent patches make use the new field at the socket lookup time to enforce that the BPF program selects only sockets with matching protocol.
--- include/net/inet_hashtables.h | 3 +++ net/dccp/proto.c | 2 +- net/ipv4/tcp_ipv4.c | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-)
include/net/inet_hashtables.h +3 -0
@@ -138,6 +138,9 @@ struct inet_hashinfo {unsigned int lhash2_mask;struct inet_listen_hashbucket *lhash2; +/* Layer 4 protocol of the stored sockets */ +int protocol; +/* All the above members are written once at bootup and * never written again _or_ are predominantly read-access. *
net/dccp/proto.c +1 -1
@@ -45,7 +45,7 @@ EXPORT_SYMBOL_GPL(dccp_statistics);struct percpu_counter dccp_orphan_count;EXPORT_SYMBOL_GPL (dccp_orphan_count); -struct inet_hashinfo dccp_hashinfo; +struct inet_hashinfo dccp_hashinfo = { .protocol =IPPROTO_DCCP };EXPORT_SYMBOL_GPL (dccp_hashinfo);/* the maximum queue length for tx in packets. 0 is no limit */
net/ipv4/tcp_ipv4.c +1 -1
@@ -87,7 +87,7 @@ static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key, __be32 daddr, __be32 saddr, const struct tcphdr *th);#endif -struct inet_hashinfo tcp_hashinfo; +struct inet_hashinfo tcp_hashinfo = { .protocol =IPPROTO_TCP };EXPORT_SYMBOL (tcp_hashinfo);static u32 tcp_v4_init_seq (const struct sk_buff *skb)
Signature · 1 line
2.20.1
RFC v2 3/12 bpf: Add verifier tests for inet_lookup context access
In reply to Jakub SitnickiExercise verifier access checks for bpf_inet_lookup context object fields.
--- .../selftests/bpf/verifier/ctx_inet_lookup.c | 696 ++++++++++++++++++ 1 file changed, 696 insertions(+) create mode 100644 tools/testing/selftests/bpf/verifier/ctx_inet_lookup.c
tools/testing/selftests/bpf/verifier/ctx_inet_lookup.c new file +696 -0
@@ -0,0 +1,696 @@ +{ + "valid 1,2,4-byte read bpf_inet_lookup remote_ip4", + .insns = { +/* 4-byte read */ +BPF_LDX_MEM (BPF_W ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup , remote_ip4)), +/* 2-byte read */ +BPF_LDX_MEM (BPF_H ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup , remote_ip4)), +BPF_LDX_MEM (BPF_H ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup , remote_ip4) +2 ), +/* 1-byte read */ +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup , remote_ip4)), +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup , remote_ip4) +3 ), + BPF_EXIT_INSN(), + }, + .result =ACCEPT , + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ +"invalid 8-byte read bpf_inet_lookup remote_ip4" , + .insns = { +/* 8-byte read */ +BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup , remote_ip4)), + BPF_EXIT_INSN(), + }, + .errstr ="invalid bpf_context access" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ +"invalid 8-byte write bpf_inet_lookup remote_ip4" , + .insns = { +BPF_MOV64_IMM (BPF_REG_0 ,0x7f000001U ), +/* 4-byte write */ +BPF_STX_MEM (BPF_DW ,BPF_REG_1 ,BPF_REG_0 , + offsetof(struct bpf_inet_lookup , remote_ip4)), + BPF_EXIT_INSN(), + }, + .errstr ="invalid bpf_context access" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ +"invalid 4-byte write bpf_inet_lookup remote_ip4" , + .insns = { +BPF_MOV64_IMM (BPF_REG_0 ,0x7f000001U ), +/* 4-byte write */ +BPF_STX_MEM (BPF_W ,BPF_REG_1 ,BPF_REG_0 , + offsetof(struct bpf_inet_lookup , remote_ip4)), + BPF_EXIT_INSN(), + }, + .errstr ="invalid bpf_context access" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ +"invalid 2-byte write bpf_inet_lookup remote_ip4" , + .insns = { +BPF_MOV64_IMM (BPF_REG_0 ,0x7f000001U ), +/* 2-byte write */ +BPF_STX_MEM (BPF_H ,BPF_REG_1 ,BPF_REG_0 , + offsetof(struct bpf_inet_lookup , remote_ip4)), + BPF_EXIT_INSN(), + }, + .errstr ="invalid bpf_context access" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ +"invalid 1-byte write bpf_inet_lookup remote_ip4" , + .insns = { +BPF_MOV64_IMM (BPF_REG_0 ,0x7f000001U ), +/* 1-byte write */ +BPF_STX_MEM (BPF_B ,BPF_REG_1 ,BPF_REG_0 , + offsetof(struct bpf_inet_lookup , remote_ip4)), + BPF_EXIT_INSN(), + }, + .errstr ="invalid bpf_context access" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ +"valid 1,2,4-byte read bpf_inet_lookup local_ip4" , + .insns = { +/* 4-byte read */ +BPF_LDX_MEM (BPF_W ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup , local_ip4)), +/* 2-byte read */ +BPF_LDX_MEM (BPF_H ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup , local_ip4)), +BPF_LDX_MEM (BPF_H ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup , local_ip4) +2 ), +/* 1-byte read */ +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup , local_ip4)), +BPF_LDX_MEM (BPF_B ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup , local_ip4) +3 ), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .result =ACCEPT , + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ +"invalid 8-byte read bpf_inet_lookup local_ip4" , + .insns = { +BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup , local_ip4)), + BPF_EXIT_INSN(), + }, + .errstr ="invalid bpf_context access" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ +"invalid 8-byte write bpf_inet_lookup local_ip4" , + .insns = { +BPF_MOV64_IMM (BPF_REG_0 ,0x7f000001U ), +BPF_STX_MEM (BPF_DW ,BPF_REG_1 ,BPF_REG_0 , + offsetof(struct bpf_inet_lookup , local_ip4)), + BPF_EXIT_INSN(), + }, + .errstr ="invalid bpf_context access" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ +"invalid 4-byte write bpf_inet_lookup local_ip4" , + .insns = { +BPF_MOV64_IMM (BPF_REG_0 ,0x7f000001U ), +BPF_STX_MEM (BPF_W ,BPF_REG_1 ,BPF_REG_0 , + offsetof(struct bpf_inet_lookup , local_ip4)), + BPF_EXIT_INSN(), + }, + .errstr ="invalid bpf_context access" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ +"invalid 2-byte write bpf_inet_lookup local_ip4" , + .insns = { +BPF_MOV64_IMM (BPF_REG_0 ,0x7f000001U ), +BPF_STX_MEM (BPF_H ,BPF_REG_1 ,BPF_REG_0 , + offsetof(struct bpf_inet_lookup , local_ip4)), + BPF_EXIT_INSN(), + }, + .errstr ="invalid bpf_context access" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ +"invalid 1-byte write bpf_inet_lookup local_ip4" , + .insns = { +BPF_MOV64_IMM (BPF_REG_0 ,0x7f000001U ), +BPF_STX_MEM (BPF_B ,BPF_REG_1 ,BPF_REG_0 , + offsetof(struct bpf_inet_lookup , local_ip4)), + BPF_EXIT_INSN(), + }, + .errstr ="invalid bpf_context access" , + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ +"valid 1,2,4-byte read bpf_inet_lookup remote_ip6" , + .insns = { +/* 4-byte read */ +BPF_LDX_MEM (BPF_W ,BPF_REG_0 ,BPF_REG_1 , + offsetof(struct bpf_inet_lookup, remote_ip6[0])), + BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, remote_ip6[3])), + /* 2-byte read */ + BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, remote_ip6[0])), + BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, + remote_ip6[3]) + 2), + /* 1-byte read */ + BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, remote_ip6[0])), + BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, + remote_ip6[3]) + 3), + BPF_EXIT_INSN(), + }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 8-byte read bpf_inet_lookup remote_ip6", + .insns = { + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, remote_ip6[0])), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 8-byte write bpf_inet_lookup remote_ip6", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 0x00000001U), + BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, remote_ip6[0])), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 4-byte write bpf_inet_lookup remote_ip6", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 0x00000001U), + BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, remote_ip6[0])), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 2-byte write bpf_inet_lookup remote_ip6", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 0x00000001U), + BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, remote_ip6[0])), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 1-byte write bpf_inet_lookup remote_ip6", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 0x00000001U), + BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, remote_ip6[0])), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "valid 1,2,4-byte read bpf_inet_lookup local_ip6", + .insns = { + /* 4-byte read */ + BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, local_ip6[0])), + BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, local_ip6[3])), + /* 2-byte read */ + BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, local_ip6[0])), + BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, local_ip6[3]) + 2), + /* 1-byte read */ + BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, local_ip6[0])), + BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, local_ip6[3]) + 3), + BPF_EXIT_INSN(), + }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 8-byte read bpf_inet_lookup local_ip6", + .insns = { + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, local_ip6[0])), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 8-byte write bpf_inet_lookup local_ip6", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 0x00000001U), + BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, local_ip6[0])), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 4-byte write bpf_inet_lookup local_ip6", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 0x00000001U), + BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, local_ip6[0])), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 2-byte write bpf_inet_lookup local_ip6", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 0x00000001U), + BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, local_ip6[0])), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 1-byte write bpf_inet_lookup local_ip6", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 0x00000001U), + BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, local_ip6[0])), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "valid 4-byte read bpf_inet_lookup remote_port", + .insns = { + BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, remote_port)), + BPF_EXIT_INSN(), + }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 8-byte read bpf_inet_lookup remote_port", + .insns = { + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, remote_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 2-byte read bpf_inet_lookup remote_port", + .insns = { + BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, remote_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 1-byte read bpf_inet_lookup remote_port", + .insns = { + BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, remote_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 8-byte write bpf_inet_lookup remote_port", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, remote_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 4-byte write bpf_inet_lookup remote_port", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, remote_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 2-byte write bpf_inet_lookup remote_port", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, remote_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 1-byte write bpf_inet_lookup remote_port", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, remote_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "valid 4-byte read bpf_inet_lookup local_port", + .insns = { + BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, local_port)), + BPF_EXIT_INSN(), + }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 8-byte read bpf_inet_lookup local_port", + .insns = { + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, local_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 2-byte read bpf_inet_lookup local_port", + .insns = { + BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, local_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 1-byte read bpf_inet_lookup local_port", + .insns = { + BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, local_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 8-byte write bpf_inet_lookup local_port", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, local_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 4-byte write bpf_inet_lookup local_port", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, local_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 2-byte write bpf_inet_lookup local_port", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, local_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 1-byte write bpf_inet_lookup local_port", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, local_port)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "valid 4-byte read bpf_inet_lookup family", + .insns = { + BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, family)), + BPF_EXIT_INSN(), + }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 8-byte read bpf_inet_lookup family", + .insns = { + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, family)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 2-byte read bpf_inet_lookup family", + .insns = { + BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, family)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 1-byte read bpf_inet_lookup family", + .insns = { + BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, family)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 8-byte write bpf_inet_lookup family", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, family)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 4-byte write bpf_inet_lookup family", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, family)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 2-byte write bpf_inet_lookup family", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, family)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 1-byte write bpf_inet_lookup family", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, family)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "valid 4-byte read bpf_inet_lookup protocol", + .insns = { + BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, protocol)), + BPF_EXIT_INSN(), + }, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 8-byte read bpf_inet_lookup protocol", + .insns = { + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, protocol)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 2-byte read bpf_inet_lookup protocol", + .insns = { + BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, protocol)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 1-byte read bpf_inet_lookup protocol", + .insns = { + BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_inet_lookup, protocol)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 8-byte write bpf_inet_lookup protocol", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, protocol)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 4-byte write bpf_inet_lookup protocol", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, protocol)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 2-byte write bpf_inet_lookup protocol", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, protocol)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +}, +{ + "invalid 1-byte write bpf_inet_lookup protocol", + .insns = { + BPF_MOV64_IMM(BPF_REG_0, 1234), + BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, + offsetof(struct bpf_inet_lookup, protocol)), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_INET_LOOKUP, +},
Signature · 1 line
2.20.1
RFC v2 5/12 udp: Store layer 4 protocol in udp_table
In reply to Jakub SitnickiBecause UDP and UDP-Lite share code we have to pass the L4 protocol identifier alongside the socket table to down call sites where distinguishing between the two is needed.
There is currently only one such call site, which by itself is not reason enough for the change.
However, subsequent patches will also make use the new udp_table field inside the socket lookup routine to enforce that the BPF program selects only sockets with matching protocol.
--- include/net/udp.h | 10 ++++++---- net/ipv4/udp.c | 15 +++++++-------- net/ipv4/udp_impl.h | 2 +- net/ipv4/udplite.c | 4 ++-- net/ipv6/udp.c | 12 ++++++------ net/ipv6/udp_impl.h | 2 +- net/ipv6/udplite.c | 2 +- 7 files changed, 24 insertions(+), 23 deletions(-)
include/net/udp.h +6 -4
@@ -63,16 +63,18 @@ struct udp_hslot {/** * struct udp_table - UDP table * -* @hash: hash table, sockets are hashed on (local port) -* @hash2: hash table, sockets are hashed on (local port, local address) -* @mask: number of slots in hash tables, minus 1 -* @log: log2(number of slots in hash table) +* @hash: hash table, sockets are hashed on (local port) +* @hash2: hash table, sockets are hashed on (local port, local address) +* @mask: number of slots in hash tables, minus 1 +* @log: log2(number of slots in hash table) +* @protocol: layer 4 protocol of the stored sockets */ struct udp_table {struct udp_hslot *hash;struct udp_hslot *hash2;unsigned int mask;unsigned int log; +int protocol; };extern struct udp_table udp_table;void udp_table_init (struct udp_table *,const char *);
net/ipv4/udp.c +7 -8
@@ -113,7 +113,7 @@#include <net/addrconf.h> #include <net/udp_tunnel.h> -struct udp_table udp_table__read_mostly ; +struct udp_table udp_table__read_mostly = { .protocol =IPPROTO_UDP };EXPORT_SYMBOL (udp_table);long sysctl_udp_mem[3 ]__read_mostly ;
@@ -2106,8 +2106,7 @@ EXPORT_SYMBOL(udp_sk_rx_dst_set);static int __udp4_lib_mcast_deliver (struct net *net,struct sk_buff *skb,struct udphdr *uh,__be32 saddr,__be32 daddr, -struct udp_table *udptable, -int proto) +struct udp_table *udptable) {struct sock *sk, *first =NULL ;unsigned short hnum =ntohs (uh->dest);
@@ -2163,7 +2162,7 @@ static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, }else {kfree_skb (skb);__UDP_INC_STATS (net,UDP_MIB_IGNOREDMULTI , - proto ==IPPROTO_UDPLITE ); + udptable->protocol ==IPPROTO_UDPLITE ); }return 0 ; }
@@ -2240,8 +2239,7 @@ static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,* All we need to do is get the socket, and then do a checksum. */ -int __udp4_lib_rcv (struct sk_buff *skb,struct udp_table *udptable, -int proto) +int __udp4_lib_rcv (struct sk_buff *skb,struct udp_table *udptable) {struct sock *sk;struct udphdr *uh;
@@ -2249,6 +2247,7 @@ int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,struct rtable *rt =skb_rtable (skb);__be32 saddr, daddr;struct net *net =dev_net (skb->dev); +int proto = udptable->protocol;/* * Validate the packet.
@@ -2289,7 +2288,7 @@ int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,if (rt->rt_flags & (RTCF_BROADCAST |RTCF_MULTICAST ))return __udp4_lib_mcast_deliver (net, skb, uh, - saddr, daddr, udptable, proto); + saddr, daddr, udptable); sk =__udp4_lib_lookup_skb (skb, uh->source, uh->dest, udptable);if (sk)
@@ -2463,7 +2462,7 @@ int udp_v4_early_demux(struct sk_buff *skb)int udp_rcv (struct sk_buff *skb) { -return __udp4_lib_rcv (skb, &udp_table,IPPROTO_UDP ); +return __udp4_lib_rcv (skb, &udp_table); }void udp_destroy_sock (struct sock *sk)
net/ipv4/udp_impl.h +1 -1
@@ -6,7 +6,7 @@#include <net/protocol.h> #include <net/inet_common.h> -int __udp4_lib_rcv (struct sk_buff *,struct udp_table *,int ); +int __udp4_lib_rcv (struct sk_buff *,struct udp_table *);int __udp4_lib_err (struct sk_buff *,u32 ,struct udp_table *);int udp_v4_get_port (struct sock *sk,unsigned short snum);
net/ipv4/udplite.c +2 -2
@@ -14,12 +14,12 @@#include <linux/proc_fs.h> #include "udp_impl.h" -struct udp_table udplite_table__read_mostly ; +struct udp_table udplite_table__read_mostly = { .protocol =IPPROTO_UDPLITE };EXPORT_SYMBOL (udplite_table);static int udplite_rcv (struct sk_buff *skb) { -return __udp4_lib_rcv (skb, &udplite_table,IPPROTO_UDPLITE ); +return __udp4_lib_rcv (skb, &udplite_table); }static int udplite_err (struct sk_buff *skb,u32 info)
net/ipv6/udp.c +6 -6
@@ -741,7 +741,7 @@ static void udp6_csum_zero_error(struct sk_buff *skb)*/ static int __udp6_lib_mcast_deliver (struct net *net,struct sk_buff *skb,const struct in6_addr *saddr,const struct in6_addr *daddr, -struct udp_table *udptable,int proto) +struct udp_table *udptable) {struct sock *sk, *first =NULL ;const struct udphdr *uh =udp_hdr (skb);
@@ -803,7 +803,7 @@ static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, }else {kfree_skb (skb);__UDP6_INC_STATS (net,UDP_MIB_IGNOREDMULTI , - proto ==IPPROTO_UDPLITE ); + udptable->protocol ==IPPROTO_UDPLITE ); }return 0 ; }
@@ -836,11 +836,11 @@ static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,return 0 ; } -int __udp6_lib_rcv (struct sk_buff *skb,struct udp_table *udptable, -int proto) +int __udp6_lib_rcv (struct sk_buff *skb,struct udp_table *udptable) {const struct in6_addr *saddr, *daddr;struct net *net =dev_net (skb->dev); +int proto = udptable->protocol;struct udphdr *uh;struct sock *sk;u32 ulen =0 ;
@@ -902,7 +902,7 @@ int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,*/ if (ipv6_addr_is_multicast (daddr))return __udp6_lib_mcast_deliver (net, skb, - saddr, daddr, udptable, proto); + saddr, daddr, udptable);/* Unicast */ sk =__udp6_lib_lookup_skb (skb, uh->source, uh->dest, udptable);
@@ -1011,7 +1011,7 @@ INDIRECT_CALLABLE_SCOPE void udp_v6_early_demux(struct sk_buff *skb) INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb) { -return __udp6_lib_rcv (skb, &udp_table,IPPROTO_UDP ); +return __udp6_lib_rcv (skb, &udp_table); }/*
net/ipv6/udp_impl.h +1 -1
@@ -8,7 +8,7 @@#include <net/inet_common.h> #include <net/transp_v6.h> -int __udp6_lib_rcv (struct sk_buff *,struct udp_table *,int ); +int __udp6_lib_rcv (struct sk_buff *,struct udp_table *);int __udp6_lib_err (struct sk_buff *,struct inet6_skb_parm *,u8 ,u8 ,int ,__be32 ,struct udp_table *);
net/ipv6/udplite.c +1 -1
@@ -14,7 +14,7 @@static int udplitev6_rcv (struct sk_buff *skb) { -return __udp6_lib_rcv (skb, &udplite_table,IPPROTO_UDPLITE ); +return __udp6_lib_rcv (skb, &udplite_table); } static int udplitev6_err(struct sk_buff *skb,
Signature · 1 line
2.20.1
RFC v2 6/12 inet: Run inet_lookup bpf program on socket lookup
In reply to Jakub SitnickiRun a BPF program before looking up the listening socket. The program can redirect the skb to a listening socket of its choice, providing it calls bpf_redirect_lookup() helper and returns BPF_REDIRECT.
This lets the user-space program mappings between packet 4-tuple and listening sockets. With the possibility to override the socket lookup from BPF, applications don't need to bind sockets to every addresses they receive on, or resort to listening on all addresses with INADDR_ANY.
Also port sharing conflicts become a non-issue. Application can listen on any free port and still receive traffic destined to its assigned service port.
--- include/net/inet_hashtables.h | 33 +++++++++++++++++++++++++++++++++ net/ipv4/inet_hashtables.c | 5 +++++ 2 files changed, 38 insertions(+)
include/net/inet_hashtables.h +33 -0
@@ -417,4 +417,37 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,int inet_hash_connect (struct inet_timewait_death_row *death_row,struct sock *sk); + +static inline struct sock *__inet_lookup_run_bpf (const struct net *net, +struct bpf_inet_lookup_kern *ctx) +{ +struct bpf_prog *prog; +int ret =BPF_OK ; + +rcu_read_lock (); + prog =rcu_dereference (net->inet_lookup_prog); +if (prog) + ret =BPF_PROG_RUN (prog, ctx); +rcu_read_unlock (); + +return ret ==BPF_REDIRECT ? ctx->redir_sk :NULL ; +} + +static inline struct sock *inet_lookup_run_bpf (const struct net *net,u8 proto, +__be32 saddr,__be16 sport, +__be32 daddr, +unsigned short hnum) +{ +struct bpf_inet_lookup_kern ctx = { + .family =AF_INET , + .protocol = proto, + .saddr = saddr, + .sport = sport, + .daddr = daddr, + .hnum = hnum, + }; + +return __inet_lookup_run_bpf (net, &ctx); +} +#endif /* _INET_HASHTABLES_H */
net/ipv4/inet_hashtables.c +5 -0
@@ -299,6 +299,11 @@ struct sock *__inet_lookup_listener(struct net *net,struct sock *result =NULL ;unsigned int hash2; + result =inet_lookup_run_bpf (net, hashinfo->protocol, + saddr, sport, daddr, hnum); +if (result) + goto done; + hash2 =ipv4_portaddr_hash (net, daddr, hnum); ilb2 =inet_lhash2_bucket (hashinfo, hash2);
Signature · 1 line
2.20.1
RFC v2 7/12 inet6: Run inet_lookup bpf program on socket lookup
In reply to Jakub SitnickiFollowing the ipv4 changes, run a BPF program attached to netns in context of which we're doing the socket lookup so that it can redirect the skb to a socket of its choice. The program runs before the listening socket lookup.
--- include/net/inet6_hashtables.h | 19 +++++++++++++++++++ net/ipv6/inet6_hashtables.c | 5 +++++ 2 files changed, 24 insertions(+)
include/net/inet6_hashtables.h +19 -0
@@ -104,6 +104,25 @@ struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo, const int dif);int inet6_hash (struct sock *sk); + +static inline struct sock *inet6_lookup_run_bpf (struct net *net,u8 proto, +const struct in6_addr *saddr, +__be16 sport, +const struct in6_addr *daddr, +unsigned short hnum) +{ +struct bpf_inet_lookup_kern ctx = { + .family =AF_INET6 , + .protocol = proto, + .saddr6 = *saddr, + .sport = sport, + .daddr6 = *daddr, + .hnum = hnum, + }; + +return __inet_lookup_run_bpf (net, &ctx); +} +#endif /* IS_ENABLED(CONFIG_IPV6) */ #define INET6_MATCH (__sk, __net, __saddr, __daddr, __ports, __dif, __sdif) \
net/ipv6/inet6_hashtables.c +5 -0
@@ -157,6 +157,11 @@ struct sock *inet6_lookup_listener(struct net *net,struct sock *result =NULL ;unsigned int hash2; + result =inet6_lookup_run_bpf (net, hashinfo->protocol, + saddr, sport, daddr, hnum); +if (result) + goto done; + hash2 =ipv6_portaddr_hash (net, daddr, hnum); ilb2 =inet_lhash2_bucket (hashinfo, hash2);
Signature · 1 line
2.20.1
RFC v2 8/12 udp: Run inet_lookup bpf program on socket lookup
In reply to Jakub SitnickiFollowing the TCP socket lookup changes, allow selecting the receiving socket from BPF before searching for bound socket by destination address and port.
As connected and bound but non-connected socket lookup currently happens in one step, we split the lookup in two phases to run BPF only after a lookup for a connected socket was a miss. Hence making sure connected UDP sockets continue to work as expected in presence of a BPF inet_lookup program.
--- net/ipv4/udp.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-)
net/ipv4/udp.c +32 -12
@@ -353,7 +353,7 @@ int udp_v4_get_port(struct sock *sk, unsigned short snum)static int compute_score (struct sock *sk,struct net *net,__be32 saddr,__be16 sport,__be32 daddr,unsigned short hnum, -int dif,int sdif) +int dif,int sdif,unsigned char state) {int score;struct inet_sock *inet;
@@ -364,6 +364,9 @@ static int compute_score(struct sock *sk, struct net *net, ipv6_only_sock(sk))return -1 ; +if (state && sk->sk_state != state) +return -1 ; +if (sk->sk_rcv_saddr != daddr)return -1 ;
@@ -411,7 +414,8 @@ static struct sock *udp4_lib_lookup2(struct net *net, __be32 daddr, unsigned int hnum, int dif, int sdif, struct udp_hslot *hslot2, - struct sk_buff *skb) + struct sk_buff *skb, + unsigned char state) { struct sock *sk, *result;int score, badness;
@@ -421,7 +425,7 @@ static struct sock *udp4_lib_lookup2(struct net *net, badness =0 ;udp_portaddr_for_each_entry_rcu (sk, &hslot2->head) { score =compute_score (sk, net, saddr, sport, - daddr, hnum, dif, sdif); + daddr, hnum, dif, sdif, state);if (score > badness) {if (sk->sk_reuseport) { hash = udp_ehashfn(net, daddr, hnum,
@@ -454,18 +458,34 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, slot2 = hash2 & udptable->mask; hslot2 = &udptable->hash2[slot2]; +/* Lookup connected sockets */ result =udp4_lib_lookup2 (net, saddr, sport, daddr, hnum, dif, sdif, - hslot2, skb); -if (!result) { - hash2 =ipv4_portaddr_hash (net,htonl (INADDR_ANY ), hnum); - slot2 = hash2 & udptable->mask; - hslot2 = &udptable->hash2[slot2]; + hslot2, skb,TCP_ESTABLISHED ); +if (result) + goto done; - result =udp4_lib_lookup2 (net, saddr, sport, -htonl (INADDR_ANY ), hnum, dif, sdif, - hslot2, skb); - } +/* Lookup redirect from BPF */ + result =inet_lookup_run_bpf (net, udptable->protocol, + saddr, sport, daddr, hnum); +if (result) + goto done; + +/* Lookup bound sockets */ + result =udp4_lib_lookup2 (net, saddr, sport, + daddr, hnum, dif, sdif, + hslot2, skb,0 ); +if (result) + goto done; + + hash2 =ipv4_portaddr_hash (net,htonl (INADDR_ANY ), hnum); + slot2 = hash2 & udptable->mask; + hslot2 = &udptable->hash2[slot2]; + + result =udp4_lib_lookup2 (net, saddr, sport, +htonl (INADDR_ANY ), hnum, dif, sdif, + hslot2, skb,0 ); +done:if (IS_ERR (result))return NULL ;return result;
Signature · 1 line
2.20.1
RFC v2 9/12 udp6: Run inet_lookup bpf program on socket lookup
In reply to Jakub SitnickiSame as for udp, split the socket lookup into two phases and let the BPF inet_lookup program select the receiving socket.
--- net/ipv6/udp.c | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-)
net/ipv6/udp.c +30 -12
@@ -101,7 +101,7 @@ void udp_v6_rehash(struct sock *sk)static int compute_score (struct sock *sk,struct net *net,const struct in6_addr *saddr,__be16 sport,const struct in6_addr *daddr,unsigned short hnum, -int dif,int sdif) +int dif,int sdif,unsigned char state) {int score;struct inet_sock *inet;
@@ -112,6 +112,9 @@ static int compute_score(struct sock *sk, struct net *net, sk->sk_family != PF_INET6)return -1 ; +if (state && sk->sk_state != state) +return -1 ; +if (!ipv6_addr_equal (&sk->sk_v6_rcv_saddr, daddr))return -1 ;
@@ -146,7 +149,7 @@ static struct sock *udp6_lib_lookup2(struct net *net, const struct in6_addr *saddr, __be16 sport, const struct in6_addr *daddr, unsigned int hnum, int dif, int sdif, struct udp_hslot *hslot2, - struct sk_buff *skb) + struct sk_buff *skb, unsigned char state) { struct sock *sk, *result;int score, badness;
@@ -156,7 +159,7 @@ static struct sock *udp6_lib_lookup2(struct net *net, badness =-1 ;udp_portaddr_for_each_entry_rcu (sk, &hslot2->head) { score =compute_score (sk, net, saddr, sport, - daddr, hnum, dif, sdif); + daddr, hnum, dif, sdif, state);if (score > badness) {if (sk->sk_reuseport) { hash = udp6_ehashfn(net, daddr, hnum,
@@ -190,19 +193,34 @@ struct sock *__udp6_lib_lookup(struct net *net, slot2 = hash2 & udptable->mask; hslot2 = &udptable->hash2[slot2]; +/* Lookup connected sockets */ result =udp6_lib_lookup2 (net, saddr, sport, daddr, hnum, dif, sdif, - hslot2, skb); -if (!result) { - hash2 =ipv6_portaddr_hash (net, &in6addr_any, hnum); - slot2 = hash2 & udptable->mask; + hslot2, skb,TCP_ESTABLISHED ); +if (result) + goto done; - hslot2 = &udptable->hash2[slot2]; +/* Lookup redirect from BPF */ + result =inet6_lookup_run_bpf (net, udptable->protocol, + saddr, sport, daddr, hnum); +if (result) + goto done; - result =udp6_lib_lookup2 (net, saddr, sport, - &in6addr_any, hnum, dif, sdif, - hslot2, skb); - } +/* Lookup bound sockets */ + result =udp6_lib_lookup2 (net, saddr, sport, + daddr, hnum, dif, sdif, + hslot2, skb,0 ); +if (result) + goto done; + + hash2 =ipv6_portaddr_hash (net, &in6addr_any, hnum); + slot2 = hash2 & udptable->mask; + hslot2 = &udptable->hash2[slot2]; + + result =udp6_lib_lookup2 (net, saddr, sport, + &in6addr_any, hnum, dif, sdif, + hslot2, skb,0 ); +done:if (IS_ERR (result))return NULL ;return result;
Signature · 1 line
2.20.1
RFC v2 10/12 bpf: Sync linux/bpf.h to tools/
In reply to Jakub SitnickiNewly added program, context type and helper is used by tests in a subsequent patch. Synchronize the header file.
--- tools/include/uapi/linux/bpf.h | 58 +++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-)
tools/include/uapi/linux/bpf.h +57 -1
@@ -173,6 +173,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_CGROUP_SYSCTL ,BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE ,BPF_PROG_TYPE_CGROUP_SOCKOPT , + BPF_PROG_TYPE_INET_LOOKUP, }; enum bpf_attach_type {
@@ -199,6 +200,7 @@ enum bpf_attach_type {BPF_CGROUP_UDP6_RECVMSG ,BPF_CGROUP_GETSOCKOPT ,BPF_CGROUP_SETSOCKOPT , +BPF_INET_LOOKUP , __MAX_BPF_ATTACH_TYPE };
@@ -2747,6 +2749,33 @@ union bpf_attr {* **-EOPNOTSUPP** kernel configuration does not enable SYN cookies * * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 +* +* int bpf_redirect_lookup(struct bpf_inet_lookup_kern *ctx, struct bpf_map *sockarray, void *key, u64 flags) +* Description +* Select a socket referenced by *map* (of type +* **BPF_MAP_TYPE_REUSEPORT_SOCKARRAY**) at index *key* to use as a +* result of listening (TCP) or bound (UDP) socket lookup. +* +* The IP family and L4 protocol in *ctx* object, populated from +* the packet that triggered the lookup, must match the selected +* socket's family and protocol. IP6_V6ONLY socket option is +* honored. +* +* To be used by **BPF_INET_LOOKUP** programs attached to the +* network namespace. Program needs to return **BPF_REDIRECT**, the +* helper's success return value, for the selected socket to be +* actually used. +* +* Return +* **BPF_REDIRECT** on success, if the socket at index *key* was selected. +* +* **-EINVAL** if *flags* are invalid (not zero). +* +* **-ENOENT** if there is no socket at index *key*. +* +* **-EPROTOTYPE** if *ctx->protocol* does not match the socket protocol. +* +* **-EAFNOSUPPORT** if socket does not accept IP version in *ctx->family*. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2859,7 +2888,8 @@ union bpf_attr {FN (sk_storage_get), \FN (sk_storage_delete), \FN (send_signal), \ -FN (tcp_gen_syncookie), +FN (tcp_gen_syncookie), \ +FN (redirect_lookup),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -3116,6 +3146,32 @@ struct bpf_tcp_sock {__u32 icsk_retransmits;/* Number of unrecovered [RTO] timeouts */ }; +/* User accessible data for inet_lookup programs. +* New fields must be added at the end. +*/ +struct bpf_inet_lookup { +__u32 family;/* AF_INET, AF_INET6 */ +__u32 protocol;/* IPROTO_TCP, IPPROTO_UDP */ +__u32 remote_ip4;/* Allows 1,2,4-byte read but no write. +* Stored in network byte order. +*/ +__u32 local_ip4;/* Allows 1,2,4-byte read and 4-byte write. +* Stored in network byte order. +*/ +__u32 remote_ip6[4 ];/* Allows 1,2,4-byte read but no write. +* Stored in network byte order. +*/ +__u32 local_ip6[4 ];/* Allows 1,2,4-byte read and 4-byte write. +* Stored in network byte order. +*/ +__u32 remote_port;/* Allows 4-byte read but no write. +* Stored in network byte order. +*/ +__u32 local_port;/* Allows 4-byte read and write. +* Stored in host byte order. +*/ +}; + struct bpf_sock_tuple { union { struct {
Signature · 1 line
2.20.1
RFC v2 11/12 libbpf: Add support for inet_lookup program type
In reply to Jakub SitnickiMake libbpf aware of the newly added program type. Reserve a section name for it.
--- tools/lib/bpf/libbpf.c | 4 ++++ tools/lib/bpf/libbpf.h | 2 ++ tools/lib/bpf/libbpf.map | 2 ++ tools/lib/bpf/libbpf_probes.c | 1 + 4 files changed, 9 insertions(+)
tools/lib/bpf/libbpf.c +4 -0
@@ -3580,6 +3580,7 @@ static bool bpf_prog_type__needs_kver(enum bpf_prog_type type)case BPF_PROG_TYPE_PERF_EVENT :case BPF_PROG_TYPE_CGROUP_SYSCTL :case BPF_PROG_TYPE_CGROUP_SOCKOPT : +case BPF_PROG_TYPE_INET_LOOKUP :return false;case BPF_PROG_TYPE_KPROBE :default :
@@ -4447,6 +4448,7 @@ BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);BPF_PROG_TYPE_FNS (raw_tracepoint,BPF_PROG_TYPE_RAW_TRACEPOINT );BPF_PROG_TYPE_FNS (xdp,BPF_PROG_TYPE_XDP );BPF_PROG_TYPE_FNS (perf_event,BPF_PROG_TYPE_PERF_EVENT ); +BPF_PROG_TYPE_FNS (inet_lookup,BPF_PROG_TYPE_INET_LOOKUP );void bpf_program__set_expected_attach_type (struct bpf_program *prog,enum bpf_attach_type type)
@@ -4542,6 +4544,8 @@ static const struct { BPF_CGROUP_GETSOCKOPT),BPF_EAPROG_SEC ("cgroup/setsockopt" ,BPF_PROG_TYPE_CGROUP_SOCKOPT ,BPF_CGROUP_SETSOCKOPT ), +BPF_EAPROG_SEC ("inet_lookup" ,BPF_PROG_TYPE_INET_LOOKUP , + BPF_INET_LOOKUP), };#undef BPF_PROG_SEC_IMPL
tools/lib/bpf/libbpf.h +2 -0
@@ -262,6 +262,7 @@ LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog); LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog); LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog); LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog); +LIBBPF_API int bpf_program__set_inet_lookup(struct bpf_program *prog); LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,enum bpf_prog_type type);LIBBPF_API void
@@ -276,6 +277,7 @@ LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog); +LIBBPF_API bool bpf_program__is_inet_lookup(const struct bpf_program *prog);/* * No need for __attribute__((packed)), all members of 'bpf_map_def'
tools/lib/bpf/libbpf.map +2 -0
@@ -67,6 +67,7 @@ LIBBPF_0.0.1 { bpf_prog_test_run; bpf_prog_test_run_xattr; bpf_program__fd; + bpf_program__is_inet_lookup; bpf_program__is_kprobe; bpf_program__is_perf_event; bpf_program__is_raw_tracepoint;
@@ -84,6 +85,7 @@ LIBBPF_0.0.1 { bpf_program__priv; bpf_program__set_expected_attach_type; bpf_program__set_ifindex; + bpf_program__set_inet_lookup; bpf_program__set_kprobe; bpf_program__set_perf_event; bpf_program__set_prep;
tools/lib/bpf/libbpf_probes.c +1 -0
@@ -102,6 +102,7 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,case BPF_PROG_TYPE_FLOW_DISSECTOR :case BPF_PROG_TYPE_CGROUP_SYSCTL :case BPF_PROG_TYPE_CGROUP_SOCKOPT : +case BPF_PROG_TYPE_INET_LOOKUP :default :break ; }
Signature · 1 line
2.20.1
RFC v2 12/12 bpf: Test redirecting listening/receiving socket lookup
In reply to Jakub SitnickiCheck that steering the packets targeted at a local (address, port) that is different than the server's bind() address with a BPF inet_lookup program works as expected for TCP or UDP over either IPv4 or IPv6. Make sure that it is possible to redirect IPv4 packets to IPv6 sockets that are not V6-only.
--- tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 5 +- tools/testing/selftests/bpf/bpf_helpers.h | 3 + .../selftests/bpf/progs/inet_lookup_progs.c | 78 +++ .../testing/selftests/bpf/test_inet_lookup.c | 522 ++++++++++++++++++ .../testing/selftests/bpf/test_inet_lookup.sh | 35 ++ 6 files changed, 642 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/inet_lookup_progs.c create mode 100644 tools/testing/selftests/bpf/test_inet_lookup.c create mode 100755 tools/testing/selftests/bpf/test_inet_lookup.sh
tools/testing/selftests/bpf/.gitignore +1 -0
@@ -44,3 +44,4 @@ test_sockopt_sk test_sockopt_multi test_sockopt_inherit test_tcp_rtt +test_inet_lookup
tools/testing/selftests/bpf/Makefile +3 -2
@@ -65,7 +65,8 @@ TEST_PROGS := test_kmod.sh \ test_tcp_check_syncookie.sh \ test_tc_tunnel.sh \ test_tc_edt.sh \ - test_xdping.sh + test_xdping.sh \ + test_inet_lookup.sh TEST_PROGS_EXTENDED := with_addr.sh \ with_tunnels.sh \
@@ -75,7 +76,7 @@ TEST_PROGS_EXTENDED := with_addr.sh \ # Compile but not part of 'make run_tests' TEST_GEN_PROGS_EXTENDED = test_libbpf_open test_sock_addr test_skb_cgroup_id_user \ flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ - test_lirc_mode2_user + test_lirc_mode2_user test_inet_lookup include ../lib.mk
tools/testing/selftests/bpf/bpf_helpers.h +3 -0
@@ -231,6 +231,9 @@ static int (*bpf_send_signal)(unsigned sig) = (void *)BPF_FUNC_send_signal;static long long (*bpf_tcp_gen_syncookie)(struct bpf_sock *sk,void *ip,int ip_len,void *tcp,int tcp_len) = (void *) BPF_FUNC_tcp_gen_syncookie; +static int (*bpf_redirect_lookup)(void *ctx,void *map,void *key, +__u64 flags) = + (void *) BPF_FUNC_redirect_lookup;/* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions
tools/testing/selftests/bpf/progs/inet_lookup_progs.c new file +78 -0
78 lines not on this page · raw message
tools/testing/selftests/bpf/test_inet_lookup.c new file +522 -0
522 lines not on this page · raw message
tools/testing/selftests/bpf/test_inet_lookup.sh new file +35 -0
@@ -0,0 +1,35 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root" + echo "FAIL" + exit 1 +fi + +# Run the script in a dedicated network namespace. +if [[ -z $(ip netns identify $$) ]]; then + ../net/in_netns.sh "$0" "$@" + exit $? +fi + +readonly IP6_1="fd00::1" +readonly IP6_2="fd00::2" + +setup() +{ + ip -6 addr add ${IP6_1}/128 dev lo + ip -6 addr add ${IP6_2}/128 dev lo +} + +cleanup() +{ + ip -6 addr del ${IP6_1}/128 dev lo + ip -6 addr del ${IP6_2}/128 dev lo +} + +trap cleanup EXIT +setup + +./test_inet_lookup +exit $?
Signature · 1 line
2.20.1