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>
27 #include "socket-util.h"
28 #include "formats-util.h"
32 #include "sd-netlink.h"
33 #include "netlink-util.h"
34 #include "netlink-internal.h"
35 #include "netlink-types.h"
37 #define GET_CONTAINER(m, i) ((i) < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->containers[i].offset) : NULL)
38 #define PUSH_CONTAINER(m, new) (m)->container_offsets[(m)->n_containers ++] = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
40 #define RTA_TYPE(rta) ((rta)->rta_type & NLA_TYPE_MASK)
42 int message_new_empty(sd_netlink *rtnl, sd_netlink_message **ret) {
43 sd_netlink_message *m;
45 assert_return(ret, -EINVAL);
47 /* Note that 'rtnl' is currently unused, if we start using it internally
48 we must take care to avoid problems due to mutual references between
49 buses and their queued messages. See sd-bus.
52 m = new0(sd_netlink_message, 1);
56 m->n_ref = REFCNT_INIT;
65 int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) {
66 _cleanup_netlink_message_unref_ sd_netlink_message *m = NULL;
67 const NLType *nl_type;
71 r = type_system_get_type(&type_system_root, &nl_type, type);
75 if (type_get_type(nl_type) != NETLINK_TYPE_NESTED)
78 r = message_new_empty(rtnl, &m);
82 size = NLMSG_SPACE(type_get_size(nl_type));
84 assert(size >= sizeof(struct nlmsghdr));
85 m->hdr = malloc0(size);
89 m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
91 type_get_type_system(nl_type, &m->containers[0].type_system);
92 m->hdr->nlmsg_len = size;
93 m->hdr->nlmsg_type = type;
101 int sd_netlink_message_request_dump(sd_netlink_message *m, int dump) {
102 assert_return(m, -EINVAL);
103 assert_return(m->hdr, -EINVAL);
104 assert_return(m->hdr->nlmsg_type == RTM_GETLINK ||
105 m->hdr->nlmsg_type == RTM_GETADDR ||
106 m->hdr->nlmsg_type == RTM_GETROUTE ||
107 m->hdr->nlmsg_type == RTM_GETNEIGH,
111 m->hdr->nlmsg_flags |= NLM_F_DUMP;
113 m->hdr->nlmsg_flags &= ~NLM_F_DUMP;
118 sd_netlink_message *sd_netlink_message_ref(sd_netlink_message *m) {
120 assert_se(REFCNT_INC(m->n_ref) >= 2);
125 sd_netlink_message *sd_netlink_message_unref(sd_netlink_message *m) {
126 if (m && REFCNT_DEC(m->n_ref) == 0) {
131 for (i = 0; i <= m->n_containers; i++)
132 free(m->containers[i].attributes);
134 sd_netlink_message_unref(m->next);
142 int sd_netlink_message_get_type(sd_netlink_message *m, uint16_t *type) {
143 assert_return(m, -EINVAL);
144 assert_return(type, -EINVAL);
146 *type = m->hdr->nlmsg_type;
151 int sd_netlink_message_is_broadcast(sd_netlink_message *m) {
152 assert_return(m, -EINVAL);
157 /* If successful the updated message will be correctly aligned, if
158 unsuccessful the old message is untouched. */
159 static int add_rtattr(sd_netlink_message *m, unsigned short type, const void *data, size_t data_length) {
161 size_t message_length, padding_length;
162 struct nlmsghdr *new_hdr;
171 assert(NLMSG_ALIGN(m->hdr->nlmsg_len) == m->hdr->nlmsg_len);
172 assert(!data || data_length);
174 /* get offset of the new attribute */
175 offset = m->hdr->nlmsg_len;
177 /* get the size of the new rta attribute (with padding at the end) */
178 rta_length = RTA_LENGTH(data_length);
180 /* get the new message size (with padding at the end) */
181 message_length = offset + RTA_ALIGN(rta_length);
183 /* realloc to fit the new attribute */
184 new_hdr = realloc(m->hdr, message_length);
189 /* get pointer to the attribute we are about to add */
190 rta = (struct rtattr *) ((uint8_t *) m->hdr + offset);
192 /* if we are inside containers, extend them */
193 for (i = 0; i < m->n_containers; i++)
194 GET_CONTAINER(m, i)->rta_len += message_length - offset;
196 /* fill in the attribute */
197 rta->rta_type = type;
198 rta->rta_len = rta_length;
200 /* we don't deal with the case where the user lies about the type
201 * and gives us too little data (so don't do that)
203 padding = mempcpy(RTA_DATA(rta), data, data_length);
205 /* if no data was passed, make sure we still initialize the padding
206 note that we can have data_length > 0 (used by some containers) */
207 padding = RTA_DATA(rta);
210 /* make sure also the padding at the end of the message is initialized */
211 padding_length = (uint8_t*)m->hdr + message_length - (uint8_t*)padding;
212 memzero(padding, padding_length);
214 /* update message size */
215 m->hdr->nlmsg_len = message_length;
220 static int message_attribute_has_type(sd_netlink_message *m, size_t *out_size, uint16_t attribute_type, uint16_t data_type) {
226 r = type_system_get_type(m->containers[m->n_containers].type_system, &type, attribute_type);
230 if (type_get_type(type) != data_type)
234 *out_size = type_get_size(type);
238 int sd_netlink_message_append_string(sd_netlink_message *m, unsigned short type, const char *data) {
242 assert_return(m, -EINVAL);
243 assert_return(!m->sealed, -EPERM);
244 assert_return(data, -EINVAL);
246 r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_STRING);
251 length = strnlen(data, size+1);
255 length = strlen(data);
257 r = add_rtattr(m, type, data, length + 1);
264 int sd_netlink_message_append_u8(sd_netlink_message *m, unsigned short type, uint8_t data) {
267 assert_return(m, -EINVAL);
268 assert_return(!m->sealed, -EPERM);
270 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U8);
274 r = add_rtattr(m, type, &data, sizeof(uint8_t));
282 int sd_netlink_message_append_u16(sd_netlink_message *m, unsigned short type, uint16_t data) {
285 assert_return(m, -EINVAL);
286 assert_return(!m->sealed, -EPERM);
288 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U16);
292 r = add_rtattr(m, type, &data, sizeof(uint16_t));
299 int sd_netlink_message_append_u32(sd_netlink_message *m, unsigned short type, uint32_t data) {
302 assert_return(m, -EINVAL);
303 assert_return(!m->sealed, -EPERM);
305 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U32);
309 r = add_rtattr(m, type, &data, sizeof(uint32_t));
316 int sd_netlink_message_append_in_addr(sd_netlink_message *m, unsigned short type, const struct in_addr *data) {
319 assert_return(m, -EINVAL);
320 assert_return(!m->sealed, -EPERM);
321 assert_return(data, -EINVAL);
323 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
327 r = add_rtattr(m, type, data, sizeof(struct in_addr));
334 int sd_netlink_message_append_in6_addr(sd_netlink_message *m, unsigned short type, const struct in6_addr *data) {
337 assert_return(m, -EINVAL);
338 assert_return(!m->sealed, -EPERM);
339 assert_return(data, -EINVAL);
341 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
345 r = add_rtattr(m, type, data, sizeof(struct in6_addr));
352 int sd_netlink_message_append_ether_addr(sd_netlink_message *m, unsigned short type, const struct ether_addr *data) {
355 assert_return(m, -EINVAL);
356 assert_return(!m->sealed, -EPERM);
357 assert_return(data, -EINVAL);
359 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_ETHER_ADDR);
363 r = add_rtattr(m, type, data, ETH_ALEN);
370 int sd_netlink_message_append_cache_info(sd_netlink_message *m, unsigned short type, const struct ifa_cacheinfo *info) {
373 assert_return(m, -EINVAL);
374 assert_return(!m->sealed, -EPERM);
375 assert_return(info, -EINVAL);
377 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_CACHE_INFO);
381 r = add_rtattr(m, type, info, sizeof(struct ifa_cacheinfo));
388 int sd_netlink_message_open_container(sd_netlink_message *m, unsigned short type) {
392 assert_return(m, -EINVAL);
393 assert_return(!m->sealed, -EPERM);
394 assert_return(m->n_containers < RTNL_CONTAINER_DEPTH, -ERANGE);
396 r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_NESTED);
398 const NLTypeSystemUnion *type_system_union;
401 r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_UNION);
405 r = sd_rtnl_message_get_family(m, &family);
409 r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, &type_system_union, type);
413 r = type_system_union_protocol_get_type_system(type_system_union,
414 &m->containers[m->n_containers + 1].type_system,
419 r = type_system_get_type_system(m->containers[m->n_containers].type_system,
420 &m->containers[m->n_containers + 1].type_system,
426 r = add_rtattr(m, type | NLA_F_NESTED, NULL, size);
430 m->containers[m->n_containers ++].offset = r;
435 int sd_netlink_message_open_container_union(sd_netlink_message *m, unsigned short type, const char *key) {
436 const NLTypeSystemUnion *type_system_union;
439 assert_return(m, -EINVAL);
440 assert_return(!m->sealed, -EPERM);
442 r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, &type_system_union, type);
446 r = type_system_union_get_type_system(type_system_union,
447 &m->containers[m->n_containers + 1].type_system,
452 r = sd_netlink_message_append_string(m, type_system_union->match, key);
456 /* do we evere need non-null size */
457 r = add_rtattr(m, type | NLA_F_NESTED, NULL, 0);
461 m->containers[m->n_containers ++].offset = r;
467 int sd_netlink_message_close_container(sd_netlink_message *m) {
468 assert_return(m, -EINVAL);
469 assert_return(!m->sealed, -EPERM);
470 assert_return(m->n_containers > 0, -EINVAL);
472 m->containers[m->n_containers].type_system = NULL;
478 static int netlink_message_read_internal(sd_netlink_message *m, unsigned short type, void **data) {
479 struct netlink_attribute *attribute;
482 assert_return(m, -EINVAL);
483 assert_return(m->sealed, -EPERM);
484 assert_return(data, -EINVAL);
485 assert(m->n_containers <= RTNL_CONTAINER_DEPTH);
486 assert(m->containers[m->n_containers].attributes);
487 assert(type < m->containers[m->n_containers].n_attributes);
489 attribute = &m->containers[m->n_containers].attributes[type];
491 if(!attribute->offset)
494 rta = (struct rtattr*)((uint8_t *) m->hdr + attribute->offset);
496 *data = RTA_DATA(rta);
498 return RTA_PAYLOAD(rta);
501 int sd_netlink_message_read_string(sd_netlink_message *m, unsigned short type, const char **data) {
505 assert_return(m, -EINVAL);
507 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_STRING);
511 r = netlink_message_read_internal(m, type, &attr_data);
514 else if (strnlen(attr_data, r) >= (size_t) r)
518 *data = (const char *) attr_data;
523 int sd_netlink_message_read_u8(sd_netlink_message *m, unsigned short type, uint8_t *data) {
527 assert_return(m, -EINVAL);
529 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U8);
533 r = netlink_message_read_internal(m, type, &attr_data);
536 else if ((size_t) r < sizeof(uint8_t))
540 *data = *(uint8_t *) attr_data;
545 int sd_netlink_message_read_u16(sd_netlink_message *m, unsigned short type, uint16_t *data) {
549 assert_return(m, -EINVAL);
551 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U16);
555 r = netlink_message_read_internal(m, type, &attr_data);
558 else if ((size_t) r < sizeof(uint16_t))
562 *data = *(uint16_t *) attr_data;
567 int sd_netlink_message_read_u32(sd_netlink_message *m, unsigned short type, uint32_t *data) {
571 assert_return(m, -EINVAL);
573 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U32);
577 r = netlink_message_read_internal(m, type, &attr_data);
580 else if ((size_t)r < sizeof(uint32_t))
584 *data = *(uint32_t *) attr_data;
589 int sd_netlink_message_read_ether_addr(sd_netlink_message *m, unsigned short type, struct ether_addr *data) {
593 assert_return(m, -EINVAL);
595 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_ETHER_ADDR);
599 r = netlink_message_read_internal(m, type, &attr_data);
602 else if ((size_t)r < sizeof(struct ether_addr))
606 memcpy(data, attr_data, sizeof(struct ether_addr));
611 int sd_netlink_message_read_cache_info(sd_netlink_message *m, unsigned short type, struct ifa_cacheinfo *info) {
615 assert_return(m, -EINVAL);
617 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_CACHE_INFO);
621 r = netlink_message_read_internal(m, type, &attr_data);
624 else if ((size_t)r < sizeof(struct ifa_cacheinfo))
628 memcpy(info, attr_data, sizeof(struct ifa_cacheinfo));
633 int sd_netlink_message_read_in_addr(sd_netlink_message *m, unsigned short type, struct in_addr *data) {
637 assert_return(m, -EINVAL);
639 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
643 r = netlink_message_read_internal(m, type, &attr_data);
646 else if ((size_t)r < sizeof(struct in_addr))
650 memcpy(data, attr_data, sizeof(struct in_addr));
655 int sd_netlink_message_read_in6_addr(sd_netlink_message *m, unsigned short type, struct in6_addr *data) {
659 assert_return(m, -EINVAL);
661 r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
665 r = netlink_message_read_internal(m, type, &attr_data);
668 else if ((size_t)r < sizeof(struct in6_addr))
672 memcpy(data, attr_data, sizeof(struct in6_addr));
677 static int netlink_container_parse(sd_netlink_message *m,
678 struct netlink_container *container,
681 unsigned int rt_len) {
682 _cleanup_free_ struct netlink_attribute *attributes = NULL;
684 attributes = new0(struct netlink_attribute, count);
688 for (; RTA_OK(rta, rt_len); rta = RTA_NEXT(rta, rt_len)) {
691 type = RTA_TYPE(rta);
693 /* if the kernel is newer than the headers we used
694 when building, we ignore out-of-range attributes */
698 if (attributes[type].offset)
699 log_debug("rtnl: message parse - overwriting repeated attribute");
701 attributes[type].offset = (uint8_t *) rta - (uint8_t *) m->hdr;
704 container->attributes = attributes;
706 container->n_attributes = count;
711 int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short type_id) {
712 const NLType *nl_type;
713 const NLTypeSystem *type_system;
719 assert_return(m, -EINVAL);
720 assert_return(m->n_containers < RTNL_CONTAINER_DEPTH, -EINVAL);
722 r = type_system_get_type(m->containers[m->n_containers].type_system,
728 type = type_get_type(nl_type);
730 if (type == NETLINK_TYPE_NESTED) {
731 r = type_system_get_type_system(m->containers[m->n_containers].type_system,
736 } else if (type == NETLINK_TYPE_UNION) {
737 const NLTypeSystemUnion *type_system_union;
739 r = type_system_get_type_system_union(m->containers[m->n_containers].type_system,
745 switch (type_system_union->match_type) {
746 case NL_MATCH_SIBLING:
750 r = sd_netlink_message_read_string(m, type_system_union->match, &key);
754 r = type_system_union_get_type_system(type_system_union,
762 case NL_MATCH_PROTOCOL:
766 r = sd_rtnl_message_get_family(m, &family);
770 r = type_system_union_protocol_get_type_system(type_system_union,
779 assert_not_reached("sd-netlink: invalid type system union type");
784 r = netlink_message_read_internal(m, type_id, &container);
792 r = netlink_container_parse(m,
793 &m->containers[m->n_containers],
794 type_system_get_count(type_system),
802 m->containers[m->n_containers].type_system = type_system;
807 int sd_netlink_message_exit_container(sd_netlink_message *m) {
808 assert_return(m, -EINVAL);
809 assert_return(m->sealed, -EINVAL);
810 assert_return(m->n_containers > 0, -EINVAL);
812 free(m->containers[m->n_containers].attributes);
813 m->containers[m->n_containers].attributes = NULL;
814 m->containers[m->n_containers].type_system = NULL;
821 uint32_t rtnl_message_get_serial(sd_netlink_message *m) {
825 return m->hdr->nlmsg_seq;
828 int sd_netlink_message_is_error(sd_netlink_message *m) {
830 assert_return(m->hdr, 0);
832 return m->hdr->nlmsg_type == NLMSG_ERROR;
835 int sd_netlink_message_get_errno(sd_netlink_message *m) {
836 struct nlmsgerr *err;
838 assert_return(m, -EINVAL);
839 assert_return(m->hdr, -EINVAL);
841 if (!sd_netlink_message_is_error(m))
844 err = NLMSG_DATA(m->hdr);
849 int sd_netlink_message_rewind(sd_netlink_message *m) {
850 const NLType *nl_type;
856 assert_return(m, -EINVAL);
858 /* don't allow appending to message once parsed */
860 rtnl_message_seal(m);
862 for (i = 1; i <= m->n_containers; i++) {
863 free(m->containers[i].attributes);
864 m->containers[i].attributes = NULL;
869 if (m->containers[0].attributes) {
870 /* top-level attributes have already been parsed */
876 r = type_system_get_type(&type_system_root, &nl_type, m->hdr->nlmsg_type);
880 type = type_get_type(nl_type);
881 size = type_get_size(nl_type);
883 if (type == NETLINK_TYPE_NESTED) {
884 const NLTypeSystem *type_system;
886 type_get_type_system(nl_type, &type_system);
888 m->containers[0].type_system = type_system;
890 r = netlink_container_parse(m,
891 &m->containers[m->n_containers],
892 type_system_get_count(type_system),
893 (struct rtattr*)((uint8_t*)NLMSG_DATA(m->hdr) + NLMSG_ALIGN(size)),
894 NLMSG_PAYLOAD(m->hdr, size));
902 void rtnl_message_seal(sd_netlink_message *m) {
909 sd_netlink_message *sd_netlink_message_next(sd_netlink_message *m) {
910 assert_return(m, NULL);