From a4b4e9ad8e35ab424e61d76ebe6654445fc61e63 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Fri, 2 Aug 2013 05:58:37 +0900 Subject: Parse IP address changes in NetlinkEvent. This adds support for parsing RTM_NEWADDR and RTM_DELADDR netlink messages received on netd's netlink socket and formatting them them so NetlinkHandler can process them. Address changes are notified in subsystem "address". Interface changes, which used to be notified in subsystem "net", are now notified in subsystem "interface" to avoid confusion. Bug: 10232006 Change-Id: I93a50e8de17014e118a42f5cc0eb90a0cbfa73cc --- libsysutils/src/NetlinkEvent.cpp | 117 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) (limited to 'libsysutils') diff --git a/libsysutils/src/NetlinkEvent.cpp b/libsysutils/src/NetlinkEvent.cpp index 4beebb7..b6da72f 100644 --- a/libsysutils/src/NetlinkEvent.cpp +++ b/libsysutils/src/NetlinkEvent.cpp @@ -23,6 +23,10 @@ #include #include +#include +#include +#include + #include #include #include @@ -70,6 +74,102 @@ void NetlinkEvent::dump() { } /* + * Decode a RTM_NEWADDR or RTM_DELADDR message. + */ +bool NetlinkEvent::parseIfAddrMessage(int type, struct ifaddrmsg *ifaddr, + int rtasize) { + struct rtattr *rta = IFA_RTA(ifaddr); + struct ifa_cacheinfo *cacheinfo = NULL; + char addrstr[INET6_ADDRSTRLEN] = ""; + + // Sanity check. + if (type != RTM_NEWADDR && type != RTM_DELADDR) { + SLOGE("parseIfAddrMessage on incorrect message type 0x%x\n", type); + return false; + } + + // For log messages. + const char *msgtype = (type == RTM_NEWADDR) ? "RTM_NEWADDR" : "RTM_DELADDR"; + + while(RTA_OK(rta, rtasize)) { + if (rta->rta_type == IFA_ADDRESS) { + // Only look at the first address, because we only support notifying + // one change at a time. + if (*addrstr != '\0') { + SLOGE("Multiple IFA_ADDRESSes in %s, ignoring\n", msgtype); + continue; + } + + // Convert the IP address to a string. + if (ifaddr->ifa_family == AF_INET) { + struct in_addr *addr4 = (struct in_addr *) RTA_DATA(rta); + if (RTA_PAYLOAD(rta) < sizeof(*addr4)) { + SLOGE("Short IPv4 address (%d bytes) in %s", + RTA_PAYLOAD(rta), msgtype); + continue; + } + inet_ntop(AF_INET, addr4, addrstr, sizeof(addrstr)); + } else if (ifaddr->ifa_family == AF_INET6) { + struct in6_addr *addr6 = (struct in6_addr *) RTA_DATA(rta); + if (RTA_PAYLOAD(rta) < sizeof(*addr6)) { + SLOGE("Short IPv6 address (%d bytes) in %s", + RTA_PAYLOAD(rta), msgtype); + continue; + } + inet_ntop(AF_INET6, addr6, addrstr, sizeof(addrstr)); + } else { + SLOGE("Unknown address family %d\n", ifaddr->ifa_family); + continue; + } + + // Find the interface name. + char ifname[IFNAMSIZ + 1]; + if (!if_indextoname(ifaddr->ifa_index, ifname)) { + SLOGE("Unknown ifindex %d in %s", ifaddr->ifa_index, msgtype); + return false; + } + + // Fill in interface information. + mAction = (type == RTM_NEWADDR) ? NlActionAdd : NlActionRemove; + mSubsystem = strdup("address"); + asprintf(&mParams[0], "ADDRESS=%s/%d", addrstr, + ifaddr->ifa_prefixlen); + asprintf(&mParams[1], "IFACE=%s", ifname); + asprintf(&mParams[2], "FLAGS=%u", ifaddr->ifa_flags); + asprintf(&mParams[3], "SCOPE=%u", ifaddr->ifa_scope); + } else if (rta->rta_type == IFA_CACHEINFO) { + // Address lifetime information. + if (cacheinfo) { + // We only support one address. + SLOGE("Multiple IFA_CACHEINFOs in %s, ignoring\n", msgtype); + continue; + } + + if (RTA_PAYLOAD(rta) < sizeof(*cacheinfo)) { + SLOGE("Short IFA_CACHEINFO (%d vs. %d bytes) in %s", + RTA_PAYLOAD(rta), sizeof(cacheinfo), msgtype); + continue; + } + + cacheinfo = (struct ifa_cacheinfo *) RTA_DATA(rta); + asprintf(&mParams[4], "PREFERRED=%u", cacheinfo->ifa_prefered); + asprintf(&mParams[5], "VALID=%u", cacheinfo->ifa_valid); + asprintf(&mParams[6], "CSTAMP=%u", cacheinfo->cstamp); + asprintf(&mParams[7], "TSTAMP=%u", cacheinfo->tstamp); + } + + rta = RTA_NEXT(rta, rtasize); + } + + if (addrstr[0] == '\0') { + SLOGE("No IFA_ADDRESS in %s\n", msgtype); + return false; + } + + return true; +} + +/* * Parse an binary message from a NETLINK_ROUTE netlink socket. */ bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) { @@ -105,7 +205,7 @@ bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) { mParams[0] = strdup(buffer); mAction = (ifi->ifi_flags & IFF_LOWER_UP) ? NlActionLinkUp : NlActionLinkDown; - mSubsystem = strdup("net"); + mSubsystem = strdup("interface"); break; } @@ -127,6 +227,21 @@ bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) { mSubsystem = strdup("qlog"); mAction = NlActionChange; + } else if (nh->nlmsg_type == RTM_NEWADDR || + nh->nlmsg_type == RTM_DELADDR) { + int len = nh->nlmsg_len - sizeof(*nh); + struct ifaddrmsg *ifa; + + if (sizeof(*ifa) > (size_t) len) { + SLOGE("Got a short RTM_xxxADDR message\n"); + continue; + } + + ifa = (ifaddrmsg *)NLMSG_DATA(nh); + size_t rtasize = IFA_PAYLOAD(nh); + if (!parseIfAddrMessage(nh->nlmsg_type, ifa, rtasize)) { + continue; + } } else { SLOGD("Unexpected netlink message. type=0x%x\n", nh->nlmsg_type); } -- cgit v1.1 From 3984276ce47c965ad02a522280a139e0a0c7e5cf Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Sat, 17 Aug 2013 03:40:31 +0900 Subject: Get rid of an infinite loop in NetlinkEvent.cpp. Bug: 10358527 Bug: 10263310 Bug: 10232006 Change-Id: I750e4bdf2000040adf214d6a772591d7bd25b350 --- libsysutils/src/NetlinkEvent.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'libsysutils') diff --git a/libsysutils/src/NetlinkEvent.cpp b/libsysutils/src/NetlinkEvent.cpp index b6da72f..01bec77 100644 --- a/libsysutils/src/NetlinkEvent.cpp +++ b/libsysutils/src/NetlinkEvent.cpp @@ -78,7 +78,7 @@ void NetlinkEvent::dump() { */ bool NetlinkEvent::parseIfAddrMessage(int type, struct ifaddrmsg *ifaddr, int rtasize) { - struct rtattr *rta = IFA_RTA(ifaddr); + struct rtattr *rta; struct ifa_cacheinfo *cacheinfo = NULL; char addrstr[INET6_ADDRSTRLEN] = ""; @@ -91,7 +91,8 @@ bool NetlinkEvent::parseIfAddrMessage(int type, struct ifaddrmsg *ifaddr, // For log messages. const char *msgtype = (type == RTM_NEWADDR) ? "RTM_NEWADDR" : "RTM_DELADDR"; - while(RTA_OK(rta, rtasize)) { + for (rta = IFA_RTA(ifaddr); RTA_OK(rta, rtasize); + rta = RTA_NEXT(rta, rtasize)) { if (rta->rta_type == IFA_ADDRESS) { // Only look at the first address, because we only support notifying // one change at a time. @@ -157,8 +158,6 @@ bool NetlinkEvent::parseIfAddrMessage(int type, struct ifaddrmsg *ifaddr, asprintf(&mParams[6], "CSTAMP=%u", cacheinfo->cstamp); asprintf(&mParams[7], "TSTAMP=%u", cacheinfo->tstamp); } - - rta = RTA_NEXT(rta, rtasize); } if (addrstr[0] == '\0') { @@ -173,10 +172,11 @@ bool NetlinkEvent::parseIfAddrMessage(int type, struct ifaddrmsg *ifaddr, * Parse an binary message from a NETLINK_ROUTE netlink socket. */ bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) { - size_t sz = size; - const struct nlmsghdr *nh = (struct nlmsghdr *) buffer; + const struct nlmsghdr *nh; - while (NLMSG_OK(nh, sz) && (nh->nlmsg_type != NLMSG_DONE)) { + for (nh = (struct nlmsghdr *) buffer; + NLMSG_OK(nh, size) && (nh->nlmsg_type != NLMSG_DONE); + nh = NLMSG_NEXT(nh, size)) { if (nh->nlmsg_type == RTM_NEWLINK) { int len = nh->nlmsg_len - sizeof(*nh); @@ -245,7 +245,6 @@ bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) { } else { SLOGD("Unexpected netlink message. type=0x%x\n", nh->nlmsg_type); } - nh = NLMSG_NEXT(nh, size); } return true; -- cgit v1.1 From f34861346d5c207912075fba9874090e4c947869 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Tue, 3 Sep 2013 00:25:14 +0900 Subject: Switch back to subsystem "net" for netlink events. The change to enable address tracking via netlink incorrectly changed the subsystem of rtnetlink events from "net" to "interface". This broke interface add/delete notifications, which come from the kernel with subsystem "net". Switch back to "net" and deal with address tracking via new action codes instead of a new subsystem. Bug: 10433320 Change-Id: Ibf30efb426949dfd02304cc1d9adb1c005a539a6 --- libsysutils/src/NetlinkEvent.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'libsysutils') diff --git a/libsysutils/src/NetlinkEvent.cpp b/libsysutils/src/NetlinkEvent.cpp index 01bec77..aae2ae7 100644 --- a/libsysutils/src/NetlinkEvent.cpp +++ b/libsysutils/src/NetlinkEvent.cpp @@ -42,6 +42,8 @@ const int NetlinkEvent::NlActionRemove = 2; const int NetlinkEvent::NlActionChange = 3; const int NetlinkEvent::NlActionLinkUp = 4; const int NetlinkEvent::NlActionLinkDown = 5; +const int NetlinkEvent::NlActionAddressUpdated = 6; +const int NetlinkEvent::NlActionAddressRemoved = 7; NetlinkEvent::NetlinkEvent() { mAction = NlActionUnknown; @@ -131,11 +133,12 @@ bool NetlinkEvent::parseIfAddrMessage(int type, struct ifaddrmsg *ifaddr, } // Fill in interface information. - mAction = (type == RTM_NEWADDR) ? NlActionAdd : NlActionRemove; - mSubsystem = strdup("address"); + mAction = (type == RTM_NEWADDR) ? NlActionAddressUpdated : + NlActionAddressRemoved; + mSubsystem = strdup("net"); asprintf(&mParams[0], "ADDRESS=%s/%d", addrstr, ifaddr->ifa_prefixlen); - asprintf(&mParams[1], "IFACE=%s", ifname); + asprintf(&mParams[1], "INTERFACE=%s", ifname); asprintf(&mParams[2], "FLAGS=%u", ifaddr->ifa_flags); asprintf(&mParams[3], "SCOPE=%u", ifaddr->ifa_scope); } else if (rta->rta_type == IFA_CACHEINFO) { @@ -205,7 +208,7 @@ bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) { mParams[0] = strdup(buffer); mAction = (ifi->ifi_flags & IFF_LOWER_UP) ? NlActionLinkUp : NlActionLinkDown; - mSubsystem = strdup("interface"); + mSubsystem = strdup("net"); break; } -- cgit v1.1