Changeset View
Changeset View
Standalone View
Standalone View
sys/netlink/netlink_io.c
- This file was added.
/*- | |||||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD | |||||
* | |||||
* Copyright (c) 2021 Ng Peng Nam Sean | |||||
Lint: Possible Spelling Mistake: Possible spelling error. You wrote 'nam', but did you mean 'name'? | |||||
* Copyright (c) 2022 Alexander V. Chernikov | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in the | |||||
* documentation and/or other materials provided with the distribution. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
*/ | |||||
#include <sys/cdefs.h> | |||||
__FBSDID("$FreeBSD$"); | |||||
#include <sys/param.h> | |||||
#include <sys/malloc.h> | |||||
#include <sys/lock.h> | |||||
#include <sys/rmlock.h> | |||||
#include <sys/mbuf.h> | |||||
#include <sys/ck.h> | |||||
#include <sys/socket.h> | |||||
#include <sys/socketvar.h> | |||||
#include <sys/syslog.h> | |||||
#include <net/if.h> | |||||
#include <net/if_var.h> | |||||
#include <net/netisr.h> | |||||
#include <netlink/netlink.h> | |||||
#include <netlink/netlink_ctl.h> | |||||
#include <netlink/netlink_var.h> | |||||
#define DEBUG_MOD_NAME nl_io | |||||
#define DEBUG_MAX_LEVEL LOG_DEBUG3 | |||||
#include <net/route/route_debug.h> | |||||
_DECLARE_DEBUG(LOG_DEBUG3); | |||||
static struct sockaddr_nl _nl_empty_src = { | |||||
.nl_len = sizeof(struct sockaddr_nl), | |||||
.nl_family = PF_NETLINK, | |||||
.nl_pid = 0 /* comes from the kernel */ | |||||
}; | |||||
static struct sockaddr *nl_empty_src = (struct sockaddr *)&_nl_empty_src; | |||||
static int nl_receive(struct mbuf *m, struct nlpcb *nlp); | |||||
Done Inline ActionsCan it be const? glebius: Can it be const? | |||||
int | |||||
nl_receive_async(struct mbuf *m, struct socket *so) | |||||
{ | |||||
struct nlpcb *nlp = sotonlpcb(so); | |||||
m->m_nextpkt = NULL; | |||||
NLP_LOCK(nlp); | |||||
if ((__predict_false(!nlp->nl_active))) { | |||||
NLP_UNLOCK(nlp); | |||||
m_free(m); | |||||
return (EINVAL); | |||||
} | |||||
/* XXX: Implement queue limits */ | |||||
if (nlp->nl_queue_head == NULL) { | |||||
nlp->nl_queue_head = m; | |||||
nlp->nl_queue_last = m; | |||||
} else { | |||||
nlp->nl_queue_last->m_nextpkt = m; | |||||
nlp->nl_queue_last = m; | |||||
} | |||||
nlp->nl_queue_length += m_length(m, NULL); | |||||
RT_LOG(LOG_DEBUG3, "enqueue, total len %ld", nlp->nl_queue_length); | |||||
if (!nlp->nl_task_pending) { | |||||
nlp->nl_task_pending = true; | |||||
taskqueue_enqueue(nlp->nl_taskqueue, &nlp->nl_task); | |||||
} | |||||
NLP_UNLOCK(nlp); | |||||
return (0); | |||||
} | |||||
static void | |||||
nl_process_received(struct nlpcb *nlp) | |||||
{ | |||||
struct mbuf *m; | |||||
NLP_LOCK(nlp); | |||||
m = nlp->nl_queue_head; | |||||
nlp->nl_queue_head = NULL; | |||||
nlp->nl_queue_last = NULL; | |||||
nlp->nl_queue_length = 0; | |||||
nlp->nl_task_pending = false; | |||||
NLP_UNLOCK(nlp); | |||||
RT_LOG(LOG_DEBUG2, "taskqueue called"); | |||||
while (m != NULL) { | |||||
struct mbuf *m_next = m->m_nextpkt; | |||||
m->m_nextpkt = NULL; | |||||
nl_receive(m, nlp); | |||||
m = m_next; | |||||
} | |||||
} | |||||
void | |||||
nl_taskqueue_handler(void *_arg, int pending) | |||||
{ | |||||
struct nlpcb *nlp = (struct nlpcb *)_arg; | |||||
struct epoch_tracker et; | |||||
CURVNET_SET(nlp->nl_socket->so_vnet); | |||||
NET_EPOCH_ENTER(et); | |||||
nl_process_received(nlp); | |||||
NET_EPOCH_EXIT(et); | |||||
CURVNET_RESTORE(); | |||||
} | |||||
bool | |||||
nl_send_one(struct mbuf *m, struct nlpcb *nlp) | |||||
{ | |||||
#if DEBUG_MAX_LEVEL > LOG_DEBUG2 | |||||
struct nlmsghdr *hdr = mtod(m, struct nlmsghdr *); | |||||
RT_LOG(LOG_DEBUG2, "TX mbuf len %u msg type %d first hdrlen %u", | |||||
m->m_len, hdr->nlmsg_type, hdr->nlmsg_len); | |||||
#endif | |||||
bool result = false; | |||||
NLP_LOCK(nlp); | |||||
if (nlp->nl_socket != NULL) { | |||||
struct socket *so = nlp->nl_socket; | |||||
if (sbappendaddr(&so->so_rcv, nl_empty_src, m, NULL) != 0) { | |||||
sorwakeup(so); | |||||
RT_LOG(LOG_DEBUG3, "TX done"); | |||||
result = true; | |||||
} else { | |||||
soroverflow(so); | |||||
m_freem(m); | |||||
RT_LOG(LOG_DEBUG, "socket RX overflow for PID %u", | |||||
nlp->nl_process_id); | |||||
} | |||||
} | |||||
NLP_UNLOCK(nlp); | |||||
return (result); | |||||
} | |||||
static bool | |||||
nl_send_one_wrapper(struct mbuf *m, struct nlpcb *nlp) | |||||
{ | |||||
if (nlp->nl_linux) { | |||||
m = mbufs_to_linux(m, nlp); | |||||
if (m == NULL) | |||||
return (false); | |||||
} | |||||
return (nl_send_one(m, nlp)); | |||||
} | |||||
/* | |||||
* Used when certain data needs to be broadcasted to the group | |||||
*/ | |||||
void | |||||
nl_send_group(struct mbuf *m, uint32_t groups_mask) | |||||
{ | |||||
struct nlpcb *nlp_last = NULL; | |||||
struct nlpcb *nlp; | |||||
NLCTL_TRACKER; | |||||
#if DEBUG_MAX_LEVEL > LOG_DEBUG2 | |||||
struct nlmsghdr *hdr = mtod(m, struct nlmsghdr *); | |||||
RT_LOG(LOG_DEBUG2, "MCAST mbuf len %u msg type %d len %u to groups 0x%X", | |||||
m->m_len, hdr->nlmsg_type, hdr->nlmsg_len, groups_mask); | |||||
#endif | |||||
struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl); | |||||
if (ctl == NULL) { | |||||
/* | |||||
* Can be the case when notification is sent within VNET | |||||
* which doesn't have any netlink sockets. | |||||
*/ | |||||
m_freem(m); | |||||
return; | |||||
} | |||||
NLCTL_RLOCK(ctl); | |||||
CK_LIST_FOREACH(nlp, &V_nl_ctl->ctl_pcb_head, nl_next) { | |||||
if (nlp->nl_groups & groups_mask) { | |||||
if (nlp_last != NULL) { | |||||
struct mbuf *m_copy; | |||||
m_copy = m_copym(m, 0, M_COPYALL, M_NOWAIT); | |||||
if (m_copy != NULL) | |||||
nl_send_one_wrapper(m_copy, nlp_last); | |||||
else { | |||||
NLP_LOCK(nlp_last); | |||||
if (nlp_last->nl_socket != NULL) | |||||
sorwakeup(nlp_last->nl_socket); | |||||
NLP_UNLOCK(nlp_last); | |||||
} | |||||
} | |||||
nlp_last = nlp; | |||||
} | |||||
} | |||||
if (nlp_last != NULL) | |||||
nl_send_one_wrapper(m, nlp_last); | |||||
else | |||||
m_freem(m); | |||||
NLCTL_RUNLOCK(ctl); | |||||
} | |||||
/* | |||||
* Sends an ack message | |||||
*/ | |||||
void | |||||
nlmsg_ack(struct nlpcb *nlp, int error, struct nlmsghdr *nlmsg) | |||||
{ | |||||
struct nlmsgerr *errmsg; | |||||
int payload_len; | |||||
uint32_t flags = nlp->nl_flags; | |||||
struct nlmsg_state ns; | |||||
bool cap_ack; | |||||
payload_len = sizeof(struct nlmsgerr); | |||||
/* | |||||
* The only case when we send the full message in the | |||||
* reply is when there is an error and NETLINK_CAP_ACK | |||||
* is not set. | |||||
*/ | |||||
cap_ack = (error == 0) || (flags & NLF_CAP_ACK); | |||||
if (!cap_ack) | |||||
payload_len += nlmsg->nlmsg_len - sizeof(struct nlmsghdr); | |||||
/* | |||||
* TODO: handle NETLINK_F_EXT_ACK sockopt | |||||
* TODO: handle cookies | |||||
*/ | |||||
int sz = payload_len + sizeof(struct nlmsghdr); | |||||
if (!nlmsg_get_socket_writer(sz, nlp, &ns)) { | |||||
RT_LOG(LOG_NOTICE, "error allocating nlmsg(%d)", sz); | |||||
return; | |||||
} | |||||
RT_LOG(LOG_DEBUG, "type-%d;payload-%d;pid-%d;seq-%d", NLMSG_ERROR, payload_len, | |||||
nlp->nl_port, nlmsg->nlmsg_seq); | |||||
nlmsg_add(&ns, nlp->nl_port, nlmsg->nlmsg_seq, NLMSG_ERROR, 0, payload_len); | |||||
errmsg = nlmsg_reserve_data(&ns, payload_len, struct nlmsgerr); | |||||
errmsg->error = error; | |||||
/* In case of error copy the whole message, else just the header */ | |||||
memcpy(&errmsg->msg, nlmsg, cap_ack ? sizeof(*nlmsg) : nlmsg->nlmsg_len); | |||||
nlmsg_end(&ns); | |||||
nlmsg_flush(&ns); | |||||
} | |||||
static int | |||||
nl_receive_message(struct nlmsghdr *hdr, int remaining_length, | |||||
struct nlpcb *nlp, struct netlink_parse_tracker *npt) | |||||
{ | |||||
nl_handler handler = nl_handlers[nlp->nl_proto]; | |||||
int error = 0; | |||||
RT_LOG(LOG_DEBUG3, "msg len: %d type: %d", hdr->nlmsg_len, hdr->nlmsg_type); | |||||
if (__predict_false(hdr->nlmsg_len > remaining_length)) { | |||||
RT_LOG(LOG_DEBUG, "invalid message"); | |||||
return (EINVAL); | |||||
} else if (__predict_false(hdr->nlmsg_len < sizeof(*hdr))) { | |||||
RT_LOG(LOG_DEBUG, "message too short: %d", hdr->nlmsg_len); | |||||
return (EINVAL); | |||||
} | |||||
/* Stamp each message with sender pid */ | |||||
hdr->nlmsg_pid = nlp->nl_port; | |||||
if (hdr->nlmsg_flags & NLM_F_REQUEST && hdr->nlmsg_type >= NLMSG_MIN_TYPE) { | |||||
RT_LOG(LOG_DEBUG2, "handling message with msg type: %d", | |||||
hdr->nlmsg_type); | |||||
struct nlmsghdr *thdr = hdr; | |||||
if (nlp->nl_linux) | |||||
thdr = nlmsg_from_linux(hdr, npt); | |||||
error = handler(thdr, npt); | |||||
RT_LOG(LOG_DEBUG2, "retcode: %d", error); | |||||
} | |||||
if ((hdr->nlmsg_flags & NLM_F_ACK) || (error != 0 && error != EINTR)) { | |||||
RT_LOG(LOG_DEBUG3, "ack"); | |||||
nlmsg_ack(nlp, error, hdr); | |||||
RT_LOG(LOG_DEBUG3, "done"); | |||||
} | |||||
return (0); | |||||
} | |||||
/* | |||||
* Processes an incoming packet, which can contain multiple netlink messages | |||||
*/ | |||||
static int | |||||
nl_receive(struct mbuf *m, struct nlpcb *nlp) | |||||
{ | |||||
int offset, buffer_length, error = 0; | |||||
struct nlmsghdr *hdr; | |||||
char *buffer; | |||||
RT_LOG(LOG_DEBUG, "RX netlink mbuf %p on %p", m, nlp->nl_socket); | |||||
int data_length = m_length(m, NULL); | |||||
buffer_length = roundup2(data_length, 8) + SCRATCH_BUFFER_SIZE; | |||||
if (nlp->nl_linux) | |||||
buffer_length += roundup2(data_length, 8); | |||||
buffer = malloc(buffer_length, M_NETLINK, M_NOWAIT | M_ZERO); | |||||
if (buffer == NULL) { | |||||
m_freem(m); | |||||
RT_LOG(LOG_DEBUG, "Unable to allocate %d bytes of memory", | |||||
buffer_length); | |||||
return (ENOMEM); | |||||
} | |||||
m_copydata(m, 0, data_length, buffer); | |||||
m_freem(m); // XXX: reuse for ack? | |||||
struct netlink_parse_tracker npt = { | |||||
.nlp = nlp, | |||||
.lb.base = &buffer[roundup2(data_length, 8)], | |||||
.lb.size = buffer_length - roundup2(data_length, 8), | |||||
}; | |||||
for (offset = 0; offset + sizeof(struct nlmsghdr) <= data_length;) { | |||||
hdr = (struct nlmsghdr *)&buffer[offset]; | |||||
/* Save length prior to calling handler */ | |||||
int msglen = NLMSG_ALIGN(hdr->nlmsg_len); | |||||
RT_LOG(LOG_DEBUG2, "parsing offset %d/%d", offset, data_length); | |||||
/* Update parse state */ | |||||
npt.hdr = hdr; | |||||
lb_clear(&npt.lb); | |||||
error = nl_receive_message(hdr, data_length - offset, nlp, &npt); | |||||
if (__predict_false(error != 0)) | |||||
break; | |||||
offset += msglen; | |||||
} | |||||
RT_LOG(LOG_DEBUG2, "packet parsing done"); | |||||
free(buffer, M_NETLINK); | |||||
return (error); | |||||
} |
Possible spelling error. You wrote 'nam', but did you mean 'name'?