0

I have 3 computers: 192.168.2.1, 192.168.2.2, 192.168.2.4 connected into an wireless ad-hoc network. I want all the traffic from 192.168.2.1 to go through 192.168.2.2 when it talks to 192.168.2.3. Basically

192.168.2.1 <---> 192.168.2.2 <---> 192.168.2.3

How do I alter the routing tables?

I tried:

[email protected]:   sudo ip route add 192.168.2.3 via 192.168.2.2 dev wlan0

[email protected]:   sudo ip route add 192.168.2.1 via 192.168.2.2 dev wlan0 

I enabled forwading on 192.168.2.2 and it works. But when I do traceroute from 192.168.2.1, it looks like it goes directly to 192.168.2.3. Now, I added few more nodes (see the route table of 192.168.2.1 below) and I still get one hop when I do traceroute. But if I log into 192.192.2.2 and I traceroute 192.168.2.5, it gives me the correct number of hops.

**ROUTE TABLE OF 192.168.2.1**

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    202    0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     303    0        0 wlan0
192.168.1.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0
192.168.2.3     192.168.2.2     255.255.255.255 UGH   0      0        0 wlan0
192.168.2.4     192.168.2.2     255.255.255.255 UGH   0      0        0 wlan0
192.168.2.5     192.168.2.2     255.255.255.255 UGH   0      0        0 wlan0


**ROUTE TABLE OF 192.168.2.2**

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
169.254.0.0     0.0.0.0         255.255.0.0     U     303    0        0 wlan0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0
192.168.2.4     192.168.2.3     255.255.255.255 UGH   0      0        0 wlan0
192.168.2.5     192.168.2.3     255.255.255.255 UGH   0      0        0 wlan0

Is it possible that the nodes do some caching?

EDIT; I have 10 nodes in total now, and this is the output from my traceroute

traceroute -4 192.168.2.10

traceroute to 192.168.2.10 (192.168.2.10), 30 hops max, 60 byte packets
1  192.168.2.2 (192.168.2.2)  10.140 ms  10.324 ms  10.398 ms
2  192.168.2.3 (192.168.2.3)  17.292 ms  17.483 ms  17.564 ms
3  192.168.2.4 (192.168.2.4)  25.646 ms  25.708 ms  26.110 ms
4  192.168.2.5 (192.168.2.5)  34.156 ms  34.416 ms  34.501 ms
5  192.168.2.6 (192.168.2.6)  44.131 ms  44.479 ms  44.560 ms
6  192.168.2.7 (192.168.2.7)  51.382 ms  43.074 ms  46.144 ms
7  192.168.2.8 (192.168.2.8)  46.129 ms  43.374 ms  53.956 ms
8  192.168.2.9 (192.168.2.9)  58.156 ms  83.625 ms  83.642 ms
9  192.168.2.10 (192.168.2.10)  83.565 ms  84.008 ms  89.355 ms
John Militer
  • 773
  • 4
  • 14
  • 29
Andrei
  • 1
  • 7
  • Have you enabled IP forwarding on .3 ? If not, it'll just ignore incoming packets that aren't destined for itself. – garethTheRed May 18 '16 at 14:27
  • yes, I did enabled it – Andrei May 18 '16 at 14:42
  • Could you edit your question and add the `route` of both the 192.168.1.2 and 192.168.1.4. Maybe the problem is that there is a default route from 192.168.1.2 to 192.168.1.4 directly through wlan0 and which is prefered. – migrc May 18 '16 at 14:51
  • Updated the question. – Andrei May 19 '16 at 14:16
  • This sounds like you might be trying to build a [mesh network](http://www.ericerfanian.com/mobile-mesh-networks-with-the-raspberry-pi-part-1/)...? – roaima May 19 '16 at 14:32
  • Also see this duplicate question [How to route 3 computer in wireless ad-hoc network](http://unix.stackexchange.com/questions/78143/how-to-route-3-computer-in-wireless-ad-hoc-network) – roaima May 19 '16 at 14:33
  • I've been through that tutorial. However, it doesn't show how to force a particular topology (see the bottom of my post). – Andrei May 19 '16 at 14:34
  • @roaima Seen that, doesn't help much. – Andrei May 19 '16 at 14:37
  • You'll need those to be /32 networks if you want to route them like that. Routing rules don't apply to local addresses as they can just be arped for. – Eric Renouf May 23 '16 at 16:23
  • Did you check what the actual traffic on the network is? Reset the routes and ARP tables (or better yet, reboot the machines) and run tcpdump -XX on the middle machine. You'll want to check the link layer addresses too. – ilkkachu May 23 '16 at 20:46
  • What are the netmasks for your IP addresses? I don't think the setup you describe is possible. If you want to route traffic from machine 1 to machine 3 through machine 2 you'll need at least 2 distinct network spaces with machine 2 existing in both of them, but machines 1 and 3 cannot have addresses in the same network if you want to have a router handle the traffic between them. Routers are only for routing traffic from one network to another, not for moving things around within a network. – Eric Renouf May 26 '16 at 12:17
  • On each node, the netmask is 255.255.255.0 – Andrei May 26 '16 at 12:57
  • @Andrei yeah, that puts them all in a local network together, and thus no routing is used/required. They're all neighbors. You would have to put them in different networks if you want to route between them, is that an acceptable solution? Where are they getting their addresses currently? – Eric Renouf May 27 '16 at 14:19
  • @EricRenouf at the moment are manually edited. See the edit. – Andrei May 27 '16 at 14:40

2 Answers2

2

It's possible that your router tells the others to send directly, with an ICMP redirect packet. Since they are all on the same network, it knows this should be possible.

I set up routes like yours between three Linux machines (mach1 <-> mach3 <-> mach2), and tested with a ping:

mach1# ping mach2 
PING mach2 (x.x.x.237) 56(84) bytes of data.
From mach3 (x.x.x.238): icmp_seq=1 Redirect Host(New nexthop: mach2 (x.x.x.237))
64 bytes from mach2 (x.x.x.237): icmp_seq=1 ttl=63 time=0.537 ms

Tcpdump on mach3 and mach2 shows mach3 sending a redirect to mach1, and after that all traffic goes directly between mach1 and mach2.

Redirects can be considered a bit fishy, since the can be used in the opposite way: to have traffic sent through a third machine, a man-in-the-middle. See e.g. https://askubuntu.com/questions/118273/what-are-icmp-redirects-and-should-they-be-blocked

Sending and receiving redirects can be controlled with some sysctls:

net.ipv4.conf.*.accept_redirects
net.ipv4.conf.*.send_redirects

After setting net.ipv4.conf.eth0.send_redirects to zero on the router and resetting the routes, I got all traffic to pass through mach3.

Now, I tested this on a switched network (and with virtual machines), so I'm not sure if being on a WLAN changes things. I don't think it should, if the machines only handle frames destined to their Ethernet address. But in theory it could be possible to pick every single frame off the air.

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
  • Unfortunately the problem is the same. I did set up net.ipv4.conf.all.send_redirects=0 and the problem is still the same. Also, I tried setting net.ipv4.conf.all.accept_redirects to 0 and still the same problem – Andrei May 24 '16 at 21:16
  • The interaction between conf.all and conf.$interface variables seems a bit confusing. There's some discussion that for some variables both must be set to enable the function, and for others only one needs to be set: http://unix.stackexchange.com/questions/90443/sysctl-proc-sys-net-ipv46-conf-whats-the-difference-between-all-defau As far as I tried, the redirect was accepted if either conf.all.accept_redirects or conf.eth0.accept_redirects was set. Also, try to tcpdump it to see if redirects are the issue at all. – ilkkachu May 27 '16 at 11:40
1

First of all, this sounds like an XY problem. What are you trying to do? I'm quite sure routing network traffic in this manner is unlikely to be a good solution to whatever your problem is.

Having said that,

Your routing tables still have a route for 192.168.2.0/24. This means 192.168.2.1 has two routes to reach 192.168.2.2, so the kernel routing subsystem considers both, sees that one is a direct route and the other isn't, and uses the direct route (because that's supposed to be cheaper).

If you don't want that, you have two options:

  • Remove the routing table entry for 192.168.2.0/24. This does mean you have to create routing table entries for all hosts on the network -- including the ones you want to reach directly.
  • Create two /24 network ranges, and give the host which is currently 192.168.2.2 an IP address in both ranges (that doesn't require a separate network interface). You can then have all other hosts route packets through that host.

Again though, I'm not sure this is the best way forward. Please explain what it is you're trying to achieve.

Wouter Verhelst
  • 9,171
  • 18
  • 43
  • If I remove the routing table for 192.168.2.0, then I can't reach 192.168.2.2 directly anymore from 192.168.2.1. I need to add 192.68.2.2 in the routing table. How do I do that? what is the command? – Andrei May 25 '16 at 11:41
  • Something like `ip route add 192.168.2.2 dev wlan0` should work. But again, I'm not convinced this is a good idea at all... – Wouter Verhelst May 25 '16 at 11:45
  • Would that be [email protected]: sudo ip route add 192.168.2.0 via 192.168.2.2 dev wlan0 and remove 192.168.2.0 from the the other nodes? – Andrei May 25 '16 at 11:45
  • The request is quite straightforward. Forcing a network topology where all the nodes are "chained". Basically, to communicate with the last node in the chain, you have to go through all the previous nodes. – Andrei May 25 '16 at 11:51
  • 1
    Yes, but why? The problem with this approach is that if you switch off any non-edge node in the network, nothing works anymore, and that you'll increase the amount of traffic sent by a large amount, thereby reducing your bandwidth and making an inherently unreliable network (wireless ad-hoc) even worse. If you're trying to improve reliability of your network, buy an access point. If you're trying to increase range, buy a repeater or a second access point. If you're trying to build your own access point, look at hostapd. What you're building here is likely to cause more problems than it solves. – Wouter Verhelst May 25 '16 at 12:04
  • Are you sure the direct route would have priority? Isn't the usual rule that the most specific route (one with the longest netmask) has priority? Of course, this may depend on the OS. – ilkkachu May 25 '16 at 20:37
  • All else being equal, you're right that a more specific route gets priority. However, all else is *not* equal here, since a route through a router is more 'expensive' than a direct, point-to-point, route. – Wouter Verhelst May 25 '16 at 21:51