LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2024, 07:38 PM   #1
pseusys
LQ Newbie
 
Registered: May 2024
Posts: 4

Rep: Reputation: 0
Unhappy 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):

Code:
#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if_addr.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
 
struct packet {
    struct nlmsghdr hdr;
    struct ifaddrmsg msg;
};
 
int main() {
    struct sockaddr_nl addr;
    memset(&addr, 0, sizeof(struct sockaddr_nl));
 
    int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
    bind(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_nl));
 
    socklen_t len =  sizeof(struct sockaddr_nl);
    getsockname(sock, (struct sockaddr *) &addr, &len);
 
    struct packet pack;
    memset(&pack, 0, sizeof(struct packet));
    pack.hdr.nlmsg_len = sizeof(struct packet);
    pack.hdr.nlmsg_type = RTM_GETADDR;
    pack.hdr.nlmsg_flags = NLM_F_REQUEST;
    pack.msg.ifa_index = 3;  // This is ignored, why?
    write(sock, &pack, sizeof(struct packet));
 
    char buf[8000];
    memset(buf, 0, 8000);
 
    int x = read(sock, buf, (socklen_t) 8000);
    for (int i = 0; i < x; i++) printf("%i \t: %i\n",i, buf[i]);
}
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?..
 
Old 05-04-2024, 07:57 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,153

Rep: Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265
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
;
 
Old 05-06-2024, 03:49 AM   #3
pseusys
LQ Newbie
 
Registered: May 2024
Posts: 4

Original Poster
Rep: Reputation: 0
Arrow

Quote:
Originally Posted by smallpond View Post
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:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if_addr.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

struct packet {
    struct nlmsghdr hdr;
    struct ifaddrmsg msg;
};

int main() {
    struct sockaddr_nl addr;
    memset(&addr, 0, sizeof(struct sockaddr_nl));
    addr.nl_family = AF_NETLINK;

    int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
    if (sock == -1) {
        printf("Couldn't create netlink socket, errno: %d!\n", errno);
        exit(1);
    }

    int err = bind(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_nl));
    if (err != 0) {
        printf("Couldn't bind netlink socket, errno: %d!\n", errno);
        exit(1);
    }

    socklen_t len =  sizeof(struct sockaddr_nl);
    getsockname(sock, (struct sockaddr *) &addr, &len);

    struct packet pack;
    memset(&pack, 0, sizeof(struct packet));
    pack.hdr.nlmsg_len = sizeof(struct packet);
    pack.hdr.nlmsg_type = RTM_GETADDR;
    pack.hdr.nlmsg_flags = NLM_F_REQUEST;
    pack.msg.ifa_index = 1;  // This gets ignored, why?
    write(sock, &pack, sizeof(struct packet));

    char buf[8000];
    memset(buf, 0, 8000);

    int x = read(sock, buf, (socklen_t) 8000);
    for (int i = 0; i < x; i++) printf("%i \t: %i\n",i, buf[i]);
}

Last edited by pseusys; 05-06-2024 at 03:53 AM. Reason: markdown changed again
 
Old 05-13-2024, 11:58 AM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,153

Rep: Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265
This code works without error for me.
 
Old 05-14-2024, 04:06 AM   #5
pseusys
LQ Newbie
 
Registered: May 2024
Posts: 4

Original Poster
Rep: Reputation: 0
Arrow

Quote:
Originally Posted by smallpond View Post
This code works without error for me.
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):

Code:
#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if_addr.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define DATA_LENGTH 32768

struct packet {
    struct nlmsghdr hdr;
    struct ifaddrmsg msg;
};

void generate_address_request(struct packet* pack, int interface_id) {
    memset(pack, 0, sizeof(struct packet));
    pack->hdr.nlmsg_len = sizeof(struct packet);
    pack->hdr.nlmsg_type = RTM_GETADDR;
    pack->hdr.nlmsg_flags = NLM_F_REQUEST;
    pack->msg.ifa_index = interface_id;  // This gets ignored, why?
}

void parse_all_packets(char* messages_bytes, int total_length) {
    int index = 0, cursor = 0;
    while (cursor < total_length) {
        struct nlmsghdr* header = (struct nlmsghdr*) (messages_bytes + cursor);
        if (header->nlmsg_type == NLMSG_ERROR) {
            int* errnumber = (int*) (messages_bytes + cursor + sizeof(struct nlmsghdr));
            printf("Couldn't parse message #%d, errno: %d!\n", index, *errnumber);
        } else if (header->nlmsg_type == NLMSG_DONE) {
            printf("Message #%d, was the last one!\n", index);
            continue;
        } else {
            struct ifaddrmsg* message = (struct ifaddrmsg*) (messages_bytes + cursor + sizeof(struct nlmsghdr));
            printf("Message #%d contains interface #%d data, e.g. prefixlen: %d!\n", index, message->ifa_index, message->ifa_prefixlen);
        }
        cursor += header->nlmsg_len;
        index++;
    }
}

int main() {
    struct sockaddr_nl addr;
    memset(&addr, 0, sizeof(struct sockaddr_nl));
    addr.nl_family = AF_NETLINK;

    int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
    if (sock == -1) {
        printf("Couldn't create netlink socket, errno: %d!\n", errno);
        exit(1);
    }

    int err = bind(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_nl));
    if (err != 0) {
        printf("Couldn't bind netlink socket, errno: %d!\n", errno);
        exit(1);
    }

    socklen_t len =  sizeof(struct sockaddr_nl);
    getsockname(sock, (struct sockaddr *) &addr, &len);

    struct packet pack;
    generate_address_request(&pack, 1);
    write(sock, &pack, sizeof(struct packet));

    char buf[DATA_LENGTH];
    memset(buf, 0, DATA_LENGTH);
    int response_len = read(sock, buf, (socklen_t) DATA_LENGTH);
    parse_all_packets(buf, response_len);
}
For me, when I compile and run this, the following happens:

Code:
> gcc main.c && ./a.out
< Couldn't parse message #0, errno: -95!
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:

Code:
> gcc main.c && ./a.out
< Message #0 contains interface #1 data, e.g. prefixlen: 8!
< Message #1 contains interface #2 data, e.g. prefixlen: 20!
< ...
Does it works differently for you? I'm curious if there's something wrong with my computers now.
Thank you again for your participation!

Last edited by pseusys; 05-14-2024 at 07:36 AM. Reason: Code blocks removed
 
Old 05-14-2024, 10:05 AM   #6
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,153

Rep: Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265
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.

https://www.rfc-editor.org/rfc/rfc3549

I get for ifa_family = AF_INET
Code:
Message #0 contains interface #1 data, e.g. prefixlen: 8!
Message #1 contains interface #3 data, e.g. prefixlen: 24!
Message #2 contains interface #4 data, e.g. prefixlen: 24!
for ifa_family = AF_INET6
Code:
Message #0 contains interface #1 data, e.g. prefixlen: 128!
Message #1 contains interface #3 data, e.g. prefixlen: 64!
 
1 members found this post helpful.
Old 05-15-2024, 03:22 AM   #7
pseusys
LQ Newbie
 
Registered: May 2024
Posts: 4

Original Poster
Rep: Reputation: 0
Arrow

Quote:
Originally Posted by smallpond View Post
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!
 
Old 05-15-2024, 07:18 AM   #8
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,153

Rep: Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
RTNETLINK answers: No such process - (SSH CLI - Ubiquiti radio changing IP address to access routed subnet in browser.) ziggy@skylinkafrica.co.z Linux - Newbie 1 05-07-2021 06:53 PM
RTNETLINK answers: Cannot assign requested address mattsoftnet Linux - Networking 2 08-17-2011 04:09 AM
bringing up interface eth1 rtnetlink answers file exists, error adding address homyangcha Linux - Newbie 7 10-20-2010 11:40 PM
[SOLVED] ./network start shows error RTNETLINK answers: File exists Error adding address [ip] FlatBallFlyer Linux - Networking 4 09-27-2010 07:15 PM
getting hardwrae address using rtnetlink and printing that address suresh_rupineni Linux - Networking 0 07-26-2006 04:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 09:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration