From 53b74bc6148b8001b4dcec700d2fc65de41679aa Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 11 Dec 2019 09:22:47 +0100 Subject: [PATCH] n-dhcp4/socket: use SO_REUSEADDR on UDP socket Otherwise, other applications cannot bind to port 0.0.0.0:68 at the same time. This is for example what dhclient wants to do. So even when running dhclient on another, unrelated interface, it would fail to bind the UDP socket and quit. Note that also systemd-networkd's DHCPv4 client sets this socket option. Presumably for the same reasons. Signed-off-by: Thomas Haller https://github.com/nettools/n-dhcp4/pull/12 --- shared/n-dhcp4/src/n-dhcp4-socket.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/shared/n-dhcp4/src/n-dhcp4-socket.c b/shared/n-dhcp4/src/n-dhcp4-socket.c index b9ac176fd7..c7e897726e 100644 --- a/shared/n-dhcp4/src/n-dhcp4-socket.c +++ b/shared/n-dhcp4/src/n-dhcp4-socket.c @@ -195,6 +195,10 @@ int n_dhcp4_c_socket_udp_new(int *sockfdp, if (sockfd < 0) return -errno; + r = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); + if (r < 0) + return -errno; + r = setsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)); if (r < 0) return -errno;