From 1c29d56938deb25dcb8d2be98a686f6c08ed53c9 Mon Sep 17 00:00:00 2001 From: gaohedong Date: Tue, 13 Oct 2026 16:14:43 +0800 Subject: [PATCH 1/6] net/udp: initialize readahead meta variables in udp_readahead. Coverity CID 3934125 / 3934127 (MISRA C-2012 Rule 9.1): src_addr_size and srcaddr are read before being explicitly initialized. CID 3934125 points at the iob_copyout() call that uses src_addr_size as the copy length, CID 3934127 at the memcpy() that copies srcaddr into msg_name. Both are filled in by an earlier iob_copyout(), but iob_copyout() has two early returns (-ESPIPE on a bad offset, 0 when the iob chain is exhausted) that write nothing, so Rule 9.1 is violated on those paths. In practice those paths are unreachable here: udp_callback() writes the whole |datalen|ifindex|src_addr_size|src_addr| header into one iob with consecutive iob_trycopyin() calls and drops the packet entirely if any of them fails, so the reader always finds a complete fixed-size header. The DEBUGASSERT() checks that follow cannot stand in for initialization either - they run after the read and compile to nothing when CONFIG_DEBUG_ASSERTIONS is disabled. This change is therefore a static-compliance fix with no runtime behaviour change. Note the earlier attempt (change 7995723) initialized only srcaddr and left src_addr_size untouched, which is why CID 3934125 kept recurring. Signed-off-by: gaohedong --- net/udp/udp_recvfrom.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/net/udp/udp_recvfrom.c b/net/udp/udp_recvfrom.c index 822907f954e3e..8cbf97233b832 100644 --- a/net/udp/udp_recvfrom.c +++ b/net/udp/udp_recvfrom.c @@ -164,7 +164,7 @@ static inline void udp_readahead(struct udp_recvfrom_s *pstate) int offset = 0; int pos; uint16_t datalen = 0; - uint8_t src_addr_size; + uint8_t src_addr_size = 0; uint8_t ifindex; uint8_t meta[UDP_READAHEAD_META_SIZE]; #ifdef CONFIG_NET_IPv6 @@ -173,6 +173,8 @@ static inline void udp_readahead(struct udp_recvfrom_s *pstate) uint8_t srcaddr[sizeof(struct sockaddr_in)]; #endif + memset(srcaddr, 0, sizeof(srcaddr)); + /* Unflatten saved connection information * Layout: |datalen|ifindex|src_addr_size|src_addr|[timestamp]|data| */ From 931722c4b921fb950b17a347bb1bb100eec07503 Mon Sep 17 00:00:00 2001 From: zhanghongyu Date: Mon, 23 Nov 2026 21:44:13 +0800 Subject: [PATCH 2/6] net/udp: Count datagrams dropped when the read-ahead buffer is full. udp_callback() drops an incoming datagram when the read-ahead buffer already holds more than SO_RCVBUF bytes, but returned without touching the statistics. The drop was therefore invisible in both /proc/net/stat and the netdev statistics, while the neighbouring "no free read-ahead buffers" path does count it. Count the datagram and log it, as that path already does. Assisted-by: Kiro:claude-opus-4 Signed-off-by: zhanghongyu --- net/udp/udp_callback.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/net/udp/udp_callback.c b/net/udp/udp_callback.c index a5c9eaee21808..0ef8923a33776 100644 --- a/net/udp/udp_callback.c +++ b/net/udp/udp_callback.c @@ -84,6 +84,14 @@ static uint16_t udp_datahandler(FAR struct net_driver_s *dev, #if CONFIG_NET_RECV_BUFSIZE > 0 if (conn->readahead && conn->readahead->io_pktlen > conn->rcvbufs) { + /* The read-ahead buffer is full, drop the datagram. */ + + ninfo("Dropped %d bytes\n", dev->d_len); + +#ifdef CONFIG_NET_STATISTICS + g_netstats.udp.drop++; +#endif + conn_unlock(&conn->sconn); netdev_iob_release(dev); return 0; From ab22fdc6161b29783e905c4cc316c5ef2a023f1b Mon Sep 17 00:00:00 2001 From: zhangshuai39 Date: Thu, 4 Jun 2026 10:10:04 +0800 Subject: [PATCH 3/6] net: use connection laddr as IPv4 source for TCP and UDP send. The IPv4 send paths in tcp_sendcommon() and udp_send() built the outgoing IP header with dev->d_ipaddr as the source address. On a multi-interface host this is wrong when the connection's bound local IP lives on a different interface than the one the packet is leaving on: the peer sees a source IP that does not match the destination it originally addressed and rejects the segment. Use conn->u.ipv4.laddr instead, mirroring the existing IPv6 paths in the same functions which use netdev_ipv6_srcaddr() with the same intent. TCP can use laddr unconditionally because it is always populated (tcp_alloc_accept copies ip->destipaddr; tcp_connect fills from conn->dev->d_ipaddr when INADDR_ANY). UDP keeps a fallback to dev->d_ipaddr because INADDR_ANY-bound sockets are common. Single-interface devices and INADDR_ANY-bound UDP sockets are unaffected: the new path produces the same value as before. Signed-off-by: zhangshuai39 --- net/tcp/tcp_send.c | 10 +++++++++- net/udp/udp_send.c | 6 +++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/net/tcp/tcp_send.c b/net/tcp/tcp_send.c index b6e860802752f..d8209bb202fa3 100644 --- a/net/tcp/tcp_send.c +++ b/net/tcp/tcp_send.c @@ -261,9 +261,17 @@ static void tcp_sendcommon(FAR struct net_driver_s *dev, else #endif { + /* Copy out of the address union: taking the address of the IPv4 + * member makes GCC 11/12 narrow the whole union to 4 bytes and + * warn about the 16-byte IPv6 read in the branch above. + */ + + in_addr_t laddr = conn->u.ipv4.laddr; + in_addr_t raddr = conn->u.ipv4.raddr; + ninfo("do IPv4 IP header build!\n"); ipv4_build_header(IPv4BUF, dev->d_len, IP_PROTO_TCP, - &dev->d_ipaddr, &conn->u.ipv4.raddr, + &laddr, &raddr, conn->sconn.s_ttl, conn->sconn.s_tos, NULL); /* Calculate TCP checksum. */ diff --git a/net/udp/udp_send.c b/net/udp/udp_send.c index 4eb2575ec8068..190c1e491b036 100644 --- a/net/udp/udp_send.c +++ b/net/udp/udp_send.c @@ -153,6 +153,7 @@ void udp_send(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn) FAR const uint16_t *laddr; #endif #ifdef CONFIG_NET_IPv4 + FAR const in_addr_t *src; in_addr_t raddr; #endif @@ -188,8 +189,11 @@ void udp_send(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn) dev->d_len = dev->d_sndlen + IPv4UDP_HDRLEN; + src = net_ipv4addr_cmp(conn->u.ipv4.laddr, INADDR_ANY) ? + &dev->d_ipaddr : &conn->u.ipv4.laddr; + ipv4_build_header(IPv4BUF, dev->d_len, IP_PROTO_UDP, - &dev->d_ipaddr, &raddr, conn->sconn.s_ttl, + src, &raddr, conn->sconn.s_ttl, conn->sconn.s_tos, NULL); #ifdef CONFIG_NET_STATISTICS From a9af1c87aad1a598274985eead65d786d59107c9 Mon Sep 17 00:00:00 2001 From: wenquan1 Date: Thu, 2 Apr 2026 09:27:55 +0800 Subject: [PATCH 4/6] net: move errahead from pkt_conn_s to socket_conn_s. Move the error-ahead queue (errahead) from pkt_conn_s to the common socket_conn_s as s_errahead. This allows other protocol stacks (e.g. UDP) to reuse the same error queue for TX timestamping support. Signed-off-by: wenquan1 --- include/nuttx/net/net.h | 4 ++++ net/pkt/pkt.h | 4 ---- net/pkt/pkt_conn.c | 8 -------- net/pkt/pkt_input.c | 2 +- net/pkt/pkt_netpoll.c | 4 ++-- net/pkt/pkt_recvmsg.c | 5 +++-- net/utils/utils.h | 6 ++++++ 7 files changed, 16 insertions(+), 17 deletions(-) diff --git a/include/nuttx/net/net.h b/include/nuttx/net/net.h index aecf69550d0c2..f0a71f0c1bdbd 100644 --- a/include/nuttx/net/net.h +++ b/include/nuttx/net/net.h @@ -257,6 +257,10 @@ struct socket_conn_s uint8_t s_ttl; /* Default time-to-live */ #endif +#ifdef CONFIG_NET_TIMESTAMP + struct iob_queue_s s_errahead; /* TX timestamp error queue */ +#endif + /* Connection-specific content may follow */ }; diff --git a/net/pkt/pkt.h b/net/pkt/pkt.h index c09670dd01a49..274bae393ea64 100644 --- a/net/pkt/pkt.h +++ b/net/pkt/pkt.h @@ -107,10 +107,6 @@ struct pkt_conn_s struct iob_queue_s readahead; /* Read-ahead buffering */ -#ifdef CONFIG_NET_TIMESTAMP - struct iob_queue_s errahead; /* Error-ahead buffering */ -#endif - FAR struct iob_s *pendiob; /* The iob currently being sent */ /* The following is a list of poll structures of threads waiting for diff --git a/net/pkt/pkt_conn.c b/net/pkt/pkt_conn.c index 0e18976704bfb..3d97e155b32cb 100644 --- a/net/pkt/pkt_conn.c +++ b/net/pkt/pkt_conn.c @@ -154,20 +154,12 @@ void pkt_free(FAR struct pkt_conn_s *conn) iob_free_queue(&conn->readahead); -#ifdef CONFIG_NET_TIMESTAMP - iob_free_queue(&conn->errahead); -#endif - /* Free the read-ahead data. pkt_in_() queues frames with the list * locked, so no new frame can come in after dq_rem(). */ iob_free_queue(&conn->readahead); -#ifdef CONFIG_NET_TIMESTAMP - iob_free_queue(&conn->errahead); -#endif - #ifdef CONFIG_NET_PKT_WRITE_BUFFERS /* Free the write queue */ diff --git a/net/pkt/pkt_input.c b/net/pkt/pkt_input.c index 87d61249c0098..fab172f6510e3 100644 --- a/net/pkt/pkt_input.c +++ b/net/pkt/pkt_input.c @@ -169,7 +169,7 @@ static int pkt_in_(FAR struct net_driver_s *dev, bool loopback) if (dev->d_iob->io_conn == &conn->sconn) { - if (pkt_datahandler(dev, conn, &conn->errahead) > 0) + if (pkt_datahandler(dev, conn, &conn->sconn.s_errahead) > 0) { pkt_callback(dev, conn, PKT_NEWDATA); } diff --git a/net/pkt/pkt_netpoll.c b/net/pkt/pkt_netpoll.c index ebd682fa52968..5f18aca9331fa 100644 --- a/net/pkt/pkt_netpoll.c +++ b/net/pkt/pkt_netpoll.c @@ -138,7 +138,7 @@ static uint32_t pkt_poll_eventhandler(FAR struct net_driver_s *dev, #ifdef CONFIG_NET_TIMESTAMP /* Check for timestamping data */ - if (!IOB_QEMPTY(&info->conn->errahead)) + if (!IOB_QEMPTY(&info->conn->sconn.s_errahead)) { eventset |= POLLPRI | POLLERR; } @@ -281,7 +281,7 @@ int pkt_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds) /* Check for timestamping data */ #ifdef CONFIG_NET_TIMESTAMP - if (!IOB_QEMPTY(&conn->errahead)) + if (!IOB_QEMPTY(&conn->sconn.s_errahead)) { eventset |= POLLPRI | POLLERR; } diff --git a/net/pkt/pkt_recvmsg.c b/net/pkt/pkt_recvmsg.c index 600796a256f01..c4660b8ad297a 100644 --- a/net/pkt/pkt_recvmsg.c +++ b/net/pkt/pkt_recvmsg.c @@ -589,9 +589,10 @@ ssize_t pkt_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg, #ifdef CONFIG_NET_TIMESTAMP if (flags & MSG_ERRQUEUE) { - if (!IOB_QEMPTY(&conn->errahead)) + if (!IOB_QEMPTY(&conn->sconn.s_errahead)) { - ret = pkt_readdata(&state, &conn->errahead, append_timestamping); + ret = pkt_readdata(&state, &conn->sconn.s_errahead, + append_timestamping); } else { diff --git a/net/utils/utils.h b/net/utils/utils.h index bec54cfd56252..0ed741b293081 100644 --- a/net/utils/utils.h +++ b/net/utils/utils.h @@ -192,6 +192,12 @@ static inline_function void conn_uninit(FAR struct socket_conn_s *sconn) { DEBUGASSERT(sconn != NULL); +#ifdef CONFIG_NET_TIMESTAMP + /* Release any error-ahead buffers attached to the connection */ + + iob_free_queue(&sconn->s_errahead); +#endif + /* Destroy the connection lock */ nxrmutex_destroy(&sconn->s_lock); From bb682837ec45bef7eafd06711a6ad773ad73d2d8 Mon Sep 17 00:00:00 2001 From: wenquan1 Date: Thu, 2 Apr 2026 09:44:01 +0800 Subject: [PATCH 5/6] net/udp: support SO_TIMESTAMPING TX path. Add TX hardware timestamping support for UDP sockets, following the existing PKT socket implementation pattern: - Tag outgoing IOBs with io_conn in both buffered and unbuffered send paths when SO_TIMESTAMPING is enabled. - In ipv4/ipv6 input, detect TX timestamp clone (io_conn != NULL) and skip validation, jumping straight to protocol dispatch. - In udp_input, match returning packets by io_conn to identify TX timestamp echoes, queue them to s_errahead. - Handle MSG_ERRQUEUE in psock_udp_recvfrom() to dequeue TX timestamp packets from s_errahead with SO_TIMESTAMPING cmsg. - Report POLLPRI|POLLERR in udp_netpoll when s_errahead is non-empty. - Skip UDP checksum verification for TX timestamp clone packets. Signed-off-by: wenquan1 --- net/devif/ipv4_input.c | 37 ++++++++++++--------- net/devif/ipv6_input.c | 37 ++++++++++++--------- net/udp/udp_callback.c | 6 +++- net/udp/udp_input.c | 51 ++++++++++++++++++++++++++-- net/udp/udp_netpoll.c | 23 +++++++++++++ net/udp/udp_recvfrom.c | 59 +++++++++++++++++++++++++++++++++ net/udp/udp_sendto_buffered.c | 7 ++++ net/udp/udp_sendto_unbuffered.c | 9 +++++ 8 files changed, 196 insertions(+), 33 deletions(-) diff --git a/net/devif/ipv4_input.c b/net/devif/ipv4_input.c index c6a42591438da..417f6e0dff116 100644 --- a/net/devif/ipv4_input.c +++ b/net/devif/ipv4_input.c @@ -232,23 +232,40 @@ static int ipv4_in(FAR struct net_driver_s *dev) bool isfrag; int ret = OK; - /* Storing reception timestamp provided by realtime - * if timestamp no provided by hardware. + /* This is where the input processing starts. + * + * Get the size of the packet minus the size of link layer header, + * and make sure that all packet processing logic knows that there + * is an IPv4 packet in the device buffer. */ + dev->d_len -= NET_LL_HDRLEN(dev); + IFF_SET_IPv4(dev->d_flags); + #ifdef CONFIG_NET_TIMESTAMP +#ifdef NET_UDP_HAVE_STACK + if (dev->d_iob->io_conn != NULL) + { + /* This is a TX timestamp clone looped back by the driver. + * Skip all validation (dest IP, checksum, etc.) and deliver + * it to the originating socket's error queue directly. + */ + + udp_ipv4_input(dev); + goto drop; + } +#endif + if ((dev->d_features & NETDEV_RX_STAMP) == 0) { clock_gettime(CLOCK_REALTIME, &dev->d_iob->io_time); } -#endif /* CONFIG_NET_TIMESTAMP */ +#endif /* Handle ARP on input then give the IPv4 packet to the network layer */ arp_ipin(dev); - /* This is where the input processing starts. */ - #ifdef CONFIG_NET_STATISTICS g_netstats.ipv4.recv++; #endif @@ -275,22 +292,12 @@ static int ipv4_in(FAR struct net_driver_s *dev) goto drop; } - /* Get the size of the packet minus the size of link layer header */ - - dev->d_len -= NET_LL_HDRLEN(dev); - if (((ipv4->vhl & IPv4_HLMASK) << 2) > dev->d_len) { nwarn("WARNING: Packet shorter than IPv4 header\n"); goto drop; } - /* Make sure that all packet processing logic knows that there is an IPv4 - * packet in the device buffer. - */ - - IFF_SET_IPv4(dev->d_flags); - /* Check the size of the packet. If the size reported to us in d_len is * smaller the size reported in the IP header, we assume that the packet * has been corrupted in transit. If the size of d_len is larger than the diff --git a/net/devif/ipv6_input.c b/net/devif/ipv6_input.c index ef6de6f609c27..4fb92f2a94955 100644 --- a/net/devif/ipv6_input.c +++ b/net/devif/ipv6_input.c @@ -229,18 +229,35 @@ static int ipv6_in(FAR struct net_driver_s *dev) bool isfrag = false; #endif - /* Storing reception timestamp provided by realtime - * if timestamp no provided by hardware. + /* This is where the input processing starts. + * + * Get the size of the packet minus the size of link layer header, + * and make sure that all packet processing logic knows that there + * is an IPv6 packet in the device buffer. */ + dev->d_len -= NET_LL_HDRLEN(dev); + IFF_SET_IPv6(dev->d_flags); + #ifdef CONFIG_NET_TIMESTAMP +#ifdef NET_UDP_HAVE_STACK + if (dev->d_iob->io_conn != NULL) + { + /* This is a TX timestamp clone looped back by the driver. + * Skip all validation and deliver it to the originating + * socket's error queue directly. + */ + + udp_ipv6_input(dev, IPv6_HDRLEN); + goto drop; + } +#endif + if ((dev->d_features & NETDEV_RX_STAMP) == 0) { clock_gettime(CLOCK_REALTIME, &dev->d_iob->io_time); } -#endif /* CONFIG_NET_TIMESTAMP */ - - /* This is where the input processing starts. */ +#endif #ifdef CONFIG_NET_STATISTICS g_netstats.ipv6.recv++; @@ -263,22 +280,12 @@ static int ipv6_in(FAR struct net_driver_s *dev) goto drop; } - /* Get the size of the packet minus the size of link layer header */ - - dev->d_len -= NET_LL_HDRLEN(dev); - if (IPv6_HDRLEN > dev->d_len) { nwarn("WARNING: Packet shorter than IPv6 header\n"); goto drop; } - /* Make sure that all packet processing logic knows that there is an IPv6 - * packet in the device buffer. - */ - - IFF_SET_IPv6(dev->d_flags); - /* Check the size of the packet. If the size reported to us in d_len is * smaller the size reported in the IP header, we assume that the packet * has been corrupted in transit. If the size of d_len is larger than the diff --git a/net/udp/udp_callback.c b/net/udp/udp_callback.c index 0ef8923a33776..60843bf6f2aba 100644 --- a/net/udp/udp_callback.c +++ b/net/udp/udp_callback.c @@ -304,7 +304,11 @@ uint32_t udp_callback(FAR struct net_driver_s *dev, conn_lock(&conn->sconn); flags = devif_conn_event(dev, flags, conn->sconn.s_list); - if ((flags & UDP_NEWDATA) != 0) + if ((flags & UDP_NEWDATA) != 0 +#ifdef CONFIG_NET_TIMESTAMP + && dev->d_iob->io_conn == NULL +#endif + ) { /* Data was not handled.. dispose of it appropriately */ diff --git a/net/udp/udp_input.c b/net/udp/udp_input.c index c48dd49d126c4..cbef1c7a1c8ce 100644 --- a/net/udp/udp_input.c +++ b/net/udp/udp_input.c @@ -55,10 +55,11 @@ #include #include "devif/devif.h" -#include "utils/utils.h" -#include "udp/udp.h" #include "icmp/icmp.h" #include "icmpv6/icmpv6.h" +#include "socket/socket.h" +#include "udp/udp.h" +#include "utils/utils.h" /**************************************************************************** * Private Functions @@ -221,6 +222,52 @@ static int udp_input(FAR struct net_driver_s *dev, unsigned int iplen) #endif int ret = OK; +#ifdef CONFIG_NET_TIMESTAMP + /* Handle TX timestamp: if the IOB was tagged with a connection on the + * send path, queue it to that connection's error-ahead buffer for + * MSG_ERRQUEUE. Skip checksum and normal RX processing entirely. + */ + + if (dev->d_iob->io_conn != NULL) + { + udp_conn_list_lock(); + + /* Validate that the connection is still in the active list. + * The connection may have been closed and freed between the + * send path (where io_conn was set) and now. + */ + + for (conn = udp_nextconn(NULL); conn != NULL; + conn = udp_nextconn(conn)) + { + if (&conn->sconn == dev->d_iob->io_conn) + { + break; + } + } + + if (conn != NULL) + { + /* Move dev->d_iob directly into the error-ahead queue + * (zero-copy), then prepare a fresh iob for the device. + */ + + iob_add_queue(dev->d_iob, &conn->sconn.s_errahead); + + /* Notify all waiters (poll and blocking recvfrom) + * through the standard callback chain. + */ + + udp_callback(dev, conn, UDP_NEWDATA); + netdev_iob_clear(dev); + } + + dev->d_len = 0; + udp_conn_list_unlock(); + return OK; + } +#endif + /* Update the count of UDP packets received */ #ifdef CONFIG_NET_STATISTICS diff --git a/net/udp/udp_netpoll.c b/net/udp/udp_netpoll.c index d792170382ade..1c7876828407e 100644 --- a/net/udp/udp_netpoll.c +++ b/net/udp/udp_netpoll.c @@ -101,6 +101,15 @@ static uint32_t udp_poll_eventhandler(FAR struct net_driver_s *dev, eventset |= POLLOUT; } +#ifdef CONFIG_NET_TIMESTAMP + /* Check for timestamping data */ + + if (!IOB_QEMPTY(&info->conn->sconn.s_errahead)) + { + eventset |= POLLPRI | POLLERR; + } +#endif + /* Awaken the caller of poll() is requested event occurred. */ poll_notify(&info->fds, 1, eventset); @@ -207,6 +216,13 @@ int udp_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds) cb->flags |= UDP_NEWDATA; } +#ifdef CONFIG_NET_TIMESTAMP + if ((fds->events & POLLPRI) != 0) + { + cb->flags |= UDP_NEWDATA; + } +#endif + /* Save the reference in the poll info structure as fds private as well * for use during poll teardown as well. */ @@ -222,6 +238,13 @@ int udp_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds) eventset |= POLLRDNORM; } +#ifdef CONFIG_NET_TIMESTAMP + if (!IOB_QEMPTY(&conn->sconn.s_errahead)) + { + eventset |= POLLPRI | POLLERR; + } +#endif + if (psock_udp_cansend(conn) >= 0) { /* Normal data may be sent without blocking (at least one byte). */ diff --git a/net/udp/udp_recvfrom.c b/net/udp/udp_recvfrom.c index 8cbf97233b832..756619876acc6 100644 --- a/net/udp/udp_recvfrom.c +++ b/net/udp/udp_recvfrom.c @@ -431,6 +431,25 @@ static uint32_t udp_eventhandler(FAR struct net_driver_s *dev, udp_terminate(pstate, -ENETUNREACH); } +#ifdef CONFIG_NET_TIMESTAMP + /* TX timestamp loopback packet: io_conn was set on the send + * path. Data is already in s_errahead (queued by udp_input). + * Wake up MSG_ERRQUEUE waiter if present, otherwise ignore. + * Must be checked before normal UDP_NEWDATA handling. + */ + + else if ((flags & UDP_NEWDATA) != 0 && + dev->d_iob->io_conn != NULL) + { + /* TX timestamp loopback packet: io_conn was set on the send + * path. Blocking recvfrom does not handle MSG_ERRQUEUE, + * return -EAGAIN so the caller can retry. + */ + + udp_terminate(pstate, -EAGAIN); + } +#endif + /* If new data is available, then complete the read action. */ else if ((flags & UDP_NEWDATA) != 0) @@ -671,6 +690,43 @@ ssize_t psock_udp_recvfrom(FAR struct socket *psock, FAR struct msghdr *msg, conn_dev_lock(&conn->sconn, dev); +#ifdef CONFIG_NET_TIMESTAMP + if (flags & MSG_ERRQUEUE) + { + if (!IOB_QEMPTY(&conn->sconn.s_errahead)) + { + FAR struct iob_s *iob; + struct timespec ts[3]; + + iob = iob_remove_queue(&conn->sconn.s_errahead); + DEBUGASSERT(iob != NULL); + + state.ir_recvlen = iob_copyout(msg->msg_iov->iov_base, iob, + msg->msg_iov->iov_len, 0); + + memset(&ts, 0, sizeof(ts)); + ts[0].tv_sec = iob->io_time.tv_sec; + ts[0].tv_nsec = iob->io_time.tv_nsec; + ts[2].tv_sec = iob->io_time.tv_sec; + ts[2].tv_nsec = iob->io_time.tv_nsec; + + cmsg_append(msg, SOL_SOCKET, SO_TIMESTAMPING, &ts, sizeof(ts)); + msg->msg_flags |= MSG_ERRQUEUE; + + iob_free_chain(iob); + ret = state.ir_recvlen; + } + else + { + /* MSG_ERRQUEUE only supports non-blocking mode */ + + ret = -EAGAIN; + } + + goto errout_with_state; + } +#endif + /* Copy the read-ahead data from the packet */ udp_readahead(&state); @@ -756,6 +812,9 @@ ssize_t psock_udp_recvfrom(FAR struct socket *psock, FAR struct msghdr *msg, } } +#ifdef CONFIG_NET_TIMESTAMP +errout_with_state: +#endif conn_dev_unlock(&conn->sconn, dev); udp_notify_recvcpu(conn); diff --git a/net/udp/udp_sendto_buffered.c b/net/udp/udp_sendto_buffered.c index e66b40ef09a61..6ecd7fd832ceb 100644 --- a/net/udp/udp_sendto_buffered.c +++ b/net/udp/udp_sendto_buffered.c @@ -466,6 +466,13 @@ static uint32_t sendto_eventhandler(FAR struct net_driver_s *dev, netdev_iob_replace(dev, wrb->wb_iob); +#ifdef CONFIG_NET_TIMESTAMP + if (_SO_GETOPT(conn->sconn.s_options, SO_TIMESTAMPING)) + { + dev->d_iob->io_conn = &conn->sconn; + } +#endif + /* Get the amount of data that we can send in the next packet. * We will send either the remaining data in the buffer I/O * buffer chain, or as much as will fit given the MSS and current diff --git a/net/udp/udp_sendto_unbuffered.c b/net/udp/udp_sendto_unbuffered.c index 37736aa3b5e69..19e14fd5a0e17 100644 --- a/net/udp/udp_sendto_unbuffered.c +++ b/net/udp/udp_sendto_unbuffered.c @@ -224,6 +224,15 @@ static uint32_t sendto_eventhandler(FAR struct net_driver_s *dev, } dev->d_len = dev->d_iob->io_pktlen; + +#ifdef CONFIG_NET_TIMESTAMP + if (_SO_GETOPT(pstate->st_conn->sconn.s_options, + SO_TIMESTAMPING)) + { + dev->d_iob->io_conn = &pstate->st_conn->sconn; + } +#endif + #ifdef NEED_IPDOMAIN_SUPPORT /* If both IPv4 and IPv6 support are enabled, then we will need to * select which one to use when generating the outgoing packet. From 1f42289102245fe79d39d3441d599a58bf02ff91 Mon Sep 17 00:00:00 2001 From: zhanghongyu Date: Mon, 23 Nov 2026 22:04:11 +0800 Subject: [PATCH 6/6] net/udp: Fix coding style in udp_input.c. checkpatch reports a missing blank line after a declaration in udp_is_mcast() and misaligned code inside the #ifdef-guarded destination-unreachable block. Both are pre-existing, in a file that this series touches. Assisted-by: Kiro:claude-opus-4 Signed-off-by: zhanghongyu --- net/udp/udp_input.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/net/udp/udp_input.c b/net/udp/udp_input.c index cbef1c7a1c8ce..2eb01f53aec2a 100644 --- a/net/udp/udp_input.c +++ b/net/udp/udp_input.c @@ -104,6 +104,7 @@ static bool udp_is_broadcast(FAR struct net_driver_s *dev) # endif { FAR struct ipv6_hdr_s *ipv6 = IPv6BUF; + return net_is_addr_mcast(ipv6->destipaddr); } #endif @@ -428,7 +429,7 @@ static int udp_input(FAR struct net_driver_s *dev, unsigned int iplen) # endif /* CONFIG_NET_IPv4 */ # ifdef CONFIG_NET_IPv6 # ifdef CONFIG_NET_IPv4 - else + else # endif { # ifdef CONFIG_NET_ICMPv6