LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   How to use rtnetlink GETADDR query to retrieve a **single** address information? (https://www.linuxquestions.org/questions/linux-networking-3/how-to-use-rtnetlink-getaddr-query-to-retrieve-a-%2A%2Asingle%2A%2A-address-information-4175736659/)

pseusys 05-02-2024 07:38 PM

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?..

smallpond 05-04-2024 07:57 AM

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
;

pseusys 05-06-2024 03:49 AM

Quote:

Originally Posted by smallpond (Post 6499795)
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]);
}


smallpond 05-13-2024 11:58 AM

This code works without error for me.

pseusys 05-14-2024 04:06 AM

Quote:

Originally Posted by smallpond (Post 6501414)
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!

smallpond 05-14-2024 10:05 AM

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!


pseusys 05-15-2024 03:22 AM

Quote:

Originally Posted by smallpond (Post 6501585)
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!

smallpond 05-15-2024 07:18 AM

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.


All times are GMT -5. The time now is 11:27 PM.