How do I route a packet to use localhost as a gateway?
127.0.0.1 is probably picking up the packets, then forwarding them on their merry way if ipv4 packet forwarding is enabled. If it's not enabled, it will drop them.
If you are trying to forward packets destined to 9.9.9.9 to 127.0.0.1, look into iptables.
Edit: try this:
iptables -t nat -A OUTPUT -d 9.9.9.9 -j DNAT --to-destination 127.0.0.1
That should redirect all traffic to 9.9.9.9 to localhost instead.
Mike
Updated on June 22, 2022Comments
-
Mike almost 2 years
I'm trying to test a gateway I wrote(see What's the easiest way to test a gateway? for context). Due to an issue I'd rather not get into, the gateway and "sender" have to be on the same machine. I have a receiver(let's say 9.9.9.9) which the gateway is able to reach.
So I'll run an application
./sendStuff 9.9.9.9
which will send some packets to that IP address.The problem is: how do I get the packets destined for 9.9.9.9 to go to the gateway on localhost? I've 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's external IP address> eth0
but neither of those pass packets through the gateway. I've verified that the correct IPs are present in
sudo route
. What can I do?
Per request, here is the route table, after running the second command(IP addresses changed to match the question. x.y.z.t is the IP of the machine I'm running this on):
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
-
Mike about 13 yearsI'm not sure what you mean by 'proxy.' The gateway accepts plain IP packets on an interface(can be lo or eth0), and if the dest IP matches a prefix(for now, let's just say 9.9.9.9) 'magically' forwards them off to the host at the other endpoint. Can you give an exampe of how to use iptables postrouting to do this?
-
Mark Rose about 13 yearsProxy might have been too specific of word. I meant anything listening on the local machine. I'll edit this note in a second with an example.
-
Mike about 13 yearsSadly, the packets are still not being routed to my application, which is listening for any IP packets sent to
lo
. Any ideas on how to get my app to receive the packets? I don't mind the packets going out over the network, so long as they stay within the subnet and are routed back to me. -
Mark Rose about 13 yearsDoes
netstat -ln
confirm the app is listening on 127.0.0.1? Never hurts to double check. You can also open up Wireshark and get it to listen onlo
.