You are not logged in.

#1 2015-10-23 04:06:59

phaseburn
Member
Registered: 2013-11-21
Posts: 9

[Solved] systemd-networkd with DHCP question...

I'm looking for a way to configure systemd-networkd to use DHCP in such a way that it will run a script whenever the link comes up, or, my DHCP-assigned IP has changed. The "whenever the link comes up" is easy enough in that I can write a unit file that depends on network to complete, but, I haven't found any way to tell systemd-networkd's dhcp server to run a script on IP change, either.

If I was running this outside of systemd, I'd just use the dhcpcd-run-hooks config directive. But, my scouring the man pages for systemd's dhcp implementation has left me a bit baffled - I'm not even sure this option exists in systemd, or if it does, what it's called.

I would think that there would have to be a way to do this - polling the IP every x seconds to detect a change is horribly wasteful, and, not using systemd is not ideal for my needs, either...

Any suggestions or solutions would be greatly appreciated.

Last edited by phaseburn (2015-10-30 19:32:28)

Offline

#2 2015-10-23 13:49:10

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: [Solved] systemd-networkd with DHCP question...

Unless someone knows a better way, try this:

Info about the DHCP leases are (currently) stored in files in /run/systemd/netif/leases/
So you could use a systemd.path unit to monitor for changes.
I'm not sure if there is an API to the files, and the files do say "Do not parse", but the format is simple and they haven't changed in a long time. As long as you're aware it may change, it should be straightforward to do what you want.

Offline

#3 2015-10-29 01:47:40

phaseburn
Member
Registered: 2013-11-21
Posts: 9

Re: [Solved] systemd-networkd with DHCP question...

I'll mark this as solved in 48 hours if nobody has a better suggestion than this...

Offline

#4 2015-10-30 03:36:01

rsmarples
Member
Registered: 2009-05-12
Posts: 287

Re: [Solved] systemd-networkd with DHCP question...

phaseburn wrote:

If I was running this outside of systemd, I'd just use the dhcpcd-run-hooks config directive. But, my scouring the man pages for systemd's dhcp implementation has left me a bit baffled

...

Any suggestions or solutions would be greatly appreciated.

DISCLAIMER: I maintain dhcpcd

If systemd networking leaves you baffled, and the dhcpcd and the dhcpcd hooks documentation is good enough, WHY are you using systemd-networkd?
Just curious.

AND just incase you missed the memo, systemd does not want you running scripts, ever, because that moves you out of the systemd ecosphere.
scripts are not dbus-y enough.

Offline

#5 2015-10-30 08:48:33

phaseburn
Member
Registered: 2013-11-21
Posts: 9

Re: [Solved] systemd-networkd with DHCP question...

A very good question! That's because I'm using dhcpcd already for IPv6 and a prefix delegation - something I have not been able to get it to do while doing IPv4 as well. The option to use systemd did exist, so I figured I minaswell try to make use of it.

My system is fairly convoluted and complicated compared to most setups. I have eth0 connected to my cable modem, eth1 connected to my LAN, and tun0 is a tunnel using OpenVPN.

Despite being the norm, the VPN tunnel is not the default route, but, it does route traffic. I've set up some iptables and ip rule scripts to properly route traffic out the VPN when marked via iptables, while the default route remains the DHCP-assigned gateway on eth0 (via systemd). Then, I am running dhcpcd to obtain an IPv6 pd from my ISP, and set up radvd for my LAN computers to get an IPv6 address over it. I am not using a DHCPv6 server. Lastly, I have some iptables rules that do masquerade out both eth0 and tun0, based on conditions (source MAC, owner/uid if locally generated, etc), and this provides internet access to the IPv4 world for my local network.

I won't even explain the other half of it involving the reverse proxies externally going back in. Needless to say, it's not a typical setup.

So, since I can't run dhcpcd for eth0 for IPv4 (when I do, it ignores my prefix delegation, and breaks my IPv6 setup), I opted to go with systemd rather than dhclient. Yes, I know there's other tools like dibbler and dhcpv6-wide, but neither of them suited my fancy. Dibbler looks to be far more complicated while accomplishing a large amount of nothing, and there's poor documentation for dhclient and dhcpv6-wide that I could find. Anyway, right now I have the whole thing set up in a way that works, and, the only missing piece is to execute a script (easily) whenever my DHCP-assigned IP changes, so I can ping my dyndns provider to tell 'em to update my hostname. I could crontab it every few minutes, if I wanted to... write a script that checks if the last recorded IP is different than the current IP, and if so, ping the host. But the proper way of implementing something like this, at least, in my opinion, is to be notified of the change in address by something, rather than having to poll it myself every X units of time. The latency should be minimized this way - I'd know about the change instantly, rather than waiting for a poll (which can take up a non-zero amount of system resources of its own just constantly querying), and the latency would be only the time it took for the DNS TTL to expire, which is a setting my host controls. It eliminates the time between polls as an addition to the zone TTL. And, it just feels like a better solution. Alas, it isn't supported. I've implemented the inotify solution, and, it does work. Not ideal, but, few things in life seem to be, hehe...

So there you have it, rsmarples, why I'm doing what I'm doing, and what I hoped to accomplish. And thank you very much for maintaining dhcpcd, you (as well as all the other Arch maintainers and developers) are doing a bang up job!

Offline

#6 2015-10-30 09:57:25

rsmarples
Member
Registered: 2009-05-12
Posts: 287

Re: [Solved] systemd-networkd with DHCP question...

phaseburn wrote:

A very good question! That's because I'm using dhcpcd already for IPv6 and a prefix delegation - something I have not been able to get it to do while doing IPv4 as well.

So, since I can't run dhcpcd for eth0 for IPv4 (when I do, it ignores my prefix delegation, and breaks my IPv6 setup)

Can you explain why?
I have PD and IPv4 working fine with dhcpcd. They are also two distinct protocols, so there should be no conflict.
If you post (or email me off forum - roy@marples.name) your dhcpcd.conf and output of `dhcpcd -dB` that should provide all the information I need to help.

So there you have it, rsmarples, why I'm doing what I'm doing, and what I hoped to accomplish. And thank you very much for maintaining dhcpcd, you (as well as all the other Arch maintainers and developers) are doing a bang up job!

Why, thanks for those kinds words smile

Offline

#7 2015-10-30 19:35:03

phaseburn
Member
Registered: 2013-11-21
Posts: 9

Re: [Solved] systemd-networkd with DHCP question...

rsmarples wrote:
phaseburn wrote:

A very good question! That's because I'm using dhcpcd already for IPv6 and a prefix delegation - something I have not been able to get it to do while doing IPv4 as well.

So, since I can't run dhcpcd for eth0 for IPv4 (when I do, it ignores my prefix delegation, and breaks my IPv6 setup)

Can you explain why?
I have PD and IPv4 working fine with dhcpcd. They are also two distinct protocols, so there should be no conflict.
If you post (or email me off forum - roy@marples.name) your dhcpcd.conf and output of `dhcpcd -dB` that should provide all the information I need to help.

I'll contact you off-forum later this weekend, and we'll see if we can't get it working using just dhcpcd. I won't be around to tinker with it until probably Sunday or Monday, though. But thanks so much for the offer to help!

Offline

Board footer

Powered by FluxBB