summaryrefslogtreecommitdiff
path: root/examples/rtnl
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rtnl')
-rw-r--r--examples/rtnl/Makefile.am12
-rw-r--r--examples/rtnl/rtnl-addr-dump.c133
-rw-r--r--examples/rtnl/rtnl-link-dump.c24
-rw-r--r--examples/rtnl/rtnl-link-dump2.c4
-rw-r--r--examples/rtnl/rtnl-link-dump3.c2
-rw-r--r--examples/rtnl/rtnl-link-event.c2
-rw-r--r--examples/rtnl/rtnl-link-set.c6
-rw-r--r--examples/rtnl/rtnl-route-add.c83
-rw-r--r--examples/rtnl/rtnl-route-dump.c21
-rw-r--r--examples/rtnl/rtnl-route-event.c341
10 files changed, 586 insertions, 42 deletions
diff --git a/examples/rtnl/Makefile.am b/examples/rtnl/Makefile.am
index 3cc8995..24769b6 100644
--- a/examples/rtnl/Makefile.am
+++ b/examples/rtnl/Makefile.am
@@ -1,10 +1,15 @@
include $(top_srcdir)/Make_global.am
-check_PROGRAMS = rtnl-link-dump rtnl-link-dump2 rtnl-link-dump3 \
+check_PROGRAMS = rtnl-addr-dump \
+ rtnl-link-dump rtnl-link-dump2 rtnl-link-dump3 \
rtnl-link-event \
rtnl-link-set \
rtnl-route-add \
- rtnl-route-dump
+ rtnl-route-dump \
+ rtnl-route-event
+
+rtnl_addr_dump_SOURCES = rtnl-addr-dump.c
+rtnl_addr_dump_LDADD = ../../src/libmnl.la
rtnl_link_dump_SOURCES = rtnl-link-dump.c
rtnl_link_dump_LDADD = ../../src/libmnl.la
@@ -26,3 +31,6 @@ rtnl_link_set_LDADD = ../../src/libmnl.la
rtnl_route_dump_SOURCES = rtnl-route-dump.c
rtnl_route_dump_LDADD = ../../src/libmnl.la
+
+rtnl_route_event_SOURCES = rtnl-route-event.c
+rtnl_route_event_LDADD = ../../src/libmnl.la
diff --git a/examples/rtnl/rtnl-addr-dump.c b/examples/rtnl/rtnl-addr-dump.c
new file mode 100644
index 0000000..6b4e52e
--- /dev/null
+++ b/examples/rtnl/rtnl-addr-dump.c
@@ -0,0 +1,133 @@
+/* This example is placed in the public domain. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+#include <arpa/inet.h>
+
+#include <libmnl/libmnl.h>
+#include <linux/if.h>
+#include <linux/if_link.h>
+#include <linux/rtnetlink.h>
+
+static int data_attr_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ /* skip unsupported attribute in user-space */
+ if (mnl_attr_type_valid(attr, IFA_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch(type) {
+ case IFA_ADDRESS:
+ if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+ break;
+ }
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static int data_cb(const struct nlmsghdr *nlh, void *data)
+{
+ struct nlattr *tb[IFLA_MAX+1] = {};
+ struct ifaddrmsg *ifa = mnl_nlmsg_get_payload(nlh);
+
+ printf("index=%d family=%d ", ifa->ifa_index, ifa->ifa_family);
+
+ mnl_attr_parse(nlh, sizeof(*ifa), data_attr_cb, tb);
+ printf("addr=");
+ if (tb[IFA_ADDRESS]) {
+ void *addr = mnl_attr_get_payload(tb[IFA_ADDRESS]);
+ char out[INET6_ADDRSTRLEN];
+
+ if (inet_ntop(ifa->ifa_family, addr, out, sizeof(out)))
+ printf("%s ", out);
+ }
+ printf("scope=");
+ switch(ifa->ifa_scope) {
+ case 0:
+ printf("global ");
+ break;
+ case 200:
+ printf("site ");
+ break;
+ case 253:
+ printf("link ");
+ break;
+ case 254:
+ printf("host ");
+ break;
+ case 255:
+ printf("nowhere ");
+ break;
+ default:
+ printf("%d ", ifa->ifa_scope);
+ break;
+ }
+
+ printf("\n");
+ return MNL_CB_OK;
+}
+
+int main(int argc, char *argv[])
+{
+ struct mnl_socket *nl;
+ char buf[MNL_SOCKET_BUFFER_SIZE];
+ struct nlmsghdr *nlh;
+ struct rtgenmsg *rt;
+ int ret;
+ unsigned int seq, portid;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <inet|inet6>\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ nlh = mnl_nlmsg_put_header(buf);
+ nlh->nlmsg_type = RTM_GETADDR;
+ nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
+ nlh->nlmsg_seq = seq = time(NULL);
+ rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg));
+ if (strcmp(argv[1], "inet") == 0)
+ rt->rtgen_family = AF_INET;
+ else if (strcmp(argv[1], "inet6") == 0)
+ rt->rtgen_family = AF_INET6;
+
+ nl = mnl_socket_open(NETLINK_ROUTE);
+ if (nl == NULL) {
+ perror("mnl_socket_open");
+ exit(EXIT_FAILURE);
+ }
+
+ if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
+ perror("mnl_socket_bind");
+ exit(EXIT_FAILURE);
+ }
+ portid = mnl_socket_get_portid(nl);
+
+ if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
+ perror("mnl_socket_sendto");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+ while (ret > 0) {
+ ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
+ if (ret <= MNL_CB_STOP)
+ break;
+ ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+ }
+ if (ret == -1) {
+ perror("error");
+ exit(EXIT_FAILURE);
+ }
+
+ mnl_socket_close(nl);
+
+ return 0;
+}
diff --git a/examples/rtnl/rtnl-link-dump.c b/examples/rtnl/rtnl-link-dump.c
index 159f3b6..f5d6312 100644
--- a/examples/rtnl/rtnl-link-dump.c
+++ b/examples/rtnl/rtnl-link-dump.c
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
+#include <arpa/inet.h>
#include <libmnl/libmnl.h>
#include <linux/if.h>
@@ -19,6 +20,12 @@ static int data_attr_cb(const struct nlattr *attr, void *data)
return MNL_CB_OK;
switch(type) {
+ case IFLA_ADDRESS:
+ if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+ break;
case IFLA_MTU:
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
perror("mnl_attr_validate");
@@ -27,7 +34,7 @@ static int data_attr_cb(const struct nlattr *attr, void *data)
break;
case IFLA_IFNAME:
if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
- perror("mnl_attr_validate2");
+ perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
break;
@@ -55,7 +62,18 @@ static int data_cb(const struct nlmsghdr *nlh, void *data)
printf("mtu=%d ", mnl_attr_get_u32(tb[IFLA_MTU]));
}
if (tb[IFLA_IFNAME]) {
- printf("name=%s", mnl_attr_get_str(tb[IFLA_IFNAME]));
+ printf("name=%s ", mnl_attr_get_str(tb[IFLA_IFNAME]));
+ }
+ if (tb[IFLA_ADDRESS]) {
+ uint8_t *hwaddr = mnl_attr_get_payload(tb[IFLA_ADDRESS]);
+ int i;
+
+ printf("hwaddr=");
+ for (i=0; i<mnl_attr_get_payload_len(tb[IFLA_ADDRESS]); i++) {
+ printf("%.2x", hwaddr[i] & 0xff);
+ if (i+1 != mnl_attr_get_payload_len(tb[IFLA_ADDRESS]))
+ printf(":");
+ }
}
printf("\n");
return MNL_CB_OK;
@@ -90,7 +108,7 @@ int main(void)
portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
- perror("mnl_socket_send");
+ perror("mnl_socket_sendto");
exit(EXIT_FAILURE);
}
diff --git a/examples/rtnl/rtnl-link-dump2.c b/examples/rtnl/rtnl-link-dump2.c
index 78f31a8..b3ca3fa 100644
--- a/examples/rtnl/rtnl-link-dump2.c
+++ b/examples/rtnl/rtnl-link-dump2.c
@@ -25,7 +25,7 @@ static int data_attr_cb(const struct nlattr *attr, void *data)
break;
case IFLA_IFNAME:
if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
- perror("mnl_attr_validate2");
+ perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
printf("name=%s ", mnl_attr_get_str(attr));
@@ -81,7 +81,7 @@ int main(void)
portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
- perror("mnl_socket_send");
+ perror("mnl_socket_sendto");
exit(EXIT_FAILURE);
}
diff --git a/examples/rtnl/rtnl-link-dump3.c b/examples/rtnl/rtnl-link-dump3.c
index a6dce6f..2521214 100644
--- a/examples/rtnl/rtnl-link-dump3.c
+++ b/examples/rtnl/rtnl-link-dump3.c
@@ -81,7 +81,7 @@ int main(void)
portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
- perror("mnl_socket_send");
+ perror("mnl_socket_sendto");
exit(EXIT_FAILURE);
}
diff --git a/examples/rtnl/rtnl-link-event.c b/examples/rtnl/rtnl-link-event.c
index 90bb8e5..123fb88 100644
--- a/examples/rtnl/rtnl-link-event.c
+++ b/examples/rtnl/rtnl-link-event.c
@@ -26,7 +26,7 @@ static int data_attr_cb(const struct nlattr *attr, void *data)
break;
case IFLA_IFNAME:
if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
- perror("mnl_attr_validate2");
+ perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
break;
diff --git a/examples/rtnl/rtnl-link-set.c b/examples/rtnl/rtnl-link-set.c
index dad8856..6086c37 100644
--- a/examples/rtnl/rtnl-link-set.c
+++ b/examples/rtnl/rtnl-link-set.c
@@ -62,19 +62,19 @@ int main(int argc, char *argv[])
sizeof(struct ifinfomsg));
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
- perror("mnl_socket_send");
+ perror("mnl_socket_sendto");
exit(EXIT_FAILURE);
}
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
if (ret == -1) {
- perror("read");
+ perror("mnl_socket_recvfrom");
exit(EXIT_FAILURE);
}
ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
if (ret == -1){
- perror("callback");
+ perror("mnl_cb_run");
exit(EXIT_FAILURE);
}
diff --git a/examples/rtnl/rtnl-route-add.c b/examples/rtnl/rtnl-route-add.c
index 6d166da..97578cd 100644
--- a/examples/rtnl/rtnl-route-add.c
+++ b/examples/rtnl/rtnl-route-add.c
@@ -14,63 +14,83 @@
int main(int argc, char *argv[])
{
+ struct mnl_socket *nl;
+ char buf[MNL_SOCKET_BUFFER_SIZE];
+ struct nlmsghdr *nlh;
+ struct rtmsg *rtm;
+ uint32_t prefix, seq, portid;
+ union {
+ in_addr_t ip;
+ struct in6_addr ip6;
+ } dst;
+ union {
+ in_addr_t ip;
+ struct in6_addr ip6;
+ } gw;
+ int iface, ret, family = AF_INET;
+
if (argc <= 3) {
printf("Usage: %s iface destination cidr [gateway]\n", argv[0]);
printf("Example: %s eth0 10.0.1.12 32 10.0.1.11\n", argv[0]);
+ printf(" %s eth0 ffff::10.0.1.12 128 fdff::1\n", argv[0]);
exit(EXIT_FAILURE);
}
- int iface;
iface = if_nametoindex(argv[1]);
if (iface == 0) {
- printf("Bad interface name\n");
+ perror("if_nametoindex");
exit(EXIT_FAILURE);
}
- in_addr_t dst;
if (!inet_pton(AF_INET, argv[2], &dst)) {
- printf("Bad destination\n");
- exit(EXIT_FAILURE);
+ if (!inet_pton(AF_INET6, argv[2], &dst)) {
+ perror("inet_pton");
+ exit(EXIT_FAILURE);
+ }
+ family = AF_INET6;
}
- uint32_t mask;
- if (sscanf(argv[3], "%u", &mask) == 0) {
- printf("Bad CIDR\n");
+ if (sscanf(argv[3], "%u", &prefix) == 0) {
+ perror("sscanf");
exit(EXIT_FAILURE);
}
- in_addr_t gw;
- if (argc >= 5 && !inet_pton(AF_INET, argv[4], &gw)) {
- printf("Bad gateway\n");
+ if (argc == 5 && !inet_pton(family, argv[4], &gw)) {
+ perror("inet_pton");
exit(EXIT_FAILURE);
}
- struct mnl_socket *nl;
- char buf[MNL_SOCKET_BUFFER_SIZE];
- struct nlmsghdr *nlh;
- struct rtmsg *rtm;
-
nlh = mnl_nlmsg_put_header(buf);
nlh->nlmsg_type = RTM_NEWROUTE;
- nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
- nlh->nlmsg_seq = time(NULL);
+ nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
+ nlh->nlmsg_seq = seq = time(NULL);
rtm = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtmsg));
- rtm->rtm_family = AF_INET;
- rtm->rtm_dst_len = mask;
+ rtm->rtm_family = family;
+ rtm->rtm_dst_len = prefix;
rtm->rtm_src_len = 0;
rtm->rtm_tos = 0;
- rtm->rtm_protocol = RTPROT_BOOT;
+ rtm->rtm_protocol = RTPROT_STATIC;
rtm->rtm_table = RT_TABLE_MAIN;
rtm->rtm_type = RTN_UNICAST;
/* is there any gateway? */
rtm->rtm_scope = (argc == 4) ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
rtm->rtm_flags = 0;
- mnl_attr_put_u32(nlh, RTA_DST, dst);
+ if (family == AF_INET)
+ mnl_attr_put_u32(nlh, RTA_DST, dst.ip);
+ else
+ mnl_attr_put(nlh, RTA_DST, sizeof(struct in6_addr), &dst);
+
mnl_attr_put_u32(nlh, RTA_OIF, iface);
- if (argc >= 5)
- mnl_attr_put_u32(nlh, RTA_GATEWAY, gw);
+ if (argc == 5) {
+ if (family == AF_INET)
+ mnl_attr_put_u32(nlh, RTA_GATEWAY, gw.ip);
+ else {
+ mnl_attr_put(nlh, RTA_GATEWAY, sizeof(struct in6_addr),
+ &gw.ip6);
+ }
+ }
nl = mnl_socket_open(NETLINK_ROUTE);
if (nl == NULL) {
@@ -82,9 +102,22 @@ int main(int argc, char *argv[])
perror("mnl_socket_bind");
exit(EXIT_FAILURE);
}
+ portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
- perror("mnl_socket_send");
+ perror("mnl_socket_sendto");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+ if (ret < 0) {
+ perror("mnl_socket_recvfrom");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
+ if (ret < 0) {
+ perror("mnl_cb_run");
exit(EXIT_FAILURE);
}
diff --git a/examples/rtnl/rtnl-route-dump.c b/examples/rtnl/rtnl-route-dump.c
index a798515..17da80b 100644
--- a/examples/rtnl/rtnl-route-dump.c
+++ b/examples/rtnl/rtnl-route-dump.c
@@ -13,6 +13,8 @@
static int data_attr_cb2(const struct nlattr *attr, void *data)
{
+ const struct nlattr **tb = data;
+
/* skip unsupported attribute in user-space */
if (mnl_attr_type_valid(attr, RTAX_MAX) < 0)
return MNL_CB_OK;
@@ -21,6 +23,8 @@ static int data_attr_cb2(const struct nlattr *attr, void *data)
perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
+
+ tb[mnl_attr_get_type(attr)] = attr;
return MNL_CB_OK;
}
@@ -51,6 +55,9 @@ static void attributes_show_ipv4(struct nlattr *tb[])
struct in_addr *addr = mnl_attr_get_payload(tb[RTA_GATEWAY]);
printf("gw=%s ", inet_ntoa(*addr));
}
+ if (tb[RTA_PRIORITY]) {
+ printf("prio=%u ", mnl_attr_get_u32(tb[RTA_PRIORITY]));
+ }
if (tb[RTA_METRICS]) {
int i;
struct nlattr *tbx[RTAX_MAX+1] = {};
@@ -64,7 +71,6 @@ static void attributes_show_ipv4(struct nlattr *tb[])
}
}
}
- printf("\n");
}
/* like inet_ntoa(), not reentrant */
@@ -102,6 +108,9 @@ static void attributes_show_ipv6(struct nlattr *tb[])
struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_GATEWAY]);
printf("gw=%s ", inet6_ntoa(*addr));
}
+ if (tb[RTA_PRIORITY]) {
+ printf("prio=%u ", mnl_attr_get_u32(tb[RTA_PRIORITY]));
+ }
if (tb[RTA_METRICS]) {
int i;
struct nlattr *tbx[RTAX_MAX+1] = {};
@@ -115,7 +124,6 @@ static void attributes_show_ipv6(struct nlattr *tb[])
}
}
}
- printf("\n");
}
static int data_ipv4_attr_cb(const struct nlattr *attr, void *data)
@@ -135,6 +143,7 @@ static int data_ipv4_attr_cb(const struct nlattr *attr, void *data)
case RTA_FLOW:
case RTA_PREFSRC:
case RTA_GATEWAY:
+ case RTA_PRIORITY:
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
@@ -164,6 +173,7 @@ static int data_ipv6_attr_cb(const struct nlattr *attr, void *data)
case RTA_TABLE:
case RTA_OIF:
case RTA_FLOW:
+ case RTA_PRIORITY:
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
@@ -175,7 +185,7 @@ static int data_ipv6_attr_cb(const struct nlattr *attr, void *data)
case RTA_GATEWAY:
if (mnl_attr_validate2(attr, MNL_TYPE_BINARY,
sizeof(struct in6_addr)) < 0) {
- perror("mnl_attr_validate");
+ perror("mnl_attr_validate2");
return MNL_CB_ERROR;
}
break;
@@ -269,7 +279,7 @@ static int data_cb(const struct nlmsghdr *nlh, void *data)
* RTM_F_EQUALIZE = 0x400: Multipath equalizer: NI
* RTM_F_PREFIX = 0x800: Prefix addresses
*/
- printf("flags=%x\n", rm->rtm_flags);
+ printf("flags=%x ", rm->rtm_flags);
switch(rm->rtm_family) {
case AF_INET:
@@ -282,6 +292,7 @@ static int data_cb(const struct nlmsghdr *nlh, void *data)
break;
}
+ printf("\n");
return MNL_CB_OK;
}
@@ -323,7 +334,7 @@ int main(int argc, char *argv[])
portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
- perror("mnl_socket_send");
+ perror("mnl_socket_sendto");
exit(EXIT_FAILURE);
}
diff --git a/examples/rtnl/rtnl-route-event.c b/examples/rtnl/rtnl-route-event.c
new file mode 100644
index 0000000..6cad9f0
--- /dev/null
+++ b/examples/rtnl/rtnl-route-event.c
@@ -0,0 +1,341 @@
+/* This example is placed in the public domain. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+#include <arpa/inet.h>
+
+#include <libmnl/libmnl.h>
+#include <linux/if.h>
+#include <linux/if_link.h>
+#include <linux/rtnetlink.h>
+
+static int data_attr_cb2(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+
+ /* skip unsupported attribute in user-space */
+ if (mnl_attr_type_valid(attr, RTAX_MAX) < 0)
+ return MNL_CB_OK;
+
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+
+ tb[mnl_attr_get_type(attr)] = attr;
+ return MNL_CB_OK;
+}
+
+static void attributes_show_ipv4(struct nlattr *tb[])
+{
+ if (tb[RTA_TABLE]) {
+ printf("table=%u ", mnl_attr_get_u32(tb[RTA_TABLE]));
+ }
+ if (tb[RTA_DST]) {
+ struct in_addr *addr = mnl_attr_get_payload(tb[RTA_DST]);
+ printf("dst=%s ", inet_ntoa(*addr));
+ }
+ if (tb[RTA_SRC]) {
+ struct in_addr *addr = mnl_attr_get_payload(tb[RTA_SRC]);
+ printf("src=%s ", inet_ntoa(*addr));
+ }
+ if (tb[RTA_OIF]) {
+ printf("oif=%u ", mnl_attr_get_u32(tb[RTA_OIF]));
+ }
+ if (tb[RTA_FLOW]) {
+ printf("flow=%u ", mnl_attr_get_u32(tb[RTA_FLOW]));
+ }
+ if (tb[RTA_PREFSRC]) {
+ struct in_addr *addr = mnl_attr_get_payload(tb[RTA_PREFSRC]);
+ printf("prefsrc=%s ", inet_ntoa(*addr));
+ }
+ if (tb[RTA_GATEWAY]) {
+ struct in_addr *addr = mnl_attr_get_payload(tb[RTA_GATEWAY]);
+ printf("gw=%s ", inet_ntoa(*addr));
+ }
+ if (tb[RTA_PRIORITY]) {
+ printf("prio=%u ", mnl_attr_get_u32(tb[RTA_PRIORITY]));
+ }
+ if (tb[RTA_METRICS]) {
+ int i;
+ struct nlattr *tbx[RTAX_MAX+1] = {};
+
+ mnl_attr_parse_nested(tb[RTA_METRICS], data_attr_cb2, tbx);
+
+ for (i=0; i<RTAX_MAX; i++) {
+ if (tbx[i]) {
+ printf("metrics[%d]=%u ",
+ i, mnl_attr_get_u32(tbx[i]));
+ }
+ }
+ }
+}
+
+/* like inet_ntoa(), not reentrant */
+static const char *inet6_ntoa(struct in6_addr in6)
+{
+ static char buf[INET6_ADDRSTRLEN];
+
+ return inet_ntop(AF_INET6, &in6.s6_addr, buf, sizeof(buf));
+}
+
+static void attributes_show_ipv6(struct nlattr *tb[])
+{
+ if (tb[RTA_TABLE]) {
+ printf("table=%u ", mnl_attr_get_u32(tb[RTA_TABLE]));
+ }
+ if (tb[RTA_DST]) {
+ struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_DST]);
+ printf("dst=%s ", inet6_ntoa(*addr));
+ }
+ if (tb[RTA_SRC]) {
+ struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_SRC]);
+ printf("src=%s ", inet6_ntoa(*addr));
+ }
+ if (tb[RTA_OIF]) {
+ printf("oif=%u ", mnl_attr_get_u32(tb[RTA_OIF]));
+ }
+ if (tb[RTA_FLOW]) {
+ printf("flow=%u ", mnl_attr_get_u32(tb[RTA_FLOW]));
+ }
+ if (tb[RTA_PREFSRC]) {
+ struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_PREFSRC]);
+ printf("prefsrc=%s ", inet6_ntoa(*addr));
+ }
+ if (tb[RTA_GATEWAY]) {
+ struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_GATEWAY]);
+ printf("gw=%s ", inet6_ntoa(*addr));
+ }
+ if (tb[RTA_PRIORITY]) {
+ printf("prio=%u ", mnl_attr_get_u32(tb[RTA_PRIORITY]));
+ }
+ if (tb[RTA_METRICS]) {
+ int i;
+ struct nlattr *tbx[RTAX_MAX+1] = {};
+
+ mnl_attr_parse_nested(tb[RTA_METRICS], data_attr_cb2, tbx);
+
+ for (i=0; i<RTAX_MAX; i++) {
+ if (tbx[i]) {
+ printf("metrics[%d]=%u ",
+ i, mnl_attr_get_u32(tbx[i]));
+ }
+ }
+ }
+}
+
+static int data_ipv4_attr_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ /* skip unsupported attribute in user-space */
+ if (mnl_attr_type_valid(attr, RTA_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch(type) {
+ case RTA_TABLE:
+ case RTA_DST:
+ case RTA_SRC:
+ case RTA_OIF:
+ case RTA_FLOW:
+ case RTA_PREFSRC:
+ case RTA_GATEWAY:
+ case RTA_PRIORITY:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+ break;
+ case RTA_METRICS:
+ if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+ break;
+ }
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static int data_ipv6_attr_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ /* skip unsupported attribute in user-space */
+ if (mnl_attr_type_valid(attr, RTA_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch(type) {
+ case RTA_TABLE:
+ case RTA_OIF:
+ case RTA_FLOW:
+ case RTA_PRIORITY:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+ break;
+ case RTA_DST:
+ case RTA_SRC:
+ case RTA_PREFSRC:
+ case RTA_GATEWAY:
+ if (mnl_attr_validate2(attr, MNL_TYPE_BINARY,
+ sizeof(struct in6_addr)) < 0) {
+ perror("mnl_attr_validate2");
+ return MNL_CB_ERROR;
+ }
+ break;
+ case RTA_METRICS:
+ if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+ break;
+ }
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static int data_cb(const struct nlmsghdr *nlh, void *data)
+{
+ struct nlattr *tb[RTA_MAX+1] = {};
+ struct rtmsg *rm = mnl_nlmsg_get_payload(nlh);
+
+ switch(nlh->nlmsg_type) {
+ case RTM_NEWROUTE:
+ printf("[NEW] ");
+ break;
+ case RTM_DELROUTE:
+ printf("[DEL] ");
+ break;
+ }
+
+ /* protocol family = AF_INET | AF_INET6 */
+ printf("family=%u ", rm->rtm_family);
+
+ /* destination CIDR, eg. 24 or 32 for IPv4 */
+ printf("dst_len=%u ", rm->rtm_dst_len);
+
+ /* source CIDR */
+ printf("src_len=%u ", rm->rtm_src_len);
+
+ /* type of service (TOS), eg. 0 */
+ printf("tos=%u ", rm->rtm_tos);
+
+ /* table id:
+ * RT_TABLE_UNSPEC = 0
+ *
+ * ... user defined values ...
+ *
+ * RT_TABLE_COMPAT = 252
+ * RT_TABLE_DEFAULT = 253
+ * RT_TABLE_MAIN = 254
+ * RT_TABLE_LOCAL = 255
+ * RT_TABLE_MAX = 0xFFFFFFFF
+ *
+ * Synonimous attribute: RTA_TABLE.
+ */
+ printf("table=%u ", rm->rtm_table);
+
+ /* type:
+ * RTN_UNSPEC = 0
+ * RTN_UNICAST = 1
+ * RTN_LOCAL = 2
+ * RTN_BROADCAST = 3
+ * RTN_ANYCAST = 4
+ * RTN_MULTICAST = 5
+ * RTN_BLACKHOLE = 6
+ * RTN_UNREACHABLE = 7
+ * RTN_PROHIBIT = 8
+ * RTN_THROW = 9
+ * RTN_NAT = 10
+ * RTN_XRESOLVE = 11
+ * __RTN_MAX = 12
+ */
+ printf("type=%u ", rm->rtm_type);
+
+ /* scope:
+ * RT_SCOPE_UNIVERSE = 0 : everywhere in the universe
+ *
+ * ... user defined values ...
+ *
+ * RT_SCOPE_SITE = 200
+ * RT_SCOPE_LINK = 253 : destination attached to link
+ * RT_SCOPE_HOST = 254 : local address
+ * RT_SCOPE_NOWHERE = 255 : not existing destination
+ */
+ printf("scope=%u ", rm->rtm_scope);
+
+ /* protocol:
+ * RTPROT_UNSPEC = 0
+ * RTPROT_REDIRECT = 1
+ * RTPROT_KERNEL = 2 : route installed by kernel
+ * RTPROT_BOOT = 3 : route installed during boot
+ * RTPROT_STATIC = 4 : route installed by administrator
+ *
+ * Values >= RTPROT_STATIC are not interpreted by kernel, they are
+ * just user-defined.
+ */
+ printf("proto=%u ", rm->rtm_protocol);
+
+ /* flags:
+ * RTM_F_NOTIFY = 0x100: notify user of route change
+ * RTM_F_CLONED = 0x200: this route is cloned
+ * RTM_F_EQUALIZE = 0x400: Multipath equalizer: NI
+ * RTM_F_PREFIX = 0x800: Prefix addresses
+ */
+ printf("flags=%x ", rm->rtm_flags);
+
+ switch(rm->rtm_family) {
+ case AF_INET:
+ mnl_attr_parse(nlh, sizeof(*rm), data_ipv4_attr_cb, tb);
+ attributes_show_ipv4(tb);
+ break;
+ case AF_INET6:
+ mnl_attr_parse(nlh, sizeof(*rm), data_ipv6_attr_cb, tb);
+ attributes_show_ipv6(tb);
+ break;
+ }
+
+ printf("\n");
+ return MNL_CB_OK;
+}
+
+int main(int argc, char *argv[])
+{
+ struct mnl_socket *nl;
+ char buf[MNL_SOCKET_BUFFER_SIZE];
+ int ret;
+
+ nl = mnl_socket_open(NETLINK_ROUTE);
+ if (nl == NULL) {
+ perror("mnl_socket_open");
+ exit(EXIT_FAILURE);
+ }
+
+ if (mnl_socket_bind(nl, RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE,
+ MNL_SOCKET_AUTOPID) < 0) {
+ perror("mnl_socket_bind");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+ while (ret > 0) {
+ ret = mnl_cb_run(buf, ret, 0, 0, data_cb, NULL);
+ if (ret <= MNL_CB_STOP)
+ break;
+ ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+ }
+ if (ret == -1) {
+ perror("error");
+ exit(EXIT_FAILURE);
+ }
+
+ mnl_socket_close(nl);
+
+ return 0;
+}