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/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/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/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_callback.c b/net/udp/udp_callback.c index a5c9eaee21808..60843bf6f2aba 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; @@ -296,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..2eb01f53aec2a 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 @@ -103,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 @@ -221,6 +223,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 @@ -381,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 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 822907f954e3e..756619876acc6 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| */ @@ -429,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) @@ -669,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); @@ -754,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_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 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. 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);