From 6373af63310f32e1408a81f4e4e069a5f0d3e59e Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Wed, 8 Oct 2014 11:57:07 +0200 Subject: [PATCH] Ensure signal-safety of signal handlers --- src/odhcp6c.c | 5 +---- src/odhcp6c.h | 1 - src/ra.c | 39 +++++++++++++++++++++++---------------- src/script.c | 25 ------------------------- 4 files changed, 24 insertions(+), 46 deletions(-) diff --git a/src/odhcp6c.c b/src/odhcp6c.c index c76c813..7cdf09e 100644 --- a/src/odhcp6c.c +++ b/src/odhcp6c.c @@ -480,11 +480,8 @@ bool odhcp6c_signal_process(void) if (ra_link_up()) signal_usr2 = true; - if (ra_updated && (bound || allow_slaac_only == 0)) + if (ra_updated && (bound || allow_slaac_only >= 0)) script_call("ra-updated"); // Immediate process urgent events - else if (ra_updated && !bound && allow_slaac_only > 0) - script_delay_call("ra-updated", allow_slaac_only); - } return signal_usr1 || signal_usr2 || signal_term; diff --git a/src/odhcp6c.h b/src/odhcp6c.h index a4343b1..f2924b0 100644 --- a/src/odhcp6c.h +++ b/src/odhcp6c.h @@ -325,7 +325,6 @@ int set_rtnetlink_addr(int ifindex, const struct in6_addr *addr, int script_init(const char *path, const char *ifname); ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src); void script_call(const char *status); -void script_delay_call(const char *status, int timeout); bool odhcp6c_signal_process(void); uint64_t odhcp6c_get_milli_time(void); diff --git a/src/ra.c b/src/ra.c index baaee9b..9c5eb22 100644 --- a/src/ra.c +++ b/src/ra.c @@ -26,7 +26,6 @@ #include #include #include -#include #include #include @@ -56,6 +55,15 @@ static char if_name[IF_NAMESIZE] = {0}; static volatile int rs_attempt = 0; static struct in6_addr lladdr = IN6ADDR_ANY_INIT; +struct { + struct icmp6_hdr hdr; + struct icmpv6_opt lladdr; +} rs = { + .hdr = {ND_ROUTER_SOLICIT, 0, 0, {{0}}}, + .lladdr = {ND_OPT_SOURCE_LINKADDR, 1, {0}}, +}; + + static void ra_send_rs(int signal __attribute__((unused))); int ra_init(const char *ifname, const struct in6_addr *ifid) @@ -93,6 +101,7 @@ int ra_init(const char *ifname, const struct in6_addr *ifid) .ifi = {.ifi_index = if_index} }; send(rtnl, &req, sizeof(req), 0); + ra_link_up(); // Filter ICMPv6 package types struct icmp6_filter filt; @@ -133,22 +142,12 @@ int ra_init(const char *ifname, const struct in6_addr *ifid) static void ra_send_rs(int signal __attribute__((unused))) { - struct { - struct icmp6_hdr hdr; - struct icmpv6_opt lladdr; - } rs = { - .hdr = {ND_ROUTER_SOLICIT, 0, 0, {{0}}}, - .lladdr = {ND_OPT_SOURCE_LINKADDR, 1, {0}}, - }; const struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, if_index}; - size_t len = sizeof(rs); - - struct ifreq ifr; - memset(&ifr, 0, sizeof(ifr)); - strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name)); - if ((rs_attempt % 2 == 0) && !ioctl(sock, SIOCGIFHWADDR, &ifr) - && memcmp(rs.lladdr.data, ifr.ifr_hwaddr.sa_data, 6)) - memcpy(rs.lladdr.data, ifr.ifr_hwaddr.sa_data, 6); + const struct icmpv6_opt llnull = {ND_OPT_SOURCE_LINKADDR, 1, {0}}; + size_t len; + + if ((rs_attempt % 2 == 0) && memcmp(&rs.lladdr, &llnull, sizeof(llnull))) + len = sizeof(rs); else len = sizeof(struct icmp6_hdr); @@ -197,6 +196,14 @@ bool ra_link_up(void) resp.msg.ifi_index != if_index) continue; + ssize_t alen = NLMSG_PAYLOAD(&resp.hdr, sizeof(resp.msg)); + for (struct rtattr *rta = (struct rtattr*)(resp.pad); + RTA_OK(rta, alen); rta = RTA_NEXT(rta, alen)) { + if (rta->rta_type == IFLA_ADDRESS && + RTA_PAYLOAD(rta) >= sizeof(rs.lladdr.data)) + memcpy(rs.lladdr.data, RTA_DATA(rta), sizeof(rs.lladdr.data)); + } + bool hascarrier = resp.msg.ifi_flags & IFF_LOWER_UP; if (!firstcall && nocarrier != !hascarrier) ret = true; diff --git a/src/script.c b/src/script.c index e389cc9..644f61f 100644 --- a/src/script.c +++ b/src/script.c @@ -41,8 +41,6 @@ static const int8_t hexvals[] = { static char *argv[4] = {NULL, NULL, NULL, NULL}; -static volatile char *delayed_call = NULL; -static bool dont_delay = false; int script_init(const char *path, const char *ifname) @@ -332,25 +330,6 @@ static void s46_to_env(enum odhcp6c_state state, const uint8_t *data, size_t len } -static void script_call_delayed(int signal __attribute__((unused))) -{ - if (delayed_call) - script_call((char*)delayed_call); -} - - -void script_delay_call(const char *status, int timeout) -{ - if (dont_delay) { - script_call(status); - } else if (!delayed_call) { - delayed_call = strdup(status); - signal(SIGALRM, script_call_delayed); - alarm(timeout); - } -} - - void script_call(const char *status) { size_t dns_len, search_len, custom_len, sntp_ip_len, ntp_ip_len, ntp_dns_len; @@ -358,10 +337,6 @@ void script_call(const char *status) size_t s46_mapt_len, s46_mape_len, s46_lw_len, passthru_len; odhcp6c_expire(); - if (delayed_call) { - alarm(0); - dont_delay = true; - } struct in6_addr *addr = odhcp6c_get_state(STATE_SERVER_ADDR, &addr_len); struct in6_addr *dns = odhcp6c_get_state(STATE_DNS, &dns_len); -- 2.30.2