aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLawrence Brakmo <brakmo@fb.com>2017-10-20 14:05:40 -0400
committerDavid S. Miller <davem@davemloft.net>2017-10-21 22:12:05 -0400
commitcd86d1fd21025fdd6daf23d1288da405e7ad0ec6 (patch)
treeb3c9cf105f588261cf2d7b03fd3a52a431cb76f9
parente6546ef6d86d0fc38e0e84ccae80e641f3fc0087 (diff)
bpf: Adding helper function bpf_getsockops
Adding support for helper function bpf_getsockops to socket_ops BPF programs. This patch only supports TCP_CONGESTION. Signed-off-by: Vlad Vysotsky <vlad@cs.ucla.edu> Acked-by: Lawrence Brakmo <brakmo@fb.com> Acked-by: Alexei Starovoitov <ast@fb.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--include/uapi/linux/bpf.h19
-rw-r--r--net/core/filter.c46
-rw-r--r--tools/testing/selftests/bpf/bpf_helpers.h3
3 files changed, 63 insertions, 5 deletions
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 1aca744c220f..f650346aaa1a 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -613,12 +613,22 @@ union bpf_attr {
613 * int bpf_setsockopt(bpf_socket, level, optname, optval, optlen) 613 * int bpf_setsockopt(bpf_socket, level, optname, optval, optlen)
614 * Calls setsockopt. Not all opts are available, only those with 614 * Calls setsockopt. Not all opts are available, only those with
615 * integer optvals plus TCP_CONGESTION. 615 * integer optvals plus TCP_CONGESTION.
616 * Supported levels: SOL_SOCKET and IPROTO_TCP 616 * Supported levels: SOL_SOCKET and IPPROTO_TCP
617 * @bpf_socket: pointer to bpf_socket 617 * @bpf_socket: pointer to bpf_socket
618 * @level: SOL_SOCKET or IPROTO_TCP 618 * @level: SOL_SOCKET or IPPROTO_TCP
619 * @optname: option name 619 * @optname: option name
620 * @optval: pointer to option value 620 * @optval: pointer to option value
621 * @optlen: length of optval in byes 621 * @optlen: length of optval in bytes
622 * Return: 0 or negative error
623 *
624 * int bpf_getsockopt(bpf_socket, level, optname, optval, optlen)
625 * Calls getsockopt. Not all opts are available.
626 * Supported levels: IPPROTO_TCP
627 * @bpf_socket: pointer to bpf_socket
628 * @level: IPPROTO_TCP
629 * @optname: option name
630 * @optval: pointer to option value
631 * @optlen: length of optval in bytes
622 * Return: 0 or negative error 632 * Return: 0 or negative error
623 * 633 *
624 * int bpf_skb_adjust_room(skb, len_diff, mode, flags) 634 * int bpf_skb_adjust_room(skb, len_diff, mode, flags)
@@ -721,7 +731,8 @@ union bpf_attr {
721 FN(sock_map_update), \ 731 FN(sock_map_update), \
722 FN(xdp_adjust_meta), \ 732 FN(xdp_adjust_meta), \
723 FN(perf_event_read_value), \ 733 FN(perf_event_read_value), \
724 FN(perf_prog_read_value), 734 FN(perf_prog_read_value), \
735 FN(getsockopt),
725 736
726/* integer value in 'imm' field of BPF_CALL instruction selects which helper 737/* integer value in 'imm' field of BPF_CALL instruction selects which helper
727 * function eBPF program intends to call 738 * function eBPF program intends to call
diff --git a/net/core/filter.c b/net/core/filter.c
index 09e011f20291..ccf62f44140a 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -3273,7 +3273,7 @@ BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3273 3273
3274static const struct bpf_func_proto bpf_setsockopt_proto = { 3274static const struct bpf_func_proto bpf_setsockopt_proto = {
3275 .func = bpf_setsockopt, 3275 .func = bpf_setsockopt,
3276 .gpl_only = true, 3276 .gpl_only = false,
3277 .ret_type = RET_INTEGER, 3277 .ret_type = RET_INTEGER,
3278 .arg1_type = ARG_PTR_TO_CTX, 3278 .arg1_type = ARG_PTR_TO_CTX,
3279 .arg2_type = ARG_ANYTHING, 3279 .arg2_type = ARG_ANYTHING,
@@ -3282,6 +3282,48 @@ static const struct bpf_func_proto bpf_setsockopt_proto = {
3282 .arg5_type = ARG_CONST_SIZE, 3282 .arg5_type = ARG_CONST_SIZE,
3283}; 3283};
3284 3284
3285BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3286 int, level, int, optname, char *, optval, int, optlen)
3287{
3288 struct sock *sk = bpf_sock->sk;
3289 int ret = 0;
3290
3291 if (!sk_fullsock(sk))
3292 goto err_clear;
3293
3294#ifdef CONFIG_INET
3295 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
3296 if (optname == TCP_CONGESTION) {
3297 struct inet_connection_sock *icsk = inet_csk(sk);
3298
3299 if (!icsk->icsk_ca_ops || optlen <= 1)
3300 goto err_clear;
3301 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
3302 optval[optlen - 1] = 0;
3303 } else {
3304 goto err_clear;
3305 }
3306 } else {
3307 goto err_clear;
3308 }
3309 return ret;
3310#endif
3311err_clear:
3312 memset(optval, 0, optlen);
3313 return -EINVAL;
3314}
3315
3316static const struct bpf_func_proto bpf_getsockopt_proto = {
3317 .func = bpf_getsockopt,
3318 .gpl_only = false,
3319 .ret_type = RET_INTEGER,
3320 .arg1_type = ARG_PTR_TO_CTX,
3321 .arg2_type = ARG_ANYTHING,
3322 .arg3_type = ARG_ANYTHING,
3323 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
3324 .arg5_type = ARG_CONST_SIZE,
3325};
3326
3285static const struct bpf_func_proto * 3327static const struct bpf_func_proto *
3286bpf_base_func_proto(enum bpf_func_id func_id) 3328bpf_base_func_proto(enum bpf_func_id func_id)
3287{ 3329{
@@ -3460,6 +3502,8 @@ static const struct bpf_func_proto *
3460 switch (func_id) { 3502 switch (func_id) {
3461 case BPF_FUNC_setsockopt: 3503 case BPF_FUNC_setsockopt:
3462 return &bpf_setsockopt_proto; 3504 return &bpf_setsockopt_proto;
3505 case BPF_FUNC_getsockopt:
3506 return &bpf_getsockopt_proto;
3463 case BPF_FUNC_sock_map_update: 3507 case BPF_FUNC_sock_map_update:
3464 return &bpf_sock_map_update_proto; 3508 return &bpf_sock_map_update_proto;
3465 default: 3509 default:
diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h
index e25dbf6038cf..609514f74482 100644
--- a/tools/testing/selftests/bpf/bpf_helpers.h
+++ b/tools/testing/selftests/bpf/bpf_helpers.h
@@ -67,6 +67,9 @@ static int (*bpf_xdp_adjust_meta)(void *ctx, int offset) =
67static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval, 67static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval,
68 int optlen) = 68 int optlen) =
69 (void *) BPF_FUNC_setsockopt; 69 (void *) BPF_FUNC_setsockopt;
70static int (*bpf_getsockopt)(void *ctx, int level, int optname, void *optval,
71 int optlen) =
72 (void *) BPF_FUNC_getsockopt;
70static int (*bpf_sk_redirect_map)(void *map, int key, int flags) = 73static int (*bpf_sk_redirect_map)(void *map, int key, int flags) =
71 (void *) BPF_FUNC_sk_redirect_map; 74 (void *) BPF_FUNC_sk_redirect_map;
72static int (*bpf_sock_map_update)(void *map, void *key, void *value, 75static int (*bpf_sock_map_update)(void *map, void *key, void *value,