chiark / gitweb /
build-sys: move more files from core/ to share/ that are generic enough
[elogind.git] / src / libsystemd-rtnl / rtnl-message.c
index 82ec1c97abaac50d0615fc6670d698a3a7c473df..1ce68626688d7ae7bc3e231bf1fd102bcf54dee3 100644 (file)
@@ -19,6 +19,7 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <linux/rtnetlink.h>
 #include <netinet/in.h>
 #include <netinet/ether.h>
 #include <stdbool.h>
@@ -45,13 +46,13 @@ static int message_new(sd_rtnl_message **ret, size_t initial_size) {
         sd_rtnl_message *m;
 
         assert_return(ret, -EINVAL);
-        assert_return(initial_size > 0, -EINVAL);
+        assert_return(initial_size >= sizeof(struct nlmsghdr), -EINVAL);
 
         m = new0(sd_rtnl_message, 1);
         if (!m)
                 return -ENOMEM;
 
-        m->hdr = calloc(initial_size, 1);
+        m->hdr = malloc0(initial_size);
         if (!m->hdr) {
                 free(m);
                 return -ENOMEM;
@@ -67,7 +68,64 @@ static int message_new(sd_rtnl_message **ret, size_t initial_size) {
         return 0;
 }
 
-int sd_rtnl_message_link_new(__u16 nlmsg_type, int index, unsigned int type, unsigned int flags, sd_rtnl_message **ret) {
+int message_new_synthetic_error(int error, uint32_t serial, sd_rtnl_message **ret) {
+        struct nlmsgerr *err;
+        int r;
+
+        assert(error <= 0);
+
+        r = message_new(ret, NLMSG_SPACE(sizeof(struct nlmsgerr)));
+        if (r < 0)
+                return r;
+
+        (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
+        (*ret)->hdr->nlmsg_type = NLMSG_ERROR;
+        (*ret)->hdr->nlmsg_seq = serial;
+
+        err = NLMSG_DATA((*ret)->hdr);
+
+        err->error = error;
+
+        return 0;
+}
+
+int sd_rtnl_message_route_new(uint16_t nlmsg_type, unsigned char rtm_family,
+                              unsigned char rtm_dst_len, unsigned char rtm_src_len,
+                              unsigned char rtm_tos, unsigned char rtm_table,
+                              unsigned char rtm_scope, unsigned char rtm_protocol,
+                              unsigned char rtm_type, unsigned rtm_flags, sd_rtnl_message **ret) {
+        struct rtmsg *rtm;
+        int r;
+
+        assert_return(nlmsg_type == RTM_NEWROUTE || nlmsg_type == RTM_DELROUTE ||
+                      nlmsg_type == RTM_GETROUTE, -EINVAL);
+        assert_return(ret, -EINVAL);
+
+        r = message_new(ret, NLMSG_SPACE(sizeof(struct rtmsg)));
+        if (r < 0)
+                return r;
+
+        (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
+        (*ret)->hdr->nlmsg_type = nlmsg_type;
+        if (nlmsg_type == RTM_NEWROUTE)
+                (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
+
+        rtm = NLMSG_DATA((*ret)->hdr);
+
+        rtm->rtm_family = rtm_family;
+        rtm->rtm_dst_len = rtm_dst_len;
+        rtm->rtm_src_len = rtm_src_len;
+        rtm->rtm_tos = rtm_tos;
+        rtm->rtm_table = rtm_table;
+        rtm->rtm_protocol = rtm_protocol;
+        rtm->rtm_scope = rtm_scope;
+        rtm->rtm_type = rtm_type;
+        rtm->rtm_flags = rtm_flags;
+
+        return 0;
+}
+
+int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, unsigned int type, unsigned int flags, sd_rtnl_message **ret) {
         struct ifinfomsg *ifi;
         int r;
 
@@ -82,7 +140,7 @@ int sd_rtnl_message_link_new(__u16 nlmsg_type, int index, unsigned int type, uns
         (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
         (*ret)->hdr->nlmsg_type = nlmsg_type;
 
-        ifi = (struct ifinfomsg *) NLMSG_DATA((*ret)->hdr);
+        ifi = NLMSG_DATA((*ret)->hdr);
 
         ifi->ifi_family = AF_UNSPEC;
         ifi->ifi_index = index;
@@ -93,7 +151,7 @@ int sd_rtnl_message_link_new(__u16 nlmsg_type, int index, unsigned int type, uns
         return 0;
 }
 
-int sd_rtnl_message_addr_new(__u16 nlmsg_type, int index, unsigned char family, unsigned char prefixlen, unsigned char flags, unsigned char scope, sd_rtnl_message **ret) {
+int sd_rtnl_message_addr_new(uint16_t nlmsg_type, int index, unsigned char family, unsigned char prefixlen, unsigned char flags, unsigned char scope, sd_rtnl_message **ret) {
         struct ifaddrmsg *ifa;
         int r;
 
@@ -108,7 +166,7 @@ int sd_rtnl_message_addr_new(__u16 nlmsg_type, int index, unsigned char family,
         (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
         (*ret)->hdr->nlmsg_type = nlmsg_type;
 
-        ifa = (struct ifaddrmsg *) NLMSG_DATA((*ret)->hdr);
+        ifa = NLMSG_DATA((*ret)->hdr);
 
         ifa->ifa_family = family;
         ifa->ifa_prefixlen = prefixlen;
@@ -135,7 +193,7 @@ sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m) {
         return NULL;
 }
 
-int sd_rtnl_message_get_type(sd_rtnl_message *m, __u16 *type) {
+int sd_rtnl_message_get_type(sd_rtnl_message *m, uint16_t *type) {
         assert_return(m, -EINVAL);
         assert_return(type, -EINVAL);
 
@@ -147,9 +205,10 @@ int sd_rtnl_message_get_type(sd_rtnl_message *m, __u16 *type) {
 /* If successful the updated message will be correctly aligned, if unsuccessful the old message is
    untouched */
 static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data, size_t data_length) {
-        __u32 rta_length, message_length;
+        uint32_t rta_length, message_length;
         struct nlmsghdr *new_hdr;
         struct rtattr *rta;
+        char *padding;
 
         assert_return(m, -EINVAL);
         assert_return(m->hdr, -EINVAL);
@@ -157,10 +216,9 @@ static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data,
         assert_return(data, -EINVAL);
         assert_return(data_length > 0, -EINVAL);
 
-        /* get the size of the new rta attribute (without padding at the end) */
+        /* get the size of the new rta attribute (with padding at the end) */
         rta_length = RTA_LENGTH(data_length);
-        /* get the new message size (with padding between the old message and the new attrib,
-         * but no padding after)
+        /* get the new message size (with padding at the end)
          */
         message_length = m->hdr->nlmsg_len + RTA_ALIGN(rta_length);
 
@@ -181,14 +239,17 @@ static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data,
         /* we don't deal with the case where the user lies about the type and gives us
          * too little data (so don't do that)
          */
-        memcpy(RTA_DATA(rta), data, data_length);
+        padding = mempcpy(RTA_DATA(rta), data, data_length);
+        /* make sure also the padding at the end of the message is initialized */
+        memset(padding, '\0', (unsigned char *) m->hdr + m->hdr->nlmsg_len - (unsigned char *) padding);
 
         return 0;
 }
 
 int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *data) {
-        __u16 rtm_type;
+        uint16_t rtm_type;
         struct ifaddrmsg *ifa;
+        struct rtmsg *rtm;
 
         assert_return(m, -EINVAL);
         assert_return(data, -EINVAL);
@@ -204,9 +265,9 @@ int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *
                                 case IFLA_QDISC:
                                         return add_rtattr(m, type, data, strlen(data) + 1);
                                 case IFLA_MTU:
-                                        return add_rtattr(m, type, data, sizeof(unsigned int));
+                                        return add_rtattr(m, type, data, sizeof(uint32_t));
                                 case IFLA_LINK:
-                                        return add_rtattr(m, type, data, sizeof(int));
+                                        return add_rtattr(m, type, data, sizeof(uint32_t));
                                 case IFLA_STATS:
                                         return add_rtattr(m, type, data, sizeof(struct rtnl_link_stats));
                                 case IFLA_ADDRESS:
@@ -237,6 +298,30 @@ int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *
                                 default:
                                         return -ENOTSUP;
                         }
+                case RTM_NEWROUTE:
+                case RTM_DELROUTE:
+                case RTM_GETROUTE:
+                        switch (type) {
+                                case RTA_DST:
+                                case RTA_SRC:
+                                case RTA_GATEWAY:
+                                        rtm = NLMSG_DATA(m->hdr);
+                                        switch (rtm->rtm_family) {
+                                                case AF_INET:
+                                                        return add_rtattr(m, type, data, sizeof(struct in_addr));
+                                                case AF_INET6:
+                                                        return add_rtattr(m, type, data, sizeof(struct in6_addr));
+                                                default:
+                                                        return -EINVAL;
+                                        }
+                                case RTA_TABLE:
+                                case RTA_PRIORITY:
+                                case RTA_IIF:
+                                case RTA_OIF:
+                                        return add_rtattr(m, type, data, sizeof(uint32_t));
+                                default:
+                                        return -ENOTSUP;
+                        }
                 default:
                         return -ENOTSUP;
         }
@@ -258,7 +343,7 @@ static int message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
 }
 
 int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
-        __u16 rtm_type;
+        uint16_t rtm_type;
 
         assert_return(m, -EINVAL);
         assert_return(data, -EINVAL);
@@ -270,22 +355,32 @@ int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data)
                 case RTM_DELLINK:
                 case RTM_GETLINK:
                         if (!m->next_rta) {
-                                struct ifinfomsg *ifi = (struct ifinfomsg *) NLMSG_DATA(m->hdr);
+                                struct ifinfomsg *ifi = NLMSG_DATA(m->hdr);
 
                                 m->next_rta = IFLA_RTA(ifi);
                                 m->remaining_size = IFLA_PAYLOAD(m->hdr);
                         }
-                        break;;
+                        break;
                 case RTM_NEWADDR:
                 case RTM_DELADDR:
                 case RTM_GETADDR:
                         if (!m->next_rta) {
-                                struct ifaddrmsg *ifa = (struct ifaddrmsg *) NLMSG_DATA(m->hdr);
+                                struct ifaddrmsg *ifa = NLMSG_DATA(m->hdr);
 
                                 m->next_rta = IFA_RTA(ifa);
-                                m->remaining_size = IFLA_PAYLOAD(m->hdr);
+                                m->remaining_size = IFA_PAYLOAD(m->hdr);
+                        }
+                        break;
+                case RTM_NEWROUTE:
+                case RTM_DELROUTE:
+                case RTM_GETROUTE:
+                        if (!m->next_rta) {
+                                struct rtmesg *rtm = NLMSG_DATA(m->hdr);
+
+                                m->next_rta = RTM_RTA(rtm);
+                                m->remaining_size = RTM_PAYLOAD(m->hdr);
                         }
-                        break;;
+                        break;
                 default:
                         return -ENOTSUP;
         }
@@ -293,16 +388,16 @@ int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data)
         return message_read(m, type, data);
 }
 
-int message_get_serial(sd_rtnl_message *m) {
+uint32_t message_get_serial(sd_rtnl_message *m) {
         assert(m);
 
         return m->hdr->nlmsg_seq;
 }
 
-int message_get_errno(sd_rtnl_message *m) {
+int sd_rtnl_message_get_errno(sd_rtnl_message *m) {
         struct nlmsgerr *err;
 
-        assert(m);
+        assert_return(m, -EINVAL);
 
         if (m->hdr->nlmsg_type != NLMSG_ERROR)
                 return 0;
@@ -327,15 +422,15 @@ static int message_receive_need(sd_rtnl *rtnl, size_t *need) {
         assert_return(need, -EINVAL);
 
         /* ioctl(rtnl->fd, FIONREAD, &need)
-        Does not appear to work on netlink sockets. libnl uses
-        MSG_PEEK instead. I don't know if that is worth the
-        extra roundtrip.
-
-        For now we simply use the maximum message size the kernel
-        may use (NLMSG_GOODSIZE), and then realloc to the actual
-        size after reading the message (hence avoiding huge memory
-        usage in case many small messages are kept around) */
-        *need = getpagesize();
+           Does not appear to work on netlink sockets. libnl uses
+           MSG_PEEK instead. I don't know if that is worth the
+           extra roundtrip.
+
+           For now we simply use the maximum message size the kernel
+           may use (NLMSG_GOODSIZE), and then realloc to the actual
+           size after reading the message (hence avoiding huge memory
+           usage in case many small messages are kept around) */
+        *need = page_size();
         if (*need > 8192UL)
                 *need = 8192UL;
 
@@ -344,13 +439,19 @@ static int message_receive_need(sd_rtnl *rtnl, size_t *need) {
 
 /* returns the number of bytes sent, or a negative error code */
 int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m) {
+        union {
+                struct sockaddr sa;
+                struct sockaddr_nl nl;
+        } addr = {
+                .nl.nl_family = AF_NETLINK,
+        };
         ssize_t k;
 
         assert_return(nl, -EINVAL);
         assert_return(m, -EINVAL);
 
         k = sendto(nl->fd, m->hdr, m->hdr->nlmsg_len,
-                        0, &nl->sockaddr.sa, sizeof(nl->sockaddr));
+                        0, &addr.sa, sizeof(addr));
         if (k < 0)
                 return (errno == EAGAIN) ? 0 : -errno;
 
@@ -361,10 +462,14 @@ int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m) {
  * which has a valid header and the correct size.
  * If nothing useful was received 0 is returned.
  * On failure, a negative error code is returned.
-*/
+ */
 int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
         sd_rtnl_message *m;
-        socklen_t addr_len = sizeof(nl->sockaddr);
+        union {
+                struct sockaddr sa;
+                struct sockaddr_nl nl;
+        } addr;
+        socklen_t addr_len;
         int r;
         ssize_t k;
         size_t need;
@@ -380,34 +485,23 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
         if (r < 0)
                 return r;
 
-        r = message_receive_need(nl, &need);
-        if (r < 0)
-                return r;
-
-        m->hdr = realloc(m->hdr, need);
-        if (!m->hdr)
-                return -ENOMEM;
+        addr_len = sizeof(addr);
 
         k = recvfrom(nl->fd, m->hdr, need,
-                        0, &nl->sockaddr.sa, &addr_len);
+                        0, &addr.sa, &addr_len);
         if (k < 0)
-                k = (errno == EAGAIN) ? 0 : -errno; /* no data? weird... */
+                k = (errno == EAGAIN) ? 0 : -errno; /* no data */
         else if (k == 0)
                 k = -ECONNRESET; /* connection was closed by the kernel */
-        else if (addr_len != sizeof(nl->sockaddr.nl) ||
-                        nl->sockaddr.nl.nl_family != AF_NETLINK)
+        else if (addr_len != sizeof(addr.nl) ||
+                        addr.nl.nl_family != AF_NETLINK)
                 k = -EIO; /* not a netlink message */
-        else if (nl->sockaddr.nl.nl_pid != 0)
+        else if (addr.nl.nl_pid != 0)
                 k = 0; /* not from the kernel */
         else if ((size_t) k < sizeof(struct nlmsghdr) ||
-                 (size_t) k < m->hdr->nlmsg_len)
+                        (size_t) k < m->hdr->nlmsg_len)
                 k = -EIO; /* too small (we do accept too big though) */
-        else if (m->hdr->nlmsg_type == NLMSG_NOOP)
-                k = 0;
-        else if (m->hdr->nlmsg_type == NLMSG_ERROR &&
-                 m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
-                k = -EIO;
-        else if ((pid_t) m->hdr->nlmsg_pid != getpid())
+        else if (m->hdr->nlmsg_pid != nl->sockaddr.nl.nl_pid)
                 k = 0; /* not for us */
 
         if (k > 0)
@@ -416,22 +510,22 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
                         case NLMSG_ERROR:
                                 if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
                                         k = -EIO;
-                                break;;
+                                break;
                         case RTM_NEWLINK:
                         case RTM_DELLINK:
                         case RTM_GETLINK:
                                 if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg)))
                                         k = -EIO;
-                                break;;
+                                break;
                         case RTM_NEWADDR:
                         case RTM_DELADDR:
                         case RTM_GETADDR:
                                 if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifaddrmsg)))
                                         k = -EIO;
-                                break;;
+                                break;
                         case NLMSG_NOOP:
                                 k = 0;
-                                break;;
+                                break;
                         default:
                                 k = 0; /* ignoring message of unknown type */
                 }