1

I am trying to connect one of my PCs to the Internet via a WiFi network using a notebook. Here is my network :

(Wifi) <=============> (Notebook) <===============> (Desktop)
       (192.168.1.x)                 (10.12.0.x)
                      (192.168.1.85                 (10.12.0.2)
                      & 10.12.0.1)

So, I have ipv4_forward enabled on my netbook, and this NAT rule :

iptables -t nat -A POSTROUTING -s 10.12.0.0/24 -j MASQUERADE

Here is my routing table on my notebook :

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.254   0.0.0.0         UG    0      0        0 wlan0
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
192.168.1.0     0.0.0.0         255.255.255.0   U     9      0        0 wlan0
192.168.1.85    127.0.0.1       255.255.255.255 UGH   303    0        0 lo

Here is my routing table on my desktop :

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.12.0.0       0.0.0.0         255.255.255.0   U     0      0        0 enp2s0
192.168.1.0     10.12.0.1       255.255.255.0   UG    0      0        0 enp2s0

I can ping everything on the 192.168.1.x network (my Internet gateway 192.168.1.254 included), but I can't add the simple route

route add default gw 192.168.1.254 enp2s0

or

ip route add default via 192.168.1.254 dev enp2s0

I keep getting errors like :

SIOCADDRT: Network is unreachable
RTNETLINK answers: Network is unreachable

And, of course, I can't ping any WAN server from my desktop. Do you have an idea of what I am doing wrong ?

Thank you.

MayeulC
  • 150
  • 10
  • 1
    I think what you are really looking for is `ip route add default via 10.12.0.1 dev enp2s0`. – Glueon Oct 15 '14 at 20:34
  • @Glueon - Make that an answer and I'll upvote it. – Evan Anderson Oct 15 '14 at 20:44
  • 2
    @MayeuIC - You're getting the `Network is unreachable` errors because the desktop machine has no route to `192.168.1.254` via any entry in the routing table. You want the desktop computer's default gateway to be the 10.12.0.1 NIC in the netbook because the desktop *can* reach 10.12.0.1. – Evan Anderson Oct 15 '14 at 20:46
  • If that is really needed to use a gateway which is outside of your network this should work: `ip route add 192.168.1.0/24 dev enp2s0` and then `ip route add default via 192.168.1.254` – Glueon Oct 15 '14 at 20:48
  • 1
    Thank you to both of you. May I ask you why I don't need to specify the gateway (192.168.1.254) ? is it because the netbook already knows it and forwards the packets to it, thus using its own routing table to forward the packets to the right destination ? (Sorry, I am not so familiar with routing) – MayeulC Oct 15 '14 at 20:50
  • @Gluenon : this `ip route add default via 192.168.1.254` is precisely what didn't work. – MayeulC Oct 15 '14 at 20:52
  • You need to do `ip route add 192.168.1.0/24 dev enp2s0` before adding 192.161.254 as a gateway thus informing your system that 192.168.1.254 which is a part of your 192.168.1.0/24 network is reachable through the enp2s0 device. But normally gateway should be within your IP's network which is 10.12.0.0/24 in your case. – Glueon Oct 15 '14 at 20:56
  • I think this route was already set up correctly (Look at the routing table) : I get the message `RTNETLINK answers: File exists` when trying to add it. (Sent from my desktop :P ) – MayeulC Oct 15 '14 at 20:59
  • 1
    @MayeulC Because your dekstop computer is not connected to 192.168.1.254 (you are not part of the 192.168.1.0/24 network). You need a routing hop to reach it, so your gateway must be what's handling this hop on the network you are connected in : your notebook. That's *routing*. – Xavier Lucas Oct 15 '14 at 21:05
  • Thank you very much, I didn't really see it like that. I guess I won't do the same mistake next time :) – MayeulC Oct 15 '14 at 21:18

1 Answers1

1

I think what you are really looking for is: ip route add default via 10.12.0.1 dev enp2s0

Glueon
  • 3,664
  • 2
  • 24
  • 32