大网数据广告活动
服务器运维问答

Forwarding ICMP response to TAP interface

· 52CE 我爱测

I am trying to do a test where I forward ICMP responses to a TAP interface. I created a TAP interface and assigned it the address 10.0.4.1/24 . My linux setup uses the interface enp0s3 as the default option. To forward any ICMP r…

问题原文

原帖链接 · Jake · CC BY-SA 4.0

I am trying to do a test where I forward ICMP responses to a TAP interface. I created a TAP interface and assigned it the address 10.0.4.1/24.

My linux setup uses the interface enp0s3 as the default option. To forward any ICMP response to the TAP interface, I tried the following rules:

# iptables -t nat -A PREROUTING -i enp0s3 -p icmp -j DNAT --to 10.0.4.1
# iptables -A FORWARD -p icmp -i enp0s3 -j ACCEPT
# iptables -t nat -A POSTROUTING -o 10.0.4.1 -j MASQUERADE

At the moment, the TAP interface is in UP state, and I have a program which is listening to the TAP interface.

If I ping an external IP like 8.8.8.8, the ping program is still able to get the response. I want to ask shouldn't the ping program not receive any response because ICMP response is getting forwarded to the TAP interface ? Also, is my approach to redirect traffic to the TAP device correct ?

已采纳解答

原帖链接 · A.B · CC BY-SA 4.0

The IP assigned to the TAP interface belongs to the host, so is a local IP. Example:

# ip -4 address show tap0
20: tap0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    inet 10.0.4.1/24 scope global tap0
       valid_lft forever preferred_lft forever
# ip route get 10.0.4.1
local 10.0.4.1 dev lo src 10.0.4.1 uid 0 
    cache <local> 

What does matter is the route to the TAP interface. That is, because of the implicit route added when the interface is brought up, any IP in 10.0.4.0/24 except 10.0.4.1.

# ip route get 10.0.4.2
10.0.4.2 dev tap0 src 10.0.4.1 uid 0 
    cache 

So if you want your program to actually see any traffic, try replacing in your rules 10.0.4.1 with for example 10.0.4.2. It's up to the attached program to actually reply from this IP if that's the goal (and if using tap rather than tun, reply to ARP as well).

Other notes:

  • you made a typo in the last rule it's a destination address so -d rather than -o. It's up to you to choose to MASQUERADE the source at all, it's not needed to route the packet to the program and have a (correct) reply sent back to the initial source.
  • it's not clear if you're actually involving an other peer to do the test. Doing the test from the host itself would not require nat/PREROUTING and filter/FORWARD but nat/OUTPUT and filter/OUTPUT.
  • If an other peer was indeed involved, as it was, your test would have been using filter/INPUT rather than filter/FORWARD and not using nat/POSTROUTING, since the destination is local. Anyway once corrected it would be using filter/FORWARD and nat/POSTROUTING.
  • if using tap (layer 2: ethernet) rather than tun (layer3: ip), when looking for this icmp (with tcpdump or in the attached program) and not seing it, look also for unanswered ARP requests.

Further reference: https://backreference.org/2010/03/26/tuntap-interface-tutorial/

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-03-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。