summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libsystemd/sd-rtnl/rtnl-internal.h11
-rw-r--r--src/libsystemd/sd-rtnl/rtnl-message.c125
-rw-r--r--src/libsystemd/sd-rtnl/test-rtnl.c57
-rw-r--r--src/systemd/sd-rtnl.h4
4 files changed, 195 insertions, 2 deletions
diff --git a/src/libsystemd/sd-rtnl/rtnl-internal.h b/src/libsystemd/sd-rtnl/rtnl-internal.h
index 0667be5838..0c17d94c59 100644
--- a/src/libsystemd/sd-rtnl/rtnl-internal.h
+++ b/src/libsystemd/sd-rtnl/rtnl-internal.h
@@ -94,7 +94,8 @@ struct sd_rtnl_message {
size_t container_offsets[RTNL_CONTAINER_DEPTH]; /* offset from hdr to each container's start */
unsigned n_containers; /* number of containers */
size_t next_rta_offset; /* offset from hdr to next rta */
-
+ size_t *rta_offset_tb;
+ unsigned short rta_tb_size;
bool sealed:1;
};
@@ -103,6 +104,14 @@ int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, size_t initial_size);
int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m);
int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret);
+int rtnl_message_read_internal(sd_rtnl_message *m, unsigned short type, void **data);
+int rtnl_message_parse(sd_rtnl_message *m,
+ size_t **rta_offset_tb,
+ unsigned short *rta_tb_size,
+ int max,
+ struct rtattr *rta,
+ unsigned int rt_len);
+
/* Make sure callbacks don't destroy the rtnl connection */
#define RTNL_DONT_DESTROY(rtnl) \
_cleanup_rtnl_unref_ _unused_ sd_rtnl *_dont_destroy_##rtnl = sd_rtnl_ref(rtnl)
diff --git a/src/libsystemd/sd-rtnl/rtnl-message.c b/src/libsystemd/sd-rtnl/rtnl-message.c
index 41a6e4ea85..dd3a8fb1d2 100644
--- a/src/libsystemd/sd-rtnl/rtnl-message.c
+++ b/src/libsystemd/sd-rtnl/rtnl-message.c
@@ -280,6 +280,7 @@ sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m) {
if (m && REFCNT_DEC(m->n_ref) <= 0) {
sd_rtnl_unref(m->rtnl);
free(m->hdr);
+ free(m->rta_offset_tb);
free(m);
}
@@ -800,6 +801,81 @@ int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data)
return 1;
}
+int rtnl_message_read_internal(sd_rtnl_message *m, unsigned short type, void **data) {
+ assert_return(m, -EINVAL);
+ assert_return(m->sealed, -EPERM);
+ assert_return(data, -EINVAL);
+ assert_return(m->rta_offset_tb, -EINVAL);
+ assert_return(type < m->rta_tb_size, -EINVAL);
+
+ if(!m->rta_offset_tb[type])
+ return -ENODATA;
+
+ *data = RTA_DATA((struct rtattr *)((uint8_t *) m->hdr + m->rta_offset_tb[type]));
+
+ return 0;
+}
+
+int sd_rtnl_message_read_string(sd_rtnl_message *m, unsigned short type, char **data) {
+ int r;
+ void *attr_data;
+
+ assert_return(data, -EINVAL);
+
+ r = rtnl_message_read_internal(m, type, &attr_data);
+ if(r < 0)
+ return r;
+
+ *data = (char *) attr_data;
+
+ return 0;
+}
+
+int sd_rtnl_message_read_u8(sd_rtnl_message *m, unsigned short type, uint8_t *data) {
+ int r;
+ void *attr_data;
+
+ assert_return(data, -EINVAL);
+
+ r = rtnl_message_read_internal(m, type, &attr_data);
+ if(r < 0)
+ return r;
+
+ *data = *(uint8_t *) attr_data;
+
+ return 0;
+}
+
+int sd_rtnl_message_read_u16(sd_rtnl_message *m, unsigned short type, uint16_t *data) {
+ int r;
+ void *attr_data;
+
+ assert_return(data, -EINVAL);
+
+ r = rtnl_message_read_internal(m, type, &attr_data);
+ if(r < 0)
+ return r;
+
+ *data = *(uint16_t *) attr_data;
+
+ return 0;
+}
+
+int sd_rtnl_message_read_u32(sd_rtnl_message *m, unsigned short type, uint32_t *data) {
+ int r;
+ void *attr_data;
+
+ assert_return(data, -EINVAL);
+
+ r = rtnl_message_read_internal(m, type, &attr_data);
+ if(r < 0)
+ return r;
+
+ *data = *(uint32_t *) attr_data;
+
+ return 0;
+}
+
int sd_rtnl_message_exit_container(sd_rtnl_message *m) {
assert_return(m, -EINVAL);
assert_return(m->sealed, -EINVAL);
@@ -872,6 +948,33 @@ static int message_receive_need(sd_rtnl *rtnl, size_t *need) {
return 0;
}
+int rtnl_message_parse(sd_rtnl_message *m,
+ size_t **rta_offset_tb,
+ unsigned short *rta_tb_size,
+ int max,
+ struct rtattr *rta,
+ unsigned int rt_len) {
+ int type;
+ size_t *tb;
+
+ tb = (size_t *) new0(size_t *, max);
+ if(!tb)
+ return -ENOMEM;
+
+ *rta_tb_size = max;
+
+ for (; RTA_OK(rta, rt_len); rta = RTA_NEXT(rta, rt_len)) {
+ type = rta->rta_type;
+
+ if (type < max && !tb[type])
+ tb[type] = (uint8_t *) rta - (uint8_t *) m->hdr;
+ }
+
+ *rta_offset_tb = tb;
+
+ return 0;
+}
+
/* returns the number of bytes sent, or a negative error code */
int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m) {
union {
@@ -961,6 +1064,14 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
ifi = NLMSG_DATA(m->hdr);
UPDATE_RTA(m, IFLA_RTA(ifi));
+
+ r = rtnl_message_parse(m,
+ &m->rta_offset_tb,
+ &m->rta_tb_size,
+ IFLA_MAX,
+ IFLA_RTA(ifi),
+ IFLA_PAYLOAD(m->hdr));
+
}
break;
case RTM_NEWADDR:
@@ -973,6 +1084,13 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
ifa = NLMSG_DATA(m->hdr);
UPDATE_RTA(m, IFA_RTA(ifa));
+
+ r = rtnl_message_parse(m,
+ &m->rta_offset_tb,
+ &m->rta_tb_size,
+ IFA_MAX,
+ IFA_RTA(ifa),
+ IFA_PAYLOAD(m->hdr));
}
break;
case RTM_NEWROUTE:
@@ -985,6 +1103,13 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
rtm = NLMSG_DATA(m->hdr);
UPDATE_RTA(m, RTM_RTA(rtm));
+
+ r = rtnl_message_parse(m,
+ &m->rta_offset_tb,
+ &m->rta_tb_size,
+ RTA_MAX,
+ RTM_RTA(rtm),
+ RTM_PAYLOAD(m->hdr));
}
break;
case NLMSG_NOOP:
diff --git a/src/libsystemd/sd-rtnl/test-rtnl.c b/src/libsystemd/sd-rtnl/test-rtnl.c
index 1016bb6d4e..dc210426cc 100644
--- a/src/libsystemd/sd-rtnl/test-rtnl.c
+++ b/src/libsystemd/sd-rtnl/test-rtnl.c
@@ -28,6 +28,7 @@
#include "rtnl-util.h"
#include "event-util.h"
#include "missing.h"
+#include "rtnl-internal.h"
static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
_cleanup_rtnl_message_unref_ sd_rtnl_message *message;
@@ -63,7 +64,10 @@ static void test_link_get(sd_rtnl *rtnl, int ifindex) {
unsigned int mtu = 1500;
unsigned int *mtu_reply;
void *data;
+ char *str_data;
uint16_t type;
+ uint8_t u8_data;
+ uint32_t u32_data;
assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
assert_se(m);
@@ -123,9 +127,21 @@ static void test_link_get(sd_rtnl *rtnl, int ifindex) {
}
}
+ assert_se(sd_rtnl_message_read_string(r, IFLA_IFNAME, &str_data) == 0);
+
+ assert_se(sd_rtnl_message_read_u8(r, IFLA_CARRIER, &u8_data) == 0);
+ assert_se(sd_rtnl_message_read_u8(r, IFLA_OPERSTATE, &u8_data) == 0);
+ assert_se(sd_rtnl_message_read_u8(r, IFLA_LINKMODE, &u8_data) == 0);
+
+ assert_se(sd_rtnl_message_read_u32(r, IFLA_MTU, &u32_data) == 0);
+ assert_se(sd_rtnl_message_read_u32(r, IFLA_GROUP, &u32_data) == 0);
+ assert_se(sd_rtnl_message_read_u32(r, IFLA_TXQLEN, &u32_data) == 0);
+ assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_TX_QUEUES, &u32_data) == 0);
+ assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_RX_QUEUES, &u32_data) == 0);
+
assert_se(sd_rtnl_flush(rtnl) >= 0);
assert_se((m = sd_rtnl_message_unref(m)) == NULL);
-
+ assert_se((r = sd_rtnl_message_unref(r)) == NULL);
}
static void test_route(void) {
@@ -134,7 +150,9 @@ static void test_route(void) {
uint32_t index = 2;
uint16_t type;
void *data;
+ uint32_t u32_data;
int r;
+ struct rtmsg *rtm;
r = sd_rtnl_message_new_route(NULL, &req, RTM_NEWROUTE, AF_INET);
if (r < 0) {
@@ -165,6 +183,19 @@ static void test_route(void) {
assert_se(sd_rtnl_message_read(req, &type, &data) > 0);
assert_se(type == RTA_OIF);
assert_se(*(uint32_t *) data == index);
+
+ rtm = NLMSG_DATA(req->hdr);
+ r = rtnl_message_parse(req,
+ &req->rta_offset_tb,
+ &req->rta_tb_size,
+ RTA_MAX,
+ RTM_RTA(rtm),
+ RTM_PAYLOAD(req->hdr));
+
+ assert_se(sd_rtnl_message_read_u32(req, RTA_GATEWAY, &u32_data) == 0);
+ assert_se(sd_rtnl_message_read_u32(req, RTA_OIF, &u32_data) == 0);
+
+ assert_se((req = sd_rtnl_message_unref(req)) == NULL);
}
static void test_multiple(void) {
@@ -226,6 +257,8 @@ static void test_event_loop(int ifindex) {
assert_se(sd_event_run(event, 0) >= 0);
assert_se(sd_rtnl_detach_event(rtnl) >= 0);
+
+ assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
}
static int pipe_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
@@ -255,6 +288,8 @@ static void test_async(int ifindex) {
assert_se(sd_rtnl_wait(rtnl, 0) >= 0);
assert_se(sd_rtnl_process(rtnl, &r) >= 0);
+
+ assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
}
static void test_pipe(int ifindex) {
@@ -277,12 +312,17 @@ static void test_pipe(int ifindex) {
assert_se(sd_rtnl_wait(rtnl, 0) >= 0);
assert_se(sd_rtnl_process(rtnl, NULL) >= 0);
}
+
+ assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
}
static void test_container(void) {
_cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
uint16_t type;
+ uint32_t u32_data;
void *data;
+ int r;
+ struct ifinfomsg *ifi;
assert_se(sd_rtnl_message_new_link(NULL, &m, RTM_NEWLINK, 0) >= 0);
@@ -320,6 +360,19 @@ static void test_container(void) {
assert_se(sd_rtnl_message_read(m, &type, &data) == 0);
assert_se(sd_rtnl_message_exit_container(m) >= 0);
*/
+
+ ifi = NLMSG_DATA(m->hdr);
+ r = rtnl_message_parse(m,
+ &m->rta_offset_tb,
+ &m->rta_tb_size,
+ IFLA_MAX,
+ IFLA_RTA(ifi),
+ IFLA_PAYLOAD(m->hdr));
+ if(r < 0)
+ return;
+
+ assert_se(sd_rtnl_message_read_u32(m, IFLA_LINKINFO, &u32_data) == 0);
+
assert_se(sd_rtnl_message_exit_container(m) == -EINVAL);
}
@@ -334,6 +387,8 @@ static void test_match(void) {
assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 1);
assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 1);
assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 0);
+
+ assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
}
int main(void) {
diff --git a/src/systemd/sd-rtnl.h b/src/systemd/sd-rtnl.h
index d56c14e58e..886a66ec72 100644
--- a/src/systemd/sd-rtnl.h
+++ b/src/systemd/sd-rtnl.h
@@ -103,6 +103,10 @@ int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type);
int sd_rtnl_message_close_container(sd_rtnl_message *m);
int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data);
+int sd_rtnl_message_read_string(sd_rtnl_message *m, unsigned short type, char **data);
+int sd_rtnl_message_read_u8(sd_rtnl_message *m, unsigned short type, uint8_t *data);
+int sd_rtnl_message_read_u16(sd_rtnl_message *m, unsigned short type, uint16_t *data);
+int sd_rtnl_message_read_u32(sd_rtnl_message *m, unsigned short type, uint32_t *data);
int sd_rtnl_message_exit_container(sd_rtnl_message *m);
int sd_rtnl_message_rewind(sd_rtnl_message *m);