Skip to content

Commit 9f8dba3

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 18557f3 commit 9f8dba3

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
@@ -290,20 +290,6 @@ int do_dhcp(const char *iface)
290290
goto cleanup;
291291
}
292292

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

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

472-
/* Drop temporary address and route, configure what we got instead */
473-
if (mod_route4(nl_sock, iface_index, RTM_DELROUTE, temp_gw) != 0) {
474-
printf("couldn't remove temporary route\n");
475-
goto cleanup;
476-
}
477-
if (mod_addr4(nl_sock, iface_index, RTM_DELADDR, temp_addr, 16) != 0) {
478-
printf("couldn't remove temporary address\n");
479-
goto cleanup;
480-
}
481-
482464
if (mod_addr4(nl_sock, iface_index, RTM_NEWADDR, addr, prefix_len) !=
483465
0) {
484466
printf("couldn't add the address provided by the DHCP server\n");
@@ -489,16 +471,7 @@ int do_dhcp(const char *iface)
489471
"couldn't add the default route provided by the DHCP server\n");
490472
goto cleanup;
491473
}
492-
493474
set_mtu(nl_sock, iface_index, mtu);
494-
} else {
495-
/* Clean up: we're clearly too cool for IPv4 */
496-
if (mod_route4(nl_sock, iface_index, RTM_DELROUTE, temp_gw) != 0) {
497-
printf("couldn't remove temporary route\n");
498-
}
499-
if (mod_addr4(nl_sock, iface_index, RTM_DELADDR, temp_addr, 16) != 0) {
500-
printf("couldn't remove temporary address\n");
501-
}
502475
}
503476

504477
ret = 0;

0 commit comments

Comments
 (0)