zzy
zzy

Reputation: 791

How to find owner socket of sk_buff in Linux kernel?

I'm trying to find the owner socket of an sk_buff instance, say, skb. My ultimate goal is to find a specific TCP option and somehow let the user space application to know. I plan to set a socket option when I find the TCP option and let the user space app to call getsockopt(). Therefore I need to know the ownership between sk_buff and sock.

I find there is a field in sk_buff:

struct sock *sk;

However, when I try to retrieve this field at tcp_parse_options in tcp_input.c, I always get skb->sk == NULL.

So I'm wondering how can I find the owner here?

Also, I find 3 places that seems to set the owner socket:

http://lxr.free-electrons.com/source/net/ipv4/tcp_input.c?v=3.11#L4181

http://lxr.free-electrons.com/source/net/ipv4/tcp_input.c?v=3.11#L4196

http://lxr.free-electrons.com/source/net/ipv4/tcp_input.c?v=3.11#L4456

I also add a new flag in sk_buff for indicating and set it at tcp_parse_options. Then I check this flag at these three places. But none of them shows the flag is set so that I cannot determine whether to set the socket option.

Any idea or suggestion for this problem?

Thanks in advance!

Upvotes: 3

Views: 2167

Answers (1)

Jay Medina
Jay Medina

Reputation: 574

From the sk_buff (skb) you can get the sock (sk) with something like this:

const struct tcphdr *th = tcp_hdr(skb);
struct sock *sk = __inet_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
if (sk)
    struct socket* = sk->sk_socket;

That worked for me. Don't forget to add the right headers.

Upvotes: 3

Related Questions