Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to be able to get a copy of packets received on port 8001 to port 8002. I have tried the following but I get an error that --tee is undefined.

sudo iptables -t nat -A PREROUTING -p TCP -s 127.0.0.1 --sport 8001 -j DNAT --to-destination 127.0.0.1:8002 --tee
share|improve this question
    
Your kernel or iptables may be too old; see Send duplicate packets over two Internet connections. –  Gilles Jun 30 '11 at 21:13

2 Answers 2

The --tee flag is not part of the DNAT chain, it is part of ROUTE. You can only use it following a declaration of -j ROUTE. You can get specific help from iptables on the subject like this:

 $ iptables -j ROUTE help

I was looking at your iptables command, and it doesn't make any sense to me. Why are you trying to match against the source and source port of a packet when in your question you said "packegs received on port"? Are you trying to split incoming traffic to hit two ports or take the output of one port and tie it to the input of another?

If the former, there are really two steps. You can't use tee to get a copy of the packet AND mangle the packet to change the port numbers at the same time. You might try this in two steps, first sending yourself a duplicate copy of the packet, then matching the copy only and mangling the destination port. WARNING: untested, consider this pseudo-code:

$ sudo iptables -A PREROUTING -t mangle -p tcp -s !127.0.0.1/32 --dport 8001 -j ROUTE --gw 127.0.0.1 --tee
$ sudo iptables -A POSTROUTING -t nat -p tcp -s 127.0.0.1/32 --dport 8001 -j DNAT --to 127.0.0.1:8002
share|improve this answer
    
Warning...another site visitor has noted that a similar version of this actually results in an infinite loop! You probably need to figure out how to tag the duplicated packet in the same action, then match on the tag. –  Caleb Apr 17 '12 at 18:00
    
ROUTE seems obsoleted, right? –  warl0ck Dec 30 '12 at 8:12
    
It'just a typo, 172.0.0.1 -> 127.0.0.1. –  Aki Nov 6 '13 at 17:29
    
N.B. to SE readers: The ROUTE target used in the above answer is obsolete as of this writing and not available in iptables on most recent distros. See serverfault.com/questions/333155/… –  Jonathan Ben-Avraham Dec 16 at 13:32

Further to Caleb's answer, if you are working with newer iptables (v1.4.14) that no longer has the ROUTE target you will need something like the following, tested on Debian Wheezy*:

iptables -A PREROUTING -t mangle -p tcp ! -s 127.0.0.1 --dport 8001 -j TEE --gateway 127.0.0.1
iptables -A OUTPUT -t nat -p tcp -s 127.0.0.1/32 --dport 8001 -j DNAT --to 127.0.0.1:8002

Test using "netcat" (man nc). In a terminal window type to following and press the Enter key:

nc -l 8002

The command will wait for input that you will type in a second terminal window.

In s second terminal window type the following and press the Enter key:

nc 127.0.0.1 8001

The command will wait for further input. Type anything and press the Enter key. After you press the Enter key in the second terminal window, the text that you typed in the second terminal window should appear in the first terminal window. PressCtrl-c in the second window to end the session.


*This syntax is not supported in RHEL/Centos (6.5 or earlier) :-( so you need to use socat to tee and forward incoming packets on the original port to two new ports. If you had processes listening on the original receiving port then you need to reconfigure them to listen on one of the tee'd ports as socat is now the listener on the original port. See this SE post for example socat syntax for port cloning.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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