How do I route a packet to use localhost as a gateway?

I am trying to check the gateway that I wrote (see What is the easiest way to test the gateway? For context). Due to a problem that I would prefer not to get, the gateway and the "sender" must be on the same machine. I have a receiver (say, 9.9.9.9) that the gateway can reach.

So, I launched an application ./sendStuff 9.9.9.9that will send some packets to this IP address.

The problem is this: how to get packets destined for 9.9.9.9 to go to the gateway on the local host? I tried:

sudo route add -host 9.9.9.9 gw 127.0.0.1 lo

sudo route add -host 9.9.9.9 gw <machine external IP address> eth0

but not one of them passes packets through the gateway. I have verified that the correct IP addresses are present in sudo route. What can I do?


Upon request, here is the route table, after running the second command (IP addresses are changed to fit the question. Xyzt is the IP address of the machine on which I run this):

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
9.9.9.9         x.y.z.t         255.255.255.255 UGH   0      0        0 eth0
x.y.z.0         0.0.0.0         255.255.254.0   U     0      0        0 eth0
0.0.0.0         <gateway addr>  0.0.0.0         UG    100    0        0 eth0
+3
source share
1 answer

127.0.0.1 probably collects the packets and then redirects them for fun if ipv4 packet forwarding is enabled. If it is not turned on, it will delete them.

If you are trying to forward packets destined for 9.9.9.9 to 127.0.0.1, check out iptables.

Edit: try the following:

iptables -t nat -A OUTPUT -d 9.9.9.9 -j DNAT --to-destination 127.0.0.1

This should redirect all traffic to 9.9.9.9 instead of localhost.

+5
source

Source: https://habr.com/ru/post/1790869/


All Articles