From: Roy Marples Date: Fri, 7 Feb 2014 14:57:19 +0000 (+0000) Subject: Ignore netlink messages with an our of range pid. X-Git-Tag: v6.3.0~36 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=119265612a49f20daa35e4c9de30a79c1c0dee80;p=thirdparty%2Fdhcpcd.git Ignore netlink messages with an our of range pid. --- diff --git a/if-linux.c b/if-linux.c index 11c68740..fe69fc95 100644 --- a/if-linux.c +++ b/if-linux.c @@ -243,6 +243,18 @@ err_netlink(struct nlmsghdr *nlm) return -1; } +/* Work out the maximum pid size */ +static inline long long +get_max_pid_t() +{ + + if (sizeof(pid_t) == sizeof(short)) return SHRT_MAX; + if (sizeof(pid_t) == sizeof(int)) return INT_MAX; + if (sizeof(pid_t) == sizeof(long)) return LONG_MAX; + if (sizeof(pid_t) == sizeof(long long)) return LLONG_MAX; + abort(); +} + static int link_route(struct nlmsghdr *nlm) { @@ -263,9 +275,15 @@ link_route(struct nlmsghdr *nlm) rtm = NLMSG_DATA(nlm); if (rtm->rtm_type != RTN_UNICAST || rtm->rtm_table != RT_TABLE_MAIN || - rtm->rtm_family != AF_INET || - nlm->nlmsg_pid == (uint32_t)getpid()) + rtm->rtm_family != AF_INET) return 1; + /* Ignore messages generated by us. + * For some reason we get messages generated by us + * with a very large value in nlmsg_pid that seems to be + * sequentially changing. Is there a better test for this? */ + if (nlm->nlmsg_pid > get_max_pid_t()) + return 1; + rta = (struct rtattr *)(void *)((char *)rtm +NLMSG_ALIGN(sizeof(*rtm))); len = NLMSG_PAYLOAD(nlm, sizeof(*rtm)); memset(&rt, 0, sizeof(rt)); @@ -328,8 +346,6 @@ link_addr(struct nlmsghdr *nlm) errno = EBADMSG; return -1; } -// if (nlm->nlmsg_pid == (uint32_t)getpid()) -// return 1; ifa = NLMSG_DATA(nlm); if (if_indextoname(ifa->ifa_index, ifn) == NULL) return -1; @@ -508,6 +524,7 @@ link_netlink(struct nlmsghdr *nlm) int manage_link(int fd) { + return get_netlink(fd, MSG_DONTWAIT, &link_netlink); }