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