diff options
author | Lorenzo Colitti <lorenzo@google.com> | 2017-10-03 18:44:11 +0900 |
---|---|---|
committer | Lorenzo Colitti <lorenzo@google.com> | 2017-10-05 14:56:44 +0900 |
commit | e439ffc762c91fbc1e3512c3b9bad4360102b3a6 (patch) | |
tree | 31ffcd7151780a72cca5823c7e2bde24b090f506 /libsysutils | |
parent | 7229576bfed017f78d90102db357de2f9f1f5b45 (diff) | |
download | system_core-e439ffc762c91fbc1e3512c3b9bad4360102b3a6.tar.gz system_core-e439ffc762c91fbc1e3512c3b9bad4360102b3a6.tar.bz2 system_core-e439ffc762c91fbc1e3512c3b9bad4360102b3a6.zip |
Stop depending on libnl.
We only use it for trivial functions. Replace them and drop the
dependency.
Bug: 67345547
Test: bullhead builds, boots
Test: CtsOsTestCases android.os.cts.StrictModeTest passes
Change-Id: I36254962284babdd1a55a32a76dd0dc92d85420c
Diffstat (limited to 'libsysutils')
-rw-r--r-- | libsysutils/Android.bp | 1 | ||||
-rw-r--r-- | libsysutils/include/sysutils/NetlinkEvent.h | 1 | ||||
-rw-r--r-- | libsysutils/src/NetlinkEvent.cpp | 53 |
3 files changed, 44 insertions, 11 deletions
diff --git a/libsysutils/Android.bp b/libsysutils/Android.bp index d076a1ae3..3a1229228 100644 --- a/libsysutils/Android.bp +++ b/libsysutils/Android.bp @@ -23,7 +23,6 @@ cc_library_shared { "libbase", "libcutils", "liblog", - "libnl", ], export_include_dirs: ["include"], diff --git a/libsysutils/include/sysutils/NetlinkEvent.h b/libsysutils/include/sysutils/NetlinkEvent.h index b80f3ea44..f9fc11b2b 100644 --- a/libsysutils/include/sysutils/NetlinkEvent.h +++ b/libsysutils/include/sysutils/NetlinkEvent.h @@ -64,6 +64,7 @@ public: bool parseNfPacketMessage(struct nlmsghdr *nh); bool parseRtMessage(const struct nlmsghdr *nh); bool parseNdUserOptMessage(const struct nlmsghdr *nh); + struct nlattr* findNlAttr(const nlmsghdr* nl, size_t hdrlen, uint16_t attr); }; #endif diff --git a/libsysutils/src/NetlinkEvent.cpp b/libsysutils/src/NetlinkEvent.cpp index 79bc88853..00b1ee228 100644 --- a/libsysutils/src/NetlinkEvent.cpp +++ b/libsysutils/src/NetlinkEvent.cpp @@ -17,6 +17,8 @@ #define LOG_TAG "NetlinkEvent" #include <arpa/inet.h> +#include <limits.h> +#include <linux/genetlink.h> #include <linux/if.h> #include <linux/if_addr.h> #include <linux/if_link.h> @@ -26,12 +28,8 @@ #include <linux/netlink.h> #include <linux/rtnetlink.h> #include <net/if.h> -#include <netinet/in.h> #include <netinet/icmp6.h> -#include <netlink/attr.h> -#include <netlink/genl/genl.h> -#include <netlink/handlers.h> -#include <netlink/msg.h> +#include <netinet/in.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> @@ -263,6 +261,18 @@ bool NetlinkEvent::parseUlogPacketMessage(const struct nlmsghdr *nh) { return true; } +static size_t nlAttrLen(const nlattr* nla) { + return nla->nla_len - NLA_HDRLEN; +} + +static const uint8_t* nlAttrData(const nlattr* nla) { + return reinterpret_cast<const uint8_t*>(nla) + NLA_HDRLEN; +} + +static uint32_t nlAttrU32(const nlattr* nla) { + return *reinterpret_cast<const uint32_t*>(nlAttrData(nla)); +} + /* * Parse a LOCAL_NFLOG_PACKET message. */ @@ -271,17 +281,17 @@ bool NetlinkEvent::parseNfPacketMessage(struct nlmsghdr *nh) { int len = 0; char* raw = NULL; - struct nlattr *uid_attr = nlmsg_find_attr(nh, sizeof(struct genlmsghdr), NFULA_UID); + struct nlattr* uid_attr = findNlAttr(nh, sizeof(struct genlmsghdr), NFULA_UID); if (uid_attr) { - uid = ntohl(nla_get_u32(uid_attr)); + uid = ntohl(nlAttrU32(uid_attr)); } - struct nlattr *payload = nlmsg_find_attr(nh, sizeof(struct genlmsghdr), NFULA_PAYLOAD); + struct nlattr* payload = findNlAttr(nh, sizeof(struct genlmsghdr), NFULA_PAYLOAD); if (payload) { /* First 256 bytes is plenty */ - len = nla_len(payload); + len = nlAttrLen(payload); if (len > 256) len = 256; - raw = (char*) nla_data(payload); + raw = (char*)nlAttrData(payload); } char* hex = (char*) calloc(1, 5 + (len * 2)); @@ -646,3 +656,26 @@ const char *NetlinkEvent::findParam(const char *paramName) { SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName); return NULL; } + +nlattr* NetlinkEvent::findNlAttr(const nlmsghdr* nh, size_t hdrlen, uint16_t attr) { + if (nh == nullptr || NLMSG_HDRLEN + NLMSG_ALIGN(hdrlen) > SSIZE_MAX) { + return nullptr; + } + + // Skip header, padding, and family header. + const ssize_t NLA_START = NLMSG_HDRLEN + NLMSG_ALIGN(hdrlen); + ssize_t left = nh->nlmsg_len - NLA_START; + uint8_t* hdr = ((uint8_t*)nh) + NLA_START; + + while (left >= NLA_HDRLEN) { + nlattr* nla = (nlattr*)hdr; + if (nla->nla_type == attr) { + return nla; + } + + hdr += NLA_ALIGN(nla->nla_len); + left -= NLA_ALIGN(nla->nla_len); + } + + return nullptr; +} |