From abba05a934aba3e4bf67568be558c3b7da5d90a1 Mon Sep 17 00:00:00 2001 From: SPRESENSE <41312067+SPRESENSE@users.noreply.github.com> Date: Wed, 18 Jan 2023 14:42:42 +0900 Subject: [PATCH] include/sys/socket.h: Add SOCK_CTRL to socket type SOCK_CTRL is added to provide special control over network drivers and daemons. Currently, SOCK_DGRAM and SOCK_STREAM perform this control, but these use socket resources. In the case of usersocket in particular, this is a waste of the device's limited socket resources. --- include/nuttx/net/netconfig.h | 44 ++++++------------------------ include/sys/socket.h | 4 +++ net/bluetooth/bluetooth_sockif.c | 2 +- net/can/can_sockif.c | 3 +- net/icmp/icmp_sockif.c | 5 ++-- net/icmpv6/icmpv6_sockif.c | 5 ++-- net/ieee802154/ieee802154_sockif.c | 2 +- net/inet/inet_sockif.c | 30 ++++++++++++++++++-- net/local/local_sockif.c | 21 ++++++++++++++ net/netlink/netlink_sockif.c | 3 +- net/pkt/pkt_sockif.c | 2 +- 11 files changed, 74 insertions(+), 47 deletions(-) diff --git a/include/nuttx/net/netconfig.h b/include/nuttx/net/netconfig.h index e9f3839408..b2e1b072d2 100644 --- a/include/nuttx/net/netconfig.h +++ b/include/nuttx/net/netconfig.h @@ -104,52 +104,24 @@ #define NET_SOCK_PROTOCOL 0 -/* SOCK_DGRAM is the preferred socket type to use when we just want a - * socket for performing driver ioctls. However, we can't use SOCK_DRAM - * if UDP is disabled. - * - * Pick a socket type (and perhaps protocol) compatible with the currently - * selected address family. +/* SOCK_CTRL is the preferred socket type to use when we just want a + * socket for performing driver ioctls. */ +#define NET_SOCK_TYPE SOCK_CTRL + #if NET_SOCK_FAMILY == AF_INET -# if defined(CONFIG_NET_UDP) -# define NET_SOCK_TYPE SOCK_DGRAM -# elif defined(CONFIG_NET_TCP) -# define NET_SOCK_TYPE SOCK_STREAM -# elif defined(CONFIG_NET_ICMP_SOCKET) -# define NET_SOCK_TYPE SOCK_DGRAM +# if !defined(CONFIG_NET_UDP) && !defined(CONFIG_NET_TCP) && \ + defined(CONFIG_NET_ICMP_SOCKET) # undef NET_SOCK_PROTOCOL # define NET_SOCK_PROTOCOL IPPROTO_ICMP # endif #elif NET_SOCK_FAMILY == AF_INET6 -# if defined(CONFIG_NET_UDP) -# define NET_SOCK_TYPE SOCK_DGRAM -# elif defined(CONFIG_NET_TCP) -# define NET_SOCK_TYPE SOCK_STREAM -# elif defined(CONFIG_NET_ICMPv6_SOCKET) -# define NET_SOCK_TYPE SOCK_DGRAM +# if !defined(CONFIG_NET_UDP) && !defined(CONFIG_NET_TCP) && \ + defined(CONFIG_NET_ICMPv6_SOCKET) # undef NET_SOCK_PROTOCOL # define NET_SOCK_PROTOCOL IPPROTO_ICMP6 # endif -#elif NET_SOCK_FAMILY == AF_LOCAL -# if defined(CONFIG_NET_LOCAL_DGRAM) -# define NET_SOCK_TYPE SOCK_DGRAM -# elif defined(CONFIG_NET_LOCAL_STREAM) -# define NET_SOCK_TYPE SOCK_STREAM -# endif -#elif NET_SOCK_FAMILY == AF_PACKET -# define NET_SOCK_TYPE SOCK_RAW -#elif NET_SOCK_FAMILY == AF_CAN -# define NET_SOCK_TYPE SOCK_RAW -#elif NET_SOCK_FAMILY == AF_IEEE802154 -# define NET_SOCK_TYPE SOCK_DGRAM -#elif NET_SOCK_FAMILY == AF_BLUETOOTH -# define NET_SOCK_TYPE SOCK_RAW -#elif NET_SOCK_FAMILY == AF_NETLINK -# define NET_SOCK_TYPE SOCK_DGRAM -#elif NET_SOCK_FAMILY == AF_RPMSG -# define NET_SOCK_TYPE SOCK_STREAM #endif /* Eliminate dependencies on other header files. This should not harm diff --git a/include/sys/socket.h b/include/sys/socket.h index 2226797ec4..b27df12f68 100644 --- a/include/sys/socket.h +++ b/include/sys/socket.h @@ -92,6 +92,10 @@ * required to read an entire packet with each read * system call. */ +#define SOCK_CTRL 6 /* SOCK_CTRL is the preferred socket type to use + * when we just want a socket for performing driver + * ioctls. This definition is not POSIX compliant. + */ #define SOCK_PACKET 10 /* Obsolete and should not be used in new programs */ #define SOCK_CLOEXEC 02000000 /* Atomically set close-on-exec flag for the new diff --git a/net/bluetooth/bluetooth_sockif.c b/net/bluetooth/bluetooth_sockif.c index b224a03d20..d9619e8a2a 100644 --- a/net/bluetooth/bluetooth_sockif.c +++ b/net/bluetooth/bluetooth_sockif.c @@ -166,7 +166,7 @@ static int bluetooth_setup(FAR struct socket *psock, int protocol) * Only SOCK_RAW is supported */ - if (psock->s_type == SOCK_RAW) + if (psock->s_type == SOCK_RAW || psock->s_type == SOCK_CTRL) { return bluetooth_sockif_alloc(psock); } diff --git a/net/can/can_sockif.c b/net/can/can_sockif.c index 9855e3b5fd..926e308eb5 100644 --- a/net/can/can_sockif.c +++ b/net/can/can_sockif.c @@ -207,7 +207,8 @@ static int can_setup(FAR struct socket *psock, int protocol) /* Verify the socket type (domain should always be PF_CAN here) */ - if (domain == PF_CAN && (type == SOCK_RAW || type == SOCK_DGRAM)) + if (domain == PF_CAN && + (type == SOCK_RAW || type == SOCK_DGRAM || type == SOCK_CTRL)) { /* Allocate the CAN socket connection structure and save it in the * new socket instance. diff --git a/net/icmp/icmp_sockif.c b/net/icmp/icmp_sockif.c index 311bce0847..2b1383410a 100644 --- a/net/icmp/icmp_sockif.c +++ b/net/icmp/icmp_sockif.c @@ -110,9 +110,10 @@ const struct sock_intf_s g_icmp_sockif = static int icmp_setup(FAR struct socket *psock, int protocol) { - /* Only SOCK_DGRAM and IPPROTO_ICMP are supported */ + /* Only SOCK_DGRAM or SOCK_CTRL and IPPROTO_ICMP are supported */ - if (psock->s_type == SOCK_DGRAM && protocol == IPPROTO_ICMP) + if ((psock->s_type == SOCK_DGRAM || psock->s_type == SOCK_CTRL) && + protocol == IPPROTO_ICMP) { /* Allocate the IPPROTO_ICMP socket connection structure and save in * the new socket instance. diff --git a/net/icmpv6/icmpv6_sockif.c b/net/icmpv6/icmpv6_sockif.c index 9b7015da9d..5ff1a4fb44 100644 --- a/net/icmpv6/icmpv6_sockif.c +++ b/net/icmpv6/icmpv6_sockif.c @@ -110,9 +110,10 @@ const struct sock_intf_s g_icmpv6_sockif = static int icmpv6_setup(FAR struct socket *psock, int protocol) { - /* Only SOCK_DGRAM and IPPROTO_ICMP6 are supported */ + /* Only SOCK_DGRAM or SOCK_CTRL and IPPROTO_ICMP6 are supported */ - if (psock->s_type == SOCK_DGRAM && protocol == IPPROTO_ICMP6) + if ((psock->s_type == SOCK_DGRAM || psock->s_type == SOCK_CTRL) && + protocol == IPPROTO_ICMP6) { /* Allocate the IPPROTO_ICMP6 socket connection structure and save in * the new socket instance. diff --git a/net/ieee802154/ieee802154_sockif.c b/net/ieee802154/ieee802154_sockif.c index d2b0303499..9f54251a56 100644 --- a/net/ieee802154/ieee802154_sockif.c +++ b/net/ieee802154/ieee802154_sockif.c @@ -156,7 +156,7 @@ static int ieee802154_setup(FAR struct socket *psock, int protocol) * Only SOCK_DGRAM is supported (since the MAC header is stripped) */ - if (psock->s_type == SOCK_DGRAM) + if (psock->s_type == SOCK_DGRAM || psock->s_type == SOCK_CTRL) { return ieee802154_sockif_alloc(psock); } diff --git a/net/inet/inet_sockif.c b/net/inet/inet_sockif.c index 61843db855..87d79099ee 100644 --- a/net/inet/inet_sockif.c +++ b/net/inet/inet_sockif.c @@ -286,6 +286,30 @@ static int inet_setup(FAR struct socket *psock, int protocol) #endif #endif /* CONFIG_NET_UDP */ +#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP) + case SOCK_CTRL: +# ifdef NET_UDP_HAVE_STACK + if (protocol == 0 || protocol == IPPROTO_UDP) + { + /* Allocate and attach the UDP connection structure */ + + return inet_udp_alloc(psock); + } + +# endif +# ifdef NET_TCP_HAVE_STACK + if (protocol == 0 || protocol == IPPROTO_TCP) + { + /* Allocate and attach the TCP connection structure */ + + return inet_tcp_alloc(psock); + } + +# endif + nerr("ERROR: Unsupported control protocol: %d\n", protocol); + return -EPROTONOSUPPORT; +#endif /* CONFIG_NET_TCP || CONFIG_NET_UDP */ + default: nerr("ERROR: Unsupported type: %d\n", psock->s_type); return -EPROTONOSUPPORT; @@ -2310,7 +2334,8 @@ FAR const struct sock_intf_s * #if defined(HAVE_PFINET_SOCKETS) && defined(CONFIG_NET_ICMP_SOCKET) /* PF_INET, ICMP data gram sockets are a special case of raw sockets */ - if (family == PF_INET && type == SOCK_DGRAM && protocol == IPPROTO_ICMP) + if (family == PF_INET && (type == SOCK_DGRAM || type == SOCK_CTRL) && + protocol == IPPROTO_ICMP) { return &g_icmp_sockif; } @@ -2319,7 +2344,8 @@ FAR const struct sock_intf_s * #if defined(HAVE_PFINET6_SOCKETS) && defined(CONFIG_NET_ICMPv6_SOCKET) /* PF_INET, ICMP data gram sockets are a special case of raw sockets */ - if (family == PF_INET6 && type == SOCK_DGRAM && protocol == IPPROTO_ICMP6) + if (family == PF_INET6 && (type == SOCK_DGRAM || type == SOCK_CTRL) && + protocol == IPPROTO_ICMP6) { return &g_icmpv6_sockif; } diff --git a/net/local/local_sockif.c b/net/local/local_sockif.c index 754ef7757f..d84fef519d 100644 --- a/net/local/local_sockif.c +++ b/net/local/local_sockif.c @@ -205,6 +205,27 @@ static int local_setup(FAR struct socket *psock, int protocol) return local_sockif_alloc(psock); #endif /* CONFIG_NET_LOCAL_DGRAM */ + case SOCK_CTRL: +#ifdef CONFIG_NET_LOCAL_STREAM + if (protocol == 0 || protocol == IPPROTO_TCP) + { + /* Allocate and attach the local connection structure */ + + return local_sockif_alloc(psock); + } + +#endif +#ifdef CONFIG_NET_LOCAL_DGRAM + if (protocol == 0 || protocol == IPPROTO_UDP) + { + /* Allocate and attach the local connection structure */ + + return local_sockif_alloc(psock); + } + +#endif + return -EPROTONOSUPPORT; + default: return -EPROTONOSUPPORT; } diff --git a/net/netlink/netlink_sockif.c b/net/netlink/netlink_sockif.c index 88dd30a9af..bf1a94b849 100644 --- a/net/netlink/netlink_sockif.c +++ b/net/netlink/netlink_sockif.c @@ -136,7 +136,8 @@ static int netlink_setup(FAR struct socket *psock, int protocol) /* Verify the socket type (domain should always be PF_NETLINK here) */ - if (domain == PF_NETLINK && (type == SOCK_RAW || type == SOCK_DGRAM)) + if (domain == PF_NETLINK && + (type == SOCK_RAW || type == SOCK_DGRAM || type == SOCK_CTRL)) { /* Allocate the NetLink socket connection structure and save it in the * new socket instance. diff --git a/net/pkt/pkt_sockif.c b/net/pkt/pkt_sockif.c index 4309e82363..dfc9c70839 100644 --- a/net/pkt/pkt_sockif.c +++ b/net/pkt/pkt_sockif.c @@ -154,7 +154,7 @@ static int pkt_setup(FAR struct socket *psock, int protocol) * Only SOCK_RAW is supported. */ - if (psock->s_type == SOCK_RAW) + if (psock->s_type == SOCK_RAW || psock->s_type == SOCK_CTRL) { return pkt_sockif_alloc(psock); }