5

I'm just starting out on a mission to learn more about networking and I've followed a tutorial (https://raspberrypihq.com/how-to-turn-a-raspberry-pi-into-a-wifi-router/) to turn my Raspberry Pi into a wifi router.

That worked really well so I modified it slightly so that I can use a tethered iphone for the internet connection - I just switched all "eth0" references to "eth1" (the iphone interface) and added a script to set everything up when the phone is plugged in.

This setup has freed up the Pi's ethernet port so I'd like to try and take this a step further and allow devices plugged into it to connect to the network. If possible, I'd like to try adding a switch so I can connect multiple devices.

I've tried fiddling around with nat & iptables with no luck so my question is, how can I connect devices on eth0 to my wlan network?


EDIT:

Some extra info below. These are from after the phone is plugged in.

Output from iptables -t nat -L:

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
MASQUERADE  all  --  anywhere             anywhere

Output from netstat -rn:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         172.20.10.1     0.0.0.0         UG        0 0          0 eth1
172.20.10.0     0.0.0.0         255.255.255.240 U         0 0          0 eth1
192.168.10.0    0.0.0.0         255.255.255.0   U         0 0          0 wlan0

EDIT 2:

The following is in my dhcpd.conf file:

subnet 192.168.10.0 netmask 255.255.255.0 {
 range 192.168.10.10 192.168.10.200;
 option broadcast-address 192.168.10.255;
 option routers 192.168.10.1;
 default-lease-time 600;
 max-lease-time 7200;
 option domain-name "local-network";
 option domain-name-servers 8.8.8.8, 8.8.4.4;
}

dhcp is configured to run on wlan0 - /etc/default/isc-dhcp-server:

INTERFACES="wlan0"

1 Answer 1

3

Strictly speaking, not much else is required. I presume you have already enabled IPv4 forwarding, and possibly also this rule:

  iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

which will allow you to NAT all of your connections. Then your routing table needs

  ip route add default via IP_ADDRESS_OF_YOUR_PHONE

which it also presumably already has. Then you are good to go.

EDIT:

Now that you posted your routing table (but please, do not use obsolete, deprecated commands like *ifconfig, route,..., just the ip command, for instance, for the routing table ip route show, to see the addresses of interfaces ip addr show,...) I see that you do not have a route to your ethernet port-clients. If the network from which you draw IP addresses port cable-connect clients is 192.168.20.0/24, then you will have to add:

   ip route add 192.168.20.0/24 dev eth1

You should not use the same network for wireless- and cable- clients.

If furthermore you need to configure dhcpd, then edit the same file /etc/dhcp/dhcpd.conf as before, and add the new network:

  subnet 192.168.10.0 netmask 255.255.255.0 {
option subnet-mask 255.255.255.0;
option routers 192.168.10.1;
range 192.168.10.100 192.168.10.200;
}

subnet 192.168.20.0 netmask 255.255.255.0 {
option subnet-mask 255.255.255.0;
option routers 192.168.20.1;
range 192.168.20.100 192.168.20.200;
}

In the file /etc/default/isc-dhcp-server, change the line INTERFACES="wlan0" to

 INTERFACES="wlan0 eth0"

and lastly give the router a good address on its eth0 NIC:

 ip link set dev eth0 down
 ip addr flush dev eth0
 ip addr add 192.168.20.1/24 dev eth0
 ip link set dev eth0 up

Restart dhcp, you should be Ok.

8
  • Thanks for your answer. I tried ip route add default via 172.20.10.1 and got the error: RTNETLINK answers: File exists. I've edited my answer with some extra info.. any ideas where I'm going wrong?
    – Tom
    Jun 11, 2014 at 12:21
  • @Tom Nowhere: it only means it already has a default gateway. However, your problem (now that you post your routing table) is that you do not have a route to reach pcs connected via cable. You will need to add that. I will add shortly an Edit to my answer Jun 11, 2014 at 12:30
  • @Tom Can you tell me whether you have already configured dhcp on eth0? Jun 11, 2014 at 12:33
  • @Tom Please see my edit. Jun 11, 2014 at 12:48
  • I've added my dhcp configuration to my original question. I followed your instructions and got connected to the Pi from my mac with the ip 192.168.20.101 but couldn't access the internet or ssh to the Pi. Any ideas?
    – Tom
    Jun 11, 2014 at 14:06

You must log in to answer this question.

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