chiark / gitweb /
rtnl: support adding VETH_INFO_PEER containers into rtnl messages
[elogind.git] / src / libsystemd / sd-rtnl / rtnl-message.c
index 87ad682349e81837bfa108ccab2f3a7eb524e73a..c780f71881abf407b447ab2ac30ec916fa363091 100644 (file)
@@ -24,6 +24,7 @@
 #include <netinet/ether.h>
 #include <stdbool.h>
 #include <unistd.h>
+#include <linux/veth.h>
 
 #include "util.h"
 #include "refcnt.h"
@@ -32,7 +33,7 @@
 #include "rtnl-util.h"
 #include "rtnl-internal.h"
 
-#define GET_CONTAINER(m, i) (i < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->container_offsets[i]) : NULL)
+#define GET_CONTAINER(m, i) ((i) < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->container_offsets[i]) : NULL)
 #define NEXT_RTA(m) ((struct rtattr*)((uint8_t*)(m)->hdr + (m)->next_rta_offset))
 #define UPDATE_RTA(m, new) (m)->next_rta_offset = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
 #define PUSH_CONTAINER(m, new) (m)->container_offsets[(m)->n_containers ++] = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
@@ -81,7 +82,7 @@ int sd_rtnl_message_route_set_dst_prefixlen(sd_rtnl_message *m, unsigned char pr
         return 0;
 }
 
-int sd_rtnl_message_route_new(uint16_t nlmsg_type, unsigned char rtm_family,
+int sd_rtnl_message_new_route(uint16_t nlmsg_type, unsigned char rtm_family,
                               sd_rtnl_message **ret) {
         struct rtmsg *rtm;
         int r;
@@ -144,7 +145,7 @@ int sd_rtnl_message_link_set_type(sd_rtnl_message *m, unsigned type) {
         return 0;
 }
 
-int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, sd_rtnl_message **ret) {
+int sd_rtnl_message_new_link(uint16_t nlmsg_type, int index, sd_rtnl_message **ret) {
         struct ifinfomsg *ifi;
         int r;
 
@@ -217,7 +218,7 @@ int sd_rtnl_message_addr_set_scope(sd_rtnl_message *m, unsigned char scope) {
         return 0;
 }
 
-int sd_rtnl_message_addr_new(uint16_t nlmsg_type, int index, unsigned char family,
+int sd_rtnl_message_new_addr(uint16_t nlmsg_type, int index, unsigned char family,
                              sd_rtnl_message **ret) {
         struct ifaddrmsg *ifa;
         int r;
@@ -410,6 +411,44 @@ int sd_rtnl_message_append_string(sd_rtnl_message *m, unsigned short type, const
         return 0;
 }
 
+int sd_rtnl_message_append_u8(sd_rtnl_message *m, unsigned short type, uint8_t data) {
+        uint16_t rtm_type;
+        int r;
+
+        assert_return(m, -EINVAL);
+        assert_return(!m->sealed, -EPERM);
+
+        r = sd_rtnl_message_get_type(m, &rtm_type);
+        if (r < 0)
+                return r;
+
+        switch (rtm_type) {
+                case RTM_NEWLINK:
+                case RTM_SETLINK:
+                case RTM_GETLINK:
+                case RTM_DELLINK:
+                        switch (type) {
+                                case IFLA_CARRIER:
+                                case IFLA_OPERSTATE:
+                                case IFLA_LINKMODE:
+                                break;
+                        default:
+                                return -ENOTSUP;
+                        }
+
+                        break;
+                default:
+                        return -ENOTSUP;
+        }
+
+        r = add_rtattr(m, type, &data, sizeof(uint8_t));
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
+
 int sd_rtnl_message_append_u16(sd_rtnl_message *m, unsigned short type, uint16_t data) {
         uint16_t rtm_type;
         int r;
@@ -467,6 +506,14 @@ int sd_rtnl_message_append_u32(sd_rtnl_message *m, unsigned short type, uint32_t
                                 case IFLA_MASTER:
                                 case IFLA_MTU:
                                 case IFLA_LINK:
+                                case IFLA_GROUP:
+                                case IFLA_TXQLEN:
+                                case IFLA_WEIGHT:
+                                case IFLA_NET_NS_FD:
+                                case IFLA_NET_NS_PID:
+                                case IFLA_PROMISCUITY:
+                                case IFLA_NUM_TX_QUEUES:
+                                case IFLA_NUM_RX_QUEUES:
                                         break;
                                 default:
                                         return -ENOTSUP;
@@ -480,6 +527,7 @@ int sd_rtnl_message_append_u32(sd_rtnl_message *m, unsigned short type, uint32_t
                                 case RTA_PRIORITY:
                                 case RTA_IIF:
                                 case RTA_OIF:
+                                case RTA_MARK:
                                         break;
                                 default:
                                         return -ENOTSUP;
@@ -653,7 +701,7 @@ int sd_rtnl_message_append_ether_addr(sd_rtnl_message *m, unsigned short type, c
         return 0;
 }
 
-int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type) {
+int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type, size_t extra) {
         uint16_t rtm_type;
 
         assert_return(m, -EINVAL);
@@ -662,16 +710,14 @@ int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type) {
         sd_rtnl_message_get_type(m, &rtm_type);
 
         if (rtnl_message_type_is_link(rtm_type)) {
+
                 if ((type == IFLA_LINKINFO && m->n_containers == 0) ||
-                    (type == IFLA_INFO_DATA && m->n_containers == 1 &&
-                     GET_CONTAINER(m, 0)->rta_type == IFLA_LINKINFO))
-                        return add_rtattr(m, type, NULL, 0);
-                else
-                        return -ENOTSUP;
-        } else
-                return -ENOTSUP;
+                    (type == IFLA_INFO_DATA && m->n_containers == 1 && GET_CONTAINER(m, 0)->rta_type == IFLA_LINKINFO) ||
+                    (type == VETH_INFO_PEER && m->n_containers == 2 && GET_CONTAINER(m, 1)->rta_type == IFLA_INFO_DATA))
+                        return add_rtattr(m, type, NULL, extra);
+        }
 
-        return 0;
+        return -ENOTSUP;
 }
 
 int sd_rtnl_message_close_container(sd_rtnl_message *m) {