* [PATCH 1/6] netlink: Add l_netlink_iter
@ 2024-08-23 16:15 Denis Kenzior
2024-08-23 16:15 ` [PATCH 2/6] unit: Use the new l_netlink_attr API Denis Kenzior
` (5 more replies)
0 siblings, 6 replies; 7+ messages in thread
From: Denis Kenzior @ 2024-08-23 16:15 UTC (permalink / raw)
To: ell; +Cc: Denis Kenzior
Move netlink attribute iterator out of genl.c and into netlink.c. Both
genl and netlink use the same attribute structure, and is applicable to
both. This makes implementing netlink and rtnl message parsers much
easier.
In order to keep source compatibility, structure the netlink iterator in
the same way as the genl iterator. In the future, the use of genl
iterators will be removed.
---
ell/ell.sym | 5 ++--
ell/genl.c | 76 +++------------------------------------------------
ell/genl.h | 18 +++++++++---
ell/netlink.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++
ell/netlink.h | 15 ++++++++++
5 files changed, 111 insertions(+), 78 deletions(-)
diff --git a/ell/ell.sym b/ell/ell.sym
index c5d1c2a9edea..1499081cb25e 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -344,8 +344,6 @@ global:
l_genl_msg_enter_nested;
l_genl_msg_leave_nested;
l_genl_attr_init;
- l_genl_attr_next;
- l_genl_attr_recurse;
l_genl_family_info_can_send;
l_genl_family_info_can_dump;
l_genl_family_info_has_group;
@@ -445,6 +443,9 @@ global:
l_netlink_message_add_header;
l_netlink_message_enter_nested;
l_netlink_message_leave_nested;
+ l_netlink_attr_init;
+ l_netlink_attr_next;
+ l_netlink_attr_recurse;
/* path */
l_basename;
l_path_find;
diff --git a/ell/genl.c b/ell/genl.c
index 5a8dd371a970..ac10cbf07ef0 100644
--- a/ell/genl.c
+++ b/ell/genl.c
@@ -1559,80 +1559,12 @@ LIB_EXPORT bool l_genl_msg_leave_nested(struct l_genl_msg *msg)
LIB_EXPORT bool l_genl_attr_init(struct l_genl_attr *attr,
struct l_genl_msg *msg)
{
- const struct nlattr *nla;
- uint32_t len;
-
- if (unlikely(!attr) || unlikely(!msg))
- return false;
-
- if (!msg->nlm ||
- msg->nlm->hdr->nlmsg_len < NLMSG_HDRLEN + GENL_HDRLEN)
- return false;
-
- nla = msg->nlm->data + NLMSG_HDRLEN + GENL_HDRLEN;
- len = msg->nlm->hdr->nlmsg_len - NLMSG_HDRLEN - GENL_HDRLEN;
-
- if (!NLA_OK(nla, len))
- return false;
-
- attr->data = NULL;
- attr->len = 0;
- attr->next_data = nla;
- attr->next_len = len;
-
- return true;
-}
-
-LIB_EXPORT bool l_genl_attr_next(struct l_genl_attr *attr,
- uint16_t *type,
- uint16_t *len,
- const void **data)
-{
- const struct nlattr *nla;
-
- if (unlikely(!attr))
- return false;
-
- nla = attr->next_data;
-
- if (!NLA_OK(nla, attr->next_len))
+ if (unlikely(!msg) || unlikely(!msg->nlm))
return false;
- if (type)
- *type = nla->nla_type & NLA_TYPE_MASK;
-
- if (len)
- *len = NLA_PAYLOAD(nla);
-
- if (data)
- *data = NLA_DATA(nla);
-
- attr->data = attr->next_data;
- attr->len = attr->next_len;
-
- attr->next_data = NLA_NEXT(nla, attr->next_len);
-
- return true;
-}
-
-LIB_EXPORT bool l_genl_attr_recurse(const struct l_genl_attr *attr,
- struct l_genl_attr *nested)
-{
- const struct nlattr *nla;
-
- if (unlikely(!attr) || unlikely(!nested))
- return false;
-
- nla = attr->data;
- if (!nla)
- return false;
-
- nested->data = NULL;
- nested->len = 0;
- nested->next_data = NLA_DATA(nla);
- nested->next_len = NLA_PAYLOAD(nla);
-
- return true;
+ return l_netlink_attr_init((struct l_netlink_attr *) attr,
+ NLMSG_HDRLEN + GENL_HDRLEN,
+ msg->nlm->data, msg->nlm->hdr->nlmsg_len) == 0;
}
/**
diff --git a/ell/genl.h b/ell/genl.h
index 3f38fd9d5710..ed5f17c4fd2e 100644
--- a/ell/genl.h
+++ b/ell/genl.h
@@ -91,10 +91,20 @@ bool l_genl_msg_enter_nested(struct l_genl_msg *msg, uint16_t type);
bool l_genl_msg_leave_nested(struct l_genl_msg *msg);
bool l_genl_attr_init(struct l_genl_attr *attr, struct l_genl_msg *msg);
-bool l_genl_attr_next(struct l_genl_attr *attr, uint16_t *type,
- uint16_t *len, const void **data);
-bool l_genl_attr_recurse(const struct l_genl_attr *attr,
- struct l_genl_attr *nested);
+
+static inline bool l_genl_attr_next(struct l_genl_attr *attr, uint16_t *type,
+ uint16_t *len, const void **data)
+{
+ return l_netlink_attr_next((struct l_netlink_attr *) attr,
+ type, len, data) == 0;
+}
+
+static inline bool l_genl_attr_recurse(const struct l_genl_attr *attr,
+ struct l_genl_attr *nested)
+{
+ return l_netlink_attr_recurse((struct l_netlink_attr *) attr,
+ (struct l_netlink_attr *) nested) == 0;
+}
bool l_genl_family_info_has_group(const struct l_genl_family_info *info,
const char *group);
diff --git a/ell/netlink.c b/ell/netlink.c
index 121a322fccf5..0be3009c736f 100644
--- a/ell/netlink.c
+++ b/ell/netlink.c
@@ -944,3 +944,78 @@ LIB_EXPORT int l_netlink_message_leave_nested(struct l_netlink_message *message)
return 0;
}
+
+LIB_EXPORT int l_netlink_attr_init(struct l_netlink_attr *iter,
+ size_t header_len,
+ const void *data, uint32_t len)
+{
+ const struct nlattr *nla;
+
+ if (unlikely(!iter) || unlikely(!data))
+ return -EINVAL;
+
+ if (len < NLA_ALIGN(header_len))
+ return -EMSGSIZE;
+
+ nla = data + NLA_ALIGN(header_len);
+ len -= NLA_ALIGN(header_len);
+
+ if (!NLA_OK(nla, len))
+ return -EMSGSIZE;
+
+ iter->data = NULL;
+ iter->len = 0;
+ iter->next_data = nla;
+ iter->next_len = len;
+
+ return 0;
+}
+
+LIB_EXPORT int l_netlink_attr_next(struct l_netlink_attr *iter,
+ uint16_t *type, uint16_t *len,
+ const void **data)
+{
+ const struct nlattr *nla;
+
+ if (unlikely(!iter))
+ return -EINVAL;
+
+ nla = iter->next_data;
+ if (!NLA_OK(nla, iter->next_len))
+ return -EMSGSIZE;
+
+ if (type)
+ *type = nla->nla_type & NLA_TYPE_MASK;
+
+ if (len)
+ *len = NLA_PAYLOAD(nla);
+
+ if (data)
+ *data = NLA_DATA(nla);
+
+ iter->data = iter->next_data;
+ iter->len = iter->next_len;
+
+ iter->next_data = NLA_NEXT(nla, iter->next_len);
+ return 0;
+}
+
+LIB_EXPORT int l_netlink_attr_recurse(const struct l_netlink_attr *iter,
+ struct l_netlink_attr *nested)
+{
+ const struct nlattr *nla;
+
+ if (unlikely(!iter) || unlikely(!nested))
+ return -EINVAL;
+
+ nla = iter->data;
+ if (!nla)
+ return false;
+
+ nested->data = NULL;
+ nested->len = 0;
+ nested->next_data = NLA_DATA(nla);
+ nested->next_len = NLA_PAYLOAD(nla);
+
+ return 0;
+}
diff --git a/ell/netlink.h b/ell/netlink.h
index 08aff74fbac2..f09d42043675 100644
--- a/ell/netlink.h
+++ b/ell/netlink.h
@@ -119,6 +119,21 @@ static inline int l_netlink_message_append_mac(struct l_netlink_message *message
return l_netlink_message_append(message, type, mac, 6);
}
+struct l_netlink_attr {
+ const struct nlattr *data;
+ uint32_t len;
+ const struct nlattr *next_data;
+ uint32_t next_len;
+};
+
+int l_netlink_attr_init(struct l_netlink_attr *attr, size_t header_len,
+ const void *data, uint32_t len);
+int l_netlink_attr_next(struct l_netlink_attr *attr,
+ uint16_t *type, uint16_t *len,
+ const void **data);
+int l_netlink_attr_recurse(const struct l_netlink_attr *iter,
+ struct l_netlink_attr *nested);
+
#ifdef __cplusplus
}
#endif
--
2.45.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 2/6] unit: Use the new l_netlink_attr API
2024-08-23 16:15 [PATCH 1/6] netlink: Add l_netlink_iter Denis Kenzior
@ 2024-08-23 16:15 ` Denis Kenzior
2024-08-23 16:15 ` [PATCH 3/6] netlink: Add l_netlink_message_append_string Denis Kenzior
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Denis Kenzior @ 2024-08-23 16:15 UTC (permalink / raw)
To: ell; +Cc: Denis Kenzior
---
unit/test-netlink.c | 29 ++++++++++++++++-------------
1 file changed, 16 insertions(+), 13 deletions(-)
diff --git a/unit/test-netlink.c b/unit/test-netlink.c
index da23ee5c9b0e..113d735adf40 100644
--- a/unit/test-netlink.c
+++ b/unit/test-netlink.c
@@ -27,31 +27,34 @@ static void getlink_callback(int error, uint16_t type, const void *data,
uint32_t len, void *user_data)
{
const struct ifinfomsg *ifi = data;
- struct rtattr *rta;
- int bytes;
+ struct l_netlink_attr attr;
char ifname[IF_NAMESIZE];
uint32_t index, flags;
+ uint16_t rta_type;
+ uint16_t rta_len;
+ const void *rta_data;
if (error)
goto done;
- bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg));
+ if (l_netlink_attr_init(&attr, sizeof(struct ifinfomsg), data, len) < 0)
+ goto done;
memset(ifname, 0, sizeof(ifname));
- index = ifi->ifi_index;
- flags = ifi->ifi_flags;
+ while (!l_netlink_attr_next(&attr, &rta_type, &rta_len, &rta_data)) {
+ if (rta_type != IFLA_IFNAME)
+ continue;
+
+ if (rta_len <= IF_NAMESIZE)
+ strcpy(ifname, rta_data);
- for (rta = IFLA_RTA(ifi); RTA_OK(rta, bytes);
- rta = RTA_NEXT(rta, bytes)) {
- switch (rta->rta_type) {
- case IFLA_IFNAME:
- if (RTA_PAYLOAD(rta) <= IF_NAMESIZE)
- strcpy(ifname, RTA_DATA(rta));
- break;
- }
+ break;
}
+ index = ifi->ifi_index;
+ flags = ifi->ifi_flags;
+
l_info("index=%d flags=0x%08x name=%s", index, flags, ifname);
done:
--
2.45.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 3/6] netlink: Add l_netlink_message_append_string
2024-08-23 16:15 [PATCH 1/6] netlink: Add l_netlink_iter Denis Kenzior
2024-08-23 16:15 ` [PATCH 2/6] unit: Use the new l_netlink_attr API Denis Kenzior
@ 2024-08-23 16:15 ` Denis Kenzior
2024-08-23 16:15 ` [PATCH 4/6] netlink: Add l_netlink_request_sent Denis Kenzior
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Denis Kenzior @ 2024-08-23 16:15 UTC (permalink / raw)
To: ell; +Cc: Denis Kenzior
---
ell/netlink.h | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/ell/netlink.h b/ell/netlink.h
index f09d42043675..b51748195ca6 100644
--- a/ell/netlink.h
+++ b/ell/netlink.h
@@ -10,6 +10,7 @@
#include <stdbool.h>
#include <stdint.h>
+#include <string.h>
#ifdef __cplusplus
extern "C" {
@@ -119,6 +120,14 @@ static inline int l_netlink_message_append_mac(struct l_netlink_message *message
return l_netlink_message_append(message, type, mac, 6);
}
+static inline int l_netlink_message_append_string(
+ struct l_netlink_message *message,
+ uint16_t type,
+ const char *str)
+{
+ return l_netlink_message_append(message, type, str, strlen(str) + 1);
+}
+
struct l_netlink_attr {
const struct nlattr *data;
uint32_t len;
--
2.45.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 4/6] netlink: Add l_netlink_request_sent
2024-08-23 16:15 [PATCH 1/6] netlink: Add l_netlink_iter Denis Kenzior
2024-08-23 16:15 ` [PATCH 2/6] unit: Use the new l_netlink_attr API Denis Kenzior
2024-08-23 16:15 ` [PATCH 3/6] netlink: Add l_netlink_message_append_string Denis Kenzior
@ 2024-08-23 16:15 ` Denis Kenzior
2024-08-23 16:15 ` [PATCH 5/6] util: Handle NULL pointers in l_safe_ato* functions Denis Kenzior
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Denis Kenzior @ 2024-08-23 16:15 UTC (permalink / raw)
To: ell; +Cc: Denis Kenzior
This is similar to l_genl_family_request_sent()
---
ell/ell.sym | 1 +
ell/netlink.c | 18 ++++++++++++++++++
ell/netlink.h | 1 +
3 files changed, 20 insertions(+)
diff --git a/ell/ell.sym b/ell/ell.sym
index 1499081cb25e..cbeb61ff0f46 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -431,6 +431,7 @@ global:
l_netlink_destroy;
l_netlink_send;
l_netlink_cancel;
+ l_netlink_request_sent;
l_netlink_register;
l_netlink_unregister;
l_netlink_set_debug;
diff --git a/ell/netlink.c b/ell/netlink.c
index 0be3009c736f..7e6ef0f760e7 100644
--- a/ell/netlink.c
+++ b/ell/netlink.c
@@ -467,6 +467,24 @@ LIB_EXPORT bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id)
return true;
}
+LIB_EXPORT bool l_netlink_request_sent(struct l_netlink *netlink,
+ unsigned int id)
+{
+ struct command *command;
+ struct nlmsghdr *hdr;
+
+ if (unlikely(!netlink || !id))
+ return false;
+
+ command = l_hashmap_lookup(netlink->command_lookup, L_UINT_TO_PTR(id));
+ if (!command)
+ return false;
+
+ hdr = command->message->hdr;
+ return l_hashmap_lookup(netlink->command_pending,
+ L_UINT_TO_PTR(hdr->nlmsg_seq));
+}
+
static bool add_membership(struct l_netlink *netlink, uint32_t group)
{
int sk, value = group;
diff --git a/ell/netlink.h b/ell/netlink.h
index b51748195ca6..368f38619155 100644
--- a/ell/netlink.h
+++ b/ell/netlink.h
@@ -37,6 +37,7 @@ unsigned int l_netlink_send(struct l_netlink *netlink,
void *user_data,
l_netlink_destroy_func_t destroy);
bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id);
+bool l_netlink_request_sent(struct l_netlink *netlink, unsigned int id);
unsigned int l_netlink_register(struct l_netlink *netlink,
uint32_t group, l_netlink_notify_func_t function,
--
2.45.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 5/6] util: Handle NULL pointers in l_safe_ato* functions
2024-08-23 16:15 [PATCH 1/6] netlink: Add l_netlink_iter Denis Kenzior
` (2 preceding siblings ...)
2024-08-23 16:15 ` [PATCH 4/6] netlink: Add l_netlink_request_sent Denis Kenzior
@ 2024-08-23 16:15 ` Denis Kenzior
2024-08-23 16:15 ` [PATCH 6/6] rtnl: Add l_rtnl_link_set_mtu Denis Kenzior
2024-08-28 15:24 ` [PATCH 1/6] netlink: Add l_netlink_iter Denis Kenzior
5 siblings, 0 replies; 7+ messages in thread
From: Denis Kenzior @ 2024-08-23 16:15 UTC (permalink / raw)
To: ell; +Cc: Denis Kenzior
---
ell/util.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/ell/util.c b/ell/util.c
index 45ec6c8110bc..f474098c082b 100644
--- a/ell/util.c
+++ b/ell/util.c
@@ -760,6 +760,9 @@ static int safe_atou(const char *s, int base, unsigned int *out_u)
LIB_EXPORT int l_safe_atou32(const char *s, uint32_t *out_u)
{
+ if (unlikely(!s))
+ return -EINVAL;
+
if (!l_ascii_isdigit(s[0]))
return -EINVAL;
@@ -808,6 +811,9 @@ LIB_EXPORT int l_safe_atox16(const char *s, uint16_t *out_x)
LIB_EXPORT int l_safe_atox32(const char *s, uint32_t *out_x)
{
+ if (unlikely(!s))
+ return -EINVAL;
+
if (!l_ascii_isxdigit(s[0]))
return -EINVAL;
--
2.45.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 6/6] rtnl: Add l_rtnl_link_set_mtu
2024-08-23 16:15 [PATCH 1/6] netlink: Add l_netlink_iter Denis Kenzior
` (3 preceding siblings ...)
2024-08-23 16:15 ` [PATCH 5/6] util: Handle NULL pointers in l_safe_ato* functions Denis Kenzior
@ 2024-08-23 16:15 ` Denis Kenzior
2024-08-28 15:24 ` [PATCH 1/6] netlink: Add l_netlink_iter Denis Kenzior
5 siblings, 0 replies; 7+ messages in thread
From: Denis Kenzior @ 2024-08-23 16:15 UTC (permalink / raw)
To: ell; +Cc: Denis Kenzior
This helper is used to set a given MTU on a network interface (link).
---
ell/ell.sym | 1 +
ell/rtnl.c | 18 ++++++++++++++++++
ell/rtnl.h | 4 ++++
3 files changed, 23 insertions(+)
diff --git a/ell/ell.sym b/ell/ell.sym
index cbeb61ff0f46..00123136a527 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -701,6 +701,7 @@ global:
l_rtnl_set_linkmode_and_operstate;
l_rtnl_set_mac;
l_rtnl_set_powered;
+ l_rtnl_link_set_mtu;
l_rtnl_ifaddr4_extract;
l_rtnl_ifaddr4_dump;
l_rtnl_ifaddr4_add;
diff --git a/ell/rtnl.c b/ell/rtnl.c
index da5d219feb48..fbad795949be 100644
--- a/ell/rtnl.c
+++ b/ell/rtnl.c
@@ -787,6 +787,24 @@ LIB_EXPORT uint32_t l_rtnl_set_powered(struct l_netlink *rtnl, int ifindex,
return l_netlink_send(rtnl, nlm, cb, user_data, destroy);
}
+LIB_EXPORT uint32_t l_rtnl_link_set_mtu(struct l_netlink *rtnl, int ifindex,
+ uint32_t mtu,
+ l_netlink_command_func_t cb, void *user_data,
+ l_netlink_destroy_func_t destroy)
+{
+ struct l_netlink_message *nlm = l_netlink_message_new(RTM_SETLINK, 0);
+ struct ifinfomsg ifi;
+
+ memset(&ifi, 0, sizeof(ifi));
+ ifi.ifi_family = AF_UNSPEC;
+ ifi.ifi_index = ifindex;
+
+ l_netlink_message_add_header(nlm, &ifi, sizeof(ifi));
+ l_netlink_message_append_u32(nlm, IFLA_MTU, mtu);
+
+ return l_netlink_send(rtnl, nlm, cb, user_data, destroy);
+}
+
LIB_EXPORT void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,
char **label, char **ip, char **broadcast)
{
diff --git a/ell/rtnl.h b/ell/rtnl.h
index fa5d99eb4b53..d44cda24d6fd 100644
--- a/ell/rtnl.h
+++ b/ell/rtnl.h
@@ -103,6 +103,10 @@ uint32_t l_rtnl_set_powered(struct l_netlink *rtnl, int ifindex, bool powered,
l_netlink_command_func_t cb, void *user_data,
l_netlink_destroy_func_t destroy);
+uint32_t l_rtnl_link_set_mtu(struct l_netlink *rtnl, int ifindex, uint32_t mtu,
+ l_netlink_command_func_t cb, void *user_data,
+ l_netlink_destroy_func_t destroy);
+
void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,
char **label, char **ip, char **broadcast);
uint32_t l_rtnl_ifaddr4_dump(struct l_netlink *rtnl, l_netlink_command_func_t cb,
--
2.45.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/6] netlink: Add l_netlink_iter
2024-08-23 16:15 [PATCH 1/6] netlink: Add l_netlink_iter Denis Kenzior
` (4 preceding siblings ...)
2024-08-23 16:15 ` [PATCH 6/6] rtnl: Add l_rtnl_link_set_mtu Denis Kenzior
@ 2024-08-28 15:24 ` Denis Kenzior
5 siblings, 0 replies; 7+ messages in thread
From: Denis Kenzior @ 2024-08-28 15:24 UTC (permalink / raw)
To: ell
On 8/23/24 11:15 AM, Denis Kenzior wrote:
> Move netlink attribute iterator out of genl.c and into netlink.c. Both
> genl and netlink use the same attribute structure, and is applicable to
> both. This makes implementing netlink and rtnl message parsers much
> easier.
>
> In order to keep source compatibility, structure the netlink iterator in
> the same way as the genl iterator. In the future, the use of genl
> iterators will be removed.
> ---
> ell/ell.sym | 5 ++--
> ell/genl.c | 76 +++------------------------------------------------
> ell/genl.h | 18 +++++++++---
> ell/netlink.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++
> ell/netlink.h | 15 ++++++++++
> 5 files changed, 111 insertions(+), 78 deletions(-)
>
All applied
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-08-28 15:24 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-23 16:15 [PATCH 1/6] netlink: Add l_netlink_iter Denis Kenzior
2024-08-23 16:15 ` [PATCH 2/6] unit: Use the new l_netlink_attr API Denis Kenzior
2024-08-23 16:15 ` [PATCH 3/6] netlink: Add l_netlink_message_append_string Denis Kenzior
2024-08-23 16:15 ` [PATCH 4/6] netlink: Add l_netlink_request_sent Denis Kenzior
2024-08-23 16:15 ` [PATCH 5/6] util: Handle NULL pointers in l_safe_ato* functions Denis Kenzior
2024-08-23 16:15 ` [PATCH 6/6] rtnl: Add l_rtnl_link_set_mtu Denis Kenzior
2024-08-28 15:24 ` [PATCH 1/6] netlink: Add l_netlink_iter Denis Kenzior
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox