2018-08-03 21:22:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/netlink/netlink_sockif.c
|
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* 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
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* 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.
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <stdbool.h>
|
2019-11-03 20:59:42 +01:00
|
|
|
#include <string.h>
|
2019-11-27 01:52:22 +01:00
|
|
|
#include <poll.h>
|
2019-11-28 04:30:01 +01:00
|
|
|
#include <sched.h>
|
2018-08-03 21:22:36 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2019-11-27 01:52:22 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2019-11-28 21:20:40 +01:00
|
|
|
#include <nuttx/wqueue.h>
|
2018-08-03 21:22:36 +02:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
|
|
|
#include "netlink/netlink.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_NETLINK
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
static int netlink_setup(FAR struct socket *psock);
|
2018-08-03 21:22:36 +02:00
|
|
|
static sockcaps_t netlink_sockcaps(FAR struct socket *psock);
|
|
|
|
static void netlink_addref(FAR struct socket *psock);
|
|
|
|
static int netlink_bind(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR const struct sockaddr *addr, socklen_t addrlen);
|
2018-08-03 21:22:36 +02:00
|
|
|
static int netlink_getsockname(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen);
|
2018-08-03 21:22:36 +02:00
|
|
|
static int netlink_getpeername(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen);
|
2018-08-03 21:22:36 +02:00
|
|
|
static int netlink_connect(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen);
|
2018-08-03 21:22:36 +02:00
|
|
|
static int netlink_poll(FAR struct socket *psock, FAR struct pollfd *fds,
|
2023-11-19 12:19:53 +01:00
|
|
|
bool setup);
|
2020-12-01 07:55:16 +01:00
|
|
|
static ssize_t netlink_sendmsg(FAR struct socket *psock,
|
|
|
|
FAR struct msghdr *msg, int flags);
|
|
|
|
static ssize_t netlink_recvmsg(FAR struct socket *psock,
|
|
|
|
FAR struct msghdr *msg, int flags);
|
2018-08-03 21:22:36 +02:00
|
|
|
static int netlink_close(FAR struct socket *psock);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
const struct sock_intf_s g_netlink_sockif =
|
|
|
|
{
|
|
|
|
netlink_setup, /* si_setup */
|
|
|
|
netlink_sockcaps, /* si_sockcaps */
|
|
|
|
netlink_addref, /* si_addref */
|
|
|
|
netlink_bind, /* si_bind */
|
|
|
|
netlink_getsockname, /* si_getsockname */
|
|
|
|
netlink_getpeername, /* si_getpeername */
|
2023-03-06 06:08:24 +01:00
|
|
|
NULL, /* si_listen */
|
2018-08-03 21:22:36 +02:00
|
|
|
netlink_connect, /* si_connect */
|
2023-03-06 06:16:26 +01:00
|
|
|
NULL, /* si_accept */
|
2018-08-03 21:22:36 +02:00
|
|
|
netlink_poll, /* si_poll */
|
2020-12-01 07:55:16 +01:00
|
|
|
netlink_sendmsg, /* si_sendmsg */
|
|
|
|
netlink_recvmsg, /* si_recvmsg */
|
2018-08-03 21:22:36 +02:00
|
|
|
netlink_close /* si_close */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-04-07 19:44:23 +02:00
|
|
|
* Name: netlink_setup
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called for socket() to verify that the provided socket type and
|
|
|
|
* protocol are usable by this address family. Perform any family-
|
|
|
|
* specific socket fields.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2019-11-03 20:59:42 +01:00
|
|
|
* psock - A pointer to a user allocated socket structure to be
|
|
|
|
* initialized.
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-09-14 14:55:45 +02:00
|
|
|
* Zero (OK) is returned on success. Otherwise, a negated errno value is
|
2018-08-03 21:22:36 +02:00
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
static int netlink_setup(FAR struct socket *psock)
|
2018-08-03 21:22:36 +02:00
|
|
|
{
|
|
|
|
int domain = psock->s_domain;
|
|
|
|
int type = psock->s_type;
|
2023-02-06 18:55:44 +01:00
|
|
|
int proto = psock->s_proto;
|
2018-08-03 21:22:36 +02:00
|
|
|
|
2019-11-03 20:59:42 +01:00
|
|
|
/* Verify that the protocol is supported */
|
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
DEBUGASSERT((unsigned int)proto <= UINT8_MAX);
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
switch (proto)
|
2018-08-03 21:22:36 +02:00
|
|
|
{
|
2019-11-03 20:59:42 +01:00
|
|
|
#ifdef CONFIG_NETLINK_ROUTE
|
|
|
|
case NETLINK_ROUTE:
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 04:22:24 +01:00
|
|
|
#ifdef CONFIG_NETLINK_NETFILTER
|
|
|
|
case NETLINK_NETFILTER:
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2019-11-03 20:59:42 +01:00
|
|
|
default:
|
|
|
|
return -EPROTONOSUPPORT;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
2019-11-03 20:59:42 +01:00
|
|
|
|
|
|
|
/* Verify the socket type (domain should always be PF_NETLINK here) */
|
|
|
|
|
2023-01-18 06:42:42 +01:00
|
|
|
if (domain == PF_NETLINK &&
|
|
|
|
(type == SOCK_RAW || type == SOCK_DGRAM || type == SOCK_CTRL))
|
2018-08-03 21:22:36 +02:00
|
|
|
{
|
2019-11-04 21:06:07 +01:00
|
|
|
/* Allocate the NetLink socket connection structure and save it in the
|
2019-11-03 20:59:42 +01:00
|
|
|
* new socket instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR struct netlink_conn_s *conn = netlink_alloc();
|
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
/* Failed to reserve a connection structure */
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the reference count on the connection structure. This
|
|
|
|
* reference count will be incremented only if the socket is
|
|
|
|
* dup'ed
|
|
|
|
*/
|
|
|
|
|
|
|
|
conn->crefs = 1;
|
|
|
|
|
|
|
|
/* Attach the connection instance to the socket */
|
|
|
|
|
|
|
|
psock->s_conn = conn;
|
|
|
|
return OK;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
2019-11-03 20:59:42 +01:00
|
|
|
return -EPROTONOSUPPORT;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_sockcaps
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the bit encoded capabilities of this socket.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - Socket structure of the socket whose capabilities are being
|
|
|
|
* queried.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The non-negative set of socket capabilities is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static sockcaps_t netlink_sockcaps(FAR struct socket *psock)
|
|
|
|
{
|
2019-11-19 14:50:46 +01:00
|
|
|
/* Permit vfcntl to set socket to non-blocking */
|
|
|
|
|
|
|
|
return SOCKCAP_NONBLOCKING;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_addref
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Increment the reference count on the underlying connection structure.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - Socket structure of the socket whose reference count will be
|
|
|
|
* incremented.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void netlink_addref(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
FAR struct netlink_conn_s *conn;
|
|
|
|
|
|
|
|
conn = psock->s_conn;
|
|
|
|
DEBUGASSERT(conn->crefs > 0 && conn->crefs < 255);
|
|
|
|
conn->crefs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* netlink_bind() gives the socket 'conn' the local address 'addr'. 'addr'
|
|
|
|
* is 'addrlen' bytes long. Traditionally, this is called "assigning a name
|
|
|
|
* to a socket." When a socket is created with socket, it exists in a name
|
|
|
|
* space (address family) but has no name assigned.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2019-11-04 21:06:07 +01:00
|
|
|
* conn NetLink socket connection structure
|
2018-08-03 21:22:36 +02:00
|
|
|
* addr Socket local address
|
|
|
|
* addrlen Length of 'addr'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; -1 on error with errno set appropriately
|
|
|
|
*
|
|
|
|
* EACCES
|
|
|
|
* The address is protected, and the user is not the superuser.
|
|
|
|
* EADDRINUSE
|
|
|
|
* The given address is already in use.
|
|
|
|
* EINVAL
|
|
|
|
* The socket is already bound to an address.
|
|
|
|
* ENOTSOCK
|
|
|
|
* psock is a descriptor for a file, not a socket.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int netlink_bind(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr *addr, socklen_t addrlen)
|
|
|
|
{
|
2019-11-03 20:59:42 +01:00
|
|
|
FAR struct sockaddr_nl *nladdr;
|
|
|
|
FAR struct netlink_conn_s *conn;
|
|
|
|
|
2023-08-30 08:46:41 +02:00
|
|
|
DEBUGASSERT(addrlen >= sizeof(struct sockaddr_nl));
|
2019-11-03 20:59:42 +01:00
|
|
|
|
|
|
|
/* Save the address information in the connection structure */
|
|
|
|
|
2020-04-07 20:31:57 +02:00
|
|
|
nladdr = (FAR struct sockaddr_nl *)addr;
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2023-02-01 11:05:58 +01:00
|
|
|
conn->pid = nladdr->nl_pid ? nladdr->nl_pid : nxsched_gettid();
|
2020-04-07 20:31:57 +02:00
|
|
|
conn->groups = nladdr->nl_groups;
|
2019-11-03 20:59:42 +01:00
|
|
|
|
|
|
|
return OK;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_getsockname
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The getsockname() function retrieves the locally-bound name of the
|
|
|
|
* specified socket, stores this address in the sockaddr structure pointed
|
|
|
|
* to by the 'addr' argument, and stores the length of this address in the
|
|
|
|
* object pointed to by the 'addrlen' argument.
|
|
|
|
*
|
|
|
|
* If the actual length of the address is greater than the length of the
|
|
|
|
* supplied sockaddr structure, the stored address will be truncated.
|
|
|
|
*
|
|
|
|
* If the socket has not been bound to a local name, the value stored in
|
|
|
|
* the object pointed to by address is unspecified.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2019-11-04 21:06:07 +01:00
|
|
|
* conn NetLink socket connection structure
|
2018-08-03 21:22:36 +02:00
|
|
|
* addr sockaddr structure to receive data [out]
|
|
|
|
* addrlen Length of sockaddr structure [in/out]
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int netlink_getsockname(FAR struct socket *psock,
|
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
|
|
|
{
|
2019-11-03 20:59:42 +01:00
|
|
|
FAR struct sockaddr_nl *nladdr;
|
2020-04-07 20:31:57 +02:00
|
|
|
FAR struct netlink_conn_s *conn;
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2023-08-30 08:46:41 +02:00
|
|
|
DEBUGASSERT(*addrlen >= sizeof(struct sockaddr_nl));
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2020-04-07 20:31:57 +02:00
|
|
|
|
2019-11-03 20:59:42 +01:00
|
|
|
/* Return the address information in the address structure */
|
|
|
|
|
|
|
|
nladdr = (FAR struct sockaddr_nl *)addr;
|
|
|
|
memset(nladdr, 0, sizeof(struct sockaddr_nl));
|
|
|
|
|
2020-04-07 20:31:57 +02:00
|
|
|
nladdr->nl_family = AF_NETLINK;
|
|
|
|
nladdr->nl_pid = conn->pid;
|
|
|
|
nladdr->nl_groups = conn->groups;
|
2019-11-03 20:59:42 +01:00
|
|
|
|
|
|
|
*addrlen = sizeof(struct sockaddr_nl);
|
|
|
|
return OK;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_getpeername
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The netlink_getpeername() function retrieves the remote-connected name
|
|
|
|
* of the specified packet socket, stores this address in the sockaddr
|
|
|
|
* structure pointed to by the 'addr' argument, and stores the length of
|
|
|
|
* this address in the object pointed to by the 'addrlen' argument.
|
|
|
|
*
|
|
|
|
* If the actual length of the address is greater than the length of the
|
|
|
|
* supplied sockaddr structure, the stored address will be truncated.
|
|
|
|
*
|
|
|
|
* If the socket has not been bound to a local name, the value stored in
|
|
|
|
* the object pointed to by address is unspecified.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* psock Socket structure of the socket to be queried
|
|
|
|
* addr sockaddr structure to receive data [out]
|
|
|
|
* addrlen Length of sockaddr structure [in/out]
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, 0 is returned, the 'addr' argument points to the address
|
|
|
|
* of the socket, and the 'addrlen' argument points to the length of the
|
|
|
|
* address. Otherwise, a negated errno value is returned. See
|
|
|
|
* getpeername() for the list of appropriate error numbers.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int netlink_getpeername(FAR struct socket *psock,
|
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
|
|
|
{
|
2020-04-07 20:31:57 +02:00
|
|
|
FAR struct sockaddr_nl *nladdr;
|
|
|
|
FAR struct netlink_conn_s *conn;
|
|
|
|
|
2023-08-30 08:46:41 +02:00
|
|
|
DEBUGASSERT(*addrlen >= sizeof(struct sockaddr_nl));
|
2020-04-07 20:31:57 +02:00
|
|
|
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2020-04-07 20:31:57 +02:00
|
|
|
|
|
|
|
/* Return the address information in the address structure */
|
|
|
|
|
|
|
|
nladdr = (FAR struct sockaddr_nl *)addr;
|
|
|
|
memset(nladdr, 0, sizeof(struct sockaddr_nl));
|
|
|
|
|
|
|
|
nladdr->nl_family = AF_NETLINK;
|
|
|
|
nladdr->nl_pid = conn->dst_pid;
|
|
|
|
nladdr->nl_groups = conn->dst_groups;
|
|
|
|
|
|
|
|
*addrlen = sizeof(struct sockaddr_nl);
|
|
|
|
return OK;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_connect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform a netlink connection
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-04-10 15:03:34 +02:00
|
|
|
* psock A reference to the structure of the socket to be connected
|
2018-08-03 21:22:36 +02:00
|
|
|
* addr The address of the remote server to connect to
|
|
|
|
* addrlen Length of address buffer
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int netlink_connect(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen)
|
|
|
|
{
|
2020-04-07 20:31:57 +02:00
|
|
|
FAR struct sockaddr_nl *nladdr;
|
|
|
|
FAR struct netlink_conn_s *conn;
|
|
|
|
|
2023-08-30 08:46:41 +02:00
|
|
|
DEBUGASSERT(addrlen >= sizeof(struct sockaddr_nl));
|
2020-04-07 20:31:57 +02:00
|
|
|
|
|
|
|
/* Save the address information in the connection structure */
|
|
|
|
|
|
|
|
nladdr = (FAR struct sockaddr_nl *)addr;
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2020-04-07 20:31:57 +02:00
|
|
|
|
|
|
|
conn->dst_pid = nladdr->nl_pid;
|
|
|
|
conn->dst_groups = nladdr->nl_groups;
|
|
|
|
|
|
|
|
return OK;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 04:30:01 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_response_available
|
|
|
|
*
|
|
|
|
* Description:
|
2019-11-28 21:20:40 +01:00
|
|
|
* Handle a Netlink response available notification.
|
2019-11-28 04:30:01 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2019-11-28 21:20:40 +01:00
|
|
|
* Standard work handler parameters
|
2019-11-28 04:30:01 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
static void netlink_response_available(FAR void *arg)
|
2019-11-28 04:30:01 +01:00
|
|
|
{
|
2020-01-19 10:44:27 +01:00
|
|
|
FAR struct netlink_conn_s *conn = arg;
|
2019-11-28 04:30:01 +01:00
|
|
|
|
2020-01-19 10:44:27 +01:00
|
|
|
DEBUGASSERT(conn != NULL);
|
2019-11-28 04:30:01 +01:00
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
/* The following should always be true ... but maybe not in some race
|
|
|
|
* condition?
|
2019-11-28 04:30:01 +01:00
|
|
|
*/
|
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
net_lock();
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
if (conn->fds != NULL)
|
2019-11-28 04:30:01 +01:00
|
|
|
{
|
|
|
|
/* Wake up the poll() with POLLIN */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&conn->fds, 1, POLLIN);
|
2019-11-28 04:30:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nwarn("WARNING: Missing references in connection.\n");
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
/* Allow another poll() */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
conn->fds = NULL;
|
2019-11-28 21:20:40 +01:00
|
|
|
|
|
|
|
net_unlock();
|
2019-11-28 04:30:01 +01:00
|
|
|
}
|
|
|
|
|
2018-08-03 21:22:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_poll
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard poll() operation redirects operations on socket descriptors
|
|
|
|
* to this function.
|
|
|
|
*
|
2019-11-27 01:52:22 +01:00
|
|
|
* POLLUP: Will never be reported
|
|
|
|
* POLLERR: Reported in the event of any failure.
|
|
|
|
* POLLOUT: Always reported if requested.
|
|
|
|
* POLLIN: Reported if requested but only when pending response data is
|
|
|
|
* available
|
|
|
|
*
|
2018-08-03 21:22:36 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* psock - An instance of the internal socket structure.
|
|
|
|
* fds - The structure describing the events to be monitored.
|
2019-11-04 21:06:07 +01:00
|
|
|
* setup - true: Setup up the poll; false: Tear down the poll
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int netlink_poll(FAR struct socket *psock, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
2019-11-28 04:30:01 +01:00
|
|
|
FAR struct netlink_conn_s *conn;
|
2020-04-07 19:50:01 +02:00
|
|
|
int ret = OK;
|
2019-11-27 01:52:22 +01:00
|
|
|
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2019-11-28 04:30:01 +01:00
|
|
|
|
2019-11-27 01:52:22 +01:00
|
|
|
/* Check if we are setting up or tearing down the poll */
|
|
|
|
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
/* If POLLOUT is selected, return immediately (maybe) */
|
|
|
|
|
|
|
|
pollevent_t revents = POLLOUT;
|
|
|
|
|
|
|
|
/* If POLLIN is selected and a response is available, return
|
|
|
|
* immediately (maybe).
|
|
|
|
*/
|
|
|
|
|
|
|
|
net_lock();
|
2020-04-13 14:49:52 +02:00
|
|
|
if (netlink_check_response(conn))
|
2019-11-27 01:52:22 +01:00
|
|
|
{
|
|
|
|
revents |= POLLIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* But return ONLY if POLLIN and/or POLLIN are included in the
|
|
|
|
* requested event set.
|
|
|
|
*/
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&fds, 1, revents);
|
|
|
|
if (fds->revents != 0)
|
2019-11-27 01:52:22 +01:00
|
|
|
{
|
|
|
|
net_unlock();
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
/* Set up to be notified when a response is available if POLLIN is
|
|
|
|
* requested.
|
2019-11-27 01:52:22 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if ((fds->events & POLLIN) != 0)
|
|
|
|
{
|
2019-11-28 21:20:40 +01:00
|
|
|
/* Some limitations: There can be only a single outstanding POLLIN
|
|
|
|
* on the Netlink connection.
|
2019-11-27 01:52:22 +01:00
|
|
|
*/
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
if (conn->fds != NULL)
|
2019-11-28 04:30:01 +01:00
|
|
|
{
|
2019-11-28 21:20:40 +01:00
|
|
|
nerr("ERROR: Multiple polls() on socket not supported.\n");
|
|
|
|
net_unlock();
|
|
|
|
return -EBUSY;
|
2019-11-28 04:30:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
/* Set up the notification */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
conn->fds = fds;
|
2019-11-28 04:30:01 +01:00
|
|
|
|
2020-04-10 15:03:34 +02:00
|
|
|
ret = netlink_notifier_setup(netlink_response_available,
|
|
|
|
conn, conn);
|
2019-11-28 04:30:01 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-11-28 21:20:40 +01:00
|
|
|
nerr("ERROR: netlink_notifier_setup() failed: %d\n", ret);
|
2022-09-19 05:08:57 +02:00
|
|
|
conn->fds = NULL;
|
2019-11-28 04:30:01 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-27 01:52:22 +01:00
|
|
|
|
|
|
|
net_unlock();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Cancel any response notifications */
|
|
|
|
|
2022-05-13 12:24:38 +02:00
|
|
|
netlink_notifier_teardown(conn);
|
2022-09-19 05:08:57 +02:00
|
|
|
conn->fds = NULL;
|
2019-11-27 01:52:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-12-01 07:55:16 +01:00
|
|
|
* Name: netlink_sendmsg
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-12-01 07:55:16 +01:00
|
|
|
* If sendmsg() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET)
|
|
|
|
* socket, the parameters 'msg_name' and 'msg_namelen' are ignored (and the
|
|
|
|
* error EISCONN may be returned when they are not NULL and 0), and the
|
|
|
|
* error ENOTCONN is returned when the socket was not actually connected.
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-12-01 07:55:16 +01:00
|
|
|
* psock A reference to the structure of the socket to be connected
|
|
|
|
* msg msg to send
|
|
|
|
* flags Send flags (ignored)
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, returns the number of characters sent. On error, a negated
|
2020-12-01 07:55:16 +01:00
|
|
|
* errno value is returned (see sendmsg() for the list of appropriate error
|
2018-08-03 21:22:36 +02:00
|
|
|
* values.
|
|
|
|
*
|
2020-12-01 07:55:16 +01:00
|
|
|
* Assumptions:
|
|
|
|
*
|
2018-08-03 21:22:36 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
static ssize_t netlink_sendmsg(FAR struct socket *psock,
|
|
|
|
FAR struct msghdr *msg, int flags)
|
2018-08-03 21:22:36 +02:00
|
|
|
{
|
2020-12-01 07:55:16 +01:00
|
|
|
FAR const void *buf = msg->msg_iov->iov_base;
|
|
|
|
FAR const struct sockaddr *to = msg->msg_name;
|
|
|
|
socklen_t tolen = msg->msg_namelen;
|
2020-04-07 20:31:57 +02:00
|
|
|
FAR struct netlink_conn_s *conn;
|
2020-12-01 07:55:16 +01:00
|
|
|
FAR struct nlmsghdr *nlmsg;
|
2020-04-07 20:31:57 +02:00
|
|
|
struct sockaddr_nl nladdr;
|
2020-12-01 07:55:16 +01:00
|
|
|
int ret;
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
/* Validity check, only single iov supported */
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
if (msg->msg_iovlen != 1)
|
|
|
|
{
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
/* Get the underlying connection structure */
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2020-12-01 07:55:16 +01:00
|
|
|
if (to == NULL)
|
|
|
|
{
|
|
|
|
/* netlink_send() */
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
/* Format the address */
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
nladdr.nl_family = AF_NETLINK;
|
|
|
|
nladdr.nl_pad = 0;
|
|
|
|
nladdr.nl_pid = conn->dst_pid;
|
|
|
|
nladdr.nl_groups = conn->dst_groups;
|
2018-08-03 21:22:36 +02:00
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
to = (FAR const struct sockaddr *)&nladdr;
|
|
|
|
tolen = sizeof(struct sockaddr_nl);
|
|
|
|
}
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
DEBUGASSERT(tolen >= sizeof(struct sockaddr_nl));
|
2019-11-03 20:59:42 +01:00
|
|
|
|
|
|
|
/* Get a reference to the netlink message */
|
|
|
|
|
|
|
|
nlmsg = (FAR struct nlmsghdr *)buf;
|
2019-11-04 02:27:58 +01:00
|
|
|
DEBUGASSERT(nlmsg->nlmsg_len >= sizeof(struct nlmsghdr));
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
switch (psock->s_proto)
|
2019-11-03 20:59:42 +01:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_NETLINK_ROUTE
|
|
|
|
case NETLINK_ROUTE:
|
2022-01-17 05:16:33 +01:00
|
|
|
ret = netlink_route_sendto(conn, nlmsg,
|
|
|
|
msg->msg_iov->iov_len, flags,
|
2020-12-01 07:55:16 +01:00
|
|
|
(FAR const struct sockaddr_nl *)to,
|
2019-11-03 20:59:42 +01:00
|
|
|
tolen);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 04:22:24 +01:00
|
|
|
#ifdef CONFIG_NETLINK_NETFILTER
|
|
|
|
case NETLINK_NETFILTER:
|
|
|
|
ret = netlink_netfilter_sendto(conn, nlmsg,
|
|
|
|
msg->msg_iov->iov_len, flags,
|
|
|
|
(FAR const struct sockaddr_nl *)to,
|
|
|
|
tolen);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2019-11-03 20:59:42 +01:00
|
|
|
default:
|
2019-11-28 21:20:40 +01:00
|
|
|
ret = -EOPNOTSUPP;
|
2019-11-03 20:59:42 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-12-01 07:55:16 +01:00
|
|
|
* Name: netlink_recvmsg
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-12-01 07:55:16 +01:00
|
|
|
* recvmsg() receives messages from a socket, and may be used to receive
|
2018-08-03 21:22:36 +02:00
|
|
|
* data on a socket whether or not it is connection-oriented.
|
|
|
|
*
|
2020-12-01 07:55:16 +01:00
|
|
|
* If msg_name is not NULL, and the underlying protocol provides the source
|
|
|
|
* address, this source address is filled in. The argument 'msg_namelen' is
|
|
|
|
* initialized to the size of the buffer associated with msg_name, and
|
|
|
|
* modified on return to indicate the actual size of the address stored
|
|
|
|
* there.
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock A pointer to a NuttX-specific, internal socket structure
|
2020-12-01 07:55:16 +01:00
|
|
|
* msg Buffer to receive the message
|
2018-08-03 21:22:36 +02:00
|
|
|
* flags Receive flags (ignored)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
static ssize_t netlink_recvmsg(FAR struct socket *psock,
|
|
|
|
FAR struct msghdr *msg, int flags)
|
2018-08-03 21:22:36 +02:00
|
|
|
{
|
2020-12-01 07:55:16 +01:00
|
|
|
FAR void *buf = msg->msg_iov->iov_base;
|
|
|
|
size_t len = msg->msg_iov->iov_len;
|
|
|
|
FAR struct sockaddr *from = msg->msg_name;
|
|
|
|
FAR socklen_t *fromlen = &msg->msg_namelen;
|
2020-04-08 12:30:28 +02:00
|
|
|
FAR struct netlink_response_s *entry;
|
2022-02-08 06:18:01 +01:00
|
|
|
FAR struct socket_conn_s *conn;
|
2023-12-19 11:38:44 +01:00
|
|
|
int ret = OK;
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2019-11-04 02:27:58 +01:00
|
|
|
DEBUGASSERT(from == NULL ||
|
|
|
|
(fromlen != NULL && *fromlen >= sizeof(struct sockaddr_nl)));
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2020-04-08 12:30:28 +02:00
|
|
|
/* Find the response to this message. The return value */
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2020-04-13 14:49:52 +02:00
|
|
|
entry = netlink_tryget_response(psock->s_conn);
|
2020-04-08 12:30:28 +02:00
|
|
|
if (entry == NULL)
|
|
|
|
{
|
2022-02-08 06:18:01 +01:00
|
|
|
conn = psock->s_conn;
|
|
|
|
|
2020-04-08 12:30:28 +02:00
|
|
|
/* No response is variable, but presumably, one is expected. Check
|
|
|
|
* if the socket has been configured for non-blocking operation.
|
|
|
|
*/
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (_SS_ISNONBLOCK(conn->s_flags) || (flags & MSG_DONTWAIT) != 0)
|
2020-04-08 12:30:28 +02:00
|
|
|
{
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2023-12-19 11:38:44 +01:00
|
|
|
/* Wait for the response. */
|
|
|
|
|
|
|
|
ret = netlink_get_response(psock->s_conn, &entry);
|
|
|
|
|
|
|
|
/* If interrupted by signals, return errno */
|
2020-04-08 12:30:28 +02:00
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
2023-12-19 11:38:44 +01:00
|
|
|
return ret;
|
2020-04-08 12:30:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > entry->msg.nlmsg_len)
|
2019-11-03 20:59:42 +01:00
|
|
|
{
|
2020-04-08 12:30:28 +02:00
|
|
|
len = entry->msg.nlmsg_len;
|
|
|
|
}
|
2019-11-04 02:27:58 +01:00
|
|
|
|
2020-04-08 12:30:28 +02:00
|
|
|
/* Copy the payload to the user buffer */
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2020-04-08 12:30:28 +02:00
|
|
|
memcpy(buf, &entry->msg, len);
|
|
|
|
kmm_free(entry);
|
|
|
|
|
|
|
|
if (from != NULL)
|
|
|
|
{
|
|
|
|
netlink_getpeername(psock, from, fromlen);
|
2019-11-03 20:59:42 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 12:30:28 +02:00
|
|
|
return len;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_close
|
|
|
|
*
|
|
|
|
* Description:
|
2019-11-04 21:06:07 +01:00
|
|
|
* Performs the close operation on a NetLink socket instance
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock Socket instance
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; -1 on error with errno set appropriately.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int netlink_close(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
FAR struct netlink_conn_s *conn = psock->s_conn;
|
2019-11-03 20:59:42 +01:00
|
|
|
int ret = OK;
|
2018-08-03 21:22:36 +02:00
|
|
|
|
|
|
|
/* Perform some pre-close operations for the NETLINK socket type. */
|
|
|
|
|
|
|
|
/* Is this the last reference to the connection structure (there
|
|
|
|
* could be more if the socket was dup'ed).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (conn->crefs <= 1)
|
|
|
|
{
|
|
|
|
/* Free the connection structure */
|
|
|
|
|
|
|
|
conn->crefs = 0;
|
|
|
|
netlink_free(psock->s_conn);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Return with error code, but free resources. */
|
|
|
|
|
|
|
|
nerr("ERROR: netlink_close failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. Just decrement the reference count */
|
|
|
|
|
|
|
|
conn->crefs--;
|
|
|
|
}
|
|
|
|
|
2019-11-03 20:59:42 +01:00
|
|
|
return ret;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_NETLINK */
|