Udhcpc.user script documentation and how to hotplug for DHCP events

发布时间 2023-04-16 12:18:40作者: lsgxeva

Udhcpc.user script documentation and how to hotplug for DHCP events

https://forum.openwrt.org/t/udhcpc-user-script-documentation-and-how-to-hotplug-for-dhcp-events/47952/10

 

Hi, guys! I've been looking for ways to execute programmes/scripts on DHCP events. To be more specific, I want to update my dynamic DNS (FreeDNS 1) when, and only when there's a potential change on the WAN interface(s) IP address(es) (yeah, I had a cron job doing it every ten minutes, but I always found that rather ugly, IMNSHO).
It took me quite a bit searching in order to find this trac entry 20 mentioning an elusive /etc/udhcpc.user script (for which there isn't even a stub in the base-files package). So, I created this one…

#!/bin/sh
env | logger
exit 0

… sent USR1 to the udhcpc PID and got quite a number of juicy variables in the log, namely $ip (the IP address), $INTERFACE (the interface name) and $interface (the physical device). With this information, I changed my script to…

#!/bin/sh
if [ "wan" = $INTERFACE ]; then
	/usr/bin/wget -q -O - 'https://sync.afraid.org/u/[token redacted]/?myip='${ip}''
fi
exit 0

… which works like a charm!
So, my question is simple: am I doing this right? Can I rely on these variables to always exist and contain the information I need, or are there any changes planned? Thanks in advance! 

 

tl;dr yes, see this doc for what variables are created https://udhcp.busybox.net/README.udhcpc 77

full info post:

Next time you are doing some detective work I suggest you do a github (or openwrt source text content) search for udhcpc.user to look around on what interacts with it in the source.
You find that such file is called at the end of /usr/share/udhcpc/default.script, and also the /lib/netifd/dhcp.script as added in that ticket you saw.

https://github.com/openwrt/openwrt/blob/master/package/network/config/netifd/files/usr/share/udhcpc/default.script 48

https://github.com/openwrt/openwrt/blob/master/package/network/config/netifd/files/lib/netifd/dhcp.script 23

If you look at the top of both scripts, the very first line is
[ -z "$1" ] && echo "Error: should be run by udhcpc" && exit 1

So yeah they are run by "udhcpc", and that is a busybox tool (embedded device light version of core commandline tools), the dhcp client used by OpenWrt.

The documentation is in its old project page https://udhcp.busybox.net/ 4 that has now been moved to busybox project infrastructure since the application was merged into busybox, and you can find the full client documentation about what variables it generates for use in scripts if you scroll down and click on Client README https://udhcp.busybox.net/README.udhcpc 77

It says:
When an event occurs, udhcpc calls the action script. The script by default is /usr/share/udhcpc/default.script
(for "events" it means something related to DHCP changes)

And then it says

 
The paramaters for enviromental variables are as follows:

$HOME		- The set $HOME env or "/"
$PATH		- the set $PATH env or "/bin:/usr/bin:/sbin:/usr/sbin"
$1		- What action the script should perform
interface	- The interface this was obtained on
ip		- The obtained IP
siaddr		- The bootp next server option
sname		- The bootp server name option
boot_file	- The bootp boot file option
subnet		- The assigend subnet mask
timezone	- Offset in seconds from UTC
router		- A list of routers
timesvr		- A list of time servers
namesvr		- A list of IEN 116 name servers
dns		- A list of DNS server
logsvr		- A list of MIT-LCS UDP log servers
cookiesvr	- A list of RFC 865 cookie servers
lprsvr		- A list of LPR servers
hostname	- The assigned hostname
bootsize	- The length in 512 octect blocks of the bootfile
domain		- The domain name of the network
swapsvr		- The IP address of the client's swap server
rootpath	- The path name of the client's root disk
ipttl		- The TTL to use for this network
mtu		- The MTU to use for this network
broadcast	- The broadcast address for this network
ntpsrv		- A list of NTP servers
wins		- A list of WINS servers
lease		- The lease time, in seconds
dhcptype	- DHCP message type (safely ignored)
serverid	- The IP of the server
message		- Reason for a DHCPNAK
tftp		- The TFTP server name
bootfile	- The bootfile name

I think this interface is not going to change anytime soon, that's a core tool, the dhcp client application used in OpenWrt.

I also think that it should be fairly trivial to add a line that calls network hotplug scripts when DHCP events happen by adding a line in the two linked scripts above, so packages can rely on DHCP events too without having to make a custom user config script like you did.

 

Hmm, more rummaging in OpenWrt sources and it seems there is a dhcp hotplug for scripts found in /etc/hotplug.d/dhcp, but only for events triggered by dnsmasq (the DHCP server used by OpenWrt).
They are not triggered for DHCP client events like what you need.

It does show how you can add hotplugging for DHCP client events, just add the same in the scripts I linked above, and it should then call stuff in /etc/hotplug.d/dhcp for DHCP client events too.

https://github.com/openwrt/openwrt/blob/master/package/network/services/dnsmasq/files/dhcp-script.sh 67

Would be cool if after testing this, someone could open a PR to merge this in OpenWrt. I'm currently away from home and I can't really test much.

 

It seems odhcpd (DHCP server for IPv6) and odhcp6c (DHCP client for IPv6) aren't triggering hotplug scripts either.

Both have a script hook they call every time there is a DHCP update so it's the same thing as above to add the hotplug functionality.
https://github.com/openwrt/openwrt/blob/master/package/network/ipv6/odhcp6c/files/dhcpv6.script 22 for odhcp6c and
https://github.com/openwrt/openwrt/blob/master/package/network/services/odhcpd/files/odhcpd-update 23 for odhcpd

 

an update: I asked on mailing list and Hans Dedecker (a core OpenWrt developer and more or less maintainer of these daemons) https://www.mail-archive.com/openwrt-devel@lists.openwrt.org/msg49345.html 14 said that the odhcpd-update script is not the same thing, and that at the moment odhcpd does not call any script on DHCP lease changes, so it cannot do any hotplug.

 

======== End