WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: WiFi: How to obtain a new DHCP-IP automatically after re-plugging USB-Wifi?  (Read 4930 times)

Offline vbs

  • Newbie
  • *
  • Posts: 21
I am using latest TC on RPi2 and I have the following problem:
After pulling my Wifi-USB-Adapter out and putting it back later then udhpc won't obtain a new DHCP-IP. So wlan0 is up but has no IP. When I manually run "udhcpc -i wlan0" to get a new IP then it works fine. But how can I trigger udhcpc automatically after wlan0 gets up? I searched quite a bit and fiddled around but cant find a solution. :(

So basically I am searching something that is similar to "/etc/network/if-up.d/" on other distros.

Many thanks!

Offline beerstein

  • Hero Member
  • *****
  • Posts: 530
hi: I am not an expert but I think you need a second DHCP server running in your network which assigns IP addresses in another IP range than your main router. Your main router fi may assign 192.168.0.1 - 192.168.0.49. Then you have your second DHCP server assign addresses between 192.168.0.50 and 192168..0.60.
Broadcasting also must not have the same IP, but I am not sure about this.
I do not know whether a static IP outside the main router/DHCP server address range might work?
t(w)o be(ers) or not t(w)o be(ers) that is the question

Offline vbs

  • Newbie
  • *
  • Posts: 21
Hm, probably I have phrased my problem poorly:
I dont need a different IP from a different IP range I just need to trigger udhcpc again to obtain an IP, because after plugging the Wifi-USB in it does not have an IP at all anymore. One solution probably would be to use a static IP for wlan0 so I dont need DHCP at all. But I dont want to switch to static IP. :(

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11049
By default there is nothing watching for network interfaces appearing. You need to install some daemon to do that.
The only barriers that can stop you are the ones you create yourself.

Offline jgrulich

  • Sr. Member
  • ****
  • Posts: 341
    • GRULICH DESIGN - R&D Lab.
There are two ways how to do so. I'm using udhcpc -i wlan0 which restores the IP as you've already mentioned.
If you're changing the dongle very often and even more if you're using several ones, it's better to use the simple script which monitors the iwconfig and when the dongle is identified, it automatically calls the udhcpc. This script is already called from rc.d during startup, but works only for the first time. You may either to modify this script to keep working after the first-shot, or copy this, modify and place to the /opt folder and call it during startup. But in that case you need to disable the first-shot one not to work in parallel.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Hi vbs
I think  udev  might be what you are looking for.
Create a file called:
Code: [Select]
/etc/udev/rules.d/15-wifi-usb.rulesPlace the following in that file:
Code: [Select]
SUBSYSTEM=="usb", ATTR{idProduct}=="1446", ATTR{idVendor}=="12d1", RUN+="udhcpc -i wlan0"Replace  1446  and  12d1  with the product and vendor IDs of your device. Save the file and run:
Code: [Select]
sudo udevadm control --reload-rules Unplug your device, wait a few seconds, plug it back in and see if you get a new IP. If it works, add the file to your backup.
Syntax for the rules was lifted from here:
http://forum.tinycorelinux.net/index.php/topic,11509.msg60770.html#msg60770
« Last Edit: April 18, 2015, 01:38:00 PM by Rich »

Offline jgrulich

  • Sr. Member
  • ****
  • Posts: 341
    • GRULICH DESIGN - R&D Lab.
Something wrong in the format. I got the error message that unknown key SYSFS{idProduct} and unknown key SYSFS{idVendor}.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Hi jgrulich
You are correct. It seems  SYSFS  has been replaced by  ATTR. Try:
Code: [Select]
SUBSYSTEM=="usb", ATTR{idProduct}=="1446", ATTR{idVendor}=="12d1", RUN+="udhcpc -i wlan0"

Offline jgrulich

  • Sr. Member
  • ****
  • Posts: 341
    • GRULICH DESIGN - R&D Lab.
I've tested and the correct format is this:
Code: (bash) [Select]
SUBSYSTEM=="usb", ATTR{idVendor}=="0bda", RUN+="/sbin/udhcpc -i wlan0"For identification is enough to use vendor ID, it's even better when you have more dongles with the same chipset, it will work for all of them.
But be careful. This gives error message at the startup, when restored via backup, because it tries to get the IP even the dongle is not inicialized and wpa_supplicant is not running yet.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Hi jgrulich
Thanks, I corrected the syntax in my original post.
Quote
But be careful. This gives error message at the startup, when restored via backup, because it tries to get the IP even the dongle is not inicialized and wpa_supplicant is not running yet.
I'm sure the rule could be made more robust. Alternatively, rather than having the rule run a command, you could have it run a
script that first tests if the dongle is ready and wpa_supplicant is running. If they are, the script runs the  udhcpc  command. If
they are not ready and running, the script just exits.

Offline jgrulich

  • Sr. Member
  • ****
  • Posts: 341
    • GRULICH DESIGN - R&D Lab.
Just tested and it works well.  Udev runs the script where the dongle and wpa is correctly stopped, wait some time and than run wpa and udhcpd. This works well at the startup and every time I remove and plug the dongle.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Hi jgrulich
Maybe you could paste a copy of your script in your next post so that  vbs  can use it as an example.

Offline vbs

  • Newbie
  • *
  • Posts: 21
Many thanks guys! This is working quite well now! :)

Only thing I added is a little startscript so it takes care of shutting down the previous udhcpc-process. Otherwise you will end up having an additional udhpc process every time you plug in the WIFI stick.

I grabbed an init-script template from here:
https://raw.githubusercontent.com/fhd/init-script-template/master/template

And did some changes and put it to '/etc/init.d/dhcp-wlan0.sh':
http://pastebin.com/aVjj06iW

This is my udev rule now:
Code: [Select]
SUBSYSTEM=="usb", ATTR{idProduct}=="5370", ATTR{idVendor}=="148f", RUN+="/etc/init.d/dhcp-wlan0.sh restart"
I am not very experienced when it comes to shell scripts so I am sure there is lots of room for optimization in the script but currently it works good enough for me.

Thanks again!