| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 16 |  | 
|  | 17 | #define LOG_TAG "NetlinkEvent" | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 18 |  | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 19 | #include <arpa/inet.h> | 
| Lorenzo Colitti | e439ffc | 2017-10-03 18:44:11 +0900 | [diff] [blame] | 20 | #include <limits.h> | 
|  | 21 | #include <linux/genetlink.h> | 
| Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 22 | #include <linux/if.h> | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 23 | #include <linux/if_addr.h> | 
|  | 24 | #include <linux/if_link.h> | 
| JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 25 | #include <linux/netfilter/nfnetlink.h> | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 26 | #include <linux/netfilter/nfnetlink_log.h> | 
| JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 27 | #include <linux/netfilter_ipv4/ipt_ULOG.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 28 | #include <linux/netlink.h> | 
|  | 29 | #include <linux/rtnetlink.h> | 
|  | 30 | #include <net/if.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 31 | #include <netinet/icmp6.h> | 
| Lorenzo Colitti | e439ffc | 2017-10-03 18:44:11 +0900 | [diff] [blame] | 32 | #include <netinet/in.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 33 | #include <stdlib.h> | 
|  | 34 | #include <string.h> | 
|  | 35 | #include <sys/socket.h> | 
|  | 36 | #include <sys/types.h> | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 37 |  | 
| JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 38 | /* From kernel's net/netfilter/xt_quota2.c */ | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 39 | const int LOCAL_QLOG_NL_EVENT = 112; | 
|  | 40 | const int LOCAL_NFLOG_PACKET = NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET; | 
| JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 41 |  | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 42 | #include <log/log.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 43 | #include <sysutils/NetlinkEvent.h> | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 44 |  | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 45 | NetlinkEvent::NetlinkEvent() { | 
| Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 46 | mAction = Action::kUnknown; | 
| San Mehat | ebfe3db | 2009-10-10 17:35:13 -0700 | [diff] [blame] | 47 | memset(mParams, 0, sizeof(mParams)); | 
|  | 48 | mPath = NULL; | 
|  | 49 | mSubsystem = NULL; | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 50 | } | 
|  | 51 |  | 
|  | 52 | NetlinkEvent::~NetlinkEvent() { | 
|  | 53 | int i; | 
|  | 54 | if (mPath) | 
|  | 55 | free(mPath); | 
|  | 56 | if (mSubsystem) | 
|  | 57 | free(mSubsystem); | 
|  | 58 | for (i = 0; i < NL_PARAMS_MAX; i++) { | 
|  | 59 | if (!mParams[i]) | 
|  | 60 | break; | 
|  | 61 | free(mParams[i]); | 
|  | 62 | } | 
|  | 63 | } | 
|  | 64 |  | 
| San Mehat | d674413 | 2009-12-24 07:17:09 -0800 | [diff] [blame] | 65 | void NetlinkEvent::dump() { | 
|  | 66 | int i; | 
|  | 67 |  | 
|  | 68 | for (i = 0; i < NL_PARAMS_MAX; i++) { | 
|  | 69 | if (!mParams[i]) | 
|  | 70 | break; | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 71 | SLOGD("NL param '%s'\n", mParams[i]); | 
| San Mehat | d674413 | 2009-12-24 07:17:09 -0800 | [diff] [blame] | 72 | } | 
|  | 73 | } | 
|  | 74 |  | 
| Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 75 | /* | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 76 | * Returns the message name for a message in the NETLINK_ROUTE family, or NULL | 
|  | 77 | * if parsing that message is not supported. | 
|  | 78 | */ | 
|  | 79 | static const char *rtMessageName(int type) { | 
|  | 80 | #define NL_EVENT_RTM_NAME(rtm) case rtm: return #rtm; | 
|  | 81 | switch (type) { | 
|  | 82 | NL_EVENT_RTM_NAME(RTM_NEWLINK); | 
|  | 83 | NL_EVENT_RTM_NAME(RTM_DELLINK); | 
|  | 84 | NL_EVENT_RTM_NAME(RTM_NEWADDR); | 
|  | 85 | NL_EVENT_RTM_NAME(RTM_DELADDR); | 
|  | 86 | NL_EVENT_RTM_NAME(RTM_NEWROUTE); | 
|  | 87 | NL_EVENT_RTM_NAME(RTM_DELROUTE); | 
|  | 88 | NL_EVENT_RTM_NAME(RTM_NEWNDUSEROPT); | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 89 | NL_EVENT_RTM_NAME(LOCAL_QLOG_NL_EVENT); | 
|  | 90 | NL_EVENT_RTM_NAME(LOCAL_NFLOG_PACKET); | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 91 | default: | 
|  | 92 | return NULL; | 
|  | 93 | } | 
|  | 94 | #undef NL_EVENT_RTM_NAME | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | /* | 
|  | 98 | * Checks that a binary NETLINK_ROUTE message is long enough for a payload of | 
|  | 99 | * size bytes. | 
|  | 100 | */ | 
|  | 101 | static bool checkRtNetlinkLength(const struct nlmsghdr *nh, size_t size) { | 
|  | 102 | if (nh->nlmsg_len < NLMSG_LENGTH(size)) { | 
|  | 103 | SLOGE("Got a short %s message\n", rtMessageName(nh->nlmsg_type)); | 
|  | 104 | return false; | 
|  | 105 | } | 
|  | 106 | return true; | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | /* | 
|  | 110 | * Utility function to log errors. | 
|  | 111 | */ | 
|  | 112 | static bool maybeLogDuplicateAttribute(bool isDup, | 
|  | 113 | const char *attributeName, | 
|  | 114 | const char *messageName) { | 
|  | 115 | if (isDup) { | 
|  | 116 | SLOGE("Multiple %s attributes in %s, ignoring\n", attributeName, messageName); | 
|  | 117 | return true; | 
|  | 118 | } | 
|  | 119 | return false; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | /* | 
|  | 123 | * Parse a RTM_NEWLINK message. | 
|  | 124 | */ | 
|  | 125 | bool NetlinkEvent::parseIfInfoMessage(const struct nlmsghdr *nh) { | 
|  | 126 | struct ifinfomsg *ifi = (struct ifinfomsg *) NLMSG_DATA(nh); | 
|  | 127 | if (!checkRtNetlinkLength(nh, sizeof(*ifi))) | 
|  | 128 | return false; | 
|  | 129 |  | 
|  | 130 | if ((ifi->ifi_flags & IFF_LOOPBACK) != 0) { | 
|  | 131 | return false; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | int len = IFLA_PAYLOAD(nh); | 
|  | 135 | struct rtattr *rta; | 
|  | 136 | for (rta = IFLA_RTA(ifi); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) { | 
|  | 137 | switch(rta->rta_type) { | 
|  | 138 | case IFLA_IFNAME: | 
|  | 139 | asprintf(&mParams[0], "INTERFACE=%s", (char *) RTA_DATA(rta)); | 
| Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 140 | mAction = (ifi->ifi_flags & IFF_LOWER_UP) ? Action::kLinkUp : | 
|  | 141 | Action::kLinkDown; | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 142 | mSubsystem = strdup("net"); | 
|  | 143 | return true; | 
|  | 144 | } | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | return false; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | /* | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 151 | * Parse a RTM_NEWADDR or RTM_DELADDR message. | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 152 | */ | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 153 | bool NetlinkEvent::parseIfAddrMessage(const struct nlmsghdr *nh) { | 
|  | 154 | struct ifaddrmsg *ifaddr = (struct ifaddrmsg *) NLMSG_DATA(nh); | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 155 | struct ifa_cacheinfo *cacheinfo = NULL; | 
|  | 156 | char addrstr[INET6_ADDRSTRLEN] = ""; | 
| Lorenzo Colitti | ef6454d | 2016-02-16 21:42:16 +0900 | [diff] [blame] | 157 | char ifname[IFNAMSIZ] = ""; | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 158 |  | 
|  | 159 | if (!checkRtNetlinkLength(nh, sizeof(*ifaddr))) | 
|  | 160 | return false; | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 161 |  | 
|  | 162 | // Sanity check. | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 163 | int type = nh->nlmsg_type; | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 164 | if (type != RTM_NEWADDR && type != RTM_DELADDR) { | 
|  | 165 | SLOGE("parseIfAddrMessage on incorrect message type 0x%x\n", type); | 
|  | 166 | return false; | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | // For log messages. | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 170 | const char *msgtype = rtMessageName(type); | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 171 |  | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 172 | struct rtattr *rta; | 
|  | 173 | int len = IFA_PAYLOAD(nh); | 
|  | 174 | for (rta = IFA_RTA(ifaddr); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) { | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 175 | if (rta->rta_type == IFA_ADDRESS) { | 
|  | 176 | // Only look at the first address, because we only support notifying | 
|  | 177 | // one change at a time. | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 178 | if (maybeLogDuplicateAttribute(*addrstr != '\0', "IFA_ADDRESS", msgtype)) | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 179 | continue; | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 180 |  | 
|  | 181 | // Convert the IP address to a string. | 
|  | 182 | if (ifaddr->ifa_family == AF_INET) { | 
|  | 183 | struct in_addr *addr4 = (struct in_addr *) RTA_DATA(rta); | 
|  | 184 | if (RTA_PAYLOAD(rta) < sizeof(*addr4)) { | 
| Mark Salyzyn | 80f63d4 | 2014-05-01 07:47:04 -0700 | [diff] [blame] | 185 | SLOGE("Short IPv4 address (%zu bytes) in %s", | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 186 | RTA_PAYLOAD(rta), msgtype); | 
|  | 187 | continue; | 
|  | 188 | } | 
|  | 189 | inet_ntop(AF_INET, addr4, addrstr, sizeof(addrstr)); | 
|  | 190 | } else if (ifaddr->ifa_family == AF_INET6) { | 
|  | 191 | struct in6_addr *addr6 = (struct in6_addr *) RTA_DATA(rta); | 
|  | 192 | if (RTA_PAYLOAD(rta) < sizeof(*addr6)) { | 
| Mark Salyzyn | 80f63d4 | 2014-05-01 07:47:04 -0700 | [diff] [blame] | 193 | SLOGE("Short IPv6 address (%zu bytes) in %s", | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 194 | RTA_PAYLOAD(rta), msgtype); | 
|  | 195 | continue; | 
|  | 196 | } | 
|  | 197 | inet_ntop(AF_INET6, addr6, addrstr, sizeof(addrstr)); | 
|  | 198 | } else { | 
|  | 199 | SLOGE("Unknown address family %d\n", ifaddr->ifa_family); | 
|  | 200 | continue; | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | // Find the interface name. | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 204 | if (!if_indextoname(ifaddr->ifa_index, ifname)) { | 
| Lorenzo Colitti | ef6454d | 2016-02-16 21:42:16 +0900 | [diff] [blame] | 205 | SLOGD("Unknown ifindex %d in %s", ifaddr->ifa_index, msgtype); | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 206 | } | 
|  | 207 |  | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 208 | } else if (rta->rta_type == IFA_CACHEINFO) { | 
|  | 209 | // Address lifetime information. | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 210 | if (maybeLogDuplicateAttribute(cacheinfo, "IFA_CACHEINFO", msgtype)) | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 211 | continue; | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 212 |  | 
|  | 213 | if (RTA_PAYLOAD(rta) < sizeof(*cacheinfo)) { | 
| Mark Salyzyn | 80f63d4 | 2014-05-01 07:47:04 -0700 | [diff] [blame] | 214 | SLOGE("Short IFA_CACHEINFO (%zu vs. %zu bytes) in %s", | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 215 | RTA_PAYLOAD(rta), sizeof(cacheinfo), msgtype); | 
|  | 216 | continue; | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | cacheinfo = (struct ifa_cacheinfo *) RTA_DATA(rta); | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 220 | } | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 221 | } | 
|  | 222 |  | 
|  | 223 | if (addrstr[0] == '\0') { | 
|  | 224 | SLOGE("No IFA_ADDRESS in %s\n", msgtype); | 
|  | 225 | return false; | 
|  | 226 | } | 
|  | 227 |  | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 228 | // Fill in netlink event information. | 
| Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 229 | mAction = (type == RTM_NEWADDR) ? Action::kAddressUpdated : | 
|  | 230 | Action::kAddressRemoved; | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 231 | mSubsystem = strdup("net"); | 
| Lorenzo Colitti | ef6454d | 2016-02-16 21:42:16 +0900 | [diff] [blame] | 232 | asprintf(&mParams[0], "ADDRESS=%s/%d", addrstr, ifaddr->ifa_prefixlen); | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 233 | asprintf(&mParams[1], "INTERFACE=%s", ifname); | 
|  | 234 | asprintf(&mParams[2], "FLAGS=%u", ifaddr->ifa_flags); | 
|  | 235 | asprintf(&mParams[3], "SCOPE=%u", ifaddr->ifa_scope); | 
|  | 236 |  | 
|  | 237 | if (cacheinfo) { | 
|  | 238 | asprintf(&mParams[4], "PREFERRED=%u", cacheinfo->ifa_prefered); | 
|  | 239 | asprintf(&mParams[5], "VALID=%u", cacheinfo->ifa_valid); | 
|  | 240 | asprintf(&mParams[6], "CSTAMP=%u", cacheinfo->cstamp); | 
|  | 241 | asprintf(&mParams[7], "TSTAMP=%u", cacheinfo->tstamp); | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | return true; | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | /* | 
|  | 248 | * Parse a QLOG_NL_EVENT message. | 
|  | 249 | */ | 
|  | 250 | bool NetlinkEvent::parseUlogPacketMessage(const struct nlmsghdr *nh) { | 
|  | 251 | const char *devname; | 
|  | 252 | ulog_packet_msg_t *pm = (ulog_packet_msg_t *) NLMSG_DATA(nh); | 
|  | 253 | if (!checkRtNetlinkLength(nh, sizeof(*pm))) | 
|  | 254 | return false; | 
|  | 255 |  | 
|  | 256 | devname = pm->indev_name[0] ? pm->indev_name : pm->outdev_name; | 
|  | 257 | asprintf(&mParams[0], "ALERT_NAME=%s", pm->prefix); | 
|  | 258 | asprintf(&mParams[1], "INTERFACE=%s", devname); | 
|  | 259 | mSubsystem = strdup("qlog"); | 
| Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 260 | mAction = Action::kChange; | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 261 | return true; | 
|  | 262 | } | 
|  | 263 |  | 
| Lorenzo Colitti | e439ffc | 2017-10-03 18:44:11 +0900 | [diff] [blame] | 264 | static size_t nlAttrLen(const nlattr* nla) { | 
|  | 265 | return nla->nla_len - NLA_HDRLEN; | 
|  | 266 | } | 
|  | 267 |  | 
|  | 268 | static const uint8_t* nlAttrData(const nlattr* nla) { | 
|  | 269 | return reinterpret_cast<const uint8_t*>(nla) + NLA_HDRLEN; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | static uint32_t nlAttrU32(const nlattr* nla) { | 
|  | 273 | return *reinterpret_cast<const uint32_t*>(nlAttrData(nla)); | 
|  | 274 | } | 
|  | 275 |  | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 276 | /* | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 277 | * Parse a LOCAL_NFLOG_PACKET message. | 
|  | 278 | */ | 
|  | 279 | bool NetlinkEvent::parseNfPacketMessage(struct nlmsghdr *nh) { | 
|  | 280 | int uid = -1; | 
|  | 281 | int len = 0; | 
|  | 282 | char* raw = NULL; | 
|  | 283 |  | 
| Lorenzo Colitti | e439ffc | 2017-10-03 18:44:11 +0900 | [diff] [blame] | 284 | struct nlattr* uid_attr = findNlAttr(nh, sizeof(struct genlmsghdr), NFULA_UID); | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 285 | if (uid_attr) { | 
| Lorenzo Colitti | e439ffc | 2017-10-03 18:44:11 +0900 | [diff] [blame] | 286 | uid = ntohl(nlAttrU32(uid_attr)); | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 287 | } | 
|  | 288 |  | 
| Lorenzo Colitti | e439ffc | 2017-10-03 18:44:11 +0900 | [diff] [blame] | 289 | struct nlattr* payload = findNlAttr(nh, sizeof(struct genlmsghdr), NFULA_PAYLOAD); | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 290 | if (payload) { | 
|  | 291 | /* First 256 bytes is plenty */ | 
| Lorenzo Colitti | e439ffc | 2017-10-03 18:44:11 +0900 | [diff] [blame] | 292 | len = nlAttrLen(payload); | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 293 | if (len > 256) len = 256; | 
| Lorenzo Colitti | e439ffc | 2017-10-03 18:44:11 +0900 | [diff] [blame] | 294 | raw = (char*)nlAttrData(payload); | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 295 | } | 
|  | 296 |  | 
|  | 297 | char* hex = (char*) calloc(1, 5 + (len * 2)); | 
|  | 298 | strcpy(hex, "HEX="); | 
|  | 299 | for (int i = 0; i < len; i++) { | 
|  | 300 | hex[4 + (i * 2)] = "0123456789abcdef"[(raw[i] >> 4) & 0xf]; | 
|  | 301 | hex[5 + (i * 2)] = "0123456789abcdef"[raw[i] & 0xf]; | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | asprintf(&mParams[0], "UID=%d", uid); | 
|  | 305 | mParams[1] = hex; | 
|  | 306 | mSubsystem = strdup("strict"); | 
| Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 307 | mAction = Action::kChange; | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 308 | return true; | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | /* | 
| Lorenzo Colitti | d7ff7ea | 2014-06-11 17:37:12 +0900 | [diff] [blame] | 312 | * Parse a RTM_NEWROUTE or RTM_DELROUTE message. | 
|  | 313 | */ | 
|  | 314 | bool NetlinkEvent::parseRtMessage(const struct nlmsghdr *nh) { | 
|  | 315 | uint8_t type = nh->nlmsg_type; | 
|  | 316 | const char *msgname = rtMessageName(type); | 
|  | 317 |  | 
|  | 318 | // Sanity check. | 
|  | 319 | if (type != RTM_NEWROUTE && type != RTM_DELROUTE) { | 
|  | 320 | SLOGE("%s: incorrect message type %d (%s)\n", __func__, type, msgname); | 
|  | 321 | return false; | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | struct rtmsg *rtm = (struct rtmsg *) NLMSG_DATA(nh); | 
|  | 325 | if (!checkRtNetlinkLength(nh, sizeof(*rtm))) | 
|  | 326 | return false; | 
|  | 327 |  | 
|  | 328 | if (// Ignore static routes we've set up ourselves. | 
|  | 329 | (rtm->rtm_protocol != RTPROT_KERNEL && | 
|  | 330 | rtm->rtm_protocol != RTPROT_RA) || | 
|  | 331 | // We're only interested in global unicast routes. | 
|  | 332 | (rtm->rtm_scope != RT_SCOPE_UNIVERSE) || | 
|  | 333 | (rtm->rtm_type != RTN_UNICAST) || | 
|  | 334 | // We don't support source routing. | 
|  | 335 | (rtm->rtm_src_len != 0) || | 
|  | 336 | // Cloned routes aren't real routes. | 
|  | 337 | (rtm->rtm_flags & RTM_F_CLONED)) { | 
|  | 338 | return false; | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | int family = rtm->rtm_family; | 
|  | 342 | int prefixLength = rtm->rtm_dst_len; | 
|  | 343 |  | 
|  | 344 | // Currently we only support: destination, (one) next hop, ifindex. | 
|  | 345 | char dst[INET6_ADDRSTRLEN] = ""; | 
|  | 346 | char gw[INET6_ADDRSTRLEN] = ""; | 
|  | 347 | char dev[IFNAMSIZ] = ""; | 
|  | 348 |  | 
|  | 349 | size_t len = RTM_PAYLOAD(nh); | 
|  | 350 | struct rtattr *rta; | 
|  | 351 | for (rta = RTM_RTA(rtm); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) { | 
|  | 352 | switch (rta->rta_type) { | 
|  | 353 | case RTA_DST: | 
|  | 354 | if (maybeLogDuplicateAttribute(*dst, "RTA_DST", msgname)) | 
|  | 355 | continue; | 
|  | 356 | if (!inet_ntop(family, RTA_DATA(rta), dst, sizeof(dst))) | 
|  | 357 | return false; | 
|  | 358 | continue; | 
|  | 359 | case RTA_GATEWAY: | 
|  | 360 | if (maybeLogDuplicateAttribute(*gw, "RTA_GATEWAY", msgname)) | 
|  | 361 | continue; | 
|  | 362 | if (!inet_ntop(family, RTA_DATA(rta), gw, sizeof(gw))) | 
|  | 363 | return false; | 
|  | 364 | continue; | 
|  | 365 | case RTA_OIF: | 
|  | 366 | if (maybeLogDuplicateAttribute(*dev, "RTA_OIF", msgname)) | 
|  | 367 | continue; | 
|  | 368 | if (!if_indextoname(* (int *) RTA_DATA(rta), dev)) | 
|  | 369 | return false; | 
|  | 370 | default: | 
|  | 371 | continue; | 
|  | 372 | } | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | // If there's no RTA_DST attribute, then: | 
|  | 376 | // - If the prefix length is zero, it's the default route. | 
|  | 377 | // - If the prefix length is nonzero, there's something we don't understand. | 
|  | 378 | //   Ignore the event. | 
|  | 379 | if (!*dst && !prefixLength) { | 
|  | 380 | if (family == AF_INET) { | 
|  | 381 | strncpy(dst, "0.0.0.0", sizeof(dst)); | 
|  | 382 | } else if (family == AF_INET6) { | 
|  | 383 | strncpy(dst, "::", sizeof(dst)); | 
|  | 384 | } | 
|  | 385 | } | 
|  | 386 |  | 
|  | 387 | // A useful route must have a destination and at least either a gateway or | 
|  | 388 | // an interface. | 
|  | 389 | if (!*dst || (!*gw && !*dev)) | 
|  | 390 | return false; | 
|  | 391 |  | 
|  | 392 | // Fill in netlink event information. | 
| Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 393 | mAction = (type == RTM_NEWROUTE) ? Action::kRouteUpdated : | 
|  | 394 | Action::kRouteRemoved; | 
| Lorenzo Colitti | d7ff7ea | 2014-06-11 17:37:12 +0900 | [diff] [blame] | 395 | mSubsystem = strdup("net"); | 
|  | 396 | asprintf(&mParams[0], "ROUTE=%s/%d", dst, prefixLength); | 
|  | 397 | asprintf(&mParams[1], "GATEWAY=%s", (*gw) ? gw : ""); | 
|  | 398 | asprintf(&mParams[2], "INTERFACE=%s", (*dev) ? dev : ""); | 
|  | 399 |  | 
|  | 400 | return true; | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | /* | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 404 | * Parse a RTM_NEWNDUSEROPT message. | 
|  | 405 | */ | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 406 | bool NetlinkEvent::parseNdUserOptMessage(const struct nlmsghdr *nh) { | 
|  | 407 | struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(nh); | 
|  | 408 | if (!checkRtNetlinkLength(nh, sizeof(*msg))) | 
|  | 409 | return false; | 
|  | 410 |  | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 411 | // Check the length is valid. | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 412 | int len = NLMSG_PAYLOAD(nh, sizeof(*msg)); | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 413 | if (msg->nduseropt_opts_len > len) { | 
|  | 414 | SLOGE("RTM_NEWNDUSEROPT invalid length %d > %d\n", | 
|  | 415 | msg->nduseropt_opts_len, len); | 
|  | 416 | return false; | 
|  | 417 | } | 
|  | 418 | len = msg->nduseropt_opts_len; | 
|  | 419 |  | 
|  | 420 | // Check address family and packet type. | 
|  | 421 | if (msg->nduseropt_family != AF_INET6) { | 
|  | 422 | SLOGE("RTM_NEWNDUSEROPT message for unknown family %d\n", | 
|  | 423 | msg->nduseropt_family); | 
|  | 424 | return false; | 
|  | 425 | } | 
|  | 426 |  | 
|  | 427 | if (msg->nduseropt_icmp_type != ND_ROUTER_ADVERT || | 
|  | 428 | msg->nduseropt_icmp_code != 0) { | 
|  | 429 | SLOGE("RTM_NEWNDUSEROPT message for unknown ICMPv6 type/code %d/%d\n", | 
|  | 430 | msg->nduseropt_icmp_type, msg->nduseropt_icmp_code); | 
|  | 431 | return false; | 
|  | 432 | } | 
|  | 433 |  | 
|  | 434 | // Find the interface name. | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 435 | char ifname[IFNAMSIZ]; | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 436 | if (!if_indextoname(msg->nduseropt_ifindex, ifname)) { | 
|  | 437 | SLOGE("RTM_NEWNDUSEROPT on unknown ifindex %d\n", | 
|  | 438 | msg->nduseropt_ifindex); | 
|  | 439 | return false; | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | // The kernel sends a separate netlink message for each ND option in the RA. | 
|  | 443 | // So only parse the first ND option in the message. | 
|  | 444 | struct nd_opt_hdr *opthdr = (struct nd_opt_hdr *) (msg + 1); | 
|  | 445 |  | 
|  | 446 | // The length is in multiples of 8 octets. | 
|  | 447 | uint16_t optlen = opthdr->nd_opt_len; | 
|  | 448 | if (optlen * 8 > len) { | 
|  | 449 | SLOGE("Invalid option length %d > %d for ND option %d\n", | 
|  | 450 | optlen * 8, len, opthdr->nd_opt_type); | 
|  | 451 | return false; | 
|  | 452 | } | 
|  | 453 |  | 
|  | 454 | if (opthdr->nd_opt_type == ND_OPT_RDNSS) { | 
|  | 455 | // DNS Servers (RFC 6106). | 
|  | 456 | // Each address takes up 2*8 octets, and the header takes up 8 octets. | 
|  | 457 | // So for a valid option with one or more addresses, optlen must be | 
|  | 458 | // odd and greater than 1. | 
|  | 459 | if ((optlen < 3) || !(optlen & 0x1)) { | 
|  | 460 | SLOGE("Invalid optlen %d for RDNSS option\n", optlen); | 
|  | 461 | return false; | 
|  | 462 | } | 
| Erik Kline | ba48ff7 | 2015-06-17 15:53:29 +0900 | [diff] [blame] | 463 | const int numaddrs = (optlen - 1) / 2; | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 464 |  | 
|  | 465 | // Find the lifetime. | 
|  | 466 | struct nd_opt_rdnss *rndss_opt = (struct nd_opt_rdnss *) opthdr; | 
| Erik Kline | ba48ff7 | 2015-06-17 15:53:29 +0900 | [diff] [blame] | 467 | const uint32_t lifetime = ntohl(rndss_opt->nd_opt_rdnss_lifetime); | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 468 |  | 
|  | 469 | // Construct "SERVERS=<comma-separated string of DNS addresses>". | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 470 | static const char kServerTag[] = "SERVERS="; | 
| Erik Kline | cc45178 | 2015-07-28 17:31:19 +0900 | [diff] [blame] | 471 | static const size_t kTagLength = strlen(kServerTag); | 
| Erik Kline | ba48ff7 | 2015-06-17 15:53:29 +0900 | [diff] [blame] | 472 | // Reserve sufficient space for an IPv6 link-local address: all but the | 
|  | 473 | // last address are followed by ','; the last is followed by '\0'. | 
| Erik Kline | cc45178 | 2015-07-28 17:31:19 +0900 | [diff] [blame] | 474 | static const size_t kMaxSingleAddressLength = | 
| Erik Kline | ba48ff7 | 2015-06-17 15:53:29 +0900 | [diff] [blame] | 475 | INET6_ADDRSTRLEN + strlen("%") + IFNAMSIZ + strlen(","); | 
| Erik Kline | cc45178 | 2015-07-28 17:31:19 +0900 | [diff] [blame] | 476 | const size_t bufsize = kTagLength + numaddrs * kMaxSingleAddressLength; | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 477 | char *buf = (char *) malloc(bufsize); | 
|  | 478 | if (!buf) { | 
|  | 479 | SLOGE("RDNSS option: out of memory\n"); | 
|  | 480 | return false; | 
|  | 481 | } | 
|  | 482 | strcpy(buf, kServerTag); | 
| Erik Kline | cc45178 | 2015-07-28 17:31:19 +0900 | [diff] [blame] | 483 | size_t pos = kTagLength; | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 484 |  | 
|  | 485 | struct in6_addr *addrs = (struct in6_addr *) (rndss_opt + 1); | 
|  | 486 | for (int i = 0; i < numaddrs; i++) { | 
|  | 487 | if (i > 0) { | 
|  | 488 | buf[pos++] = ','; | 
|  | 489 | } | 
|  | 490 | inet_ntop(AF_INET6, addrs + i, buf + pos, bufsize - pos); | 
|  | 491 | pos += strlen(buf + pos); | 
| Erik Kline | ba48ff7 | 2015-06-17 15:53:29 +0900 | [diff] [blame] | 492 | if (IN6_IS_ADDR_LINKLOCAL(addrs + i)) { | 
|  | 493 | buf[pos++] = '%'; | 
|  | 494 | pos += strlcpy(buf + pos, ifname, bufsize - pos); | 
|  | 495 | } | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 496 | } | 
|  | 497 | buf[pos] = '\0'; | 
|  | 498 |  | 
| Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 499 | mAction = Action::kRdnss; | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 500 | mSubsystem = strdup("net"); | 
|  | 501 | asprintf(&mParams[0], "INTERFACE=%s", ifname); | 
|  | 502 | asprintf(&mParams[1], "LIFETIME=%u", lifetime); | 
|  | 503 | mParams[2] = buf; | 
|  | 504 | } else { | 
|  | 505 | SLOGD("Unknown ND option type %d\n", opthdr->nd_opt_type); | 
|  | 506 | return false; | 
|  | 507 | } | 
|  | 508 |  | 
|  | 509 | return true; | 
|  | 510 | } | 
|  | 511 |  | 
|  | 512 | /* | 
|  | 513 | * Parse a binary message from a NETLINK_ROUTE netlink socket. | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 514 | * | 
|  | 515 | * Note that this function can only parse one message, because the message's | 
|  | 516 | * content has to be stored in the class's member variables (mAction, | 
|  | 517 | * mSubsystem, etc.). Invalid or unrecognized messages are skipped, but if | 
|  | 518 | * there are multiple valid messages in the buffer, only the first one will be | 
|  | 519 | * returned. | 
|  | 520 | * | 
|  | 521 | * TODO: consider only ever looking at the first message. | 
| Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 522 | */ | 
|  | 523 | bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) { | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 524 | struct nlmsghdr *nh; | 
| Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 525 |  | 
| Lorenzo Colitti | 9683456 | 2013-08-17 03:40:31 +0900 | [diff] [blame] | 526 | for (nh = (struct nlmsghdr *) buffer; | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 527 | NLMSG_OK(nh, (unsigned) size) && (nh->nlmsg_type != NLMSG_DONE); | 
| Lorenzo Colitti | 9683456 | 2013-08-17 03:40:31 +0900 | [diff] [blame] | 528 | nh = NLMSG_NEXT(nh, size)) { | 
| JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 529 |  | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 530 | if (!rtMessageName(nh->nlmsg_type)) { | 
|  | 531 | SLOGD("Unexpected netlink message type %d\n", nh->nlmsg_type); | 
|  | 532 | continue; | 
|  | 533 | } | 
|  | 534 |  | 
| Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 535 | if (nh->nlmsg_type == RTM_NEWLINK) { | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 536 | if (parseIfInfoMessage(nh)) | 
|  | 537 | return true; | 
| JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 538 |  | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 539 | } else if (nh->nlmsg_type == LOCAL_QLOG_NL_EVENT) { | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 540 | if (parseUlogPacketMessage(nh)) | 
|  | 541 | return true; | 
| JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 542 |  | 
| Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 543 | } else if (nh->nlmsg_type == RTM_NEWADDR || | 
|  | 544 | nh->nlmsg_type == RTM_DELADDR) { | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 545 | if (parseIfAddrMessage(nh)) | 
|  | 546 | return true; | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 547 |  | 
| Lorenzo Colitti | d7ff7ea | 2014-06-11 17:37:12 +0900 | [diff] [blame] | 548 | } else if (nh->nlmsg_type == RTM_NEWROUTE || | 
|  | 549 | nh->nlmsg_type == RTM_DELROUTE) { | 
|  | 550 | if (parseRtMessage(nh)) | 
|  | 551 | return true; | 
|  | 552 |  | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 553 | } else if (nh->nlmsg_type == RTM_NEWNDUSEROPT) { | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 554 | if (parseNdUserOptMessage(nh)) | 
|  | 555 | return true; | 
| Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 556 |  | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 557 | } else if (nh->nlmsg_type == LOCAL_NFLOG_PACKET) { | 
|  | 558 | if (parseNfPacketMessage(nh)) | 
|  | 559 | return true; | 
|  | 560 |  | 
| JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 561 | } | 
| Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 562 | } | 
|  | 563 |  | 
| Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 564 | return false; | 
| Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 565 | } | 
|  | 566 |  | 
| David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 567 | /* If the string between 'str' and 'end' begins with 'prefixlen' characters | 
|  | 568 | * from the 'prefix' array, then return 'str + prefixlen', otherwise return | 
|  | 569 | * NULL. | 
|  | 570 | */ | 
|  | 571 | static const char* | 
|  | 572 | has_prefix(const char* str, const char* end, const char* prefix, size_t prefixlen) | 
|  | 573 | { | 
| Yunlian Jiang | 33f6717 | 2017-02-07 15:39:25 -0800 | [diff] [blame] | 574 | if ((end - str) >= (ptrdiff_t)prefixlen && | 
|  | 575 | (prefixlen == 0 || !memcmp(str, prefix, prefixlen))) { | 
| David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 576 | return str + prefixlen; | 
| Yunlian Jiang | 33f6717 | 2017-02-07 15:39:25 -0800 | [diff] [blame] | 577 | } else { | 
| David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 578 | return NULL; | 
| Yunlian Jiang | 33f6717 | 2017-02-07 15:39:25 -0800 | [diff] [blame] | 579 | } | 
| David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 580 | } | 
|  | 581 |  | 
|  | 582 | /* Same as strlen(x) for constant string literals ONLY */ | 
|  | 583 | #define CONST_STRLEN(x)  (sizeof(x)-1) | 
|  | 584 |  | 
|  | 585 | /* Convenience macro to call has_prefix with a constant string literal  */ | 
|  | 586 | #define HAS_CONST_PREFIX(str,end,prefix)  has_prefix((str),(end),prefix,CONST_STRLEN(prefix)) | 
|  | 587 |  | 
|  | 588 |  | 
| Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 589 | /* | 
|  | 590 | * Parse an ASCII-formatted message from a NETLINK_KOBJECT_UEVENT | 
|  | 591 | * netlink socket. | 
|  | 592 | */ | 
|  | 593 | bool NetlinkEvent::parseAsciiNetlinkMessage(char *buffer, int size) { | 
| Mike J. Chen | 17260b1 | 2011-06-23 15:00:30 -0700 | [diff] [blame] | 594 | const char *s = buffer; | 
|  | 595 | const char *end; | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 596 | int param_idx = 0; | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 597 | int first = 1; | 
|  | 598 |  | 
| David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 599 | if (size == 0) | 
|  | 600 | return false; | 
|  | 601 |  | 
|  | 602 | /* Ensure the buffer is zero-terminated, the code below depends on this */ | 
|  | 603 | buffer[size-1] = '\0'; | 
|  | 604 |  | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 605 | end = s + size; | 
|  | 606 | while (s < end) { | 
|  | 607 | if (first) { | 
| David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 608 | const char *p; | 
|  | 609 | /* buffer is 0-terminated, no need to check p < end */ | 
|  | 610 | for (p = s; *p != '@'; p++) { | 
|  | 611 | if (!*p) { /* no '@', should not happen */ | 
|  | 612 | return false; | 
|  | 613 | } | 
|  | 614 | } | 
|  | 615 | mPath = strdup(p+1); | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 616 | first = 0; | 
|  | 617 | } else { | 
| David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 618 | const char* a; | 
|  | 619 | if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != NULL) { | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 620 | if (!strcmp(a, "add")) | 
| Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 621 | mAction = Action::kAdd; | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 622 | else if (!strcmp(a, "remove")) | 
| Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 623 | mAction = Action::kRemove; | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 624 | else if (!strcmp(a, "change")) | 
| Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 625 | mAction = Action::kChange; | 
| David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 626 | } else if ((a = HAS_CONST_PREFIX(s, end, "SEQNUM=")) != NULL) { | 
|  | 627 | mSeq = atoi(a); | 
|  | 628 | } else if ((a = HAS_CONST_PREFIX(s, end, "SUBSYSTEM=")) != NULL) { | 
|  | 629 | mSubsystem = strdup(a); | 
|  | 630 | } else if (param_idx < NL_PARAMS_MAX) { | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 631 | mParams[param_idx++] = strdup(s); | 
| David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 632 | } | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 633 | } | 
| David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 634 | s += strlen(s) + 1; | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 635 | } | 
|  | 636 | return true; | 
|  | 637 | } | 
|  | 638 |  | 
| Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 639 | bool NetlinkEvent::decode(char *buffer, int size, int format) { | 
| Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 640 | if (format == NetlinkListener::NETLINK_FORMAT_BINARY | 
|  | 641 | || format == NetlinkListener::NETLINK_FORMAT_BINARY_UNICAST) { | 
| Mike J. Chen | 17260b1 | 2011-06-23 15:00:30 -0700 | [diff] [blame] | 642 | return parseBinaryNetlinkMessage(buffer, size); | 
|  | 643 | } else { | 
|  | 644 | return parseAsciiNetlinkMessage(buffer, size); | 
|  | 645 | } | 
| Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 646 | } | 
|  | 647 |  | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 648 | const char *NetlinkEvent::findParam(const char *paramName) { | 
| Chih-Wei Huang | 80ec37a | 2010-07-14 14:00:41 +0800 | [diff] [blame] | 649 | size_t len = strlen(paramName); | 
| David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 650 | for (int i = 0; i < NL_PARAMS_MAX && mParams[i] != NULL; ++i) { | 
| Chih-Wei Huang | 80ec37a | 2010-07-14 14:00:41 +0800 | [diff] [blame] | 651 | const char *ptr = mParams[i] + len; | 
|  | 652 | if (!strncmp(mParams[i], paramName, len) && *ptr == '=') | 
|  | 653 | return ++ptr; | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 654 | } | 
|  | 655 |  | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 656 | SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName); | 
| San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 657 | return NULL; | 
|  | 658 | } | 
| Lorenzo Colitti | e439ffc | 2017-10-03 18:44:11 +0900 | [diff] [blame] | 659 |  | 
|  | 660 | nlattr* NetlinkEvent::findNlAttr(const nlmsghdr* nh, size_t hdrlen, uint16_t attr) { | 
|  | 661 | if (nh == nullptr || NLMSG_HDRLEN + NLMSG_ALIGN(hdrlen) > SSIZE_MAX) { | 
|  | 662 | return nullptr; | 
|  | 663 | } | 
|  | 664 |  | 
|  | 665 | // Skip header, padding, and family header. | 
|  | 666 | const ssize_t NLA_START = NLMSG_HDRLEN + NLMSG_ALIGN(hdrlen); | 
|  | 667 | ssize_t left = nh->nlmsg_len - NLA_START; | 
|  | 668 | uint8_t* hdr = ((uint8_t*)nh) + NLA_START; | 
|  | 669 |  | 
|  | 670 | while (left >= NLA_HDRLEN) { | 
|  | 671 | nlattr* nla = (nlattr*)hdr; | 
|  | 672 | if (nla->nla_type == attr) { | 
|  | 673 | return nla; | 
|  | 674 | } | 
|  | 675 |  | 
|  | 676 | hdr += NLA_ALIGN(nla->nla_len); | 
|  | 677 | left -= NLA_ALIGN(nla->nla_len); | 
|  | 678 | } | 
|  | 679 |  | 
|  | 680 | return nullptr; | 
|  | 681 | } |