Skip to contentSkip to conversation

bpf: enable bpf_{g,s}etsockopt in BPF_CGROUP_UDP{4,6}_SENDMSG

PATCH Superseded Stanislav Fomichev 2 participants 3 messages Jan 27, 2021

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: enable bpf_{g,s}etsockopt in BPF_CGROUP_UDP{4,6}_SENDMSG

Can be used to query/modify socket state for unconnected UDP sendmsg. Those hooks run as BPF_CGROUP_RUN_SA_PROG_LOCK and operate on a locked socket.

---
 net/core/filter.c                                 | 4 ++++
 tools/testing/selftests/bpf/progs/sendmsg4_prog.c | 7 +++++++
 tools/testing/selftests/bpf/progs/sendmsg6_prog.c | 7 +++++++
 3 files changed, 18 insertions(+)
net/core/filter.c +4 -0
index 9ab94e90d660..3d7f78a19565 100644
@@ -7023,6 +7023,8 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		case BPF_CGROUP_INET6_BIND:
 		case BPF_CGROUP_INET4_CONNECT:
 		case BPF_CGROUP_INET6_CONNECT:
+		case BPF_CGROUP_UDP4_SENDMSG:
+		case BPF_CGROUP_UDP6_SENDMSG:
 			return &bpf_sock_addr_setsockopt_proto;
 		default:
 			return NULL;
@@ -7033,6 +7035,8 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		case BPF_CGROUP_INET6_BIND:
 		case BPF_CGROUP_INET4_CONNECT:
 		case BPF_CGROUP_INET6_CONNECT:
+		case BPF_CGROUP_UDP4_SENDMSG:
+		case BPF_CGROUP_UDP6_SENDMSG:
 			return &bpf_sock_addr_getsockopt_proto;
 		default:
 			return NULL;
tools/testing/selftests/bpf/progs/sendmsg4_prog.c +7 -0
index 092d9da536f3..fcac40a05c3d 100644
@@ -21,9 +21,16 @@ int _version SEC("version") = 1;
 SEC("cgroup/sendmsg4")
 int sendmsg_v4_prog(struct bpf_sock_addr *ctx)
 {
+	int prio;
+
 	if (ctx->type != SOCK_DGRAM)
 		return 0;
 
+	if (bpf_getsockopt(ctx, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
+		return 0;
+	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
+		return 0;
+
 	/* Rewrite source. */
 	if (ctx->msg_src_ip4 == bpf_htonl(SRC1_IP4) ||
 	    ctx->msg_src_ip4 == bpf_htonl(SRC2_IP4)) {
tools/testing/selftests/bpf/progs/sendmsg6_prog.c +7 -0
index 255a432bc163..50b46961d08a 100644
@@ -25,9 +25,16 @@ int _version SEC("version") = 1;
 SEC("cgroup/sendmsg6")
 int sendmsg_v6_prog(struct bpf_sock_addr *ctx)
 {
+	int prio;
+
 	if (ctx->type != SOCK_DGRAM)
 		return 0;
 
+	if (bpf_getsockopt(ctx, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
+		return 0;
+	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
+		return 0;
+
 	/* Rewrite source. */
 	if (ctx->msg_src_ip6[3] == bpf_htonl(1) ||
 	    ctx->msg_src_ip6[3] == bpf_htonl(0)) {
Signature · 1 line
2.30.0.280.ga3ce27912f-goog
#2 lore raw
In reply to Daniel Borkmann
Quoted 29 lines · Daniel Borkmann
Quoted 27 lines · Stanislav Fomichev

Can be used to query/modify socket state for unconnected UDP sendmsg. Those hooks run as BPF_CGROUP_RUN_SA_PROG_LOCK and operate on a locked socket.

---
  net/core/filter.c                                 | 4 ++++
  tools/testing/selftests/bpf/progs/sendmsg4_prog.c | 7 +++++++
  tools/testing/selftests/bpf/progs/sendmsg6_prog.c | 7 +++++++
  3 files changed, 18 insertions(+)
net/core/filter.c +2 -0
index 9ab94e90d660..3d7f78a19565 100644
@@ -7023,6 +7023,8 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
              case BPF_CGROUP_INET6_BIND:
              case BPF_CGROUP_INET4_CONNECT:
              case BPF_CGROUP_INET6_CONNECT:
+             case BPF_CGROUP_UDP4_SENDMSG:
+             case BPF_CGROUP_UDP6_SENDMSG:
                      return &bpf_sock_addr_setsockopt_proto;
              default:
                      return NULL;
@@ -7033,6 +7035,8 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
              case BPF_CGROUP_INET6_BIND:
              case BPF_CGROUP_INET4_CONNECT:
Quoted 8 lines

net/core/filter.c sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · line 7040

Quoted 4 lines
net/core/filter.c +2 -0
              case BPF_CGROUP_INET6_CONNECT:
+             case BPF_CGROUP_UDP4_SENDMSG:
+             case BPF_CGROUP_UDP6_SENDMSG:
                      return &bpf_sock_addr_getsockopt_proto;

Patch looks good, could we at this point also add all the others that run under BPF_CGROUP_RUN_SA_PROG_LOCK while at it, that is v4/v6 flavors of recvmsg as well as peername/sockname?

Sounds good, will resend with more hooks added.

maintainer
#1 lore raw
In reply to Stanislav Fomichev
Quoted 25 lines · Stanislav Fomichev

Can be used to query/modify socket state for unconnected UDP sendmsg. Those hooks run as BPF_CGROUP_RUN_SA_PROG_LOCK and operate on a locked socket.

---
  net/core/filter.c                                 | 4 ++++
  tools/testing/selftests/bpf/progs/sendmsg4_prog.c | 7 +++++++
  tools/testing/selftests/bpf/progs/sendmsg6_prog.c | 7 +++++++
  3 files changed, 18 insertions(+)
net/core/filter.c +2 -0
index 9ab94e90d660..3d7f78a19565 100644
@@ -7023,6 +7023,8 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  		case BPF_CGROUP_INET6_BIND:
  		case BPF_CGROUP_INET4_CONNECT:
  		case BPF_CGROUP_INET6_CONNECT:
+		case BPF_CGROUP_UDP4_SENDMSG:
+		case BPF_CGROUP_UDP6_SENDMSG:
  			return &bpf_sock_addr_setsockopt_proto;
  		default:
  			return NULL;

net/core/filter.c sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · line 7040

Quoted 6 lines
net/core/filter.c +2 -0
@@ -7033,6 +7035,8 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  		case BPF_CGROUP_INET6_BIND:
  		case BPF_CGROUP_INET4_CONNECT:
  		case BPF_CGROUP_INET6_CONNECT:
+		case BPF_CGROUP_UDP4_SENDMSG:
+		case BPF_CGROUP_UDP6_SENDMSG:
  			return &bpf_sock_addr_getsockopt_proto;

Patch looks good, could we at this point also add all the others that run under BPF_CGROUP_RUN_SA_PROG_LOCK while at it, that is v4/v6 flavors of recvmsg as well as peername/sockname?

Thanks,
Daniel