@@ -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
@@ -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:
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(+)