| Linode Forum https://forum.linode.com/ |
|
| OpenVPN + iptables / NAT routing https://forum.linode.com/viewtopic.php?f=19&t=4218 |
Page 1 of 1 |
| Author: | mikeage [ Sat May 16, 2009 10:44 pm ] |
| Post subject: | OpenVPN + iptables / NAT routing |
Hi, I'm trying to set up an OpenVPN VPN, which will carry some (but not all) traffic from the clients to the internet via the OpenVPN server. My OpenVPN server has a public IP on eth0, and is using tap0 to create a local network, 192.168.2.x. I have a client which connects from local IP 192.168.1.101 and gets VPN IP 192.168.2.3. On the server, I ran: Code: iptables -A INPUT -i tap+ -j ACCEPT On the client, the default remains to route via 192.168.1.1. In order to point it to 192.168.2.1 for HTTP, I ran Code: ip rule add fwmark 0x50 table 200 Now, if I try accessing a website on the client (say, wget google.com), it just hangs there. On the server, I can see Code: $ sudo tcpdump -n -i tap0 Where 74.125.67.100 is the IP it gets for google.com . Why isn't the MASQUERADE working? More precisely, I see that the source showing up as 192.168.1.101 -- shouldn't there be something to indicate that it came from the VPN? |
|
| Author: | tin0x3cc [ Sat May 16, 2009 11:00 pm ] |
| Post subject: | |
Pardon me if this sounds dumb to you, but have you actualy enabled IP forwarding? It's disabled by default on most distributions. net.ipv4.ip_forward should be set to 1 with sysctl. Sorry again if this is too obvious, because most of your post is actualy way over my little head :-) Hope it helps... |
|
| Author: | mikeage [ Sat May 16, 2009 11:14 pm ] |
| Post subject: | |
Yes. Code: $ cat /proc/sys/net/ipv4/ip_forward |
|
| Author: | hoopycat [ Sun May 17, 2009 3:20 am ] |
| Post subject: | Re: OpenVPN + iptables / NAT routing |
mikeage wrote: Code: iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o eth0 -j MASQUERADE Code: 05:39:07.928358 IP 192.168.1.101.34941 > 74.125.67.100.80: S 4254520618:4254520618(0) win 5840 <mss 1334,sackOK,timestamp 558838 0,nop,wscale 5> Your iptables line is looking for packets originating from 192.168.2.0/24 and going to eth0, while the source of the packet is actually 192.168.1.101. Changing the iptables line to match 192.168.1.0/24 should clear it up. |
|
| Author: | mikeage [ Sun May 17, 2009 3:30 am ] |
| Post subject: | |
Hoopycat -- do you know why this is? 192.168.1.101 is the random local IP address that my client has; tomorrow, it could be anything else. As far as the server is concerned, the only fixed address is 192.168.2.2 . How can I have the packets coming from the VPN come from that source? EDIT: This also doesn't appear to be working: Code: # sudo iptables -t nat -L -v Although no connection can be established on outgoing port 80 [maybe the problem is with the return?] |
|
| Author: | tin0x3cc [ Sun May 17, 2009 4:50 am ] |
| Post subject: | |
mike: have you tried setting up a simple vpn first? Something without all the extra things to specify which kind of traffic goes through? |
|
| Author: | mikeage [ Sun May 17, 2009 4:53 am ] |
| Post subject: | |
Yep; it works just fine. In fact, I'm using it right now.... |
|
| Author: | tin0x3cc [ Sun May 17, 2009 5:02 am ] |
| Post subject: | |
So the client can use the vpn, it all stops working only when you try to have only http traffic going through? edit: I'm asking because I see you're using bridge mode (why?), while routing mode is usually recommended. Anyway, you're using bridged mode, yet we see no mention of br0 in your config. Is forwarding on on the br0 interface? |
|
| Author: | mikeage [ Sun May 17, 2009 5:37 am ] |
| Post subject: | |
The client can still use the VPN, but when I try and have traffic from the client routed to the internet via the VPN, that traffic never makes it out. tcpdump suggests that it is making it to the VPN gateway. I have no fundamental interest in routing vs. bridging; I have no network on the server to speak of (it's just my linode). I did switch from dev tun to dev tap a while ago, since I found the /30 addressing to be quite confusing. |
|
| Author: | hoopycat [ Sun May 17, 2009 10:25 am ] |
| Post subject: | |
mikeage wrote: Hoopycat -- do you know why this is? 192.168.1.101 is the random local IP address that my client has; tomorrow, it could be anything else. As far as the server is concerned, the only fixed address is 192.168.2.2 . How can I have the packets coming from the VPN come from that source? You can enable NAT on the VPN client (192.168.2.2). Then, the packets will appear to be coming from 192.168.2.2 when they hit your server. However, you'd then be NATting traffic twice, which is somewhat silly. (Remember, the packet *originated* at 192.168.1.101; 192.168.2.2 is merely an intermediate router.) Quote: EDIT: This also doesn't appear to be working:
Code: # sudo iptables -t nat -L -v Although no connection can be established on outgoing port 80 [maybe the problem is with the return?] Hmmm... if you tcpdump eth0, are you seeing the traffic going out with the right addresses? I'm wondering if having that ACCEPT in PREROUTING is messing things up and causing it to skip the POSTROUTING... a skim through the man page doesn't indicate anything for sure, but a SNAT in POSTROUTING will cause it to ignore any further rules, so that'd be my next thing to try |
|
| Author: | mikeage [ Sun May 17, 2009 10:41 am ] |
| Post subject: | |
hoopycat wrote: You can enable NAT on the VPN client (192.168.2.2). Then, the packets will appear to be coming from 192.168.2.2 when they hit your server. However, you'd then be NATting traffic twice, which is somewhat silly. (Remember, the packet *originated* at 192.168.1.101; 192.168.2.2 is merely an intermediate router.) True, but my concern is that in defining the rules, I can easily add 192.168.2.x to my iptables setup. I don't know how I can know ahead of time that the VPN will be passing traffic through from 192.168.1.101. Maybe tomorrow I'll connect to my VPN from a very generous public wifi hotspot, and I'd have a regular public IP? hoopycat wrote: Hmmm... if you tcpdump eth0, are you seeing the traffic going out with the right addresses? I'm wondering if having that ACCEPT in PREROUTING is messing things up and causing it to skip the POSTROUTING... a skim through the man page doesn't indicate anything for sure, but a SNAT in POSTROUTING will cause it to ignore any further rules, so that'd be my next thing to try
Nope Code: # iptables -t nat -F And then I ran telnet google.com 80 from my client, while monitoring the server... Code: # tcpdump -i tap0 |
|
| Author: | hoopycat [ Sun May 17, 2009 11:22 am ] |
| Post subject: | |
mikeage wrote: True, but my concern is that in defining the rules, I can easily add 192.168.2.x to my iptables setup. I don't know how I can know ahead of time that the VPN will be passing traffic through from 192.168.1.101. Maybe tomorrow I'll connect to my VPN from a very generous public wifi hotspot, and I'd have a regular public IP? Right now, you've got two machines involved at the endpoint: your workstation (192.168.1.101) and your VPN gateway (192.168.2.2). If you connect straight from your workstation (or laptop, or whatever), your traffic should be originating from 192.168.2.0/24 or whatever the OpenVPN stuff is assigning. mikeage wrote: And then I ran telnet google.com 80 from my client, while monitoring the server...
Code: # tcpdump -i tap0 How about on the eth0 side? Do you see it as 192.168.1.101 or something else? (For the record, I do use OpenVPN, but not NAT; for NAT-like taste with half the calories, I use ssh -D. I'm a wee bit lazy like that. |
|
| Author: | mikeage [ Sun May 17, 2009 1:49 pm ] |
| Post subject: | |
hoopycat wrote: Right now, you've got two machines involved at the endpoint: your workstation (192.168.1.101) and your VPN gateway (192.168.2.2). If you connect straight from your workstation (or laptop, or whatever), your traffic should be originating from 192.168.2.0/24 or whatever the OpenVPN stuff is assigning. 192.168.2.2 is the address my laptop gets from the VPN. The VPN server (aka my linode) is 192.168.2.1. hoopycat wrote: How about on the eth0 side? Do you see it as 192.168.1.101 or something else?
Since you asked Code: $ sudo tcpdump -i eth0 -n | grep 74.125.45.100 It may not be obvious at first from the logs, but the traffic here was in bursts (retries?). I'm really leaning towards a problem with the return traffic, but I don't know what else needs to be set up to have it re-masquerade back. I'm also not sure if I should double NAT it... what do you think? I've also used SOCKS5 proxies (or just an SSH tunnel to 3128): my goal here is to set up a PC to always use a transparent squid proxy (to prevent direct access to the internet). |
|
| Author: | mikeage [ Sun May 17, 2009 3:37 pm ] |
| Post subject: | |
Ok... I just tried something else. I disabled by entire firewall and tried to do just plain NAT from the VPN client to the internet. Code: $ sudo iptables -v -L I did set up logging on the forwarded packets that were not being accepted: Code: $ dmesg | tail [btw, I did switch my test machine from 192.168.1.101 to 192.168.1.100.] If I'm reading this correctly, the packets returning from google's server are being sent to 192.168.1.100, but my VPS doesn't know how to get there (since it only recognizes the machine called 192.168.1.100 by the name of 192.168.2.2) Maybe I do need double NAT? |
|
| Author: | hoopycat [ Mon May 18, 2009 7:40 am ] |
| Post subject: | |
Alas, you've officially exceeded my knowledge of iptables. |
|
| Page 1 of 1 | All times are UTC-04:00 |
| Powered by phpBB® Forum Software © phpBB Group http://www.phpbb.com/ |
|