Roy Marples [Fri, 9 Oct 2020 19:21:52 +0000 (20:21 +0100)]
compat: Use libbsd's setproctitle(3)
Linux PRCTL variant, although more light weight, doesn't work on
some kernels.
This weighs in around 1k more, but always works.
It does play around with environ and args but unlike other
similar variants doesn't appear to stamp on what you actually
use in the program.
Roy Marples [Wed, 7 Oct 2020 13:11:47 +0000 (14:11 +0100)]
dhcpcd: Simplify the link handling even more
Move the IS_LINK_UP macro to if_is_link_up function to reduce
binary size.
Rather than DHCPCD_LINK option controlling the carrier state,
use it in if_is_link_up to determine the outcome.
Roy Marples [Mon, 28 Sep 2020 16:09:38 +0000 (17:09 +0100)]
BSD: struct if_data->ifi_link_state is the single source of truth
Vastly improve and simplify link detection on BSD.
dhcpcd either examines the whole system via getifaddrs(3) or
reacts to events via route(4).
In both cases we have struct if_data which has ifi_link_state.
Armed with this knowledge, we no longer need SIOCGIFDATA or
SIOCGIFMEDIA.
To solve the issue of newly attached interfaces having
LINK_STATE_UNKNOWN or some interfaces not even changing it,
we only change the local knowledge of interface flags when
reports them by getifaddrs(3) or route(4) when we change them.
For example, if we set IFF_UP and it succeeds we don't set this
internally until reported by the kernel as above.
This keeps flags and link state in sync with each other.
The hope is that the kernel can set the real link state before
it reports IFF_UP.
As such, we no longer require the poll option or need to enter a
tight loop for old interfaces.
Roy Marples [Thu, 24 Sep 2020 02:31:43 +0000 (03:31 +0100)]
BSD: NetBSD is the odd man out with SIOCGIFDATA
So setup the #defines like so.
On OpenBSD, pledge blocks it and there is no escape.
Luckily we already allow indirect ioctls via privsep so it works fine.
Roy Marples [Sat, 19 Sep 2020 13:40:50 +0000 (14:40 +0100)]
Linux: detect network namespace and deny udev in one
udev says whether an interface name is stable or not.
In a network namespace, udev claims the interface does not exist.
This makes sense because udev only operates in the root namespace.
Roy Marples [Sat, 12 Sep 2020 19:14:47 +0000 (20:14 +0100)]
dhcpcd: Only manipulate stdin, stdout and stderr when valid
UNIX application expect these to exist even if pointed at /dev/null.
We cannot change which fd they use, it's always 0, 1 and 2.
But if these fd's are not open when dhcpcd is called, they could
be assigned to dhcpcd internals.
In this instance we should not use the streams in anyway or form.
Roy Marples [Sun, 6 Sep 2020 01:41:08 +0000 (02:41 +0100)]
dhcpcd: Redirect stdout/stderr to the launcher stderr descriptor
This actually make life really simple!
We no longer need to redirect stdout/stderr to /dev/null for privsep
and any script output is now captured again - and it all goes to stderr
as it should even if a script wants it to go to stdout.
On the happy path, only the master process will actually log anything
to stderr so we turn that off after we "fork".
On the unhappy path, logging to stderr/stdout *may* fail because
the launcher process *may* have exited.
We *could* have the master process as an intermediary but that's
just excess code to avoid errors which *should* not happen.
Regardless, any errror should still hit syslog.
Roy Marples [Sat, 5 Sep 2020 15:16:22 +0000 (16:16 +0100)]
dhcpcd: Setup a socketpair in the launcher to write to stderr
Rather than duping stderr down to the processes.
This allows us to reopen stdout and stderr onto /dev/null right
away and means only the launcher process writes to stderr.
The downside is that any stdout from the script is now lost.
If that's needed, we could setup a stdout socketpair as well.
Roy Marples [Tue, 1 Sep 2020 10:56:53 +0000 (11:56 +0100)]
DHCP: Don't set address lifetimes when extending leases
Otherwise the kernel WILL remove them.
dhcpcd already manages address removal when needed because
some OS's do not support address lifetimes even for IPv6.
Roy Marples [Sun, 16 Aug 2020 17:52:17 +0000 (18:52 +0100)]
privsep: Set a zero length receive buffer for write only sockets
We cannot use shutdown(2) because they are not connected.
Constantly draining would be a waste of CPU time, so just let
the buffer overflow. To ease the kernel as much as we can, set
a zero length buffer.
The kernel may still allocate a small buffer, but this is kernel
dependant and we're just trying to be helpful.