Store the interface index in the client_config before forking off the

subprocesses.  ndhc-ifch can then use the stored interface index when
setting the interface ip/subnet/broadcast via netlink instead of having
to use ioctl to re-fetch the interface index.
This commit is contained in:
Nicholas J. Kain 2014-03-12 15:07:37 -04:00
parent 80f82c2451
commit b7b353acdc
2 changed files with 12 additions and 41 deletions

View File

@ -112,29 +112,6 @@ static int add_rtattr(struct nlmsghdr *n, size_t max_length, int type,
return 0;
}
static int get_ifindex(const char *name)
{
struct ifreq ifr;
int sk, err;
if (!name)
return -1;
sk = socket(PF_INET, SOCK_DGRAM, 0);
if (sk < 0)
return -1;
memset(&ifr, 0, sizeof ifr);
strnkcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
err = ioctl(sk, SIOCGIFINDEX, &ifr);
close(sk);
if (err < 0)
return -1;
return ifr.ifr_ifindex;
}
// 32-bit position values are relatively prime to 37, so the residue mod37
// gives a unique mapping for each value. Gives correct result for v=0.
static int trailz(uint32_t v)
@ -163,7 +140,7 @@ void perform_ip_subnet_bcast(const char *str_ipaddr,
struct sockaddr_nl nl_addr;
struct nlmsghdr *header;
struct ifaddrmsg *ifaddrmsg;
int nls, ifidx, r;
int nls, r;
uint8_t prefixlen;
if (!str_ipaddr) {
@ -177,13 +154,6 @@ void perform_ip_subnet_bcast(const char *str_ipaddr,
return;
}
ifidx = get_ifindex(client_config.interface);
if (ifidx < 0) {
log_line("%s: (%s) can't get interface index",
client_config.interface, __func__);
return;
}
if (inet_pton(AF_INET, str_ipaddr, &ipaddr) <= 0) {
log_line("%s: (%s) bad interface ip address: '%s'",
client_config.interface, __func__, str_ipaddr);
@ -220,7 +190,7 @@ void perform_ip_subnet_bcast(const char *str_ipaddr,
ifaddrmsg->ifa_prefixlen = prefixlen;
ifaddrmsg->ifa_flags = IFA_F_PERMANENT;
ifaddrmsg->ifa_scope = RT_SCOPE_UNIVERSE;
ifaddrmsg->ifa_index = ifidx;
ifaddrmsg->ifa_index = client_config.ifindex;
if (add_rtattr(header, sizeof request, IFA_LOCAL,
&ipaddr, sizeof ipaddr) < 0) {

View File

@ -301,15 +301,6 @@ static void ndhc_main(void) {
write_pid(pidfile);
}
if ((cs.nlFd = nl_open(NETLINK_ROUTE, RTMGRP_LINK, &cs.nlPortId)) < 0) {
log_line("FATAL - failed to open netlink socket");
exit(EXIT_FAILURE);
}
if (nl_getifdata(&cs) < 0) {
log_line("FATAL - failed to get interface MAC and index");
exit(EXIT_FAILURE);
}
open_leasefile();
imprison(chroot_dir);
@ -523,11 +514,21 @@ int main(int argc, char **argv)
if (!strncmp(chroot_dir, "", sizeof chroot_dir))
suicide("FATAL - No chroot path specified. Refusing to run.");
if ((cs.nlFd = nl_open(NETLINK_ROUTE, RTMGRP_LINK, &cs.nlPortId)) < 0) {
log_line("FATAL - failed to open netlink socket");
exit(EXIT_FAILURE);
}
if (nl_getifdata(&cs) < 0) {
log_line("FATAL - failed to get interface MAC and index");
exit(EXIT_FAILURE);
}
create_ipc_pipes();
pid_t ifch_pid = fork();
if (ifch_pid == 0) {
close(pToNdhcR);
close(pToIfchW);
close(cs.nlFd);
ifch_main();
} else if (ifch_pid > 0) {
close(pToIfchR);