diff --git a/src/libnm-platform/nm-linux-platform.c b/src/libnm-platform/nm-linux-platform.c index a01761ca80..7996a5821d 100644 --- a/src/libnm-platform/nm-linux-platform.c +++ b/src/libnm-platform/nm-linux-platform.c @@ -2670,7 +2670,7 @@ _wireguard_read_info(NMPlatform *platform /* used only as logging context */ ifindex, wireguard_family_id); - msg = nlmsg_alloc(); + msg = nlmsg_alloc(0); if (!genlmsg_put(msg, NL_AUTO_PORT, @@ -2879,7 +2879,7 @@ _wireguard_create_change_nlmsgs(NMPlatform *platfo again: - msg = nlmsg_alloc(); + msg = nlmsg_alloc(0); if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, @@ -5026,7 +5026,7 @@ _nl_msg_new_address(uint16_t nlmsg_type, nm_assert(NM_IN_SET(family, AF_INET, AF_INET6)); nm_assert(NM_IN_SET(nlmsg_type, RTM_NEWADDR, RTM_DELADDR)); - msg = nlmsg_alloc_simple(nlmsg_type, nlmsg_flags); + msg = nlmsg_alloc_new(0, nlmsg_type, nlmsg_flags); addr_len = family == AF_INET ? sizeof(in_addr_t) : sizeof(struct in6_addr); @@ -5133,7 +5133,7 @@ _nl_msg_new_route(uint16_t nlmsg_type, uint16_t nlmsg_flags, const NMPObject *ob NM_IN_SET(NMP_OBJECT_GET_TYPE(obj), NMP_OBJECT_TYPE_IP4_ROUTE, NMP_OBJECT_TYPE_IP6_ROUTE)); nm_assert(NM_IN_SET(nlmsg_type, RTM_NEWROUTE, RTM_DELROUTE)); - msg = nlmsg_alloc_simple(nlmsg_type, nlmsg_flags); + msg = nlmsg_alloc_new(0, nlmsg_type, nlmsg_flags); if (nlmsg_append_struct(msg, &rtmsg) < 0) goto nla_put_failure; @@ -5225,7 +5225,7 @@ _nl_msg_new_routing_rule(uint16_t nlmsg_type, const guint8 addr_size = nm_utils_addr_family_to_size(routing_rule->addr_family); guint32 table; - msg = nlmsg_alloc_simple(nlmsg_type, nlmsg_flags); + msg = nlmsg_alloc_new(0, nlmsg_type, nlmsg_flags); table = routing_rule->table; @@ -5344,7 +5344,7 @@ _nl_msg_new_qdisc(uint16_t nlmsg_type, uint16_t nlmsg_flags, const NMPlatformQdi .tcm_info = qdisc->info, }; - msg = nlmsg_alloc_simple(nlmsg_type, nlmsg_flags | NMP_NLM_FLAG_F_ECHO); + msg = nlmsg_alloc_new(0, nlmsg_type, nlmsg_flags | NMP_NLM_FLAG_F_ECHO); if (nlmsg_append_struct(msg, &tcm) < 0) goto nla_put_failure; @@ -5432,7 +5432,7 @@ _nl_msg_new_tfilter(uint16_t nlmsg_type, uint16_t nlmsg_flags, const NMPlatformT .tcm_info = tfilter->info, }; - msg = nlmsg_alloc_simple(nlmsg_type, nlmsg_flags | NMP_NLM_FLAG_F_ECHO); + msg = nlmsg_alloc_new(0, nlmsg_type, nlmsg_flags | NMP_NLM_FLAG_F_ECHO); if (nlmsg_append_struct(msg, &tcm) < 0) goto nla_put_failure; @@ -7227,7 +7227,7 @@ _nl_msg_new_dump_rtnl(NMPObjectType obj_type, int preferred_addr_family) nm_assert(klass); nm_assert(klass->rtm_gettype > 0); - nlmsg = nlmsg_alloc_simple(klass->rtm_gettype, NLM_F_DUMP); + nlmsg = nlmsg_alloc_new(0, klass->rtm_gettype, NLM_F_DUMP); if (klass->addr_family != AF_UNSPEC) { /* if the class specifies a particular address family, then it is preferred. */ @@ -7272,7 +7272,7 @@ _nl_msg_new_dump_genl_families(void) { nm_auto_nlmsg struct nl_msg *nlmsg = NULL; - nlmsg = nlmsg_alloc_size(nlmsg_total_size(GENL_HDRLEN)); + nlmsg = nlmsg_alloc(nlmsg_total_size(GENL_HDRLEN)); if (!genlmsg_put(nlmsg, NL_AUTO_PORT, @@ -9528,7 +9528,7 @@ tc_delete(NMPlatform *platform, log_tag = "do-delete-tc"; } - msg = nlmsg_alloc_simple(nlmsg_type, NMP_NLM_FLAG_F_ECHO); + msg = nlmsg_alloc_new(0, nlmsg_type, NMP_NLM_FLAG_F_ECHO); if (nlmsg_append_struct(msg, &tcm) < 0) goto nla_put_failure; @@ -10252,7 +10252,7 @@ mptcp_addr_update(NMPlatform *platform, NMOptionBool add, const NMPlatformMptcpA cmd_str, nm_platform_mptcp_addr_to_string(addr, sbuf, sizeof(sbuf))); - nlmsg = nlmsg_alloc_size(nlmsg_total_size(GENL_HDRLEN) + 200); + nlmsg = nlmsg_alloc(nlmsg_total_size(GENL_HDRLEN) + 200); if (!genlmsg_put(nlmsg, NL_AUTO_PORT, @@ -10343,7 +10343,7 @@ mptcp_addrs_dump(NMPlatform *platform) return NULL; } - nlmsg = nlmsg_alloc_size(nlmsg_total_size(GENL_HDRLEN)); + nlmsg = nlmsg_alloc(nlmsg_total_size(GENL_HDRLEN)); if (!genlmsg_put(nlmsg, NL_AUTO_PORT, diff --git a/src/libnm-platform/nm-netlink.c b/src/libnm-platform/nm-netlink.c index 36841d5626..e08eee5e0a 100644 --- a/src/libnm-platform/nm-netlink.c +++ b/src/libnm-platform/nm-netlink.c @@ -364,11 +364,22 @@ nla_reserve(struct nl_msg *msg, int attrtype, int attrlen) /*****************************************************************************/ +/** + * Allocate a new netlink message. + * + * Allocates a new netlink message without any further payload. If @len is zero, + * the maximum payload size is set to the size of one memory page. + * + * @return Newly allocated netlink message or NULL. + */ struct nl_msg * -nlmsg_alloc_size(size_t len) +nlmsg_alloc(size_t len) { struct nl_msg *nm; + if (len == 0) + len = nm_utils_getpagesize(); + if (len < sizeof(struct nlmsghdr)) len = sizeof(struct nlmsghdr); else if (len > UINT32_MAX) @@ -384,38 +395,23 @@ nlmsg_alloc_size(size_t len) return nm; } -/** - * Allocate a new netlink message with the default maximum payload size. - * - * Allocates a new netlink message without any further payload. The - * maximum payload size defaults to PAGESIZE or as otherwise specified - * with nlmsg_set_default_size(). - * - * @return Newly allocated netlink message or NULL. - */ -struct nl_msg * -nlmsg_alloc(void) -{ - return nlmsg_alloc_size(nm_utils_getpagesize()); -} - struct nl_msg * nlmsg_alloc_convert(struct nlmsghdr *hdr) { struct nl_msg *nm; - nm = nlmsg_alloc_size(NLMSG_ALIGN(hdr->nlmsg_len)); + nm = nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len)); memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len); return nm; } struct nl_msg * -nlmsg_alloc_simple(uint16_t nlmsgtype, uint16_t flags) +nlmsg_alloc_new(size_t size, uint16_t nlmsgtype, uint16_t flags) { struct nl_msg *nm; struct nlmsghdr *new; - nm = nlmsg_alloc(); + nm = nlmsg_alloc(size); new = nm->nm_nlh; new->nlmsg_type = nlmsgtype; new->nlmsg_flags = flags; @@ -928,7 +924,7 @@ genl_ctrl_resolve(struct nl_sock *sk, const char *name) .valid_arg = &response_data, }; - msg = nlmsg_alloc(); + msg = nlmsg_alloc(0); if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, GENL_ID_CTRL, 0, 0, CTRL_CMD_GETFAMILY, 1)) return -ENOMEM; diff --git a/src/libnm-platform/nm-netlink.h b/src/libnm-platform/nm-netlink.h index 44ed70af75..634be2b4a6 100644 --- a/src/libnm-platform/nm-netlink.h +++ b/src/libnm-platform/nm-netlink.h @@ -393,13 +393,11 @@ nla_parse_nested(struct nlattr *tb[], /*****************************************************************************/ -struct nl_msg *nlmsg_alloc(void); - -struct nl_msg *nlmsg_alloc_size(size_t max); +struct nl_msg *nlmsg_alloc(size_t len); struct nl_msg *nlmsg_alloc_convert(struct nlmsghdr *hdr); -struct nl_msg *nlmsg_alloc_simple(uint16_t nlmsgtype, uint16_t flags); +struct nl_msg *nlmsg_alloc_new(size_t size, uint16_t nlmsgtype, uint16_t flags); void *nlmsg_reserve(struct nl_msg *n, uint32_t len, uint32_t pad); diff --git a/src/libnm-platform/tests/test-nm-platform.c b/src/libnm-platform/tests/test-nm-platform.c index c351f014e1..cd54df9291 100644 --- a/src/libnm-platform/tests/test-nm-platform.c +++ b/src/libnm-platform/tests/test-nm-platform.c @@ -27,10 +27,9 @@ test_use_symbols(void) (void (*)(void)) nlmsg_hdr, (void (*)(void)) nlmsg_reserve, (void (*)(void)) nla_reserve, - (void (*)(void)) nlmsg_alloc_size, - (void (*)(void)) nlmsg_alloc, (void (*)(void)) nlmsg_alloc_convert, - (void (*)(void)) nlmsg_alloc_simple, + (void (*)(void)) nlmsg_alloc_new, + (void (*)(void)) nlmsg_alloc, (void (*)(void)) nlmsg_free, (void (*)(void)) nlmsg_append, (void (*)(void)) nlmsg_parse, diff --git a/src/libnm-platform/wifi/nm-wifi-utils-nl80211.c b/src/libnm-platform/wifi/nm-wifi-utils-nl80211.c index 37edd928e3..c7ee0473de 100644 --- a/src/libnm-platform/wifi/nm-wifi-utils-nl80211.c +++ b/src/libnm-platform/wifi/nm-wifi-utils-nl80211.c @@ -86,7 +86,7 @@ _nl80211_alloc_msg(guint16 genl_family_id, int ifindex, int phy, uint8_t cmd, ui { nm_auto_nlmsg struct nl_msg *msg = NULL; - msg = nlmsg_alloc(); + msg = nlmsg_alloc(0); genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, genl_family_id, 0, flags, cmd, 0); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); if (phy != -1) diff --git a/src/libnm-platform/wpan/nm-wpan-utils.c b/src/libnm-platform/wpan/nm-wpan-utils.c index 675efe5a59..03c9581f8e 100644 --- a/src/libnm-platform/wpan/nm-wpan-utils.c +++ b/src/libnm-platform/wpan/nm-wpan-utils.c @@ -79,7 +79,7 @@ _nl802154_alloc_msg(guint16 genl_family_id, int ifindex, uint8_t cmd, uint16_t f { nm_auto_nlmsg struct nl_msg *msg = NULL; - msg = nlmsg_alloc(); + msg = nlmsg_alloc(0); if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, genl_family_id, 0, flags, cmd, 0)) goto nla_put_failure; NLA_PUT_U32(msg, NL802154_ATTR_IFINDEX, ifindex);