How to do a RTM_GETNSID?

87 views
Skip to first unread message

TheDiveO

unread,
May 5, 2018, 4:30:26 PM5/5/18
to pyroute2-dev

I'm trying to do a IPRoute().nlm_request(req, RTM_GETNSID). I'm lost as to how to correctly build the request message and send it, even after reading several source code files (and documentation) of pyroute2 for some hours. What I think I understood is this:


req=nsidmsg()
req['family']=socket.AF_UNSPEC
req['attrs'] = [('NETNSA_FD', netnsf.fileno())]
res=IPRoute().nlm_request(req, RTM_GETNSID)

...but is this correct at all? The problem is that I always get a dump of all NETNSA_NSID in form of RTM_NEWNSID events. I would expect a reply of exactly one NETNSA_NSID, the one corresponding with the NETNSA_FD, where the fd is the one from /proc/12345/ns/net.


What am I doing wrong here?

TheDiveO

unread,
May 6, 2018, 5:54:57 PM5/6/18
to pyroute2-dev
It seems as if there's a problem with the request encoding:

I'm trying to do a `IPRoute().nlm_request(req, RTM_GETNSID)`. I'm using pyroute2 0.5.0.

from pyroute2 import IPRoute
import socket
from pyroute2.netlink import NLM_F_REQUEST
import pyroute2.netlink.rtnl as rtnl
import pyroute2.netlink.rtnl.nsidmsg as nsidmsg

netstack = IPRoute()
with open('/proc/{}/ns/net'.format(12345), "r") as netnsf: # PID 12345
    req = nsidmsg.nsidmsg()
    req['rtgen_family'] = 0

    req['attrs'] = [('NETNSA_FD', netnsf.fileno())]
    req.encode()
    ret = netstack.nlm_request(req, rtnl.RTM_GETNSID, NLM_F_REQUEST)


The request fails with error 22, invalid argument. Strace shows this, indicating that only 25 octets are indicated in the header:

sendto(5, {{len=25, type=0x5a /* NLMSG_??? */, flags=NLM_F_REQUEST, seq=255, pid=31204}, "\x00\x00\x00\x00\x08\x00\x03\x00\x06"}, 28, 0, {sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, 12) = 28

As the FD should be encoded as uint32, I would expect at least three more octets for the FD.

Comparing this to a RTM_GETNSID request from `lsns` I see that the request should have a length of 28.

Peter Saveliev

unread,
May 8, 2018, 1:10:41 PM5/8/18
to TheDiveO, pyroute2-dev
> What am I doing wrong here?

Nothing wrong, it should work. Let me play a bit to investigate.

--
You received this message because you are subscribed to the Google Groups "pyroute2-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyroute2-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Saveliev

unread,
May 8, 2018, 1:12:01 PM5/8/18
to TheDiveO, pyroute2-dev
but pls keep in mind that the functionality isn't released yet :) I just hadn't time to complete it. Will do asap.

--

TheDiveO

unread,
May 8, 2018, 1:49:57 PM5/8/18
to pyroute2-dev
Didn't realize that I did directly hit, without taking aim, some bleeding edge spot. :) At least I have a somewhat terrible hack to get it working, that's what hacks are supposedly for. Thank you for looking into it!
Reply all
Reply to author
Forward
0 new messages