Score:1

Linux router used to redirect traffic from the outside to an internal device, but the internal device needs to respond using another port

in flag

I am working on a router between an external radio/modem and my device, where the router makes a dial-up connection using the ppp protocol to the radio and sets an iptables rules between the received radio IP and my device's IP.

I have the following routes:

Destination     Gateway            Genmask         Flags Metric Ref    Use Iface
default         192.168.1000.101   0.0.0.0         UG    0      0        0 ppp0
192.168.100.101 *                  255.255.255.255 UH    0      0        0 ppp0
192.168.164.0   *                  255.255.255.0   U     0      0        0 eth0

And here are my following iptables rules:

iptables -A INPUT -j ACCEPT
iptables -A OUTPUT -j ACCEPT
iptables -A FORWARD -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -t nat -A PREROUTING -i ppp0 -p tcp -j DNAT --to 192.168.0.110
iptables -t nat -A PREROUTING -i ppp0 -p udp -j DNAT --to 192.168.0.110
iptables -t nat -A POSTROUTING --source 0/0 -o ppp0 -j MASQUERADE

With these rules I can access everything on my device like telnet, web server and some other communications using high ports.

My problem is that I need to connect to my device using a source port with a destination port, and this is not a problem, but my device needs to respond using another port it created for this communication to continue.

For example, I try to make a connection using source port 2000 with destination port 2001 and my device's response returns using source port 21550 and destination port 2000. But the response don't get back to the client connected to the other side of the radio.

By using tcpdump inside my router I saw the device response, but I couldn't figure out how to handle this port changes in the iptables rules to send it back to the client connected to the other side of the radio.

Any help will be greatly appreciated!

Michael Hampton avatar
cz flag
Don't use NAT. Just route normally.
VBalestero avatar
in flag
Michael thanks for the quick answer. But I do note understand your answer, you mean not to use iptables, only the route table, is that correct?
Ron Maupin avatar
us flag
NAT is _not_ a substitute for routing. You do not use NAT unless you must (private to public or overlapping addressing). You do not have any reason to NAT since you have non-overlapping, private addressing, so you can simply route and bypass all the problems of NAT.
VBalestero avatar
in flag
Thanks for the quick answers, I will try it out without iptables.
djdomi avatar
za flag
Moreover, i see you try to use some ports, please tell us what you are trying to fix (X and Y Problem)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.