int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) { _cleanup_netlink_message_unref_ sd_netlink_message *m = NULL; const NLType *nl_type; size_t size; int r; r = type_system_get_type(&type_system_root, &nl_type, type); if (r < 0) return r; if (type_get_type(nl_type) != NETLINK_TYPE_NESTED) return -EINVAL; r = message_new_empty(rtnl, &m); if (r < 0) return r; size = NLMSG_SPACE(type_get_size(nl_type)); assert(size >= sizeof(struct nlmsghdr)); m->hdr = malloc0(size); if (!m->hdr) return -ENOMEM; m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; type_get_type_system(nl_type, &m->containers[0].type_system); m->hdr->nlmsg_len = size; m->hdr->nlmsg_type = type; *ret = m; m = NULL; return 0; }
int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t type) { _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL; const NLType *nl_type; size_t size; int r; r = type_system_get_type(NULL, &nl_type, type); if (r < 0) return r; assert(nl_type->type == NLA_NESTED); r = message_new_empty(rtnl, &m); if (r < 0) return r; size = NLMSG_SPACE(nl_type->size); assert(size >= sizeof(struct nlmsghdr)); m->hdr = malloc0(size); if (!m->hdr) return -ENOMEM; m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; m->container_type_system[0] = nl_type->type_system; m->hdr->nlmsg_len = size; m->hdr->nlmsg_type = type; *ret = m; m = NULL; return 0; }
static int genl_message_new(sd_netlink *nl, sd_genl_family family, uint16_t nlmsg_type, uint8_t cmd, sd_netlink_message **ret) { int r; struct genlmsghdr *genl; const NLType *genl_cmd_type, *nl_type; const NLTypeSystem *type_system; size_t size; _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; assert_return(nl->protocol == NETLINK_GENERIC, -EINVAL); r = type_system_get_type(&genl_family_type_system_root, &genl_cmd_type, family); if (r < 0) return r; r = message_new_empty(nl, &m); if (r < 0) return r; size = NLMSG_SPACE(sizeof(struct genlmsghdr)); m->hdr = malloc0(size); if (!m->hdr) return -ENOMEM; m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; type_get_type_system(genl_cmd_type, &type_system); r = type_system_get_type(type_system, &nl_type, cmd); if (r < 0) return r; m->hdr->nlmsg_len = size; m->hdr->nlmsg_type = nlmsg_type; type_get_type_system(nl_type, &m->containers[0].type_system); genl = NLMSG_DATA(m->hdr); genl->cmd = cmd; genl->version = genl_families[family].version; *ret = TAKE_PTR(m); return 0; }
/* returns the number of bytes sent, or a negative error code */ int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m) { union { struct sockaddr sa; struct sockaddr_nl nl; } addr = { .nl.nl_family = AF_NETLINK, }; ssize_t k; assert(nl); assert(m); assert(m->hdr); k = sendto(nl->fd, m->hdr, m->hdr->nlmsg_len, 0, &addr.sa, sizeof(addr)); if (k < 0) return (errno == EAGAIN) ? 0 : -errno; return k; } static int socket_recv_message(int fd, struct iovec *iov, uint32_t *_group, bool peek) { uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(struct nl_pktinfo))]; struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 1, .msg_control = cred_buffer, .msg_controllen = sizeof(cred_buffer), }; struct cmsghdr *cmsg; uint32_t group = 0; bool auth = false; int r; assert(fd >= 0); assert(iov); r = recvmsg(fd, &msg, MSG_TRUNC | (peek ? MSG_PEEK : 0)); if (r < 0) { /* no data */ if (errno == ENOBUFS) log_debug("rtnl: kernel receive buffer overrun"); return (errno == EAGAIN) ? 0 : -errno; } else if (r == 0) /* connection was closed by the kernel */ return -ECONNRESET; for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_CREDENTIALS && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) { struct ucred *ucred = (void *)CMSG_DATA(cmsg); /* from the kernel */ if (ucred->uid == 0 && ucred->pid == 0) auth = true; } else if (cmsg->cmsg_level == SOL_NETLINK && cmsg->cmsg_type == NETLINK_PKTINFO && cmsg->cmsg_len == CMSG_LEN(sizeof(struct nl_pktinfo))) { struct nl_pktinfo *pktinfo = (void *)CMSG_DATA(cmsg); /* multi-cast group */ group = pktinfo->group; } } if (!auth) /* not from the kernel, ignore */ return 0; if (group) *_group = group; return r; } /* On success, the number of bytes received is returned and *ret points to the received message * which has a valid header and the correct size. * If nothing useful was received 0 is returned. * On failure, a negative error code is returned. */ int socket_read_message(sd_rtnl *rtnl) { _cleanup_rtnl_message_unref_ sd_rtnl_message *first = NULL; struct iovec iov = {}; uint32_t group = 0; bool multi_part = false, done = false; struct nlmsghdr *new_msg; size_t len; int r; unsigned i = 0; assert(rtnl); assert(rtnl->rbuffer); assert(rtnl->rbuffer_allocated >= sizeof(struct nlmsghdr)); /* read nothing, just get the pending message size */ r = socket_recv_message(rtnl->fd, &iov, &group, true); if (r <= 0) return r; else len = (size_t)r; /* make room for the pending message */ if (!greedy_realloc((void **)&rtnl->rbuffer, &rtnl->rbuffer_allocated, len, sizeof(uint8_t))) return -ENOMEM; iov.iov_base = rtnl->rbuffer; iov.iov_len = rtnl->rbuffer_allocated; /* read the pending message */ r = socket_recv_message(rtnl->fd, &iov, &group, false); if (r <= 0) return r; else len = (size_t)r; if (len > rtnl->rbuffer_allocated) /* message did not fit in read buffer */ return -EIO; if (NLMSG_OK(rtnl->rbuffer, len) && rtnl->rbuffer->nlmsg_flags & NLM_F_MULTI) { multi_part = true; for (i = 0; i < rtnl->rqueue_partial_size; i++) { if (rtnl_message_get_serial(rtnl->rqueue_partial[i]) == rtnl->rbuffer->nlmsg_seq) { first = rtnl->rqueue_partial[i]; break; } } } for (new_msg = rtnl->rbuffer; NLMSG_OK(new_msg, len); new_msg = NLMSG_NEXT(new_msg, len)) { _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL; const NLType *nl_type; if (!group && new_msg->nlmsg_pid != rtnl->sockaddr.nl.nl_pid) /* not broadcast and not for us */ continue; if (new_msg->nlmsg_type == NLMSG_NOOP) /* silently drop noop messages */ continue; if (new_msg->nlmsg_type == NLMSG_DONE) { /* finished reading multi-part message */ done = true; break; } /* check that we support this message type */ r = type_system_get_type(NULL, &nl_type, new_msg->nlmsg_type); if (r < 0) { if (r == -ENOTSUP) log_debug("sd-rtnl: ignored message with unknown type: %u", new_msg->nlmsg_type); continue; } /* check that the size matches the message type */ if (new_msg->nlmsg_len < NLMSG_LENGTH(nl_type->size)) continue; r = message_new_empty(rtnl, &m); if (r < 0) return r; m->hdr = memdup(new_msg, new_msg->nlmsg_len); if (!m->hdr) return -ENOMEM; /* seal and parse the top-level message */ r = sd_rtnl_message_rewind(m); if (r < 0) return r; /* push the message onto the multi-part message stack */ if (first) m->next = first; first = m; m = NULL; } if (len) log_debug("sd-rtnl: discarding %zu bytes of incoming message", len); if (!first) return 0; if (!multi_part || done) { /* we got a complete message, push it on the read queue */ r = rtnl_rqueue_make_room(rtnl); if (r < 0) return r; rtnl->rqueue[rtnl->rqueue_size ++] = first; first = NULL; if (multi_part && (i < rtnl->rqueue_partial_size)) { /* remove the message form the partial read queue */ memmove(rtnl->rqueue_partial + i,rtnl->rqueue_partial + i + 1, sizeof(sd_rtnl_message*) * (rtnl->rqueue_partial_size - i - 1)); rtnl->rqueue_partial_size --; } return 1; } else { /* we only got a partial multi-part message, push it on the partial read queue */ if (i < rtnl->rqueue_partial_size) { rtnl->rqueue_partial[i] = first; } else { r = rtnl_rqueue_partial_make_room(rtnl); if (r < 0) return r; rtnl->rqueue_partial[rtnl->rqueue_partial_size ++] = first; } first = NULL; return 0; } }