zhhyu7 opened a new pull request, #22231:
URL: https://github.com/apache/nuttx/pull/22231
## Summary
Six changes to the UDP stack, picked from a downstream tree and adapted.
Two are read-ahead fixes:
udp_readahead() read src_addr_size and srcaddr before initializing them
(Coverity CID 3934125 / 3934127, MISRA C-2012 Rule 9.1). Both are filled in by
an earlier iob_copyout(), but that function has two
early returns, -ESPIPE on a bad offset and 0 on an exhausted chain, which
write nothing. The paths are unreachable today because udp_callback() always
writes the metadata, so this removes a latent trap
rather than a live bug.
udp_callback() drops a datagram when the read-ahead buffer already holds
more than SO_RCVBUF bytes, but returned without touching the statistics, so the
loss was invisible in /proc/net/stat. The adjacent
"no free read-ahead buffers" path does count it. It now counts and logs the
drop the same way.
One is a source-address fix. The IPv4 send paths in tcp_sendcommon() and
udp_send() built the IP header with dev->d_ipaddr. On a multi-interface host
that is wrong when the connection's bound local
address lives on a different interface than the one the packet leaves on:
the peer sees a source that does not match the destination it addressed and
rejects the datagram. They now use the connection's
bound local address, which is what the IPv6 paths in the same two functions
already do via netdev_ipv6_srcaddr().
The remaining three add SO_TIMESTAMPING on the UDP TX path, following the
pattern the PKT sockets already use:
- errahead moves from pkt_conn_s to socket_conn_s as s_errahead, so more
than one protocol can use the error queue. The PKT users are updated in the
same commit.
- Outgoing IOBs are tagged with io_conn in both the buffered and the
unbuffered send path; ipv4_input() and ipv6_input() recognise a TX timestamp
clone by that field and go straight to protocol dispatch;
udp_input() matches the echo back to the socket and queues it on s_errahead;
psock_udp_recvfrom() serves it on MSG_ERRQUEUE.
- The last commit fixes two pre-existing checkpatch errors in udp_input.c, a
file this series touches.
Where the downstream version depended on infrastructure this tree does not
have, the logic was rewritten against what is here rather than pulling in more
commits. In particular the timestamp control
message is appended inline with cmsg_append(), exactly as
append_timestamping() in net/pkt/pkt_recvmsg.c does, instead of introducing a
new helper.
## Impact
struct socket_conn_s gains s_errahead under CONFIG_NET_TIMESTAMP and struct
pkt_conn_s loses errahead. Out-of-tree code that touches pkt_conn_s::errahead
directly must use conn->sconn.s_errahead instead.
All in-tree users are converted here.
The IPv4 source-address change alters which address appears in outgoing
headers for a socket bound to a specific local address on a multi-interface
host. That is the point of the fix, but it is a visible
behaviour change on such setups. A socket bound to INADDR_ANY keeps using
dev->d_ipaddr and is unaffected.
The new drop counter makes a previously silent loss show up in the UDP
Dropped column. Anything that parses those statistics will see a number that
was always zero start to move.
SO_TIMESTAMPING on UDP is new functionality behind CONFIG_NET_TIMESTAMP,
which is off by default. With it off the only remaining changes are the two
read-ahead fixes, the source-address fix and the style
fix.
## Testing
sim:dynconns with CONFIG_SIM_NET_BRIDGE=y, CONFIG_DEBUG_ASSERTIONS=y,
CONFIG_NET_STATISTICS=y, tcpecho on port 80 and apps/examples/udp with
EXAMPLES_UDP_SERVER1=y on port 5471. Host bridge nuttx0
10.0.1.1/24, target 10.0.1.2/24. Built and run both with
CONFIG_NET_TIMESTAMP=n and =y; the results below are identical for both.
UDP, 256 datagrams. The target's udpserver validates the sequence byte and
the payload pattern of every datagram and exits on the first mismatch:
udp client: sent 256 datagrams of 96 bytes (24576 bytes payload)
datagrams accepted by target: 256
server error lines: none
Malformed IPv4 input, sent from a raw socket with a forged ip.total_len, to
confirm the read-ahead paths stay sane on input the stack must reject:
ip.total_len=4000 with only 60 bytes on the wire -> rejected
ip.total_len=10 (smaller than the IP header) -> rejected
node responsive afterwards: 3 packets transmitted, 3 received, 0% packet loss
Statistics after the run:
UDP Received 0105 Dropped 0000
IPv4 Received 011a Dropped 0014 (includes the two malformed datagrams)
TCP unaffected, 4 concurrent connections echoing 256 KiB each:
conn0..conn3: sent=262144 recv=262144 err=None
ICMP: 4 transmitted, 4 received, 0% packet loss. No assertions, no
segmentation fault, node alive after every run.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]