Thread (6 messages) flat view 6 messages, 3 authors, 2021-10-21

Re: Retrieving the network namespace of a socket

From: Sergey Ryazanov <ryazanov.s.a@gmail.com>
Date: 2021-10-20 20:01:30
Also in: lkml

On Wed, Oct 20, 2021 at 7:34 PM Sargun Dhillon [off-list ref] wrote:
On Wed, Oct 20, 2021 at 05:03:56PM +0300, Sergey Ryazanov wrote:
quoted
On Wed, Oct 20, 2021 at 12:57 PM Sargun Dhillon [off-list ref] wrote:
quoted
I'm working on a problem where I need to determine which network namespace a
given socket is in. I can currently bruteforce this by using INET_DIAG, and
enumerating namespaces and working backwards.
Namespace is not a per-socket, but a per-process attribute. So each
socket of a process belongs to the same namespace.

Could you elaborate what kind of problem you are trying to solve?
Maybe there is a more simple solution. for it.
That's not entirely true. See the folowing code:

int main() {
        int fd1, fd2;
        fd1 = socket(AF_INET, SOCK_STREAM, 0);
        assert(fd1 >= 0);
        assert(unshare(CLONE_NEWNET) == 0);
        fd2 = socket(AF_INET, SOCK_STREAM, 0);
        assert(fd2 >= 0);
}

fd1 and fd2 have different sock_net.
Ouch, I totally missed this case. Thank you for reminding me.

--
Sergey
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help