2

I'm completely lost. I'm a real beginnqer about Linux networking.

What I’m trying to accomplish :

As bobstro said, I want to allow a wifi-connected UPnP camera to be moved around between wifi networks. The RPI is connected to these networks, the camera is connected to the RPi. But the RPi is not an access point. It’s connected to a server via TCP or websocket and receives orders to take pictures via the camera. It then sends back the picture via websocket or TCP.

My situation :

I have three interfaces on my Raspberry Pi: eth0, wlan0, and wlan1 (2 Wi-Fi USB dongles).

  • wlan1 will be used to connect to a Wi-Fi camera via SSDP.

  • wlan0 will be used to have a TCP connection to a server (ip/domain name known). I know nothing about the Wi-Fi network wlan0 is connected to via DHCP. It could change from time to time. It doesn't know the gateway specially.

  • eth0 will be used for debug SSH access, via my MacBook Air connection sharing service by example.


        Server <---> Internet <--wlan0--> RPI <--wlan1--> Camera
                                           |
                                          etho
                                           |
                                        Macbook

Currently, all these interfaces are up. Here's the ifconfig output (wlan1 and eth0 are on the same network, but that will not be the case in the future):

eth0      Link encap:Ethernet  HWaddr b8:27:eb:8c:e2:10
          inet addr:192.168.0.19  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1174 errors:0 dropped:0 overruns:0 frame:0
          TX packets:141 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:93616 (91.4 KiB)  TX bytes:12342 (12.0 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  HWaddr c0:4a:00:2a:ab:6c
          inet addr:192.168.43.44  Bcast:192.168.43.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1142 (1.1 KiB)  TX bytes:1145 (1.1 KiB)

wlan1     Link encap:Ethernet  HWaddr 74:da:38:0c:c6:f0
          inet addr:192.168.2.2  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1480 errors:0 dropped:0 overruns:0 frame:0
          TX packets:155 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:222449 (217.2 KiB)  TX bytes:29711 (29.0 KiB)

And here's the output of sudo route -n:

0.0.0.0         192.168.0.254   0.0.0.0         UG    0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan1
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.43.0    0.0.0.0         255.255.255.0   U     0      0        0 wlan0

And here /etc/network/interfaces:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
        post-up route del default dev wlan0

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
        wpa-ssid "wifi1"
        wpa-psk "psw1"

auto wlan1
allow-hotplug wlan1
iface wlan1 inet dhcp
        wpa-ssid "wifi2"
        wpa-psk "pwd2"

If I understand correctly what I've read in the past 24 hours, I need to do routing stuff. But I don't understand what and how.

Do I have to change the default route? If so, how can I manage to do that since I don't know the gateway for wlan0 or wlan1 before connecting to the network? Is that an impossible problem to solve?

This post might be a duplicate; but since yesterday I have found nothing helpful, so at least I need some help to find an original post. :)

Edit: As goldilocks mentioned, my question is broad. I'm really sorry. I don't know anything about networking. I don't know how to split this into more specific questions.

So, maybe a first failed attempt:

I've tried sudo ip route del default to remove the default eth0 gateway. Then sudo ip route add default dev wlan0, the command works but results in this routing table:

0.0.0.0         0.0.0.0         0.0.0.0         U     0      0        0 wlan0

whereas I want the Raspberry Pi to find the wlan0 wifi router IP (I don't know it and it could change) and use it as gateway. It would be a good first step.

Edit 2: wlan1 is now on network 192.168.2.x.

4
  • You haven't actually said what does and doesn't work with your existing configuration. Someone might come along, look and this, and write the whole thing for you, but if they don't, you should break it down and ask more specific questions (here, not at Rpi.SE) about the things that don't work.
    – goldilocks
    May 12, 2015 at 19:09
  • Hi goldilocks. I understand that my question is to large. I'm really sorry. I don't know anything about networking. I don't know how to split this into more specific questions. I've edit my post to include the "first thing that doesn't work". Hope you understand my lake of knowledge. May 12, 2015 at 19:28
  • That's a strange setup - You've got two interfaces on the 192.168.0.0 network: eth0 and wlan1. I would suggest you change one. Imagine a IP packet coming from wlan0 to a 192.168.0.x IP address. Which way would it be routed? Via eth0 or via wlan1? The routing table tells us it could be either. Change one to a different subnet, such as 192.168.1.0. May 12, 2015 at 19:52
  • Yes, it was for testing purpose. I've now set wlan1 to be connected to the 192.169.2.X network. May 12, 2015 at 20:00

3 Answers 3

1

I can give you a sort of partial answer that has to do with how I would approach this, with the caveat that I don't use a networking configuration service at boot; I just write my own scripts to get whatever I want done.

That said, to test this you can just could just comment out everything in /etc/network/interfaces except for lo and wlan0, using the latter as your default route with DHCP. This should mean the system boots up that way. The configuration for this I'll leave for you to figure out, but there is one issue: You have to know that that subnet mask doesn't collide with the others. All of these have to be different. Presumably there are two that are under your control or can be known in advance.

The first one is the SSDP camera subnet, and a problem here is getting dhclient to configure a lease for that when it's already maintaining one on wlan0. You'd think this would be easy, but after looking around a bit, it seems it is not. You could try this, but in my opinion there's two perhaps less complicated solutions:

  • Use a static IP. This is certainly the easiest and most foolproof way.
  • Use dhcpcd as well as/instead of dhclient. I don't know how to do this, but it does look to be possible.

Using a static IP is easier because it also means you can hard code it. Presuming this subnet is 192.168.0.0/24 and the ip is 192.168.0.101:

ip link set wlan1 up
wpa_supplicant -B -c /path/to/essid/config -i wlan1
ip addr add 192.168.0.101 dev wlan1
ip route add 192.168.0.0/24 via 192.168.0.101

Then it's pretty much the same with the eth connection to the Mac, with which it must be easy to use a static IP:

ip link set eth0 up
ip addr add 192.168.1.10
ip route add 192.168.1.0/24 via 192.168.1.10
1
  • Thanks a lot @goldilocks ! I will test your solution and will use static ip for eth0 and wlan1. So if I understand well, wlan0 will be the default route for every script running on the Pi because the two other will be set after boot ? May 13, 2015 at 6:03
1

I'm understanding that you want to use the RPi to allow a wifi-connected UPnP camera to be moved around between networks and use the RPi to connect to a wireless network at each site.

Is that correct? Is there some reason you can't access the camera directly from the network without the RPi routing?

Do you have routing enabled on the RPi? To enable routing interactively, use:

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

To make it permanent, edit /etc/sysctl.conf and uncomment out the line that says net.ipv4.ip_forward = 1.

sudo nano /etc/sysctl.conf

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

Looking at your configuration, the RPi will get the address for WLAN0 via DHCP which should provide it the default gateway.

You don't describe how the camera is connected. Do you want to use the RPi as an access point? If so, you need to set up hostapd and configure the RPi as a DHCP server so that the camera connects to the RPi via wireless and is assigned an IP address and default gateway pointing back to the RPi.

You probably do NOT want the eth0 interface to be used as the default gateway on the RPi if it's for diagnostic use only.

You need to clarify exactly what it is you're trying to do rather than telling us how you've tried to do it. What you've done might be the wrong approach.

If it helps, here's how I'm using a similarly configured RPi:

One wifi interface is used solely for configuring the RPi. I have it configured as an access point using hostapd and dnsmasq. When I first place the RPi at a site, I connect to this AP from my MacBook to configure it. My MacBook is assigned an IP address and default gateway pointing to the RPi via dnsmasq. Once configured, this interface is not normally used, but can be accessed as needed.

One wifi interface is used for connecting to the client network at each site for remote access. I configure the interface using wicd-curses and verify that I can ping it from their network. I can then VPN to the site and access the RPi. WhenI connect to the site wifi network, my RPi is assigned a default gateway pointing to the site Internet router via DHCP.

The wired interface is used to connect to the test network. It is configured with a fixed IP address via /etc/network/interfaces.

I do NOT use routing since I don't want traffic flowing between networks. All traffic terminates at the RPi. Using a few scripts, however, I can turn on routing as needed. I have scripts to use the RPi as a bridge or NAT router, depending on my needs.

If you can more completely describe what you are trying to accomplish, an answer should be straightforward.

5
  • Hi, thanks a lot for your response. I've edited my message to include a "what I'm trying to accomplish" section. You're right, I want a UPnP camera to be moved around wifi networks. But the RPi is not an access point. It’s connected to a server via TCP or websocket and receives orders to take pictures via the camera. It then sends back the picture via websocket or TCP. In your case, you activated routing this way in order to make the access point worked ? Thanks for your example, I have considered using a VPN too, but have preferred the TCP way because of performance. May 13, 2015 at 5:57
  • From what you've described, I don't think you need necessarily need routing. Can you just connect the camera to the same wifi network as the RPi? If both the camera and RPi get their IP configuration via DHCP, they'll both be on the same subnet, and both will have a default gateway pointing to the wifi network's router. You should be able to talk from the RPi to the camera over this (wlan0) network. You should also, if you're careful, be able to connect your MacBook to the RPi via the wired (eth0) interface, although it could also talk over the same wifi network.
    – bobstro
    May 13, 2015 at 13:24
  • I can't connect my camera directly to the wifi network. The camera is by itself an access point. My RPi is working like a bridge between the camera and internet by being connected to the two networks. May 13, 2015 at 16:10
  • Ah, OK. So one important question is whether the camera provides a DHCP server, or do you manually configure your RPi with a fixed IP address to communicate with it? You don't want the default gateway on your RPi to be over-written. The next question is what default gateway the camera is configured with. If you want your RPi to route the traffic, the camera's default gw should point to our RPi's IP address. If you want to configure the RPi as a bridge (not what you've described, but just being thorough), the camera's default gateway needs to point to the router on the site's wifi network.
    – bobstro
    May 13, 2015 at 18:36
  • Yes, actually I don't have that camera yet but I've tested with dhcp on wlan1 and everything is working fine (see my own response below). The dhcp client doesn't seems to override the default gateway neither with wlan0 nor with wlan1. May 13, 2015 at 19:57
1

So, it seems I've managed to find a (temporary ?) solution where wlan0 is the default route. I don't need to know the wifi gateway.

The key is to follow @goldilocks idea to script routing processes. But this solution allow to keep eth0 and wlan1 to run on dhcp.

So I've added the file /etc/dhcp/dhclient-enter-hooks.d/post-wlan0 (and chmod +x to it) which contains :

# post-wlan0 script
# Goal : activate default route via wlan0 after wlan0 is up

#echo  "post-wlan0 start" >> /tmp/testwlan0.debug
if [ "$interface" = "wlan0" ]; then

        #echo  "wlan0 detected" >> /tmp/testwlan0.debug
        if [ -n "$new_routers" ] ; then

                #echo  "setting route" >> /tmp/testwlan0.debug

                sudo ip route del default
                sudo ip route add default via $new_routers dev wlan0

        fi
fi

Actually, this script is an hook which is fired when the dhcp client is working. See /etc/dhcp/dhclient-enter-hooks.d/debug for more infos.

So when wlan0 is up, it removes the old default route and add a new one with the wlan0 router ip ($new_routers).

Output of sudo route -n :

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.43.1    0.0.0.0         UG    0      0        0 wlan0
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan1
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.43.0    0.0.0.0         255.255.255.0   U     0      0        0 wlan0

I can ping a device in eth0 or wlan1 networks. And I'm sshing via eth0.

@goldilocks and @bobstro : Thanks a lot for your help ! Is this solution seems good to you ?


My config :

/etc/network/interfaces :

auto lo
iface lo inet loopback

# Connection to the world
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
        wpa-ssid "wifi1"
        wpa-psk "pwd1"

# Debugging connection
auto eth0
iface eth0 inet dhcp

# Connection to the future camera
auto wlan1
allow-hotplug wlan1
iface wlan1 inet dhcp
        wpa-ssid "wifi2"
        wpa-psk "pwd2"

Output of ifconfig :

eth0      Link encap:Ethernet  HWaddr b8:27:eb:8c:e2:10  
          inet addr:192.168.2.2  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1371 errors:0 dropped:0 overruns:0 frame:0
          TX packets:494 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:145112 (141.7 KiB)  TX bytes:87219 (85.1 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  HWaddr c0:4a:00:2a:ab:6c  
          inet addr:192.168.43.44  Bcast:192.168.43.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:230 errors:0 dropped:0 overruns:0 frame:0
          TX packets:323 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:22412 (21.8 KiB)  TX bytes:32270 (31.5 KiB)

wlan1     Link encap:Ethernet  HWaddr 74:da:38:0c:c6:f0  
          inet addr:192.168.0.14  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1942 errors:0 dropped:1 overruns:0 frame:0
          TX packets:30 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:305388 (298.2 KiB)  TX bytes:3302 (3.2 KiB)

Edit : solution tested with an UPnP device somewhere in wlan1. I had to add the route sudo ip route add 239.255.255.250 dev wlan1 for UPnP discovering to go through the good interface.

I've use this NodeJS code for testing.

You must log in to answer this question.

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