为什么tcpdump能收到UDP广播而netcat或socat不能收到?

问题描述 投票:0回答:2

我有一个单独的调制解调器和路由器,将调制解调器配置为网桥。调制解调器托管一个 LAN,其 IP 地址为 192.186.2.1。为了能够访问调制解调器的 Web UI,路由器在 WAN 端口上有一个额外的接口,在调制解调器的 LAN 中具有固定的 IP 地址 (192.168.2.10)。

调制解调器通过 UDP 广播 VDSL 状态数据。我想在路由器上监听这些数据包,但我无法使用

netcat
socat
捕获它们。不过,它们肯定在那里,因为它们出现在 TCP 转储中。

# tcpdump -i wan broadcast -v
tcpdump: listening on wan, link-type EN10MB (Ethernet), capture size 262144 bytes
14:19:25.579815 IP (tos 0x0, ttl 254, id 54981, offset 0, flags [none], proto UDP (17), length 144)
    0.0.0.0.13253 > 255.255.255.255.4944: UDP, length 116
14:19:35.580363 IP (tos 0x0, ttl 254, id 54982, offset 0, flags [none], proto UDP (17), length 144)
    0.0.0.0.13306 > 255.255.255.255.4944: UDP, length 116
14:19:45.580894 IP (tos 0x0, ttl 254, id 54983, offset 0, flags [none], proto UDP (17), length 144)
    0.0.0.0.13368 > 255.255.255.255.4944: UDP, length 116

但是

socat
(使用此处描述的so-bindtodevice:https://unix.stackexchange.com/a/663180)没有看到任何东西......

# socat -v udp-recv:4944,so-bindtodevice=wan -

<nothing happens>

netcat
也没有(指定接口如下:https://unix.stackexchange.com/q/368690)。

# netcat -v -ul 4944 -s 192.168.2.10

<nothing happens>

为什么

socat
收不到广播包?我该如何配置才能做到这一点?

背景信息

  • 调制解调器:Draytek Vigor 130 (3.8.5_BT)
  • 路由器:Linksys WRT1900ACS (OpenWrt 22.03.2)

路由器上的WAN口和桥接接口,WAN口连接调制解调器:

# ip a
...
7: wan@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
    link/ether 5a:ef:68:b7:05:bc brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.10/24 brd 192.168.2.255 scope global wan
       valid_lft forever preferred_lft forever
    inet 79.71.35.166/19 brd 79.71.63.255 scope global wan
       valid_lft forever preferred_lft forever
    inet6 fe80::58ef:68ff:feb7:5bc/64 scope link 
       valid_lft forever preferred_lft forever
10: br-lan: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
    link/ether 58:ef:68:b7:05:bc brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.1/24 brd 192.168.1.255 scope global br-lan
       valid_lft forever preferred_lft forever
    inet6 fde7:9501:870b::1/60 scope global noprefixroute 
       valid_lft forever preferred_lft forever
    inet6 fe80::5aef:68ff:feb7:5bc/64 scope link 
       valid_lft forever preferred_lft forever

路由表显示 192.168.2.0 使用 WAN 接口。

# ip route
default via 79.71.32.1 dev wan  src 79.71.35.166 
79.71.32.0/19 dev wan scope link  src 79.71.35.166 
192.168.1.0/24 dev br-lan scope link  src 192.168.1.1 
192.168.2.0/24 dev wan scope link  src 192.168.2.10 

软件版本。

# tcpdump --version
tcpdump version 4.9.3
libpcap version 1.10.1 (with TPACKET_V3)


# netcat --version
netcat (The GNU Netcat) 0.7.1
Copyright (C) 2002 - 2003  Giovanni Giacobbi

This program comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of this program under the terms of
the GNU General Public License.
For more information about these matters, see the file named COPYING.

Original idea and design by Avian Research <[email protected]>,
Written by Giovanni Giacobbi <[email protected]>.


# socat -V
socat by Gerhard Rieger and contributors - see www.dest-unreach.org
socat version 1.7.4.1 on 1672494323
   running on Linux version #0 SMP Fri Oct 14 22:44:41 2022, release 5.10.146, machine armv7l
features:
  #define WITH_STDIO 1
  #define WITH_FDNUM 1
  #define WITH_FILE 1
  #define WITH_CREAT 1
  #define WITH_GOPEN 1
  #define WITH_TERMIOS 1
  #define WITH_PIPE 1
  #define WITH_UNIX 1
  #define WITH_ABSTRACT_UNIXSOCKET 1
  #define WITH_IP4 1
  #define WITH_IP6 1
  #define WITH_RAWIP 1
  #define WITH_GENERICSOCKET 1
  #define WITH_INTERFACE 1
  #define WITH_TCP 1
  #define WITH_UDP 1
  #define WITH_SCTP 1
  #define WITH_LISTEN 1
  #define WITH_SOCKS4 1
  #define WITH_SOCKS4A 1
  #define WITH_VSOCK 1
  #define WITH_PROXY 1
  #define WITH_SYSTEM 1
  #define WITH_EXEC 1
  #undef WITH_READLINE
  #define WITH_TUN 1
  #define WITH_PTY 1
  #undef WITH_OPENSSL
  #undef WITH_FIPS
  #undef WITH_LIBWRAP
  #define WITH_SYCLS 1
  #define WITH_FILAN 1
  #define WITH_RETRY 1
  #define WITH_MSGLEVEL 0 /*debug*/

反向路径过滤已禁用(例如https://stackoverflow.com/a/48256271/3508733

# cat /proc/sys/net/ipv4/conf/all/rp_filter 
0
# cat /proc/sys/net/ipv4/conf/wan/rp_filter 
0
# cat /proc/sys/net/ipv4/conf/eth0/rp_filter
0

netcat
socat
添加广播选项没有帮助:

# netcat -v -ul 4944 -s 192.168.2.10 -b
netcat: unrecognized option: b
Try `netcat --help' for more information.
# socat -v -d -d udp4-recv:4944,so-bindtodevice=wan,so-broadcast -
2023/01/03 16:42:17 socat[10747] N reading from and writing to stdio
2023/01/03 16:42:17 socat[10747] N starting data transfer loop with FDs [5,5] and [0,1]

<nothing happens>
linux udp tcpdump netcat
2个回答
0
投票

我遇到了和你一样的问题。我使用 Boost.Asio 编写了一个 GigE UDP 广播发现函数。该功能在 Windows 上运行良好,但在 Linux 上,程序无法接收来自其他网络的 UDP 响应。我可以在 Wireshark 中看到响应,但程序没有收到它们。在比较 Windows 和 Linux 上的路由表时,我注意到 Windows 有 255.255.255.255 的网络目标规则,但 Linux 显然没有。不知道是不是这个原因。


0
投票

我尝试过 SOCAT

我使用了你的socat和我的特定设置广播UDP数据包:

^Cdrazen@HP-ProBook-640G1:~$ socat -v udp-recv:4944,so-bindtodevice=wlo1 -
> 2023/12/01 13:42:43.921297  length=12 from=5 to=16
testing1
testing1
> 2023/12/01 13:43:08.252901  length=15 from=17 to=31
testing2......
testing2......
> 2023/12/01 13:43:23.456512  length=15 from=32 to=46
testing3------
testing3------
> 2023/12/01 13:51:19.697565  length=8 from=47 to=54
TEST OK
TEST OK

我重写了你的 netcat 命令来发送广播数据包:

drazen@HP-ProBook-640G1:~$ netcat -v -ub 255.255.255.255 4944 -s 192.168.0.17
Connection to 255.255.255.255 4944 port [udp/*] succeeded!
testing1
testing2......                                       
testing3------

你的命令做错了什么,它使用了监听标志 -l 而不是广播标志 -b,并且它缺少目标地址 255.255.255.255

© www.soinside.com 2019 - 2024. All rights reserved.