9

My laptop (use's Ubuntu 13.10) is connected to internet using a wired connection at eth0 with ip 192.168.42.13 and it also acts as a wifi hotspot at wlan0 with the ip 192.168.1.1 (i used airbase-ng to set up the ap). Now I have an android device that needs to get connected to that wifi. I see that my android is able to connect to the hotspot and receive the following network settings from the dhcp server I set up. (I used a tool call Net Info II from PlayStore to see the below details on my android).

# My android network settings (connected to the wlan0 of my laptop):
ip address: 192.168.1.10
subnetmask: 255.255.255.0
gateway   : 192.168.1.254
ap-mac    : xx:xx:xx:xx:xx:xx

# My pc's eth0 configuration
ip address: 192.168.42.13
submetmask: 255.255.255.0
gateway   : 192.168.42.129
iface mac : yy:yy:yy:yy:yy:yy

# My pc's wlan0 (hotspot interface) configuration
ip address: 192.168.1.1
subnetmask: 255.255.255.0
gateway   : 192.168.1.1
iface mac : zz:zz:zz:zz:zz:zz

This thread here talks about a similar problem. I see that I need to forward packets between eth0 and wlan0 network to connect my wireless ap's devices to internet. Now How do I forward the packets I receive from wlan0 to eth0 and eth0 to wlan0 to get my device connected to internet ?

5
  • 1
    You might have another problem: why does your Android device list 192.168.1.254 as its gateway? I thought you only had two interfaces in your set-up Commented Dec 5, 2013 at 13:17
  • I might not have configured my dhcpd.conf properly. I should set it to 192.168.1.1 I guess. Thanks for the correction !!!
    – vikkyhacks
    Commented Dec 5, 2013 at 13:20
  • Another thing: do you have a rule in your pc's routing table telling it to use IF wlan0 for the 192.168.1.0/24 network? Commented Dec 5, 2013 at 13:22
  • No I dont, How do I add it ? and is it necessary that I add it, since 192.168.1.1 is tied to to wlan0 will that rule be necessary ?
    – vikkyhacks
    Commented Dec 5, 2013 at 13:30
  • 1
    It is necessary. The command is sudo route add -net 192.168.1.0/24 dev wlan0 Commented Dec 5, 2013 at 13:33

1 Answer 1

7

You first need to enable IP forwarding on your system. To do it just this once, use

sysctl -w net.ipv4.ip_forward=1

or

echo 1 > /proc/sys/net/ipv4/ip_forward

as root. If you want to make it permanent, edit

/etc/sysctl.conf

and add a line containing

net.ipv4.ip_forward = 1

This will be applied on reboot or when you run

sysctl -p /etc/sysctl.conf

Once that is done, you probably need to NAT connections, as it is unlikely your router knows that the 192.168.1.0/24 network is accessible through 192.168.42.13. If 192.168.42.13 is static, you can use

iptables -t nat -A POSTROUTING -i wlan0 -s 192.168.1.0/24 -j SNAT --to-source 192.168.42.13

otherwise use

iptables -t nat -A POSTROUTING -i wlan0 -s 192.168.1.0/24 -j MASQUERADE

Check the Ubuntu IptablesHowTo for how to save those rules when rebooting.

3
  • many thanks for your answer, but I am very new to iptables. The way I see your rule you seem to redirect traffic from wlan0 to 192.168.42.13 but am I not supposed to do the reverse as well, to forward traffic from 192.168.42.13 to wlan0. Or is that done automatically ?
    – vikkyhacks
    Commented Dec 5, 2013 at 13:02
  • 1
    @vikkyhacks When you set up SNAT, returning packets are automatically returned to their proper destination. Netfilter tracks the connections to know which IP originally sent the packet and forward the replies back to that address. Commented Dec 5, 2013 at 18:14
  • 5
    I couldn't use iptables -i option together with POSTROUTING. This command helped me out: iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE Commented Jun 13, 2014 at 10:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .