1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <netinet/in.h>
23 #include <netinet/ether.h>
26 #include <linux/veth.h>
33 #include "rtnl-util.h"
34 #include "rtnl-internal.h"
36 #define GET_CONTAINER(m, i) ((i) < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->container_offsets[i]) : NULL)
37 #define NEXT_RTA(m) ((struct rtattr*)((uint8_t*)(m)->hdr + (m)->next_rta_offset))
38 #define UPDATE_RTA(m, new) (m)->next_rta_offset = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
39 #define PUSH_CONTAINER(m, new) (m)->container_offsets[(m)->n_containers ++] = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
41 int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, size_t initial_size) {
44 assert_return(ret, -EINVAL);
45 assert_return(initial_size >= sizeof(struct nlmsghdr), -EINVAL);
47 m = new0(sd_rtnl_message, 1);
51 m->hdr = malloc0(initial_size);
57 m->n_ref = REFCNT_INIT;
59 m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
63 m->rtnl = sd_rtnl_ref(rtnl);
70 int sd_rtnl_message_route_set_dst_prefixlen(sd_rtnl_message *m, unsigned char prefixlen) {
73 assert_return(m, -EINVAL);
74 assert_return(m->hdr, -EINVAL);
75 assert_return(rtnl_message_type_is_route(m->hdr->nlmsg_type), -EINVAL);
77 rtm = NLMSG_DATA(m->hdr);
79 if ((rtm->rtm_family == AF_INET && prefixlen > 32) ||
80 (rtm->rtm_family == AF_INET6 && prefixlen > 128))
83 rtm->rtm_dst_len = prefixlen;
88 int sd_rtnl_message_new_route(sd_rtnl *rtnl, sd_rtnl_message **ret,
89 uint16_t nlmsg_type, unsigned char rtm_family) {
93 assert_return(rtnl_message_type_is_route(nlmsg_type), -EINVAL);
94 assert_return(rtm_family == AF_INET || rtm_family == AF_INET6, -EINVAL);
95 assert_return(ret, -EINVAL);
97 r = message_new(rtnl, ret, NLMSG_SPACE(sizeof(struct rtmsg)));
101 (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
102 (*ret)->hdr->nlmsg_type = nlmsg_type;
103 if (nlmsg_type == RTM_NEWROUTE)
104 (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
106 rtm = NLMSG_DATA((*ret)->hdr);
108 UPDATE_RTA(*ret, RTM_RTA(rtm));
110 rtm->rtm_family = rtm_family;
111 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
112 rtm->rtm_type = RTN_UNICAST;
113 rtm->rtm_table = RT_TABLE_MAIN;
114 rtm->rtm_protocol = RTPROT_BOOT;
119 int sd_rtnl_message_link_set_flags(sd_rtnl_message *m, unsigned flags, unsigned change) {
120 struct ifinfomsg *ifi;
122 assert_return(m, -EINVAL);
123 assert_return(m->hdr, -EINVAL);
124 assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
125 assert_return(change, -EINVAL);
127 ifi = NLMSG_DATA(m->hdr);
129 ifi->ifi_flags = flags;
130 ifi->ifi_change = change;
135 int sd_rtnl_message_link_set_type(sd_rtnl_message *m, unsigned type) {
136 struct ifinfomsg *ifi;
138 assert_return(m, -EINVAL);
139 assert_return(m->hdr, -EINVAL);
140 assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
142 ifi = NLMSG_DATA(m->hdr);
144 ifi->ifi_type = type;
149 int sd_rtnl_message_new_link(sd_rtnl *rtnl, sd_rtnl_message **ret,
150 uint16_t nlmsg_type, int index) {
151 struct ifinfomsg *ifi;
154 assert_return(rtnl_message_type_is_link(nlmsg_type), -EINVAL);
155 assert_return(nlmsg_type == RTM_NEWLINK ||
156 nlmsg_type == RTM_SETLINK || index > 0, -EINVAL);
157 assert_return(ret, -EINVAL);
159 r = message_new(rtnl, ret, NLMSG_SPACE(sizeof(struct ifinfomsg)));
163 (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
164 (*ret)->hdr->nlmsg_type = nlmsg_type;
165 if (nlmsg_type == RTM_NEWLINK)
166 (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE;
168 ifi = NLMSG_DATA((*ret)->hdr);
170 ifi->ifi_family = AF_UNSPEC;
171 ifi->ifi_index = index;
173 UPDATE_RTA(*ret, IFLA_RTA(ifi));
178 int sd_rtnl_message_addr_set_prefixlen(sd_rtnl_message *m, unsigned char prefixlen) {
179 struct ifaddrmsg *ifa;
181 assert_return(m, -EINVAL);
182 assert_return(m->hdr, -EINVAL);
183 assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
185 ifa = NLMSG_DATA(m->hdr);
187 if ((ifa->ifa_family == AF_INET && prefixlen > 32) ||
188 (ifa->ifa_family == AF_INET6 && prefixlen > 128))
191 ifa->ifa_prefixlen = prefixlen;
196 int sd_rtnl_message_addr_set_flags(sd_rtnl_message *m, unsigned char flags) {
197 struct ifaddrmsg *ifa;
199 assert_return(m, -EINVAL);
200 assert_return(m->hdr, -EINVAL);
201 assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
203 ifa = NLMSG_DATA(m->hdr);
205 ifa->ifa_flags = flags;
210 int sd_rtnl_message_addr_set_scope(sd_rtnl_message *m, unsigned char scope) {
211 struct ifaddrmsg *ifa;
213 assert_return(m, -EINVAL);
214 assert_return(m->hdr, -EINVAL);
215 assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
217 ifa = NLMSG_DATA(m->hdr);
219 ifa->ifa_scope = scope;
224 int sd_rtnl_message_new_addr(sd_rtnl *rtnl, sd_rtnl_message **ret,
225 uint16_t nlmsg_type, int index,
226 unsigned char family) {
227 struct ifaddrmsg *ifa;
230 assert_return(rtnl_message_type_is_addr(nlmsg_type), -EINVAL);
231 assert_return(index > 0, -EINVAL);
232 assert_return(family == AF_INET || family == AF_INET6, -EINVAL);
233 assert_return(ret, -EINVAL);
235 r = message_new(rtnl, ret, NLMSG_SPACE(sizeof(struct ifaddrmsg)));
239 (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
240 (*ret)->hdr->nlmsg_type = nlmsg_type;
241 if (nlmsg_type == RTM_GETADDR && family == AF_INET)
242 (*ret)->hdr->nlmsg_flags |= NLM_F_DUMP;
244 ifa = NLMSG_DATA((*ret)->hdr);
246 ifa->ifa_index = index;
247 ifa->ifa_family = family;
248 if (family == AF_INET)
249 ifa->ifa_prefixlen = 32;
250 else if (family == AF_INET6)
251 ifa->ifa_prefixlen = 128;
253 UPDATE_RTA(*ret, IFA_RTA(ifa));
258 sd_rtnl_message *sd_rtnl_message_ref(sd_rtnl_message *m) {
260 assert_se(REFCNT_INC(m->n_ref) >= 2);
265 sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m) {
266 if (m && REFCNT_DEC(m->n_ref) <= 0) {
267 sd_rtnl_unref(m->rtnl);
275 int sd_rtnl_message_get_type(sd_rtnl_message *m, uint16_t *type) {
276 assert_return(m, -EINVAL);
277 assert_return(type, -EINVAL);
279 *type = m->hdr->nlmsg_type;
284 int sd_rtnl_message_link_get_ifindex(sd_rtnl_message *m, int *ifindex) {
285 struct ifinfomsg *ifi;
287 assert_return(m, -EINVAL);
288 assert_return(m->hdr, -EINVAL);
289 assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
290 assert_return(ifindex, -EINVAL);
292 ifi = NLMSG_DATA(m->hdr);
294 *ifindex = ifi->ifi_index;
299 int sd_rtnl_message_link_get_flags(sd_rtnl_message *m, unsigned *flags) {
300 struct ifinfomsg *ifi;
302 assert_return(m, -EINVAL);
303 assert_return(m->hdr, -EINVAL);
304 assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
305 assert_return(flags, -EINVAL);
307 ifi = NLMSG_DATA(m->hdr);
309 *flags = ifi->ifi_flags;
314 /* If successful the updated message will be correctly aligned, if
315 unsuccessful the old message is untouched. */
316 static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data, size_t data_length) {
317 uint32_t rta_length, message_length;
318 struct nlmsghdr *new_hdr;
326 assert(NLMSG_ALIGN(m->hdr->nlmsg_len) == m->hdr->nlmsg_len);
327 assert(!data || data_length > 0);
328 assert(data || m->n_containers < RTNL_CONTAINER_DEPTH);
330 /* get the size of the new rta attribute (with padding at the end) */
331 rta_length = RTA_LENGTH(data_length);
333 /* get the new message size (with padding at the end) */
334 message_length = m->hdr->nlmsg_len + RTA_ALIGN(rta_length);
336 /* realloc to fit the new attribute */
337 new_hdr = realloc(m->hdr, message_length);
342 /* get pointer to the attribute we are about to add */
343 rta = (struct rtattr *) ((uint8_t *) m->hdr + m->hdr->nlmsg_len);
345 /* if we are inside containers, extend them */
346 for (i = 0; i < m->n_containers; i++)
347 GET_CONTAINER(m, i)->rta_len += message_length - m->hdr->nlmsg_len;
349 /* fill in the attribute */
350 rta->rta_type = type;
351 rta->rta_len = rta_length;
353 /* this is the start of a new container */
354 m->container_offsets[m->n_containers ++] = m->hdr->nlmsg_len;
356 /* we don't deal with the case where the user lies about the type
357 * and gives us too little data (so don't do that)
359 padding = mempcpy(RTA_DATA(rta), data, data_length);
360 /* make sure also the padding at the end of the message is initialized */
362 (uint8_t *) m->hdr + message_length - (uint8_t *) padding);
365 /* update message size */
366 m->hdr->nlmsg_len = message_length;
371 int sd_rtnl_message_append_string(sd_rtnl_message *m, unsigned short type, const char *data) {
375 assert_return(m, -EINVAL);
376 assert_return(!m->sealed, -EPERM);
377 assert_return(data, -EINVAL);
379 r = sd_rtnl_message_get_type(m, &rtm_type);
383 /* check that the type is correct */
389 if (m->n_containers == 1) {
390 if (GET_CONTAINER(m, 0)->rta_type != IFLA_LINKINFO ||
391 type != IFLA_INFO_KIND)
407 if (type != IFA_LABEL)
414 r = add_rtattr(m, type, data, strlen(data) + 1);
421 int sd_rtnl_message_append_u8(sd_rtnl_message *m, unsigned short type, uint8_t data) {
425 assert_return(m, -EINVAL);
426 assert_return(!m->sealed, -EPERM);
428 r = sd_rtnl_message_get_type(m, &rtm_type);
451 r = add_rtattr(m, type, &data, sizeof(uint8_t));
459 int sd_rtnl_message_append_u16(sd_rtnl_message *m, unsigned short type, uint16_t data) {
463 assert_return(m, -EINVAL);
464 assert_return(!m->sealed, -EPERM);
466 r = sd_rtnl_message_get_type(m, &rtm_type);
470 /* check that the type is correct */
476 if (m->n_containers == 2 &&
477 GET_CONTAINER(m, 0)->rta_type == IFLA_LINKINFO &&
478 GET_CONTAINER(m, 1)->rta_type == IFLA_INFO_DATA &&
479 type == IFLA_VLAN_ID)
488 r = add_rtattr(m, type, &data, sizeof(uint16_t));
495 int sd_rtnl_message_append_u32(sd_rtnl_message *m, unsigned short type, uint32_t data) {
499 assert_return(m, -EINVAL);
500 assert_return(!m->sealed, -EPERM);
502 r = sd_rtnl_message_get_type(m, &rtm_type);
506 /* check that the type is correct */
520 case IFLA_NET_NS_PID:
521 case IFLA_PROMISCUITY:
522 case IFLA_NUM_TX_QUEUES:
523 case IFLA_NUM_RX_QUEUES:
524 case IFLA_MACVLAN_MODE:
548 r = add_rtattr(m, type, &data, sizeof(uint32_t));
555 int sd_rtnl_message_append_in_addr(sd_rtnl_message *m, unsigned short type, const struct in_addr *data) {
556 struct ifaddrmsg *ifa;
561 assert_return(m, -EINVAL);
562 assert_return(!m->sealed, -EPERM);
563 assert_return(data, -EINVAL);
565 r = sd_rtnl_message_get_type(m, &rtm_type);
569 /* check that the type is correct */
579 ifa = NLMSG_DATA(m->hdr);
581 if (ifa->ifa_family != AF_INET)
596 rtm = NLMSG_DATA(m->hdr);
598 if (rtm->rtm_family != AF_INET)
610 r = add_rtattr(m, type, data, sizeof(struct in_addr));
617 int sd_rtnl_message_append_in6_addr(sd_rtnl_message *m, unsigned short type, const struct in6_addr *data) {
618 struct ifaddrmsg *ifa;
623 assert_return(m, -EINVAL);
624 assert_return(!m->sealed, -EPERM);
625 assert_return(data, -EINVAL);
627 r = sd_rtnl_message_get_type(m, &rtm_type);
631 /* check that the type is correct */
641 ifa = NLMSG_DATA(m->hdr);
643 if (ifa->ifa_family != AF_INET6)
658 rtm = NLMSG_DATA(m->hdr);
660 if (rtm->rtm_family != AF_INET6)
671 r = add_rtattr(m, type, data, sizeof(struct in6_addr));
678 int sd_rtnl_message_append_ether_addr(sd_rtnl_message *m, unsigned short type, const struct ether_addr *data) {
682 assert_return(m, -EINVAL);
683 assert_return(!m->sealed, -EPERM);
684 assert_return(data, -EINVAL);
686 sd_rtnl_message_get_type(m, &rtm_type);
705 r = add_rtattr(m, type, data, ETH_ALEN);
712 int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type) {
715 assert_return(m, -EINVAL);
716 assert_return(!m->sealed, -EPERM);
718 sd_rtnl_message_get_type(m, &rtm_type);
720 if (rtnl_message_type_is_link(rtm_type)) {
722 if ((type == IFLA_LINKINFO && m->n_containers == 0) ||
723 (type == IFLA_INFO_DATA && m->n_containers == 1 &&
724 GET_CONTAINER(m, 0)->rta_type == IFLA_LINKINFO))
725 return add_rtattr(m, type, NULL, 0);
726 else if (type == VETH_INFO_PEER && m->n_containers == 2 &&
727 GET_CONTAINER(m, 1)->rta_type == IFLA_INFO_DATA &&
728 GET_CONTAINER(m, 0)->rta_type == IFLA_LINKINFO)
729 return add_rtattr(m, type, NULL, sizeof(struct ifinfomsg));
735 int sd_rtnl_message_close_container(sd_rtnl_message *m) {
736 assert_return(m, -EINVAL);
737 assert_return(!m->sealed, -EPERM);
738 assert_return(m->n_containers > 0, -EINVAL);
745 int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
746 size_t remaining_size;
750 assert_return(m, -EINVAL);
751 assert_return(m->sealed, -EPERM);
752 assert_return(m->next_rta_offset, -EINVAL);
753 assert_return(type, -EINVAL);
754 assert_return(data, -EINVAL);
756 /* only read until the end of the current container */
758 remaining_size = GET_CONTAINER(m, m->n_containers - 1)->rta_len -
759 (m->next_rta_offset -
760 m->container_offsets[m->n_containers - 1]);
762 remaining_size = m->hdr->nlmsg_len - m->next_rta_offset;
764 if (!RTA_OK(NEXT_RTA(m), remaining_size))
767 /* if we read a container, enter it and return its type */
768 r = sd_rtnl_message_get_type(m, &rtm_type);
772 *type = NEXT_RTA(m)->rta_type;
774 if (rtnl_message_type_is_link(rtm_type) &&
775 ((m->n_containers == 0 &&
776 NEXT_RTA(m)->rta_type == IFLA_LINKINFO) ||
777 (m->n_containers == 1 &&
778 GET_CONTAINER(m, 0)->rta_type == IFLA_LINKINFO &&
779 NEXT_RTA(m)->rta_type == IFLA_INFO_DATA))) {
781 PUSH_CONTAINER(m, NEXT_RTA(m));
782 UPDATE_RTA(m, RTA_DATA(NEXT_RTA(m)));
784 *data = RTA_DATA(NEXT_RTA(m));
785 UPDATE_RTA(m, RTA_NEXT(NEXT_RTA(m), remaining_size));
791 int sd_rtnl_message_exit_container(sd_rtnl_message *m) {
792 assert_return(m, -EINVAL);
793 assert_return(m->sealed, -EINVAL);
794 assert_return(m->n_containers > 0, -EINVAL);
801 uint32_t rtnl_message_get_serial(sd_rtnl_message *m) {
805 return m->hdr->nlmsg_seq;
808 int sd_rtnl_message_get_errno(sd_rtnl_message *m) {
809 struct nlmsgerr *err;
811 assert_return(m, -EINVAL);
812 assert_return(m->hdr, -EINVAL);
814 if (m->hdr->nlmsg_type != NLMSG_ERROR)
817 err = NLMSG_DATA(m->hdr);
822 int rtnl_message_seal(sd_rtnl *nl, sd_rtnl_message *m) {
832 m->hdr->nlmsg_seq = nl->serial++;
836 r = sd_rtnl_message_rewind(m);
843 static int message_receive_need(sd_rtnl *rtnl, size_t *need) {
847 /* ioctl(rtnl->fd, FIONREAD, &need)
848 Does not appear to work on netlink sockets. libnl uses
849 MSG_PEEK instead. I don't know if that is worth the
852 For now we simply use the maximum message size the kernel
853 may use (NLMSG_GOODSIZE), and then realloc to the actual
854 size after reading the message (hence avoiding huge memory
855 usage in case many small messages are kept around) */
863 /* returns the number of bytes sent, or a negative error code */
864 int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m) {
867 struct sockaddr_nl nl;
869 .nl.nl_family = AF_NETLINK,
877 k = sendto(nl->fd, m->hdr, m->hdr->nlmsg_len,
878 0, &addr.sa, sizeof(addr));
880 return (errno == EAGAIN) ? 0 : -errno;
885 /* On success, the number of bytes received is returned and *ret points to the received message
886 * which has a valid header and the correct size.
887 * If nothing useful was received 0 is returned.
888 * On failure, a negative error code is returned.
890 int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
894 struct sockaddr_nl nl;
904 r = message_receive_need(nl, &need);
908 r = message_new(nl, &m, need);
912 /* don't allow sealing/appending to received messages */
915 addr_len = sizeof(addr);
917 k = recvfrom(nl->fd, m->hdr, need,
918 0, &addr.sa, &addr_len);
920 k = (errno == EAGAIN) ? 0 : -errno; /* no data */
922 k = -ECONNRESET; /* connection was closed by the kernel */
923 else if (addr_len != sizeof(addr.nl) ||
924 addr.nl.nl_family != AF_NETLINK)
925 k = -EIO; /* not a netlink message */
926 else if (addr.nl.nl_pid != 0)
927 k = 0; /* not from the kernel */
928 else if ((size_t) k < sizeof(struct nlmsghdr) ||
929 (size_t) k < m->hdr->nlmsg_len)
930 k = -EIO; /* too small (we do accept too big though) */
931 else if (m->hdr->nlmsg_pid && m->hdr->nlmsg_pid != nl->sockaddr.nl.nl_pid)
932 k = 0; /* not broadcast and not for us */
935 switch (m->hdr->nlmsg_type) {
936 /* check that the size matches the message type */
938 if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
945 if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg)))
948 struct ifinfomsg *ifi;
950 ifi = NLMSG_DATA(m->hdr);
951 UPDATE_RTA(m, IFLA_RTA(ifi));
957 if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifaddrmsg)))
960 struct ifaddrmsg *ifa;
962 ifa = NLMSG_DATA(m->hdr);
963 UPDATE_RTA(m, IFA_RTA(ifa));
969 if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtmsg)))
974 rtm = NLMSG_DATA(m->hdr);
975 UPDATE_RTA(m, RTM_RTA(rtm));
982 k = 0; /* ignoring message of unknown type */
986 sd_rtnl_message_unref(m);
988 /* we probably allocated way too much memory, give it back */
989 m->hdr = realloc(m->hdr, m->hdr->nlmsg_len);
996 int sd_rtnl_message_rewind(sd_rtnl_message *m) {
997 struct ifinfomsg *ifi;
998 struct ifaddrmsg *ifa;
1001 assert_return(m, -EINVAL);
1002 assert_return(m->sealed, -EPERM);
1003 assert_return(m->hdr, -EINVAL);
1005 switch(m->hdr->nlmsg_type) {
1010 ifi = NLMSG_DATA(m->hdr);
1011 UPDATE_RTA(m, IFLA_RTA(ifi));
1017 ifa = NLMSG_DATA(m->hdr);
1018 UPDATE_RTA(m, IFA_RTA(ifa));
1024 rtm = NLMSG_DATA(m->hdr);
1025 UPDATE_RTA(m, RTM_RTA(rtm));
1032 m->n_containers = 0;