Hi,
I have a TP-Link TL-WR1043N/ND v1 router running OpenWrt Chaos Calmer 15.05.1.
I have separate subnets for my wired LAN (192.168.123.0/24) and WiFi (192.168.125.0/24).
There is a NAS connected to the wired LAN (192.168.123.10) running twonky media server. On wifi there are some mobile devices (phone, tablet) with uPnP media player software.
The mobile devices need to see the Twonky server on the NAS. This is done by SSDP multicast messages (239.255.255.250 on port 1900). The multicasts are forwarded from LAN to Wifi and from Wifi to LAN (and this works).
There are 2 ways for client devices to see the Twonky server:
1) The Twonky server sends out a SSDP multicast Notify message. This message is received by the client and the server shows up in the library list. The disadvantage is that the server is sending this Notify message only once every minute. So you have to wait for max a minute before the server shows in the library.
2) The client software sends out a SSDP multicast M-Search message from a random UDP port to UDP port 1900. The twonky server receives this message, and sends back a UDP message from a random UDP port to the UDP port the M-Search message was received from. For example: 192.168.123.10 is the server, 192.168.123.50 is the client (I use the same subnet for this example). 192.168.123.50 is sending a M-Search message to 239.255.255.250:1900 from port 46359 (or another, because it's random). 192.168.123.10 is receiving this message, and sends a unicast message back to 192.168.123.50:46359 from port 39099 (also random). This way the client is instantly aware of the server and it shows up in the library.
The first method is working both in single and multiple subnet (with multicast routing). The second is only working when server and client are in the same subnet. When in different subnets, the server is receiving the M-Search message, but isn't sending a response (probably because it's programmed to not send a response when the request is from another subnet).
I was thinking I could fool the server by using NAT.
When adding the following rule to iptables the same M-Search message is received by the server, with the difference that the source address is now the router, which is in the same subnet:
iptables -t nat -I POSTROUTING -o eth0.1 -d 239.255.255.250 -p udp --dport 1900 -j SNAT --to 192.168.123.1
This works, the server is now sending back an answer to 192.168.123.1. But then I have a problem: the answer is send to the router, and not arriving at the client. This is probably because the answer is non-related to the question (UDP and a random source port instead of 1900 that was used in the question) and the router probably has no clue this packet is related to the SNAT rule.
I can make a manual rule like this:
iptables -t nat -I PREROUTING -i eth0.1 -s 192.168.123.10 -d 192.168.123.1 -p udp -j DNAT --to 192.168.125.42
192.168.125.42 is the address of the client (my phone). This works. The answer is now arriving at the client, and the server shows in the library. The drawback is that this rule is not very specific: there might be other UDP packets from 192.168.123.10 to 192.168.123.1, but because I don't know the port numbers of the answer, I can't be more specific. The other drawback is that you can only make a rule this way for a single client. If you have multiple clients in your wifi, you can't define a rule for all of them (because you can't make the rule more specific).
My question: What can I do to make this work? Can I configure iptables in a way that it recognizes the answer as an answer on a snatted question so it can dnat it automatically? Or can I dynamically add a rule to iptables the moment a client sends a M-Search message, use the port number the message came from in that new rule, and automatically delete it a few seconds later? Or port triggering maybe?
Best regards,
Marc