From b7b353acdc492109e455a69448cdef3cb72ee176 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Wed, 12 Mar 2014 15:07:37 -0400 Subject: [PATCH] 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. --- ndhc/ifset.c | 34 ++-------------------------------- ndhc/ndhc.c | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 41 deletions(-) diff --git a/ndhc/ifset.c b/ndhc/ifset.c index 075952f..0c35924 100644 --- a/ndhc/ifset.c +++ b/ndhc/ifset.c @@ -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) { diff --git a/ndhc/ndhc.c b/ndhc/ndhc.c index 6da240a..ac5c38d 100644 --- a/ndhc/ndhc.c +++ b/ndhc/ndhc.c @@ -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);