Comments on: Getting Linux routing table using netlink https://olegkutkov.me/2019/03/24/getting-linux-routing-table-using-netlink/ Programming, electronics and diy projects Sat, 25 Mar 2023 11:41:32 +0000 hourly 1 https://wordpress.org/?v=6.5.3 By: Baa https://olegkutkov.me/2019/03/24/getting-linux-routing-table-using-netlink/#comment-1455 Sat, 25 Mar 2023 11:41:32 +0000 http://olegkutkov.me/?p=821#comment-1455 Hi,
I have change the code to send request IPv4/IPv6 in main, like below

int main()
{
int nl_sock = open_netlink();

if (do_route_dump_requst(nl_sock, AF_INET) < 0) {
perror("Failed to perfom request");
close(nl_sock);
return -1;
}

get_route_dump_response(nl_sock);

if (do_route_dump_requst(nl_sock, AF_INET6) < 0) {
perror("Failed to perfom request");
close(nl_sock);
return -1;
}

get_route_dump_response(nl_sock);

close (nl_sock);

return 0;

}

and change int do_route_dump_requst(int sock) to
int do_route_dump_requst(int sock, int family)
{

int do_route_dump_requst(int sock, int family)

}

but the IPv6 always show “Wrong message length: No error information”
if I change AF_INET to AF_UNSPEC , it’s will working fine.

Can you give me some comment?
Thanks

]]>
By: Ort https://olegkutkov.me/2019/03/24/getting-linux-routing-table-using-netlink/#comment-1120 Wed, 26 Jan 2022 08:06:27 +0000 http://olegkutkov.me/?p=821#comment-1120 Hi Oleg,
Thanks for the useful information!

Is there any way to know if a specific route exists in the routing table? For example to use RTM_GETROUTE but also to specify the ip address of the route we’re searching? I don’t want all the entries in the routing table, only a specific entry.
Thanks!

]]>
By: Oleg Kutkov https://olegkutkov.me/2019/03/24/getting-linux-routing-table-using-netlink/#comment-959 Mon, 09 Nov 2020 21:31:24 +0000 http://olegkutkov.me/?p=821#comment-959 In reply to Sandy.

Hello. It’s no problem.
This code is just adopted for the IPv4, you can easily switch to IPv6.
Just replace everywhere AF_INET to AF_INET6 + remove the address length check at line 119:

if ((r->rtm_dst_len != 24) && (r->rtm_dst_len != 16)) {
return;
}

This code just skips your IPv6 addresses due to IPv6 rtm_dst_len == 128.
Without this check IPv6 table can be printed without any problems:

$ ./print_route
Main routing table IPv6
::1/128 dev lo
fe80::/64 dev enp4s0
::1/128 dev lo
fe80::d448:77d5:68fa:bf6b/128 dev enp4s0
ff00::/8 dev enp4s0

You can completely remove this check or better adopt it to the 128 bit address lengths.

]]>
By: Sandy https://olegkutkov.me/2019/03/24/getting-linux-routing-table-using-netlink/#comment-958 Mon, 09 Nov 2020 20:57:05 +0000 http://olegkutkov.me/?p=821#comment-958 This code is not working for IPV6 if I change family to AF_INET6 as specified above.
nl_request.rtm.rtm_family = AF_INET6

Please help

]]>
By: Digvijay https://olegkutkov.me/2019/03/24/getting-linux-routing-table-using-netlink/#comment-939 Wed, 23 Sep 2020 12:24:29 +0000 http://olegkutkov.me/?p=821#comment-939 Very useful. Nicely coded.
I think there should be a return statement in the following block otherwise it can crash:

if (h->nlmsg_type == NLMSG_ERROR) {
perror("netlink reported error");
free(buf);
}

]]>
By: Oleg Kutkov https://olegkutkov.me/2019/03/24/getting-linux-routing-table-using-netlink/#comment-930 Tue, 18 Aug 2020 20:05:48 +0000 http://olegkutkov.me/?p=821#comment-930 In reply to Savva.

Hello!
Can you repeat your tests with command ip r ?
This command is also based on netlink. Command “route -n” uses old-style ioctl requests.

]]>
By: Savva https://olegkutkov.me/2019/03/24/getting-linux-routing-table-using-netlink/#comment-929 Tue, 18 Aug 2020 02:38:49 +0000 http://olegkutkov.me/?p=821#comment-929 Hey Oleg, thanks for the post! Very useful. I’m using this code on an ad-hoc network (with olsrd). I am seeing at least a 20 second delay between an ip address appearing among the results of the “route -n” command and the same ip address appearing among the outputs of the get_route_dump_response() function. Any insights as to why this might be happening?

]]>
By: Modifying Linux network routes using netlink - Oleg Kutkov personal blog https://olegkutkov.me/2019/03/24/getting-linux-routing-table-using-netlink/#comment-76 Wed, 28 Aug 2019 23:14:21 +0000 http://olegkutkov.me/?p=821#comment-76 […] time we talked about getting Linux routing table with simple Netlink code. Now it’s time to do more […]

]]>
By: Oleg Kutkov https://olegkutkov.me/2019/03/24/getting-linux-routing-table-using-netlink/#comment-74 Mon, 03 Jun 2019 07:59:22 +0000 http://olegkutkov.me/?p=821#comment-74 In reply to Mike B.

Thanks!
New article is upcoming. Stay tuned.

]]>
By: Mike B https://olegkutkov.me/2019/03/24/getting-linux-routing-table-using-netlink/#comment-73 Sun, 02 Jun 2019 20:14:45 +0000 http://olegkutkov.me/?p=821#comment-73 very useful, waiting for next articles about netlink

]]>