Skip to content

Commit 117a3c3

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 7e394e2 commit 117a3c3

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;
@@ -479,16 +471,6 @@ int do_dhcp(const char *iface)
479471
/* Calculate prefix length from netmask */
480472
unsigned char prefix_len = count_leading_ones(ntohl(netmask.s_addr));
481473

482-
/* Drop temporary address and route, configure what we got instead */
483-
if (mod_route4(nl_sock, iface_index, RTM_DELROUTE, temp_gw) != 0) {
484-
printf("couldn't remove temporary route\n");
485-
goto cleanup;
486-
}
487-
if (mod_addr4(nl_sock, iface_index, RTM_DELADDR, temp_addr, 16) != 0) {
488-
printf("couldn't remove temporary address\n");
489-
goto cleanup;
490-
}
491-
492474
if (mod_addr4(nl_sock, iface_index, RTM_NEWADDR, addr, prefix_len) !=
493475
0) {
494476
printf("couldn't add the address provided by the DHCP server\n");
@@ -499,16 +481,7 @@ int do_dhcp(const char *iface)
499481
"couldn't add the default route provided by the DHCP server\n");
500482
goto cleanup;
501483
}
502-
503484
set_mtu(nl_sock, iface_index, mtu);
504-
} else {
505-
/* Clean up: we're clearly too cool for IPv4 */
506-
if (mod_route4(nl_sock, iface_index, RTM_DELROUTE, temp_gw) != 0) {
507-
printf("couldn't remove temporary route\n");
508-
}
509-
if (mod_addr4(nl_sock, iface_index, RTM_DELADDR, temp_addr, 16) != 0) {
510-
printf("couldn't remove temporary address\n");
511-
}
512485
}
513486

514487
ret = 0;

0 commit comments

Comments
 (0)