How to use rtnetlink GETADDR query to retrieve a **single** address information?
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to use rtnetlink GETADDR query to retrieve a **single** address information?
Good morning!
I am interested in one specific issue about rtnetlink library. I found a similar question on stackoverflow (unanswered), and now I am facing a similar problem.
Let's imagine I'm connected to internet via device with index 3 and name "wlo1" right now. I would like to use the following code snippet in order to receive the address information (such as prefixlength). In order to do that I'm trying to use the following code (taken from the stackoverflow question and modified):
Instead of the requested address information, I receive an error message (errno number is stored in 16th byte and is -95, which, I assume, means "operation not permitted").
Do you have any idea what's going on? If I add NLM_F_DUMP flag, I can get information about all addresses available, but I'm only interested in one. Is there any configuration I am missing? Do I do something wrong?..
You should be checking for errors from socket and bind calls. But I think the main error is missing
Code:
addr.nl_family = AF_NETLINK
;
Thank you for your advice! However, if I'm doing it right, it still doesn't work.
I changed the code, now it checks for socket creation and binding messages. Still, however, I receive errno -95 querying for a single address only.
That's the code I'm using now:
Thank you for your response!
However, I would like to make sure this really works for you (because I've tried on two different machines and it didn't work for any).
Have you checked the error code existence? Have you checked if the response message data is correct?
If that wouldn't bother you much, I've updated my code to check these parameters automatically (instead of just outputting raw byte values):
However, if I change the #4 line of generate_address_request function to pack->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT; (i.e. add NLM_F_ROOT flag, that would dump all the routes instead of returning the one requested by pack->msg.ifa_index only), I can receive the expected result:
I should have said "runs for me" rather than works, as I didn't check the response. When I run yours, I also get error 95.
From the RFC it looks like you need NLM_F_DUMP to match entries from the complete list, however I couldn't get that to work to select an interface. It does work to select ifa_family.
From the RFC it looks like you need NLM_F_DUMP to match entries from the complete list, however I couldn't get that to work to select an interface. It does work to select ifa_family.
Okay, thank you! That's already better than nothing.
Yes, indeed, when NLM_F_DUMP or NLM_F_ROOT flags are used, all the interface list is returned instead of the one we are searching for - and it's possible to iterate through them in order to find the one.
Honestly, I would suspect it to be a bug because it is not stated anywhere throughout the docs that the interface querying by index functionality is not allowed or is not intended, on the contrary, that use case looks normal for rtnetlink. Also, ALL the programs I have seen that use rtnetlink for address querying iterate through all network interfaces available.
That is interesting that this issue was not discussed during all these years (or at least I haven't found any discussions of it).
Do you think I could contribute to the kernel sources or ask the developers directly about it? I'm a bit confused how to do that, however I guess I should be allowed to since Linux kernel is open source.
Thank you for your time and investigation of my small issue!
It's relatively easy to post bug fixes to the appropriate kernel mailing list, discuss them, and have them incorporated by that subsystem's maintainer. I've done several. It is much harder to do the same with a new feature unless you can show a need for it. It is impossible to make a change that breaks existing functionality. It won't be accepted.
In this case, the reason your proposed change wouldn't be approved is that you want to do code in the kernel that can easily be done in user space.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.