From: John Fastabend <john.fastabend@gmail.com>
To: davem@davemloft.net, ast@fb.com
Cc: netdev@vger.kernel.org, daniel@iogearbox.net
Subject: [RFC PATCH 5/6] net: bpf, add skb to sk lookup routines
Date: Thu, 03 Aug 2017 16:38:16 -0700 [thread overview]
Message-ID: <20170803233816.12107.48409.stgit@john-Precision-Tower-5810> (raw)
In-Reply-To: <20170803232443.12107.24752.stgit@john-Precision-Tower-5810>
Add some useful skb to sk routines to fine ports on a connected
socket.
This is for testing, we may prefer to put sk in bpf sk_buff
representation and access these fields directly. Similar to
sock ops ctx access.
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
include/uapi/linux/bpf.h | 2 ++
net/core/filter.c | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index a89e831..c626c8f 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -604,6 +604,8 @@ enum bpf_attach_type {
FN(redirect_map), \
FN(sk_redirect_map), \
FN(map_ctx_update_elem), \
+ FN(skb_get_local_port), \
+ FN(skb_get_remote_port), \
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
* function eBPF program intends to call
diff --git a/net/core/filter.c b/net/core/filter.c
index 2644f2d..3234200 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2993,6 +2993,38 @@ static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
.arg1_type = ARG_PTR_TO_CTX,
};
+BPF_CALL_1(bpf_get_remote_port, struct sk_buff *, skb)
+{
+ struct sock *sk = skb->sk;//sk_to_full_sk(skb->sk);
+
+ if (!sk)// || !sk_fullsock(sk))
+ return overflowuid;
+ return sk->sk_dport;
+}
+
+static const struct bpf_func_proto bpf_skb_get_remote_port_proto = {
+ .func = bpf_get_remote_port,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_CTX,
+};
+
+BPF_CALL_1(bpf_get_local_port, struct sk_buff *, skb)
+{
+ struct sock *sk = skb->sk;
+
+ if (!sk)// || !sk_fullsock(sk))
+ return overflowuid;
+ return sk->sk_num;
+}
+
+static const struct bpf_func_proto bpf_skb_get_local_port_proto = {
+ .func = bpf_get_local_port,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_CTX,
+};
+
BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
int, level, int, optname, char *, optval, int, optlen)
{
@@ -3135,6 +3167,10 @@ static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
return &bpf_get_socket_cookie_proto;
case BPF_FUNC_get_socket_uid:
return &bpf_get_socket_uid_proto;
+ case BPF_FUNC_skb_get_remote_port:
+ return &bpf_skb_get_remote_port_proto;
+ case BPF_FUNC_skb_get_local_port:
+ return &bpf_skb_get_local_port_proto;
case BPF_FUNC_sk_redirect_map:
return &bpf_sk_redirect_map_proto;
case BPF_FUNC_map_ctx_update_elem:
next prev parent reply other threads:[~2017-08-03 23:38 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-03 23:36 [RFC PATCH 0/6] BPF socket redirect John Fastabend
2017-08-03 23:36 ` [RFC PATCH 1/6] net: early init support for strparser John Fastabend
2017-08-03 23:37 ` [RFC PATCH 2/6] net: add sendmsg_locked and sendpage_locked to af_inet6 John Fastabend
2017-08-03 23:37 ` [RFC PATCH 3/6] net: fixes for skb_send_sock John Fastabend
2017-08-03 23:37 ` [RFC PATCH 4/6] net: sockmap with sk redirect support John Fastabend
2017-08-04 4:22 ` Tom Herbert
2017-08-04 4:49 ` John Fastabend
2017-08-04 4:49 ` John Fastabend
2017-08-03 23:38 ` John Fastabend [this message]
2017-08-03 23:38 ` [RFC PATCH 6/6] net: sockmap sample program John Fastabend
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170803233816.12107.48409.stgit@john-Precision-Tower-5810 \
--to=john.fastabend@gmail.com \
--cc=ast@fb.com \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox