Skip to content

Commit 5c28233

Browse files
mtjhrcslp
authored andcommitted
init: use SO_BINDTODEVICE instead of temp address for DHCP
Replace the temporary link-local address (169.254.1.1) workaround with SO_BINDTODEVICE. The temp address caused the kernel to use 169.254.1.1 as the source IP in DHCP packets; gvproxy then tried to reply to that address and failed with "no route to host". With this change the source IP should be 0.0.0.0, which is what RFC 2131 requires for DHCPDISCOVER. Signed-off-by: Matej Hrica <mhrica@redhat.com>
1 parent 0d77db4 commit 5c28233

1 file changed

Lines changed: 6 additions & 33 deletions

File tree

init/dhcp.c

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,6 @@ int do_dhcp(const char *iface)
291291
goto cleanup;
292292
}
293293

294-
/* Temporary link-local address and route avoid the need for raw sockets */
295-
struct in_addr temp_addr;
296-
inet_pton(AF_INET, "169.254.1.1", &temp_addr);
297-
struct in_addr temp_gw = {.s_addr = INADDR_ANY};
298-
299-
if (mod_route4(nl_sock, iface_index, RTM_NEWROUTE, temp_gw) != 0) {
300-
printf("couldn't add temporary route\n");
301-
goto cleanup;
302-
}
303-
if (mod_addr4(nl_sock, iface_index, RTM_NEWADDR, temp_addr, 16) != 0) {
304-
printf("couldn't add temporary address\n");
305-
goto cleanup;
306-
}
307-
308294
/* Send request (DHCPDISCOVER) */
309295
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
310296
if (sock < 0) {
@@ -319,6 +305,12 @@ int do_dhcp(const char *iface)
319305
goto cleanup;
320306
}
321307

308+
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, iface,
309+
strlen(iface) + 1) < 0) {
310+
perror("setsockopt SO_BINDTODEVICE failed");
311+
goto cleanup;
312+
}
313+
322314
/* Bind to port 68 (DHCP client) */
323315
memset(&bind_addr, 0, sizeof(bind_addr));
324316
bind_addr.sin_family = AF_INET;
@@ -476,16 +468,6 @@ int do_dhcp(const char *iface)
476468
/* Calculate prefix length from netmask */
477469
unsigned char prefix_len = count_leading_ones(ntohl(netmask.s_addr));
478470

479-
/* Drop temporary address and route, configure what we got instead */
480-
if (mod_route4(nl_sock, iface_index, RTM_DELROUTE, temp_gw) != 0) {
481-
printf("couldn't remove temporary route\n");
482-
goto cleanup;
483-
}
484-
if (mod_addr4(nl_sock, iface_index, RTM_DELADDR, temp_addr, 16) != 0) {
485-
printf("couldn't remove temporary address\n");
486-
goto cleanup;
487-
}
488-
489471
if (mod_addr4(nl_sock, iface_index, RTM_NEWADDR, addr, prefix_len) !=
490472
0) {
491473
printf("couldn't add the address provided by the DHCP server\n");
@@ -496,16 +478,7 @@ int do_dhcp(const char *iface)
496478
"couldn't add the default route provided by the DHCP server\n");
497479
goto cleanup;
498480
}
499-
500481
set_mtu(nl_sock, iface_index, mtu);
501-
} else {
502-
/* Clean up: we're clearly too cool for IPv4 */
503-
if (mod_route4(nl_sock, iface_index, RTM_DELROUTE, temp_gw) != 0) {
504-
printf("couldn't remove temporary route\n");
505-
}
506-
if (mod_addr4(nl_sock, iface_index, RTM_DELADDR, temp_addr, 16) != 0) {
507-
printf("couldn't remove temporary address\n");
508-
}
509482
}
510483

511484
ret = 0;

0 commit comments

Comments
 (0)