I am trying to configure AP with redirection incoming traffic through TOR. My AP now has 2 network interfaces:

  1. eth0 - have internet access (
  2. wlan0 - intranet, without internet (

For accessing internet from wlan0 I did usual stuff: enabled forwarding and masquerading. For traffic redirection(to tor) I did iptables redirection. So, entire iptables looks like:

TOR_UID=$(id -ur debian-tor)

# masqaerade for wlan0
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

iptables -t nat -A OUTPUT -m owner --uid-owner $TOR_UID -j RETURN
iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 53

for NET in $NON_TOR; do
    iptables -t nat -A OUTPUT -d $NET -j RETURN

iptables -t nat -A OUTPUT -p tcp -j DNAT --to-destination
# iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $TRANS_PORT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT   

Tor config:

AutomapHostsOnResolve 1
DNSPort 53
ControlPort 9051

But for AP clients this does not work. For all conenctions originated from AP redirection to tor works perfectly. Here is a fragment of captured traffic from AP client (mobile phone) which consists the request to ident.me :

enter image description here

You can notice, that redirection rules which was described in iptables was ignored for masqueraded traffic. Why does this happen and how can it be fixed?

This is how the same request initiated by AP looks like:

enter image description here


Your firewall rules seem correct. However, redirection of packets that enters the AP should be configured in the PREROUTING chain. Rules added into the OUTPUT chain affect locally-generated packets only.

So, the following rule should be added:

iptables -t nat -A PREROUTING -i wlan0 -p tcp -j DNAT --to-destination

Depending on the policy set at the FORWARD chain in filter table, the following rule may be needed too:

iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT   
  • Thanks for this explanation about redirection of AP packets! Now I understood the point and made it work. – AseN May 3 '18 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.