Commit graph

3267 commits

Author SHA1 Message Date
Bowen Wang
a4ce775a50 net/rpmsg/rpmsg_sockif: fix compile error about nxsched_getpid
need include <nuttx/sched.h>

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
2025-12-31 02:36:29 +08:00
zhangkai25
711c58767a net/arp: Skip ARP request if ip address is invalid
Supports no ARP reply for invalid routing addresses in cross-segment communication

Signed-off-by: zhangkai25 <zhangkai25@xiaomi.com>
2025-12-31 02:36:09 +08:00
dongjiuzhu1
b8585b9b0c net/can: fix poll setup to properly find and use free pollfd slots
Bug Description:
The original code always used conn->pollinfo[0] (the first element) to store
new poll setup context, regardless of whether that slot was already in use.
This caused multiple poll operations on the same CAN socket to overwrite each
other's context, leading to:
- Lost poll waiters when multiple threads poll the same socket
- Memory corruption in pollfd structures
- Undefined behavior when poll_teardown tries to clean up

Root Cause:
The code directly assigned `info = conn->pollinfo` without checking if the
slot was available, effectively always using pollinfo[0]. When a second
thread called poll() on the same socket, it would overwrite the first
thread's poll context.

Solution:
1. Initialize info to NULL instead of conn->pollinfo
2. Before setting up poll, iterate through all CONFIG_NET_CAN_NPOLLWAITERS
   slots to find the first free slot (where fds == NULL)
3. Return -EBUSY if no free slots are available
4. During teardown, properly mark the slot as free by setting fds = NULL

Additional Changes:
- Added CONFIG_NET_CAN_NPOLLWAITERS Kconfig option (default 4) to make the
  maximum number of concurrent poll waiters configurable
- Changed hardcoded array size from 4 to CONFIG_NET_CAN_NPOLLWAITERS
- Fixed lock ordering in teardown to ensure fds is cleared before unlock

Impact:
- Enables multiple threads to safely poll the same CAN socket concurrently
- Prevents poll context corruption in multi-threaded applications
- Provides proper resource management with -EBUSY when all slots are full
- Makes the number of supported concurrent pollers configurable per use case

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-31 02:35:42 +08:00
dongjiuzhu1
d1bc2800e3 net/local: simplify file descriptor passing using shared filep references
This commit removes unnecessary file duplication and memory allocation when
passing file descriptors through Unix domain sockets, leveraging the new
filep reference counting framework.

Background:
With the new filep framework, file structures (filep) can be safely shared
across processes using reference counting. When passing file descriptors
through SCM_RIGHTS control messages on local sockets, the previous
implementation unnecessarily duplicated the entire file structure.

Changes:

1. In local_sendctl() (sender side):
   - Removed allocation of filep2 structure (kmm_zalloc)
   - Removed file_dup2() call that copied the file structure
   - Now directly stores the original filep with its reference count
   - Eliminates memory allocation overhead and potential failure points

2. In local_recvctl() (receiver side):
   - Changed from file_close() + kmm_free() to file_put()
   - file_put() properly decrements reference count and handles cleanup
   - Consistent with the new filep reference counting model

3. In local_freectl() (cleanup on error):
   - Changed from file_close() + kmm_free() to file_put()
   - Ensures proper reference count management during error paths

Technical Details:
The new filep framework ensures that:
- Multiple file descriptors across different processes can reference the
  same underlying filep structure safely
- Reference counting (via file_get/file_put) manages lifetime correctly
- The underlying file object is only released when all references are gone

This change is safe because file_dup() in the receiver already calls
file_get() internally to increment the reference count for the new fd,
so the filep remains valid after the sender calls file_put().

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-31 02:29:46 +08:00
zhangkai25
7a6b9e9407 net/neighbor: Skip Neighbor request if ip address is invalid at ipv6
Supports no Neighbor request for invalid routing addresses in cross-segment communication

Signed-off-by: zhangkai25 <zhangkai25@xiaomi.com>
2025-12-30 22:06:21 +08:00
wenquan1
1f5cd930a4 net/arp: support ARP entry learned on gratuitous ARP response
learn arp when received gratuitiout arp

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:10:50 +08:00
wenquan1
7e4c3f4a60 net/arp: support ARP entry learned on gratuitous ARP response
learn arp when receive gratuitous arp response

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:10:50 +08:00
wenquan1
dfaed9b855 net/icmp: Support icmp timestamp request
When received a icmp timestamp request, send a icmp timestamp reply.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:05:46 +08:00
wenquan1
d2887ef15f net/tcp: fix send RST bug when receive syn retransmit in syn-rcvd state
Fix send RST bug when receive syn retransmit in syn-rcvd state

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
476e193812 net/tcp: ignore fin packet in syn-sent state
Ignore fin packet in syn-sent state

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
e3c2ab8267 net/tcp: drop the packet with neither SYN or RST flag set in syn-sent state
Drop the packet with neither SYN or RST flag set in syn-sent state

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
b09b5dfc9c net/tcp: ignore a RST control message in syn-send state
Ignore a RST control message in syn-send state

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
12de7e6dd5 net/tcp: ignore a segment carrying an unacceptable ACK and RST in syn-send state
Ignore a segment carrying an unacceptable ACK and RST in syn-send state

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
62f8c400b6 net/tcp: send a reset if the SYN is in the window buf error
According rfc793 p71, after step1 check OTW seq, and step2 check RST flag, then check syn flag.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
c4494c714c net/tcp: support OTW SEQ number check except SYN-SEND state
According rfc793 p69, from state SYN-RECEIVED to state TIME-WAIT, if the incoming segment is not acceptable, an acknowledgment should be sent in reply (unless the RST bit is set):
<SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK>

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
758b6dd847 net/tcp: support OTW SEQ number check in SYN-RCVD state
According rfc793 p69, In SYN-RCVD state, if the incoming segment is not acceptable, an acknowledgment should be sent in reply (unless the RST bit is set):
<SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK>

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 21:04:52 +08:00
wenquan1
ee5009295c tcp_reset: Modify RST packet responses to comply with RFC 793.
According to RFC793, the flags of TCP reply packet should be as follows:
1. `RST` if flags of request packet has `ACK`
2. `RST|ACK` if flags of request packet has no `ACK`, at the same time, the sequence should be set to zero.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 11:44:38 +08:00
wenquan1
424a924f65 net/ipv6: Drop IP packet shorter than length in IP header
If the length of received ip packet is shorter than length in ip header, drop it.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 11:28:38 +08:00
zhanghongyu
6d916a4b0a arp_send: call arp_update before txnotify
if update is called after tx, in an environment with very low
latency (such as a simulator), the received arp_response will
be overwritten by update (NULL), resulting in arp learning success,
but the packets are always not sent correctly and are converted
into arp packets.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-30 11:28:22 +08:00
wenquan1
4ae08155f9 net/arp: limit arp request when arp is in progress to prevent ARP flooding
Wait a fixed number of milliseconds before sending a retry ARP request for the same destination if the IP address mapping is not exist to prevent ARP flooding

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 11:28:22 +08:00
wenquan1
6b30226c0e Revert "net/arp: modify some flow of arp return failure"
This reverts commit 2863f1d8d6da477dfef7013f93dd9b34efb4f378.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 11:28:22 +08:00
wenquan1
033299710f net/arp: fix the problem of arp notify and flood arp sending
when a arp response reached, it should notify all tasks waiting the address. if the address is unreachable, do not send arp request to prevent arp flooding.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-30 11:28:22 +08:00
gaohedong
ea31ceac23 net/ethernet: fix compilation issues
fix compilation issues

Signed-off-by: gaohedong <gaohedong@xiaomi.com>
2025-12-29 23:31:51 +08:00
wenquan1
c0fa2cae3d net/tcp: fix conn->work use after free in worker queue list
in multi-core cpu, tcp_free and tcp_timer_expiry->tcp_timer will work in parallel, after tcp_free call work_cancle, tcp->timer will call tcp_update_timer to re-add work to worker queue, then tcp_free free conn, in this condition, it will result use after free.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 22:42:49 +08:00
wenquan1
cc0cc552fc net/tcp: discard fin packet in LISTEN state
Discard fin packet in LISTEN state

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 22:42:23 +08:00
wenquan1
fb7dcc3d89 net/tcp: fix error check of OTW ACK sequence of zero-window probe ack
Due to illegal sndseq_max increase when send zero-window probe or keealive probe

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 22:39:56 +08:00
wenquan1
74ab7816a1 net/tcp: send a reset if ack is not acceptable in syn-received state
In SYN-RCVD state, if the ACK is not acceptable, send a reset.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 22:39:56 +08:00
wenquan1
8ead446ee4 net/tcp: support OTW check and response with an empty acknowledgment segment
According RFC793,p72~p73 In states from ESTABLISHED to LASTACK:"If the ACK acks something not yet sent (SEG.ACK > SND.NXT) then send an ACK, drop the segment, and return"

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 22:39:56 +08:00
liqinhui
facb9d4e65 net/tcp: Initialize the sndseq_max.
Initialize conn->sndseq_max when tcp conn alloc

Signed-off-by: liqinhui <liqinhui@xiaomi.com>
2025-12-29 22:39:56 +08:00
zhanghongyu
a91c50243e net/tcp: send reset when retransmitted number greater than TCP_MAXRTX
in the current process, if tcpstate is at TCP_FIN_WAIT_1, TCP_CLOSING,
and TCP_LAST_ACK, and the peer does not reply TCP_ACK, it will be
retransmitted permanently. to avoid continuous occupation of resources,
now when the retransmission equals TCP_MAXRTX, a reset message will be
sent and the conn resources will be reclaimed in tcp_close_work.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 22:38:25 +08:00
wenquan1
11ea1de01d net/tcp: fix tcp conn lock leak when port in use in tcp_selectport
Fix tcp conn lock leak when port in use in tcp_selectport.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 10:33:18 -03:00
zhanghongyu
6addf24fa2 net/tcp: replace TCP_CLOSE with TCP_ABORT
since we added TCP_TXCLOSE and TCP_RXCLOSE, only RST-like exception
flows now trigger TCP_CLOSE events, so replace the remaining
TCP_CLOSE with TCP_ABORT.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 19:22:23 +08:00
zhanghongyu
fa47ab7bd2 net/tcp: add TCP_RXCLOSE to handle rx is shutdown
notify the local waiting recv process; otherwise, after the peer TX
is closed, the wait of the recv process will continue to wait.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 19:22:23 +08:00
zhanghongyu
2ad01e2aab net/tcp: support recv packet in the TCP_FIN_WAIT_1/2 state
shutdown should send TCP_FIN packet.

close should send TCP_RST packet when the data in readahead has not been
                                 read and NEW_DATA has arrived.
             send TCP_FIN packet when in other cases.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 19:22:23 +08:00
zhanghongyu
0588d08d75 net/tcp: remove SYN_RCVD state conn by listener created when free listener
after the listener fd is closed, the half-open connections associated
with the listener are not reclaimed. According to the protocol standard,
they should be reclaimed in a timely manner.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 19:20:01 +08:00
zhanghongyu
93cba3da64 net/tcp: add tcp_conn_cmp to compare tcp connection
encapsulate similar code into a function to simplify code logic.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 19:20:01 +08:00
gaohedong
8638e9ce43 net/devif: fix poll issue when ifdown
When the interface is in a down state, it triggers a poll busy loop issue.

Signed-off-by: gaohedong <gaohedong@xiaomi.com>
2025-12-29 19:18:55 +08:00
gaohedong
e1e6d8821e net/icmp: check src ip for icmp request message
Check src ip for icmp request message. Drop the request message if src ip is broadcast/multicast.

Signed-off-by: gaohedong <gaohedong@xiaomi.com>
2025-12-29 19:18:11 +08:00
wenquan1
0891fa5cfb net/ipv4: Send ICMP Destination Unreachable for unknown protocol
According RFC1122 3.2.2.1, A host SHOULD generate Destination Unreachable messages with code: 2(Protocol Unreachable), when the designated transport protocol is not supported.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
2025-12-29 19:15:57 +08:00
gaohedong
ca7c6e2a6f net/arp: dont learn arp information if interface is noarp
Dont learn arp information from ip packet if interface is noarp

Signed-off-by: gaohedong <gaohedong@xiaomi.com>
2025-12-29 14:38:36 +08:00
gaohedong
7e6138f258 net/ioctl: add a socket interface for ioctl
Add a socket family for ioctl.

Signed-off-by: gaohedong <gaohedong@xiaomi.com>
2025-12-29 14:38:15 +08:00
zhanghongyu
0900b21217 net: change conn lock from mutex to rmutex
UDP and PKT will hold the dev lock and conn lock when calling back the
protocol stack from the network dirver. At this time, if the poll_notify
notifies usrsock_server and executes poll_teardown, a conn double lock
will appear. so we changed the lock to a recursive lock

sched_dumpstack
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/Libs/libc/sched/sched_dumpstack.c:71
__assert
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/libs/libc/assert/lib_assert.c:49
nxsem_wait_slow
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/sched/semaphore/sem_wait.c:102 (discriminator 1)
nxmutex_lock
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/include/nuttx/mutex.h:514
udp_pollteardown
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/udp/udp_netpoll.c:278
usrsock_rpmsg_poll_setup
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/usrsock/usrsock_rpmsg_server.c:1232
usrsock_rpmsg_poll_cb
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/usrsock/usrsock_rpmsg_server.c:1299
poll_notify
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/fs/vfs/fs_poll.c:296
udp_poll_eventhandler
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/udp/udp_netpoll.c:105
devif_conn_event
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/devif/devif_callback.c:481
udp_callback
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/udp/udp_callback.c:324
udp_input_conn
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/udp/udp_input.c:150
ipv4_in
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/devif/ipv4_input.c:425
ipv4_input
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/net/devif/ipv4_input.c:533
tun_net_receive_tun
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/net/tun.c:574
tun_net_receive_tun
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/net/tun.c:574
file_writev_compat
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/fs/vfs/fs_write.c:89
rpmsgdev_write_handler
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/misc/rpmsgdev_server.c:354
rpmsg_virtio_process_rx_buffer
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/rpmsg/rpmsg_virtio.c:256
rpmsg_virtio_rx_worker
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/drivers/rpmsg/rpmsg_virtio.c:291
work_dispatch
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/sched/wqueue/kwork_thread.c:233
work_thread
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/sched/wqueue/kwork_thread.c:293
nxtask_start
/home/work/ssd1/workspace/MiRTOS-062-dev-system-CI/nuttx/sched/task/task_start.c:99

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 09:42:18 +08:00
zhanghongyu
c8713a1b48 net: protect the older network cards driver's tx and rx process
Add netdev_lock in xxx_input and txavail to adapt to drivers that do not
use upperhalf.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 09:42:18 +08:00
zhanghongyu
6b625dfdf2 net/inet: replace net_lock with conn_lock
Protect icmp resources through conn_lock

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 09:42:18 +08:00
zhanghongyu
8ef876562e net/icmpv6: replace net_lock with conn_lock and conn_lock_dev
Protect icmpv6 resources through netdev_lock, conn_lock, and icmpv6_list_lock

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 09:42:18 +08:00
zhanghongyu
5032377c34 net/tcp: replace net_lock with conn_lock and conn_lock_dev
Protect tcp resources through netdev_lock, conn_lock, and tcp_list_lock

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 09:42:18 +08:00
wangchen
6196550f8f tcp:add net_lock to protect tcp resource
add net_lock in the tcp_callback_cleanup & Set the devif_callback_s of
tcp_callback_s to a two-dimensional pointer.

Signed-off-by: wangchen <wangchen41@xiaomi.com>
2025-12-29 09:42:18 +08:00
zhanghongyu
02e7ed7cc4 net/can: replace net_lock with conn_lock and conn_lock_dev
Protect can resources through netdev_lock, conn_lock, and can_list_lock

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2025-12-29 09:42:18 +08:00
wangjinjing1
4fa74e4d18 net/can: add write_q free for callback alloc error handling
Release the corresponding resources held when the alloc fails

Signed-off-by: wangjinjing1 <wangjinjing1@xiaomi.com>
2025-12-29 09:42:18 +08:00
wangjinjing1
99584588fc net/can: modify net_unlock location before txnotify for can_sendmsg_buffered.c
Release the lock early to reduce the number of thread switches.

Signed-off-by: wangjinjing1 <wangjinjing1@xiaomi.com>
2025-12-29 09:42:18 +08:00