casaroli commented on code in PR #22487: URL: https://github.com/apache/nuttx/pull/22487#discussion_r4721527438
########## net/netlink/netlink_generic.c: ########## @@ -0,0 +1,690 @@ +/**************************************************************************** + * net/netlink/netlink_generic.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <sys/types.h> +#include <string.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> + +#include <nuttx/net/netlink.h> +#include <nuttx/net/genetlink.h> +#include <nuttx/kmalloc.h> +#include <nuttx/mutex.h> + +#include "netlink/netlink.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Family registry - stores pointers to registered families */ + +static FAR struct genl_family *g_genl_families[GENL_MAX_FAMILIES]; +static mutex_t g_genl_lock = NXMUTEX_INITIALIZER; +static int g_genl_family_count; +static uint16_t g_genl_next_id = GENL_START_ALLOC; +static uint16_t g_genl_next_mcgrp = 1; /* Multicast group IDs start at 1 */ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: add_mcast_grp_attr + * + * Description: + * Helper to serialize one multicast group as nested rtattr. + * + * Input Parameters: + * buf - Buffer to write the attribute data. + * offset - Current offset in the buffer. + * index - Index of the multicast group. + * gname - Name of the multicast group. + * gid - ID of the multicast group. + * + * Returned Value: + * The aligned length of the added attribute. + * + ****************************************************************************/ + +static int add_mcast_grp_attr(FAR uint8_t *buf, int offset, + int index, FAR const char *gname, int gid) +{ + FAR struct rtattr *grp = (FAR struct rtattr *)(buf + offset); + FAR uint8_t *ptr = (FAR uint8_t *)RTA_DATA(grp); + FAR struct rtattr *attr; + int len = 0; + + grp->rta_type = index | NLA_F_NESTED; + + /* Group Name */ + + attr = (FAR struct rtattr *)ptr; + attr->rta_type = CTRL_ATTR_MCAST_GRP_NAME; + attr->rta_len = RTA_LENGTH(strlen(gname) + 1); + strlcpy((FAR char *)RTA_DATA(attr), gname, GENL_NAMSIZ); + len += RTA_ALIGN(attr->rta_len); + + /* Group ID */ + + attr = (FAR struct rtattr *)(ptr + len); + attr->rta_type = CTRL_ATTR_MCAST_GRP_ID; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = gid; + len += RTA_ALIGN(attr->rta_len); + + grp->rta_len = RTA_LENGTH(len); + return RTA_ALIGN(grp->rta_len); +} + +/**************************************************************************** + * Name: send_family_info + * + * Description: + * Build and queue a CTRL_CMD_NEWFAMILY response for one family. + * This is used both for CTRL_CMD_GETFAMILY replies and DUMP mode. + * + * Input Parameters: + * conn - The netlink connection to send the response to. + * req - The request message header. + * family - The generic netlink family to report. + * dump - True if this is part of a dump operation (NLM_F_MULTI). + * + * Returned Value: + * The length of the netlink message on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int send_family_info(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req, + FAR const struct genl_family *family, + bool dump) +{ + FAR struct netlink_response_s *response; + FAR struct genlmsghdr *genl; + FAR struct nlmsghdr *nlh; + FAR struct rtattr *attr; + FAR uint8_t *attr_ptr; + FAR uint8_t *buf; + int attr_len; + int buf_len; + int offset; + + /* Calculate required buffer size: + * base headers + fixed attrs + family name + mcast groups + */ + + buf_len = NLMSG_HDRLEN + sizeof(struct genlmsghdr) + + RTA_SPACE(2) /* FAMILY_ID */ + + RTA_SPACE(GENL_NAMSIZ) /* FAMILY_NAME */ + + RTA_SPACE(4) * 3; /* VERSION+HDRSIZE+MAXATTR */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + int i; + + for (i = 0; i < family->n_mcgrps; i++) + { + buf_len += RTA_SPACE(RTA_SPACE(GENL_NAMSIZ) + RTA_SPACE(4)); + } + + buf_len += RTA_SPACE(0); /* mcast root header */ + } + + response = (FAR struct netlink_response_s *) + kmm_zalloc(sizeof(struct netlink_response_s) + NLMSG_ALIGN(buf_len)); + if (response == NULL) + { + return -ENOMEM; + } + + buf = (FAR uint8_t *)&response->msg; + nlh = (FAR struct nlmsghdr *)buf; + + nlh->nlmsg_type = GENL_ID_CTRL; + nlh->nlmsg_flags = dump ? NLM_F_MULTI : 0; + nlh->nlmsg_seq = req->nlmsg_seq; + nlh->nlmsg_pid = req->nlmsg_pid; + + genl = (FAR struct genlmsghdr *)NLMSG_DATA(nlh); + genl->cmd = CTRL_CMD_NEWFAMILY; + genl->version = 1; + + offset = NLMSG_ALIGN(sizeof(struct genlmsghdr)); + attr_ptr = (FAR uint8_t *)genl + offset; + + /* CTRL_ATTR_FAMILY_ID */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_FAMILY_ID; + attr->rta_len = RTA_LENGTH(2); + *(FAR uint16_t *)RTA_DATA(attr) = family->id; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_FAMILY_NAME */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_FAMILY_NAME; + attr->rta_len = RTA_LENGTH(strlen(family->name) + 1); + strlcpy((FAR char *)RTA_DATA(attr), family->name, GENL_NAMSIZ); + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_VERSION */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_VERSION; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->version; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_HDRSIZE */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_HDRSIZE; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->hdrsize; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_MAXATTR */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_MAXATTR; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->maxattr; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_MCAST_GROUPS — only if the family has multicast groups */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + FAR struct rtattr *mcast = (FAR struct rtattr *)attr_ptr; + FAR uint8_t *grp; + int grp_len = 0; + int i; + + mcast->rta_type = CTRL_ATTR_MCAST_GROUPS | NLA_F_NESTED; + grp = (FAR uint8_t *)RTA_DATA(mcast); + + for (i = 0; i < family->n_mcgrps; i++) + { + grp_len += add_mcast_grp_attr(grp, grp_len, + i + 1, + family->mcgrps[i].name, + family->mcgrp_offset + i); + } + + mcast->rta_len = RTA_LENGTH(grp_len); + attr_len = RTA_ALIGN(mcast->rta_len); + attr_ptr += attr_len; + offset += attr_len; + } + + nlh->nlmsg_len = NLMSG_LENGTH(offset); + + netlink_add_response(conn, response); + return nlh->nlmsg_len; +} + +/**************************************************************************** + * Name: send_ack_response + * + * Description: + * Send an ACK (NLMSG_ERROR with error=0) response. + * + * Input Parameters: + * conn - The netlink connection to send the ACK to. + * req - The request message header. + * + * Returned Value: + * Zero (OK) is returned on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int send_ack_response(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req) +{ + FAR struct netlink_response_s *response; + FAR struct nlmsgerr *err; + int payload_len = sizeof(struct nlmsgerr); + + response = (FAR struct netlink_response_s *) + kmm_zalloc(sizeof(struct netlink_response_s) + NLMSG_ALIGN(payload_len)); + if (response == NULL) + { + return -ENOMEM; + } + + response->msg.nlmsg_len = NLMSG_LENGTH(payload_len); + response->msg.nlmsg_type = NLMSG_ERROR; + response->msg.nlmsg_flags = 0; + response->msg.nlmsg_seq = req->nlmsg_seq; + response->msg.nlmsg_pid = req->nlmsg_pid; + + err = (FAR struct nlmsgerr *)NLMSG_DATA(&response->msg); + err->error = 0; + memcpy(&err->msg, req, sizeof(struct nlmsghdr)); + + netlink_add_response(conn, response); + return 0; +} + +/**************************************************************************** + * Name: netlink_ctrl_getfamily + * + * Description: + * Handle a CTRL_CMD_GETFAMILY request. Supports both single-family + * lookup by name and NLM_F_DUMP to enumerate all families. + * + * Input Parameters: + * conn - The netlink connection handling the request. + * req - The request message header. + * + * Returned Value: + * The length of the request message on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int netlink_ctrl_getfamily(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req) +{ + FAR const struct genl_family *fam; + FAR struct genlmsghdr *ghdr; + FAR struct nlattr *na; + FAR char *family_name = NULL; + int rem; + int ret; + int i; + + ghdr = (FAR struct genlmsghdr *)NLMSG_DATA(req); + na = (FAR struct nlattr *)((FAR char *)ghdr + GENL_HDRLEN); + rem = req->nlmsg_len - NLMSG_HDRLEN - GENL_HDRLEN; + + while (sizeof(struct nlattr) <= rem) + { + if (na->nla_len < sizeof(struct nlattr) || na->nla_len > rem) + { + break; + } + + if (nla_type(na) == CTRL_ATTR_FAMILY_NAME) + { + family_name = nla_data(na); + } + + na = nla_next(na, &rem); + } + + if (req->nlmsg_flags & NLM_F_DUMP) + { + FAR struct netlink_response_s *done_resp; + FAR struct nlmsghdr *done_hdr; + + /* Dump the built-in nlctrl family */ + + struct genl_family ctrl_fam; + + memset(&ctrl_fam, 0, sizeof(ctrl_fam)); + ctrl_fam.id = GENL_ID_CTRL; + strlcpy(ctrl_fam.name, "nlctrl", GENL_NAMSIZ); + ctrl_fam.version = 1; + ctrl_fam.maxattr = CTRL_ATTR_MAX; + send_family_info(conn, req, &ctrl_fam, true); + + /* Dump all registered families */ + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i]) + { + send_family_info(conn, req, g_genl_families[i], true); + } + } + + /* Send NLMSG_DONE terminator */ + + done_resp = kmm_zalloc(sizeof(*done_resp)); + if (done_resp == NULL) + { + return -ENOMEM; + } + + done_hdr = &done_resp->msg; + done_hdr->nlmsg_len = NLMSG_LENGTH(0); + done_hdr->nlmsg_type = NLMSG_DONE; + done_hdr->nlmsg_flags = NLM_F_MULTI; + done_hdr->nlmsg_seq = req->nlmsg_seq; + done_hdr->nlmsg_pid = req->nlmsg_pid; + + netlink_add_response(conn, done_resp); + return req->nlmsg_len; + } + + /* Single family lookup by name */ + + if (family_name) + { + /* Check built-in nlctrl */ + + if (strcmp(family_name, "nlctrl") == 0) + { + struct genl_family ctrl_fam; + + memset(&ctrl_fam, 0, sizeof(ctrl_fam)); + ctrl_fam.id = GENL_ID_CTRL; + strlcpy(ctrl_fam.name, "nlctrl", GENL_NAMSIZ); + ctrl_fam.version = 1; + ctrl_fam.maxattr = CTRL_ATTR_MAX; + send_family_info(conn, req, &ctrl_fam, false); + + if (req->nlmsg_flags & NLM_F_ACK) + { + send_ack_response(conn, req); + } + + return req->nlmsg_len; + } + + /* Search registered families */ + + fam = genl_family_find_byname(family_name); + if (fam) + { + ret = send_family_info(conn, req, fam, false); + if (ret < 0) + { + return ret; + } + + if (req->nlmsg_flags & NLM_F_ACK) + { + send_ack_response(conn, req); + } + + return req->nlmsg_len; + } + } + + nerr("netlink_ctrl_getfamily: family '%s' not found\n", + family_name ? family_name : "(null)"); + return -ENOENT; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: genl_register_family + * + * Description: + * Register a generic netlink family with the netlink subsystem. + * Allocates a dynamic family ID and multicast group IDs if needed. + * + * Input Parameters: + * family - Pointer to the generic netlink family structure to register. + * + * Returned Value: + * Zero (OK) is returned on success. + * -EEXIST is returned if a family with the same name already exists. + * -ENOMEM is returned if the maximum number of families is reached. + * + ****************************************************************************/ + +int genl_register_family(FAR struct genl_family *family) +{ + int ret = 0; + int i; + + DEBUGASSERT(family != NULL && family->name[0] != '\0'); + + nxmutex_lock(&g_genl_lock); + + /* Check for duplicate */ + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i] && + strcmp(g_genl_families[i]->name, family->name) == 0) + { + ret = -EEXIST; + goto out; + } + } + + if (g_genl_family_count >= GENL_MAX_FAMILIES) + { + ret = -ENOMEM; + goto out; + } + + /* Allocate a dynamic family ID */ + + family->id = g_genl_next_id++; + + /* Allocate multicast group IDs */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + family->mcgrp_offset = g_genl_next_mcgrp; + g_genl_next_mcgrp += family->n_mcgrps; + } + else + { + family->mcgrp_offset = 0; + } + + /* Store in registry */ + + g_genl_families[g_genl_family_count++] = family; + + ninfo("genl: registered family '%s' id=%d mcgrp_offset=%d\n", + family->name, family->id, family->mcgrp_offset); + +out: + nxmutex_unlock(&g_genl_lock); + return ret; +} + +/**************************************************************************** + * Name: genl_unregister_family + * + * Description: + * Unregister a generic netlink family from the netlink subsystem. + * + * Input Parameters: + * family - Pointer to the generic netlink family structure to unregister. + * + * Returned Value: + * Zero (OK) is returned on success. + * -ENOENT is returned if the family was not found in the registry. + * + ****************************************************************************/ + +int genl_unregister_family(FAR const struct genl_family *family) +{ + int ret = -ENOENT; + int i; + + DEBUGASSERT(family != NULL); + + nxmutex_lock(&g_genl_lock); + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i] == family) + { + /* Shift remaining entries down */ + + g_genl_family_count--; + g_genl_families[i] = g_genl_families[g_genl_family_count]; + g_genl_families[g_genl_family_count] = NULL; + ret = 0; + break; + } + } + + nxmutex_unlock(&g_genl_lock); + return ret; +} + +/**************************************************************************** + * Name: genl_family_find_byname + * + * Description: + * Find a registered generic netlink family by name. + * + * Input Parameters: + * name - The name of the family to find. + * + * Returned Value: + * Pointer to the generic netlink family structure if found. + * NULL is returned if the family was not found. + * + ****************************************************************************/ + +FAR const struct genl_family *genl_family_find_byname(FAR const char *name) +{ + int i; + + DEBUGASSERT(name != NULL); + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i] && + strcmp(g_genl_families[i]->name, name) == 0) + { + return g_genl_families[i]; + } + } + + return NULL; +} + +/**************************************************************************** + * Name: genl_family_find_byid + * + * Description: + * Find a registered generic netlink family by ID. + * + * Input Parameters: + * id - The family ID to search for. + * + * Returned Value: + * Pointer to the generic netlink family structure if found. + * NULL is returned if the family was not found. + * + ****************************************************************************/ + +FAR const struct genl_family *genl_family_find_byid(uint16_t id) +{ + int i; + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i] && g_genl_families[i]->id == id) + { + return g_genl_families[i]; + } + } + + return NULL; +} + +/**************************************************************************** + * Name: netlink_generic_sendto + * + * Description: + * Perform the sendto() operation for the NETLINK_GENERIC protocol. + * Dispatches to the controller (GENL_ID_CTRL) or to a registered + * family's cmd_handler based on nlmsg_type. + * + * Input Parameters: + * conn - The netlink connection. + * msg - The netlink message to send. + * len - Length of the message. + * flags - Send flags. + * to - Destination address (sockaddr_nl). + * tolen - Length of the destination address. + * + * Returned Value: + * The length of the request message on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +ssize_t netlink_generic_sendto(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *msg, + size_t len, int flags, + FAR const struct sockaddr_nl *to, + socklen_t tolen) +{ + FAR const struct genl_family *family; + + DEBUGASSERT(conn != NULL && msg != NULL); + + /* Handle controller messages (CTRL_CMD_GETFAMILY etc.) */ + + if (msg->nlmsg_type == GENL_ID_CTRL) + { + FAR struct genlmsghdr *ghdr; + + ghdr = (FAR struct genlmsghdr *)NLMSG_DATA(msg); + if (ghdr->cmd == CTRL_CMD_GETFAMILY) Review Comment: A request of only a `nlmsghdr` (`nlmsg_len` 16, type `GENL_ID_CTRL`) passes the checks in `netlink_sendmsg()`, and this line reads `ghdr->cmd` after the end of the request. Also `rem` at line 339 is negative when `nlmsg_len` is less than 20. On sim with `CONFIG_SIM_ASAN=y` this gives `heap-buffer-overflow` in `netlink_generic_sendto netlink/netlink_generic.c:671`. Fix: in `netlink_generic_sendto()`, return `-EINVAL` when `msg->nlmsg_len < NLMSG_LENGTH(GENL_HDRLEN)`. ########## net/netlink/netlink_generic.c: ########## @@ -0,0 +1,690 @@ +/**************************************************************************** + * net/netlink/netlink_generic.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <sys/types.h> +#include <string.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> + +#include <nuttx/net/netlink.h> +#include <nuttx/net/genetlink.h> +#include <nuttx/kmalloc.h> +#include <nuttx/mutex.h> + +#include "netlink/netlink.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Family registry - stores pointers to registered families */ + +static FAR struct genl_family *g_genl_families[GENL_MAX_FAMILIES]; +static mutex_t g_genl_lock = NXMUTEX_INITIALIZER; +static int g_genl_family_count; +static uint16_t g_genl_next_id = GENL_START_ALLOC; +static uint16_t g_genl_next_mcgrp = 1; /* Multicast group IDs start at 1 */ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: add_mcast_grp_attr + * + * Description: + * Helper to serialize one multicast group as nested rtattr. + * + * Input Parameters: + * buf - Buffer to write the attribute data. + * offset - Current offset in the buffer. + * index - Index of the multicast group. + * gname - Name of the multicast group. + * gid - ID of the multicast group. + * + * Returned Value: + * The aligned length of the added attribute. + * + ****************************************************************************/ + +static int add_mcast_grp_attr(FAR uint8_t *buf, int offset, + int index, FAR const char *gname, int gid) +{ + FAR struct rtattr *grp = (FAR struct rtattr *)(buf + offset); + FAR uint8_t *ptr = (FAR uint8_t *)RTA_DATA(grp); + FAR struct rtattr *attr; + int len = 0; + + grp->rta_type = index | NLA_F_NESTED; + + /* Group Name */ + + attr = (FAR struct rtattr *)ptr; + attr->rta_type = CTRL_ATTR_MCAST_GRP_NAME; + attr->rta_len = RTA_LENGTH(strlen(gname) + 1); + strlcpy((FAR char *)RTA_DATA(attr), gname, GENL_NAMSIZ); + len += RTA_ALIGN(attr->rta_len); + + /* Group ID */ + + attr = (FAR struct rtattr *)(ptr + len); + attr->rta_type = CTRL_ATTR_MCAST_GRP_ID; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = gid; + len += RTA_ALIGN(attr->rta_len); + + grp->rta_len = RTA_LENGTH(len); + return RTA_ALIGN(grp->rta_len); +} + +/**************************************************************************** + * Name: send_family_info + * + * Description: + * Build and queue a CTRL_CMD_NEWFAMILY response for one family. + * This is used both for CTRL_CMD_GETFAMILY replies and DUMP mode. + * + * Input Parameters: + * conn - The netlink connection to send the response to. + * req - The request message header. + * family - The generic netlink family to report. + * dump - True if this is part of a dump operation (NLM_F_MULTI). + * + * Returned Value: + * The length of the netlink message on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int send_family_info(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req, + FAR const struct genl_family *family, + bool dump) +{ + FAR struct netlink_response_s *response; + FAR struct genlmsghdr *genl; + FAR struct nlmsghdr *nlh; + FAR struct rtattr *attr; + FAR uint8_t *attr_ptr; + FAR uint8_t *buf; + int attr_len; + int buf_len; + int offset; + + /* Calculate required buffer size: + * base headers + fixed attrs + family name + mcast groups + */ + + buf_len = NLMSG_HDRLEN + sizeof(struct genlmsghdr) + + RTA_SPACE(2) /* FAMILY_ID */ + + RTA_SPACE(GENL_NAMSIZ) /* FAMILY_NAME */ + + RTA_SPACE(4) * 3; /* VERSION+HDRSIZE+MAXATTR */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + int i; + + for (i = 0; i < family->n_mcgrps; i++) + { + buf_len += RTA_SPACE(RTA_SPACE(GENL_NAMSIZ) + RTA_SPACE(4)); + } + + buf_len += RTA_SPACE(0); /* mcast root header */ + } + + response = (FAR struct netlink_response_s *) + kmm_zalloc(sizeof(struct netlink_response_s) + NLMSG_ALIGN(buf_len)); + if (response == NULL) + { + return -ENOMEM; + } + + buf = (FAR uint8_t *)&response->msg; + nlh = (FAR struct nlmsghdr *)buf; + + nlh->nlmsg_type = GENL_ID_CTRL; + nlh->nlmsg_flags = dump ? NLM_F_MULTI : 0; + nlh->nlmsg_seq = req->nlmsg_seq; + nlh->nlmsg_pid = req->nlmsg_pid; + + genl = (FAR struct genlmsghdr *)NLMSG_DATA(nlh); + genl->cmd = CTRL_CMD_NEWFAMILY; + genl->version = 1; + + offset = NLMSG_ALIGN(sizeof(struct genlmsghdr)); + attr_ptr = (FAR uint8_t *)genl + offset; + + /* CTRL_ATTR_FAMILY_ID */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_FAMILY_ID; + attr->rta_len = RTA_LENGTH(2); + *(FAR uint16_t *)RTA_DATA(attr) = family->id; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_FAMILY_NAME */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_FAMILY_NAME; + attr->rta_len = RTA_LENGTH(strlen(family->name) + 1); + strlcpy((FAR char *)RTA_DATA(attr), family->name, GENL_NAMSIZ); + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_VERSION */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_VERSION; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->version; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_HDRSIZE */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_HDRSIZE; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->hdrsize; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_MAXATTR */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_MAXATTR; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->maxattr; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_MCAST_GROUPS — only if the family has multicast groups */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + FAR struct rtattr *mcast = (FAR struct rtattr *)attr_ptr; + FAR uint8_t *grp; + int grp_len = 0; + int i; + + mcast->rta_type = CTRL_ATTR_MCAST_GROUPS | NLA_F_NESTED; + grp = (FAR uint8_t *)RTA_DATA(mcast); + + for (i = 0; i < family->n_mcgrps; i++) + { + grp_len += add_mcast_grp_attr(grp, grp_len, + i + 1, + family->mcgrps[i].name, + family->mcgrp_offset + i); + } + + mcast->rta_len = RTA_LENGTH(grp_len); + attr_len = RTA_ALIGN(mcast->rta_len); + attr_ptr += attr_len; + offset += attr_len; + } + + nlh->nlmsg_len = NLMSG_LENGTH(offset); + + netlink_add_response(conn, response); + return nlh->nlmsg_len; +} + +/**************************************************************************** + * Name: send_ack_response + * + * Description: + * Send an ACK (NLMSG_ERROR with error=0) response. + * + * Input Parameters: + * conn - The netlink connection to send the ACK to. + * req - The request message header. + * + * Returned Value: + * Zero (OK) is returned on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int send_ack_response(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req) +{ + FAR struct netlink_response_s *response; + FAR struct nlmsgerr *err; + int payload_len = sizeof(struct nlmsgerr); + + response = (FAR struct netlink_response_s *) + kmm_zalloc(sizeof(struct netlink_response_s) + NLMSG_ALIGN(payload_len)); + if (response == NULL) + { + return -ENOMEM; + } + + response->msg.nlmsg_len = NLMSG_LENGTH(payload_len); + response->msg.nlmsg_type = NLMSG_ERROR; + response->msg.nlmsg_flags = 0; + response->msg.nlmsg_seq = req->nlmsg_seq; + response->msg.nlmsg_pid = req->nlmsg_pid; + + err = (FAR struct nlmsgerr *)NLMSG_DATA(&response->msg); + err->error = 0; + memcpy(&err->msg, req, sizeof(struct nlmsghdr)); + + netlink_add_response(conn, response); + return 0; +} + +/**************************************************************************** + * Name: netlink_ctrl_getfamily + * + * Description: + * Handle a CTRL_CMD_GETFAMILY request. Supports both single-family + * lookup by name and NLM_F_DUMP to enumerate all families. + * + * Input Parameters: + * conn - The netlink connection handling the request. + * req - The request message header. + * + * Returned Value: + * The length of the request message on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int netlink_ctrl_getfamily(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req) +{ + FAR const struct genl_family *fam; + FAR struct genlmsghdr *ghdr; + FAR struct nlattr *na; + FAR char *family_name = NULL; + int rem; + int ret; + int i; + + ghdr = (FAR struct genlmsghdr *)NLMSG_DATA(req); + na = (FAR struct nlattr *)((FAR char *)ghdr + GENL_HDRLEN); + rem = req->nlmsg_len - NLMSG_HDRLEN - GENL_HDRLEN; + + while (sizeof(struct nlattr) <= rem) + { + if (na->nla_len < sizeof(struct nlattr) || na->nla_len > rem) + { + break; + } + + if (nla_type(na) == CTRL_ATTR_FAMILY_NAME) + { + family_name = nla_data(na); Review Comment: `family_name` points into the request, but nothing checks that the attribute holds a NUL. With an empty `CTRL_ATTR_FAMILY_NAME` at the end of the request, or a name without a NUL, `strcmp()` at line 407 and `genl_family_find_byname()` read after the end of the request, and the `nerr()` at line 446 prints it with `%s`. On sim with `CONFIG_SIM_ASAN=y` this gives `heap-buffer-overflow` in `strcmp` from `netlink_ctrl_getfamily netlink/netlink_generic.c:407`. Fix: use the name only when `nla_len(na) > 0`, `nla_len(na) <= GENL_NAMSIZ` and `memchr(nla_data(na), '\0', nla_len(na)) != NULL`, or parse with `nla_parse()` and an `NLA_NUL_STRING` policy. ########## include/sys/socket.h: ########## @@ -280,6 +283,9 @@ #define SOL_RFCOMM 18 /* See options in include/netpacket/bluetooth.h */ #define SOL_PACKET 19 +#define SOL_NETLINK 20 + +/* Protocol-level socket options may begin with this value */ Review Comment: This comment has no definition after it. Also `PF_BRIDGE`, `AF_BRIDGE` (line 48, 68) and `SO_ATTACH_FILTER` (line 248) have no user in this PR, and `netlink_setsockopt()` returns `-ENOPROTOOPT` for `SO_ATTACH_FILTER`. Fix: remove the comment and the three definitions, or add them in the PR that uses them. ########## net/netlink/netlink_generic.c: ########## @@ -0,0 +1,690 @@ +/**************************************************************************** + * net/netlink/netlink_generic.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <sys/types.h> +#include <string.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> + +#include <nuttx/net/netlink.h> +#include <nuttx/net/genetlink.h> +#include <nuttx/kmalloc.h> +#include <nuttx/mutex.h> + +#include "netlink/netlink.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Family registry - stores pointers to registered families */ + +static FAR struct genl_family *g_genl_families[GENL_MAX_FAMILIES]; +static mutex_t g_genl_lock = NXMUTEX_INITIALIZER; +static int g_genl_family_count; +static uint16_t g_genl_next_id = GENL_START_ALLOC; +static uint16_t g_genl_next_mcgrp = 1; /* Multicast group IDs start at 1 */ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: add_mcast_grp_attr + * + * Description: + * Helper to serialize one multicast group as nested rtattr. + * + * Input Parameters: + * buf - Buffer to write the attribute data. + * offset - Current offset in the buffer. + * index - Index of the multicast group. + * gname - Name of the multicast group. + * gid - ID of the multicast group. + * + * Returned Value: + * The aligned length of the added attribute. + * + ****************************************************************************/ + +static int add_mcast_grp_attr(FAR uint8_t *buf, int offset, + int index, FAR const char *gname, int gid) +{ + FAR struct rtattr *grp = (FAR struct rtattr *)(buf + offset); + FAR uint8_t *ptr = (FAR uint8_t *)RTA_DATA(grp); + FAR struct rtattr *attr; + int len = 0; + + grp->rta_type = index | NLA_F_NESTED; + + /* Group Name */ + + attr = (FAR struct rtattr *)ptr; + attr->rta_type = CTRL_ATTR_MCAST_GRP_NAME; + attr->rta_len = RTA_LENGTH(strlen(gname) + 1); + strlcpy((FAR char *)RTA_DATA(attr), gname, GENL_NAMSIZ); + len += RTA_ALIGN(attr->rta_len); + + /* Group ID */ + + attr = (FAR struct rtattr *)(ptr + len); + attr->rta_type = CTRL_ATTR_MCAST_GRP_ID; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = gid; + len += RTA_ALIGN(attr->rta_len); + + grp->rta_len = RTA_LENGTH(len); + return RTA_ALIGN(grp->rta_len); +} + +/**************************************************************************** + * Name: send_family_info + * + * Description: + * Build and queue a CTRL_CMD_NEWFAMILY response for one family. + * This is used both for CTRL_CMD_GETFAMILY replies and DUMP mode. + * + * Input Parameters: + * conn - The netlink connection to send the response to. + * req - The request message header. + * family - The generic netlink family to report. + * dump - True if this is part of a dump operation (NLM_F_MULTI). + * + * Returned Value: + * The length of the netlink message on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int send_family_info(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req, + FAR const struct genl_family *family, + bool dump) +{ + FAR struct netlink_response_s *response; + FAR struct genlmsghdr *genl; + FAR struct nlmsghdr *nlh; + FAR struct rtattr *attr; + FAR uint8_t *attr_ptr; + FAR uint8_t *buf; + int attr_len; + int buf_len; + int offset; + + /* Calculate required buffer size: + * base headers + fixed attrs + family name + mcast groups + */ + + buf_len = NLMSG_HDRLEN + sizeof(struct genlmsghdr) + + RTA_SPACE(2) /* FAMILY_ID */ + + RTA_SPACE(GENL_NAMSIZ) /* FAMILY_NAME */ + + RTA_SPACE(4) * 3; /* VERSION+HDRSIZE+MAXATTR */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + int i; + + for (i = 0; i < family->n_mcgrps; i++) + { + buf_len += RTA_SPACE(RTA_SPACE(GENL_NAMSIZ) + RTA_SPACE(4)); + } + + buf_len += RTA_SPACE(0); /* mcast root header */ + } + + response = (FAR struct netlink_response_s *) + kmm_zalloc(sizeof(struct netlink_response_s) + NLMSG_ALIGN(buf_len)); + if (response == NULL) + { + return -ENOMEM; + } + + buf = (FAR uint8_t *)&response->msg; + nlh = (FAR struct nlmsghdr *)buf; + + nlh->nlmsg_type = GENL_ID_CTRL; + nlh->nlmsg_flags = dump ? NLM_F_MULTI : 0; + nlh->nlmsg_seq = req->nlmsg_seq; + nlh->nlmsg_pid = req->nlmsg_pid; + + genl = (FAR struct genlmsghdr *)NLMSG_DATA(nlh); + genl->cmd = CTRL_CMD_NEWFAMILY; + genl->version = 1; + + offset = NLMSG_ALIGN(sizeof(struct genlmsghdr)); + attr_ptr = (FAR uint8_t *)genl + offset; + + /* CTRL_ATTR_FAMILY_ID */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_FAMILY_ID; + attr->rta_len = RTA_LENGTH(2); + *(FAR uint16_t *)RTA_DATA(attr) = family->id; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_FAMILY_NAME */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_FAMILY_NAME; + attr->rta_len = RTA_LENGTH(strlen(family->name) + 1); + strlcpy((FAR char *)RTA_DATA(attr), family->name, GENL_NAMSIZ); + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_VERSION */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_VERSION; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->version; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_HDRSIZE */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_HDRSIZE; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->hdrsize; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_MAXATTR */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_MAXATTR; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->maxattr; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_MCAST_GROUPS — only if the family has multicast groups */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + FAR struct rtattr *mcast = (FAR struct rtattr *)attr_ptr; + FAR uint8_t *grp; + int grp_len = 0; + int i; + + mcast->rta_type = CTRL_ATTR_MCAST_GROUPS | NLA_F_NESTED; + grp = (FAR uint8_t *)RTA_DATA(mcast); + + for (i = 0; i < family->n_mcgrps; i++) + { + grp_len += add_mcast_grp_attr(grp, grp_len, + i + 1, + family->mcgrps[i].name, + family->mcgrp_offset + i); + } + + mcast->rta_len = RTA_LENGTH(grp_len); + attr_len = RTA_ALIGN(mcast->rta_len); + attr_ptr += attr_len; + offset += attr_len; + } + + nlh->nlmsg_len = NLMSG_LENGTH(offset); + + netlink_add_response(conn, response); + return nlh->nlmsg_len; +} + +/**************************************************************************** + * Name: send_ack_response + * + * Description: + * Send an ACK (NLMSG_ERROR with error=0) response. + * + * Input Parameters: + * conn - The netlink connection to send the ACK to. + * req - The request message header. + * + * Returned Value: + * Zero (OK) is returned on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int send_ack_response(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req) +{ + FAR struct netlink_response_s *response; + FAR struct nlmsgerr *err; + int payload_len = sizeof(struct nlmsgerr); + + response = (FAR struct netlink_response_s *) + kmm_zalloc(sizeof(struct netlink_response_s) + NLMSG_ALIGN(payload_len)); + if (response == NULL) + { + return -ENOMEM; + } + + response->msg.nlmsg_len = NLMSG_LENGTH(payload_len); + response->msg.nlmsg_type = NLMSG_ERROR; + response->msg.nlmsg_flags = 0; + response->msg.nlmsg_seq = req->nlmsg_seq; + response->msg.nlmsg_pid = req->nlmsg_pid; + + err = (FAR struct nlmsgerr *)NLMSG_DATA(&response->msg); + err->error = 0; + memcpy(&err->msg, req, sizeof(struct nlmsghdr)); + + netlink_add_response(conn, response); + return 0; +} + +/**************************************************************************** + * Name: netlink_ctrl_getfamily + * + * Description: + * Handle a CTRL_CMD_GETFAMILY request. Supports both single-family + * lookup by name and NLM_F_DUMP to enumerate all families. + * + * Input Parameters: + * conn - The netlink connection handling the request. + * req - The request message header. + * + * Returned Value: + * The length of the request message on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int netlink_ctrl_getfamily(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req) +{ + FAR const struct genl_family *fam; + FAR struct genlmsghdr *ghdr; + FAR struct nlattr *na; + FAR char *family_name = NULL; + int rem; + int ret; + int i; + + ghdr = (FAR struct genlmsghdr *)NLMSG_DATA(req); + na = (FAR struct nlattr *)((FAR char *)ghdr + GENL_HDRLEN); + rem = req->nlmsg_len - NLMSG_HDRLEN - GENL_HDRLEN; + + while (sizeof(struct nlattr) <= rem) + { + if (na->nla_len < sizeof(struct nlattr) || na->nla_len > rem) + { + break; + } + + if (nla_type(na) == CTRL_ATTR_FAMILY_NAME) + { + family_name = nla_data(na); + } + + na = nla_next(na, &rem); + } + + if (req->nlmsg_flags & NLM_F_DUMP) + { + FAR struct netlink_response_s *done_resp; + FAR struct nlmsghdr *done_hdr; + + /* Dump the built-in nlctrl family */ + + struct genl_family ctrl_fam; + + memset(&ctrl_fam, 0, sizeof(ctrl_fam)); + ctrl_fam.id = GENL_ID_CTRL; + strlcpy(ctrl_fam.name, "nlctrl", GENL_NAMSIZ); + ctrl_fam.version = 1; + ctrl_fam.maxattr = CTRL_ATTR_MAX; + send_family_info(conn, req, &ctrl_fam, true); + + /* Dump all registered families */ + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i]) + { + send_family_info(conn, req, g_genl_families[i], true); + } + } + + /* Send NLMSG_DONE terminator */ + + done_resp = kmm_zalloc(sizeof(*done_resp)); + if (done_resp == NULL) + { + return -ENOMEM; + } + + done_hdr = &done_resp->msg; + done_hdr->nlmsg_len = NLMSG_LENGTH(0); + done_hdr->nlmsg_type = NLMSG_DONE; + done_hdr->nlmsg_flags = NLM_F_MULTI; + done_hdr->nlmsg_seq = req->nlmsg_seq; + done_hdr->nlmsg_pid = req->nlmsg_pid; + + netlink_add_response(conn, done_resp); + return req->nlmsg_len; + } + + /* Single family lookup by name */ + + if (family_name) + { + /* Check built-in nlctrl */ + + if (strcmp(family_name, "nlctrl") == 0) + { + struct genl_family ctrl_fam; + + memset(&ctrl_fam, 0, sizeof(ctrl_fam)); + ctrl_fam.id = GENL_ID_CTRL; + strlcpy(ctrl_fam.name, "nlctrl", GENL_NAMSIZ); + ctrl_fam.version = 1; + ctrl_fam.maxattr = CTRL_ATTR_MAX; + send_family_info(conn, req, &ctrl_fam, false); + + if (req->nlmsg_flags & NLM_F_ACK) + { + send_ack_response(conn, req); + } + + return req->nlmsg_len; + } + + /* Search registered families */ + + fam = genl_family_find_byname(family_name); + if (fam) + { + ret = send_family_info(conn, req, fam, false); + if (ret < 0) + { + return ret; + } + + if (req->nlmsg_flags & NLM_F_ACK) + { + send_ack_response(conn, req); + } + + return req->nlmsg_len; + } + } + + nerr("netlink_ctrl_getfamily: family '%s' not found\n", + family_name ? family_name : "(null)"); + return -ENOENT; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: genl_register_family + * + * Description: + * Register a generic netlink family with the netlink subsystem. + * Allocates a dynamic family ID and multicast group IDs if needed. + * + * Input Parameters: + * family - Pointer to the generic netlink family structure to register. + * + * Returned Value: + * Zero (OK) is returned on success. + * -EEXIST is returned if a family with the same name already exists. + * -ENOMEM is returned if the maximum number of families is reached. + * + ****************************************************************************/ + +int genl_register_family(FAR struct genl_family *family) +{ + int ret = 0; + int i; + + DEBUGASSERT(family != NULL && family->name[0] != '\0'); + + nxmutex_lock(&g_genl_lock); + + /* Check for duplicate */ + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i] && + strcmp(g_genl_families[i]->name, family->name) == 0) + { + ret = -EEXIST; + goto out; + } + } + + if (g_genl_family_count >= GENL_MAX_FAMILIES) + { + ret = -ENOMEM; + goto out; + } + + /* Allocate a dynamic family ID */ + + family->id = g_genl_next_id++; Review Comment: `g_genl_next_id` and `g_genl_next_mcgrp` only increase; `genl_unregister_family()` gives nothing back. After some register and unregister cycles, the group ids are more than 32, but `NETLINK_ADD_MEMBERSHIP` and `netlink_add_broadcast()` accept only groups 1 to 32. The family id also has no limit at `GENL_MAX_ID`. Fix: return `-ENOSPC` when `g_genl_next_mcgrp + n_mcgrps - 1 > 32` or when the id is more than `GENL_MAX_ID`, or use the free ids again. ########## net/netlink/netlink_generic.c: ########## @@ -0,0 +1,690 @@ +/**************************************************************************** + * net/netlink/netlink_generic.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <sys/types.h> +#include <string.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> + +#include <nuttx/net/netlink.h> +#include <nuttx/net/genetlink.h> +#include <nuttx/kmalloc.h> +#include <nuttx/mutex.h> + +#include "netlink/netlink.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Family registry - stores pointers to registered families */ + +static FAR struct genl_family *g_genl_families[GENL_MAX_FAMILIES]; +static mutex_t g_genl_lock = NXMUTEX_INITIALIZER; +static int g_genl_family_count; +static uint16_t g_genl_next_id = GENL_START_ALLOC; +static uint16_t g_genl_next_mcgrp = 1; /* Multicast group IDs start at 1 */ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: add_mcast_grp_attr + * + * Description: + * Helper to serialize one multicast group as nested rtattr. + * + * Input Parameters: + * buf - Buffer to write the attribute data. + * offset - Current offset in the buffer. + * index - Index of the multicast group. + * gname - Name of the multicast group. + * gid - ID of the multicast group. + * + * Returned Value: + * The aligned length of the added attribute. + * + ****************************************************************************/ + +static int add_mcast_grp_attr(FAR uint8_t *buf, int offset, + int index, FAR const char *gname, int gid) +{ + FAR struct rtattr *grp = (FAR struct rtattr *)(buf + offset); + FAR uint8_t *ptr = (FAR uint8_t *)RTA_DATA(grp); + FAR struct rtattr *attr; + int len = 0; + + grp->rta_type = index | NLA_F_NESTED; + + /* Group Name */ + + attr = (FAR struct rtattr *)ptr; + attr->rta_type = CTRL_ATTR_MCAST_GRP_NAME; + attr->rta_len = RTA_LENGTH(strlen(gname) + 1); + strlcpy((FAR char *)RTA_DATA(attr), gname, GENL_NAMSIZ); + len += RTA_ALIGN(attr->rta_len); + + /* Group ID */ + + attr = (FAR struct rtattr *)(ptr + len); + attr->rta_type = CTRL_ATTR_MCAST_GRP_ID; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = gid; + len += RTA_ALIGN(attr->rta_len); + + grp->rta_len = RTA_LENGTH(len); + return RTA_ALIGN(grp->rta_len); +} + +/**************************************************************************** + * Name: send_family_info + * + * Description: + * Build and queue a CTRL_CMD_NEWFAMILY response for one family. + * This is used both for CTRL_CMD_GETFAMILY replies and DUMP mode. + * + * Input Parameters: + * conn - The netlink connection to send the response to. + * req - The request message header. + * family - The generic netlink family to report. + * dump - True if this is part of a dump operation (NLM_F_MULTI). + * + * Returned Value: + * The length of the netlink message on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int send_family_info(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req, + FAR const struct genl_family *family, + bool dump) +{ + FAR struct netlink_response_s *response; + FAR struct genlmsghdr *genl; + FAR struct nlmsghdr *nlh; + FAR struct rtattr *attr; + FAR uint8_t *attr_ptr; + FAR uint8_t *buf; + int attr_len; + int buf_len; + int offset; + + /* Calculate required buffer size: + * base headers + fixed attrs + family name + mcast groups + */ + + buf_len = NLMSG_HDRLEN + sizeof(struct genlmsghdr) + + RTA_SPACE(2) /* FAMILY_ID */ + + RTA_SPACE(GENL_NAMSIZ) /* FAMILY_NAME */ + + RTA_SPACE(4) * 3; /* VERSION+HDRSIZE+MAXATTR */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + int i; + + for (i = 0; i < family->n_mcgrps; i++) + { + buf_len += RTA_SPACE(RTA_SPACE(GENL_NAMSIZ) + RTA_SPACE(4)); + } + + buf_len += RTA_SPACE(0); /* mcast root header */ + } + + response = (FAR struct netlink_response_s *) + kmm_zalloc(sizeof(struct netlink_response_s) + NLMSG_ALIGN(buf_len)); + if (response == NULL) + { + return -ENOMEM; + } + + buf = (FAR uint8_t *)&response->msg; + nlh = (FAR struct nlmsghdr *)buf; + + nlh->nlmsg_type = GENL_ID_CTRL; + nlh->nlmsg_flags = dump ? NLM_F_MULTI : 0; + nlh->nlmsg_seq = req->nlmsg_seq; + nlh->nlmsg_pid = req->nlmsg_pid; + + genl = (FAR struct genlmsghdr *)NLMSG_DATA(nlh); + genl->cmd = CTRL_CMD_NEWFAMILY; + genl->version = 1; + + offset = NLMSG_ALIGN(sizeof(struct genlmsghdr)); + attr_ptr = (FAR uint8_t *)genl + offset; + + /* CTRL_ATTR_FAMILY_ID */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_FAMILY_ID; + attr->rta_len = RTA_LENGTH(2); + *(FAR uint16_t *)RTA_DATA(attr) = family->id; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_FAMILY_NAME */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_FAMILY_NAME; + attr->rta_len = RTA_LENGTH(strlen(family->name) + 1); + strlcpy((FAR char *)RTA_DATA(attr), family->name, GENL_NAMSIZ); + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_VERSION */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_VERSION; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->version; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_HDRSIZE */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_HDRSIZE; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->hdrsize; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_MAXATTR */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_MAXATTR; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->maxattr; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_MCAST_GROUPS — only if the family has multicast groups */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + FAR struct rtattr *mcast = (FAR struct rtattr *)attr_ptr; + FAR uint8_t *grp; + int grp_len = 0; + int i; + + mcast->rta_type = CTRL_ATTR_MCAST_GROUPS | NLA_F_NESTED; + grp = (FAR uint8_t *)RTA_DATA(mcast); + + for (i = 0; i < family->n_mcgrps; i++) + { + grp_len += add_mcast_grp_attr(grp, grp_len, + i + 1, + family->mcgrps[i].name, + family->mcgrp_offset + i); + } + + mcast->rta_len = RTA_LENGTH(grp_len); + attr_len = RTA_ALIGN(mcast->rta_len); + attr_ptr += attr_len; + offset += attr_len; + } + + nlh->nlmsg_len = NLMSG_LENGTH(offset); + + netlink_add_response(conn, response); + return nlh->nlmsg_len; +} + +/**************************************************************************** + * Name: send_ack_response + * + * Description: + * Send an ACK (NLMSG_ERROR with error=0) response. + * + * Input Parameters: + * conn - The netlink connection to send the ACK to. + * req - The request message header. + * + * Returned Value: + * Zero (OK) is returned on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int send_ack_response(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req) +{ + FAR struct netlink_response_s *response; + FAR struct nlmsgerr *err; + int payload_len = sizeof(struct nlmsgerr); + + response = (FAR struct netlink_response_s *) + kmm_zalloc(sizeof(struct netlink_response_s) + NLMSG_ALIGN(payload_len)); + if (response == NULL) + { + return -ENOMEM; + } + + response->msg.nlmsg_len = NLMSG_LENGTH(payload_len); + response->msg.nlmsg_type = NLMSG_ERROR; + response->msg.nlmsg_flags = 0; + response->msg.nlmsg_seq = req->nlmsg_seq; + response->msg.nlmsg_pid = req->nlmsg_pid; + + err = (FAR struct nlmsgerr *)NLMSG_DATA(&response->msg); + err->error = 0; + memcpy(&err->msg, req, sizeof(struct nlmsghdr)); + + netlink_add_response(conn, response); + return 0; +} + +/**************************************************************************** + * Name: netlink_ctrl_getfamily + * + * Description: + * Handle a CTRL_CMD_GETFAMILY request. Supports both single-family + * lookup by name and NLM_F_DUMP to enumerate all families. + * + * Input Parameters: + * conn - The netlink connection handling the request. + * req - The request message header. + * + * Returned Value: + * The length of the request message on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int netlink_ctrl_getfamily(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req) +{ + FAR const struct genl_family *fam; + FAR struct genlmsghdr *ghdr; + FAR struct nlattr *na; + FAR char *family_name = NULL; + int rem; + int ret; + int i; + + ghdr = (FAR struct genlmsghdr *)NLMSG_DATA(req); + na = (FAR struct nlattr *)((FAR char *)ghdr + GENL_HDRLEN); + rem = req->nlmsg_len - NLMSG_HDRLEN - GENL_HDRLEN; + + while (sizeof(struct nlattr) <= rem) + { + if (na->nla_len < sizeof(struct nlattr) || na->nla_len > rem) + { + break; + } + + if (nla_type(na) == CTRL_ATTR_FAMILY_NAME) + { + family_name = nla_data(na); + } + + na = nla_next(na, &rem); + } + + if (req->nlmsg_flags & NLM_F_DUMP) + { + FAR struct netlink_response_s *done_resp; + FAR struct nlmsghdr *done_hdr; + + /* Dump the built-in nlctrl family */ + + struct genl_family ctrl_fam; + + memset(&ctrl_fam, 0, sizeof(ctrl_fam)); + ctrl_fam.id = GENL_ID_CTRL; + strlcpy(ctrl_fam.name, "nlctrl", GENL_NAMSIZ); + ctrl_fam.version = 1; + ctrl_fam.maxattr = CTRL_ATTR_MAX; + send_family_info(conn, req, &ctrl_fam, true); + + /* Dump all registered families */ + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i]) + { + send_family_info(conn, req, g_genl_families[i], true); + } + } + + /* Send NLMSG_DONE terminator */ + + done_resp = kmm_zalloc(sizeof(*done_resp)); + if (done_resp == NULL) + { + return -ENOMEM; + } + + done_hdr = &done_resp->msg; + done_hdr->nlmsg_len = NLMSG_LENGTH(0); + done_hdr->nlmsg_type = NLMSG_DONE; + done_hdr->nlmsg_flags = NLM_F_MULTI; + done_hdr->nlmsg_seq = req->nlmsg_seq; + done_hdr->nlmsg_pid = req->nlmsg_pid; + + netlink_add_response(conn, done_resp); + return req->nlmsg_len; + } + + /* Single family lookup by name */ + + if (family_name) + { + /* Check built-in nlctrl */ + + if (strcmp(family_name, "nlctrl") == 0) + { + struct genl_family ctrl_fam; + + memset(&ctrl_fam, 0, sizeof(ctrl_fam)); + ctrl_fam.id = GENL_ID_CTRL; + strlcpy(ctrl_fam.name, "nlctrl", GENL_NAMSIZ); + ctrl_fam.version = 1; + ctrl_fam.maxattr = CTRL_ATTR_MAX; + send_family_info(conn, req, &ctrl_fam, false); + + if (req->nlmsg_flags & NLM_F_ACK) + { + send_ack_response(conn, req); + } + + return req->nlmsg_len; + } + + /* Search registered families */ + + fam = genl_family_find_byname(family_name); + if (fam) + { + ret = send_family_info(conn, req, fam, false); + if (ret < 0) + { + return ret; + } + + if (req->nlmsg_flags & NLM_F_ACK) + { + send_ack_response(conn, req); + } + + return req->nlmsg_len; + } + } + + nerr("netlink_ctrl_getfamily: family '%s' not found\n", + family_name ? family_name : "(null)"); + return -ENOENT; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: genl_register_family + * + * Description: + * Register a generic netlink family with the netlink subsystem. + * Allocates a dynamic family ID and multicast group IDs if needed. + * + * Input Parameters: + * family - Pointer to the generic netlink family structure to register. + * + * Returned Value: + * Zero (OK) is returned on success. + * -EEXIST is returned if a family with the same name already exists. + * -ENOMEM is returned if the maximum number of families is reached. + * + ****************************************************************************/ + +int genl_register_family(FAR struct genl_family *family) +{ + int ret = 0; + int i; + + DEBUGASSERT(family != NULL && family->name[0] != '\0'); + + nxmutex_lock(&g_genl_lock); + + /* Check for duplicate */ + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i] && + strcmp(g_genl_families[i]->name, family->name) == 0) + { + ret = -EEXIST; + goto out; + } + } + + if (g_genl_family_count >= GENL_MAX_FAMILIES) + { + ret = -ENOMEM; + goto out; + } + + /* Allocate a dynamic family ID */ + + family->id = g_genl_next_id++; + + /* Allocate multicast group IDs */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + family->mcgrp_offset = g_genl_next_mcgrp; + g_genl_next_mcgrp += family->n_mcgrps; + } + else + { + family->mcgrp_offset = 0; + } + + /* Store in registry */ + + g_genl_families[g_genl_family_count++] = family; + + ninfo("genl: registered family '%s' id=%d mcgrp_offset=%d\n", + family->name, family->id, family->mcgrp_offset); + +out: + nxmutex_unlock(&g_genl_lock); + return ret; +} + +/**************************************************************************** + * Name: genl_unregister_family + * + * Description: + * Unregister a generic netlink family from the netlink subsystem. + * + * Input Parameters: + * family - Pointer to the generic netlink family structure to unregister. + * + * Returned Value: + * Zero (OK) is returned on success. + * -ENOENT is returned if the family was not found in the registry. + * + ****************************************************************************/ + +int genl_unregister_family(FAR const struct genl_family *family) +{ + int ret = -ENOENT; + int i; + + DEBUGASSERT(family != NULL); + + nxmutex_lock(&g_genl_lock); + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i] == family) + { + /* Shift remaining entries down */ + + g_genl_family_count--; + g_genl_families[i] = g_genl_families[g_genl_family_count]; + g_genl_families[g_genl_family_count] = NULL; + ret = 0; + break; + } + } + + nxmutex_unlock(&g_genl_lock); + return ret; +} + +/**************************************************************************** + * Name: genl_family_find_byname + * + * Description: + * Find a registered generic netlink family by name. + * + * Input Parameters: + * name - The name of the family to find. + * + * Returned Value: + * Pointer to the generic netlink family structure if found. + * NULL is returned if the family was not found. + * + ****************************************************************************/ + +FAR const struct genl_family *genl_family_find_byname(FAR const char *name) +{ + int i; + + DEBUGASSERT(name != NULL); + + for (i = 0; i < g_genl_family_count; i++) Review Comment: `genl_family_find_byname()`, `genl_family_find_byid()` and the dump loop at line 374 read `g_genl_families` and `g_genl_family_count` without `g_genl_lock`. `genl_unregister_family()` moves the last entry into the free slot under the lock, so a lookup at the same time can miss a family or use one that is gone. Fix: hold `g_genl_lock` in the two lookups and around the dump loop. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
