Message ID | cba44439b801e5ddc1170e5be787f4dc93a2d7f9.1610406333.git.daniel@iogearbox.net (mailing list archive) |
---|---|
State | Accepted |
Delegated to: | BPF |
Headers | show |
Series | [bpf-next,v2,1/2] bpf: allow to retrieve sol_socket opts from sock_addr progs | expand |
Context | Check | Description |
---|---|---|
netdev/cover_letter | success | Link |
netdev/fixes_present | success | Link |
netdev/patch_count | success | Link |
netdev/tree_selection | success | Clearly marked for bpf-next |
netdev/subject_prefix | success | Link |
netdev/cc_maintainers | warning | 7 maintainers not CCed: kafai@fb.com kpsingh@kernel.org andrii@kernel.org songliubraving@fb.com kuba@kernel.org john.fastabend@gmail.com davem@davemloft.net |
netdev/source_inline | success | Was 0 now: 0 |
netdev/verify_signedoff | success | Link |
netdev/module_param | success | Was 0 now: 0 |
netdev/build_32bit | success | Errors and warnings before: 45 this patch: 45 |
netdev/kdoc | success | Errors and warnings before: 0 this patch: 0 |
netdev/verify_fixes | success | Link |
netdev/checkpatch | warning | WARNING: line length of 83 exceeds 80 columns |
netdev/build_allmodconfig_warn | success | Errors and warnings before: 45 this patch: 45 |
netdev/header_inline | success | Link |
netdev/stable | success | Stable not CCed |
On Mon, Jan 11, 2021 at 3:09 PM Daniel Borkmann <daniel@iogearbox.net> wrote: > > The _bpf_setsockopt() is able to set some of the SOL_SOCKET level options, > however, _bpf_getsockopt() has little support to actually retrieve them. > This small patch adds few misc options such as SO_MARK, SO_PRIORITY and > SO_BINDTOIFINDEX. For the latter getter and setter are added. The mark and > priority in particular allow to retrieve the options from BPF cgroup hooks > to then implement custom behavior / settings on the syscall hooks compared > to other sockets that stick to the defaults, for example. > > Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> > Acked-by: Yonghong Song <yhs@fb.com> Applied.
diff --git a/net/core/filter.c b/net/core/filter.c index 255aeee72402..9ab94e90d660 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4770,6 +4770,10 @@ static int _bpf_setsockopt(struct sock *sk, int level, int optname, ifindex = dev->ifindex; dev_put(dev); } + fallthrough; + case SO_BINDTOIFINDEX: + if (optname == SO_BINDTOIFINDEX) + ifindex = val; ret = sock_bindtoindex(sk, ifindex, false); break; case SO_KEEPALIVE: @@ -4932,8 +4936,25 @@ static int _bpf_getsockopt(struct sock *sk, int level, int optname, sock_owned_by_me(sk); + if (level == SOL_SOCKET) { + if (optlen != sizeof(int)) + goto err_clear; + + switch (optname) { + case SO_MARK: + *((int *)optval) = sk->sk_mark; + break; + case SO_PRIORITY: + *((int *)optval) = sk->sk_priority; + break; + case SO_BINDTOIFINDEX: + *((int *)optval) = sk->sk_bound_dev_if; + break; + default: + goto err_clear; + } #ifdef CONFIG_INET - if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) { + } else if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) { struct inet_connection_sock *icsk; struct tcp_sock *tp; @@ -4986,12 +5007,12 @@ static int _bpf_getsockopt(struct sock *sk, int level, int optname, default: goto err_clear; } +#endif #endif } else { goto err_clear; } return 0; -#endif err_clear: memset(optval, 0, optlen); return -EINVAL;