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 | */ |
| 16 | #include <stdlib.h> |
San Mehat | 3d40729 | 2009-05-07 08:49:30 -0700 | [diff] [blame] | 17 | #include <string.h> |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 18 | |
| 19 | #define LOG_TAG "NetlinkEvent" |
| 20 | #include <cutils/log.h> |
| 21 | |
| 22 | #include <sysutils/NetlinkEvent.h> |
| 23 | |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/socket.h> |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 26 | #include <netinet/in.h> |
Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 27 | #include <netinet/icmp6.h> |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 28 | #include <arpa/inet.h> |
| 29 | #include <net/if.h> |
| 30 | |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 31 | #include <linux/if.h> |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 32 | #include <linux/if_addr.h> |
| 33 | #include <linux/if_link.h> |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 34 | #include <linux/netfilter/nfnetlink.h> |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 35 | #include <linux/netfilter/nfnetlink_log.h> |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 36 | #include <linux/netfilter_ipv4/ipt_ULOG.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 | |
| 42 | #include <linux/netlink.h> |
| 43 | #include <linux/rtnetlink.h> |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 44 | |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 45 | #include <netlink/attr.h> |
| 46 | #include <netlink/genl/genl.h> |
| 47 | #include <netlink/handlers.h> |
| 48 | #include <netlink/msg.h> |
| 49 | |
Jeff Sharkey | 3183729 | 2015-03-16 10:52:03 -0700 | [diff] [blame^] | 50 | // STOPSHIP: remove these deprecated constants once we have updated prebuilts |
| 51 | const int NetlinkEvent::NlActionUnknown = static_cast<int>(Action::kUnknown); |
| 52 | const int NetlinkEvent::NlActionAdd = static_cast<int>(Action::kAdd); |
| 53 | const int NetlinkEvent::NlActionRemove = static_cast<int>(Action::kRemove); |
| 54 | const int NetlinkEvent::NlActionChange = static_cast<int>(Action::kChange); |
| 55 | const int NetlinkEvent::NlActionLinkDown = static_cast<int>(Action::kLinkDown); |
| 56 | const int NetlinkEvent::NlActionLinkUp = static_cast<int>(Action::kLinkUp); |
| 57 | const int NetlinkEvent::NlActionAddressUpdated = static_cast<int>(Action::kAddressUpdated); |
| 58 | const int NetlinkEvent::NlActionAddressRemoved = static_cast<int>(Action::kAddressRemoved); |
| 59 | const int NetlinkEvent::NlActionRdnss = static_cast<int>(Action::kRdnss); |
| 60 | const int NetlinkEvent::NlActionRouteUpdated = static_cast<int>(Action::kRouteUpdated); |
| 61 | const int NetlinkEvent::NlActionRouteRemoved = static_cast<int>(Action::kRouteRemoved); |
| 62 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 63 | NetlinkEvent::NetlinkEvent() { |
Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 64 | mAction = Action::kUnknown; |
San Mehat | ebfe3db | 2009-10-10 17:35:13 -0700 | [diff] [blame] | 65 | memset(mParams, 0, sizeof(mParams)); |
| 66 | mPath = NULL; |
| 67 | mSubsystem = NULL; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | NetlinkEvent::~NetlinkEvent() { |
| 71 | int i; |
| 72 | if (mPath) |
| 73 | free(mPath); |
| 74 | if (mSubsystem) |
| 75 | free(mSubsystem); |
| 76 | for (i = 0; i < NL_PARAMS_MAX; i++) { |
| 77 | if (!mParams[i]) |
| 78 | break; |
| 79 | free(mParams[i]); |
| 80 | } |
| 81 | } |
| 82 | |
San Mehat | d674413 | 2009-12-24 07:17:09 -0800 | [diff] [blame] | 83 | void NetlinkEvent::dump() { |
| 84 | int i; |
| 85 | |
| 86 | for (i = 0; i < NL_PARAMS_MAX; i++) { |
| 87 | if (!mParams[i]) |
| 88 | break; |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 89 | SLOGD("NL param '%s'\n", mParams[i]); |
San Mehat | d674413 | 2009-12-24 07:17:09 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 93 | /* |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 94 | * Returns the message name for a message in the NETLINK_ROUTE family, or NULL |
| 95 | * if parsing that message is not supported. |
| 96 | */ |
| 97 | static const char *rtMessageName(int type) { |
| 98 | #define NL_EVENT_RTM_NAME(rtm) case rtm: return #rtm; |
| 99 | switch (type) { |
| 100 | NL_EVENT_RTM_NAME(RTM_NEWLINK); |
| 101 | NL_EVENT_RTM_NAME(RTM_DELLINK); |
| 102 | NL_EVENT_RTM_NAME(RTM_NEWADDR); |
| 103 | NL_EVENT_RTM_NAME(RTM_DELADDR); |
| 104 | NL_EVENT_RTM_NAME(RTM_NEWROUTE); |
| 105 | NL_EVENT_RTM_NAME(RTM_DELROUTE); |
| 106 | NL_EVENT_RTM_NAME(RTM_NEWNDUSEROPT); |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 107 | NL_EVENT_RTM_NAME(LOCAL_QLOG_NL_EVENT); |
| 108 | NL_EVENT_RTM_NAME(LOCAL_NFLOG_PACKET); |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 109 | default: |
| 110 | return NULL; |
| 111 | } |
| 112 | #undef NL_EVENT_RTM_NAME |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Checks that a binary NETLINK_ROUTE message is long enough for a payload of |
| 117 | * size bytes. |
| 118 | */ |
| 119 | static bool checkRtNetlinkLength(const struct nlmsghdr *nh, size_t size) { |
| 120 | if (nh->nlmsg_len < NLMSG_LENGTH(size)) { |
| 121 | SLOGE("Got a short %s message\n", rtMessageName(nh->nlmsg_type)); |
| 122 | return false; |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Utility function to log errors. |
| 129 | */ |
| 130 | static bool maybeLogDuplicateAttribute(bool isDup, |
| 131 | const char *attributeName, |
| 132 | const char *messageName) { |
| 133 | if (isDup) { |
| 134 | SLOGE("Multiple %s attributes in %s, ignoring\n", attributeName, messageName); |
| 135 | return true; |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Parse a RTM_NEWLINK message. |
| 142 | */ |
| 143 | bool NetlinkEvent::parseIfInfoMessage(const struct nlmsghdr *nh) { |
| 144 | struct ifinfomsg *ifi = (struct ifinfomsg *) NLMSG_DATA(nh); |
| 145 | if (!checkRtNetlinkLength(nh, sizeof(*ifi))) |
| 146 | return false; |
| 147 | |
| 148 | if ((ifi->ifi_flags & IFF_LOOPBACK) != 0) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | int len = IFLA_PAYLOAD(nh); |
| 153 | struct rtattr *rta; |
| 154 | for (rta = IFLA_RTA(ifi); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) { |
| 155 | switch(rta->rta_type) { |
| 156 | case IFLA_IFNAME: |
| 157 | asprintf(&mParams[0], "INTERFACE=%s", (char *) RTA_DATA(rta)); |
Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 158 | mAction = (ifi->ifi_flags & IFF_LOWER_UP) ? Action::kLinkUp : |
| 159 | Action::kLinkDown; |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 160 | mSubsystem = strdup("net"); |
| 161 | return true; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | /* |
Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 169 | * Parse a RTM_NEWADDR or RTM_DELADDR message. |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 170 | */ |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 171 | bool NetlinkEvent::parseIfAddrMessage(const struct nlmsghdr *nh) { |
| 172 | struct ifaddrmsg *ifaddr = (struct ifaddrmsg *) NLMSG_DATA(nh); |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 173 | struct ifa_cacheinfo *cacheinfo = NULL; |
| 174 | char addrstr[INET6_ADDRSTRLEN] = ""; |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 175 | char ifname[IFNAMSIZ]; |
| 176 | |
| 177 | if (!checkRtNetlinkLength(nh, sizeof(*ifaddr))) |
| 178 | return false; |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 179 | |
| 180 | // Sanity check. |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 181 | int type = nh->nlmsg_type; |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 182 | if (type != RTM_NEWADDR && type != RTM_DELADDR) { |
| 183 | SLOGE("parseIfAddrMessage on incorrect message type 0x%x\n", type); |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | // For log messages. |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 188 | const char *msgtype = rtMessageName(type); |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 189 | |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 190 | struct rtattr *rta; |
| 191 | int len = IFA_PAYLOAD(nh); |
| 192 | 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] | 193 | if (rta->rta_type == IFA_ADDRESS) { |
| 194 | // Only look at the first address, because we only support notifying |
| 195 | // one change at a time. |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 196 | if (maybeLogDuplicateAttribute(*addrstr != '\0', "IFA_ADDRESS", msgtype)) |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 197 | continue; |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 198 | |
| 199 | // Convert the IP address to a string. |
| 200 | if (ifaddr->ifa_family == AF_INET) { |
| 201 | struct in_addr *addr4 = (struct in_addr *) RTA_DATA(rta); |
| 202 | if (RTA_PAYLOAD(rta) < sizeof(*addr4)) { |
Mark Salyzyn | 80f63d4 | 2014-05-01 07:47:04 -0700 | [diff] [blame] | 203 | SLOGE("Short IPv4 address (%zu bytes) in %s", |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 204 | RTA_PAYLOAD(rta), msgtype); |
| 205 | continue; |
| 206 | } |
| 207 | inet_ntop(AF_INET, addr4, addrstr, sizeof(addrstr)); |
| 208 | } else if (ifaddr->ifa_family == AF_INET6) { |
| 209 | struct in6_addr *addr6 = (struct in6_addr *) RTA_DATA(rta); |
| 210 | if (RTA_PAYLOAD(rta) < sizeof(*addr6)) { |
Mark Salyzyn | 80f63d4 | 2014-05-01 07:47:04 -0700 | [diff] [blame] | 211 | SLOGE("Short IPv6 address (%zu bytes) in %s", |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 212 | RTA_PAYLOAD(rta), msgtype); |
| 213 | continue; |
| 214 | } |
| 215 | inet_ntop(AF_INET6, addr6, addrstr, sizeof(addrstr)); |
| 216 | } else { |
| 217 | SLOGE("Unknown address family %d\n", ifaddr->ifa_family); |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | // Find the interface name. |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 222 | if (!if_indextoname(ifaddr->ifa_index, ifname)) { |
| 223 | SLOGE("Unknown ifindex %d in %s", ifaddr->ifa_index, msgtype); |
| 224 | return false; |
| 225 | } |
| 226 | |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 227 | } else if (rta->rta_type == IFA_CACHEINFO) { |
| 228 | // Address lifetime information. |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 229 | if (maybeLogDuplicateAttribute(cacheinfo, "IFA_CACHEINFO", msgtype)) |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 230 | continue; |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 231 | |
| 232 | if (RTA_PAYLOAD(rta) < sizeof(*cacheinfo)) { |
Mark Salyzyn | 80f63d4 | 2014-05-01 07:47:04 -0700 | [diff] [blame] | 233 | SLOGE("Short IFA_CACHEINFO (%zu vs. %zu bytes) in %s", |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 234 | RTA_PAYLOAD(rta), sizeof(cacheinfo), msgtype); |
| 235 | continue; |
| 236 | } |
| 237 | |
| 238 | cacheinfo = (struct ifa_cacheinfo *) RTA_DATA(rta); |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 239 | } |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | if (addrstr[0] == '\0') { |
| 243 | SLOGE("No IFA_ADDRESS in %s\n", msgtype); |
| 244 | return false; |
| 245 | } |
| 246 | |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 247 | // Fill in netlink event information. |
Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 248 | mAction = (type == RTM_NEWADDR) ? Action::kAddressUpdated : |
| 249 | Action::kAddressRemoved; |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 250 | mSubsystem = strdup("net"); |
| 251 | asprintf(&mParams[0], "ADDRESS=%s/%d", addrstr, |
| 252 | ifaddr->ifa_prefixlen); |
| 253 | asprintf(&mParams[1], "INTERFACE=%s", ifname); |
| 254 | asprintf(&mParams[2], "FLAGS=%u", ifaddr->ifa_flags); |
| 255 | asprintf(&mParams[3], "SCOPE=%u", ifaddr->ifa_scope); |
| 256 | |
| 257 | if (cacheinfo) { |
| 258 | asprintf(&mParams[4], "PREFERRED=%u", cacheinfo->ifa_prefered); |
| 259 | asprintf(&mParams[5], "VALID=%u", cacheinfo->ifa_valid); |
| 260 | asprintf(&mParams[6], "CSTAMP=%u", cacheinfo->cstamp); |
| 261 | asprintf(&mParams[7], "TSTAMP=%u", cacheinfo->tstamp); |
| 262 | } |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Parse a QLOG_NL_EVENT message. |
| 269 | */ |
| 270 | bool NetlinkEvent::parseUlogPacketMessage(const struct nlmsghdr *nh) { |
| 271 | const char *devname; |
| 272 | ulog_packet_msg_t *pm = (ulog_packet_msg_t *) NLMSG_DATA(nh); |
| 273 | if (!checkRtNetlinkLength(nh, sizeof(*pm))) |
| 274 | return false; |
| 275 | |
| 276 | devname = pm->indev_name[0] ? pm->indev_name : pm->outdev_name; |
| 277 | asprintf(&mParams[0], "ALERT_NAME=%s", pm->prefix); |
| 278 | asprintf(&mParams[1], "INTERFACE=%s", devname); |
| 279 | mSubsystem = strdup("qlog"); |
Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 280 | mAction = Action::kChange; |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 281 | return true; |
| 282 | } |
| 283 | |
| 284 | /* |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 285 | * Parse a LOCAL_NFLOG_PACKET message. |
| 286 | */ |
| 287 | bool NetlinkEvent::parseNfPacketMessage(struct nlmsghdr *nh) { |
| 288 | int uid = -1; |
| 289 | int len = 0; |
| 290 | char* raw = NULL; |
| 291 | |
| 292 | struct nlattr *uid_attr = nlmsg_find_attr(nh, sizeof(struct genlmsghdr), NFULA_UID); |
| 293 | if (uid_attr) { |
| 294 | uid = ntohl(nla_get_u32(uid_attr)); |
| 295 | } |
| 296 | |
| 297 | struct nlattr *payload = nlmsg_find_attr(nh, sizeof(struct genlmsghdr), NFULA_PAYLOAD); |
| 298 | if (payload) { |
| 299 | /* First 256 bytes is plenty */ |
| 300 | len = nla_len(payload); |
| 301 | if (len > 256) len = 256; |
| 302 | raw = (char*) nla_data(payload); |
| 303 | } |
| 304 | |
| 305 | char* hex = (char*) calloc(1, 5 + (len * 2)); |
| 306 | strcpy(hex, "HEX="); |
| 307 | for (int i = 0; i < len; i++) { |
| 308 | hex[4 + (i * 2)] = "0123456789abcdef"[(raw[i] >> 4) & 0xf]; |
| 309 | hex[5 + (i * 2)] = "0123456789abcdef"[raw[i] & 0xf]; |
| 310 | } |
| 311 | |
| 312 | asprintf(&mParams[0], "UID=%d", uid); |
| 313 | mParams[1] = hex; |
| 314 | mSubsystem = strdup("strict"); |
Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 315 | mAction = Action::kChange; |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 316 | return true; |
| 317 | } |
| 318 | |
| 319 | /* |
Lorenzo Colitti | d7ff7ea | 2014-06-11 17:37:12 +0900 | [diff] [blame] | 320 | * Parse a RTM_NEWROUTE or RTM_DELROUTE message. |
| 321 | */ |
| 322 | bool NetlinkEvent::parseRtMessage(const struct nlmsghdr *nh) { |
| 323 | uint8_t type = nh->nlmsg_type; |
| 324 | const char *msgname = rtMessageName(type); |
| 325 | |
| 326 | // Sanity check. |
| 327 | if (type != RTM_NEWROUTE && type != RTM_DELROUTE) { |
| 328 | SLOGE("%s: incorrect message type %d (%s)\n", __func__, type, msgname); |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | struct rtmsg *rtm = (struct rtmsg *) NLMSG_DATA(nh); |
| 333 | if (!checkRtNetlinkLength(nh, sizeof(*rtm))) |
| 334 | return false; |
| 335 | |
| 336 | if (// Ignore static routes we've set up ourselves. |
| 337 | (rtm->rtm_protocol != RTPROT_KERNEL && |
| 338 | rtm->rtm_protocol != RTPROT_RA) || |
| 339 | // We're only interested in global unicast routes. |
| 340 | (rtm->rtm_scope != RT_SCOPE_UNIVERSE) || |
| 341 | (rtm->rtm_type != RTN_UNICAST) || |
| 342 | // We don't support source routing. |
| 343 | (rtm->rtm_src_len != 0) || |
| 344 | // Cloned routes aren't real routes. |
| 345 | (rtm->rtm_flags & RTM_F_CLONED)) { |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | int family = rtm->rtm_family; |
| 350 | int prefixLength = rtm->rtm_dst_len; |
| 351 | |
| 352 | // Currently we only support: destination, (one) next hop, ifindex. |
| 353 | char dst[INET6_ADDRSTRLEN] = ""; |
| 354 | char gw[INET6_ADDRSTRLEN] = ""; |
| 355 | char dev[IFNAMSIZ] = ""; |
| 356 | |
| 357 | size_t len = RTM_PAYLOAD(nh); |
| 358 | struct rtattr *rta; |
| 359 | for (rta = RTM_RTA(rtm); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) { |
| 360 | switch (rta->rta_type) { |
| 361 | case RTA_DST: |
| 362 | if (maybeLogDuplicateAttribute(*dst, "RTA_DST", msgname)) |
| 363 | continue; |
| 364 | if (!inet_ntop(family, RTA_DATA(rta), dst, sizeof(dst))) |
| 365 | return false; |
| 366 | continue; |
| 367 | case RTA_GATEWAY: |
| 368 | if (maybeLogDuplicateAttribute(*gw, "RTA_GATEWAY", msgname)) |
| 369 | continue; |
| 370 | if (!inet_ntop(family, RTA_DATA(rta), gw, sizeof(gw))) |
| 371 | return false; |
| 372 | continue; |
| 373 | case RTA_OIF: |
| 374 | if (maybeLogDuplicateAttribute(*dev, "RTA_OIF", msgname)) |
| 375 | continue; |
| 376 | if (!if_indextoname(* (int *) RTA_DATA(rta), dev)) |
| 377 | return false; |
| 378 | default: |
| 379 | continue; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // If there's no RTA_DST attribute, then: |
| 384 | // - If the prefix length is zero, it's the default route. |
| 385 | // - If the prefix length is nonzero, there's something we don't understand. |
| 386 | // Ignore the event. |
| 387 | if (!*dst && !prefixLength) { |
| 388 | if (family == AF_INET) { |
| 389 | strncpy(dst, "0.0.0.0", sizeof(dst)); |
| 390 | } else if (family == AF_INET6) { |
| 391 | strncpy(dst, "::", sizeof(dst)); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // A useful route must have a destination and at least either a gateway or |
| 396 | // an interface. |
| 397 | if (!*dst || (!*gw && !*dev)) |
| 398 | return false; |
| 399 | |
| 400 | // Fill in netlink event information. |
Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 401 | mAction = (type == RTM_NEWROUTE) ? Action::kRouteUpdated : |
| 402 | Action::kRouteRemoved; |
Lorenzo Colitti | d7ff7ea | 2014-06-11 17:37:12 +0900 | [diff] [blame] | 403 | mSubsystem = strdup("net"); |
| 404 | asprintf(&mParams[0], "ROUTE=%s/%d", dst, prefixLength); |
| 405 | asprintf(&mParams[1], "GATEWAY=%s", (*gw) ? gw : ""); |
| 406 | asprintf(&mParams[2], "INTERFACE=%s", (*dev) ? dev : ""); |
| 407 | |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | /* |
Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 412 | * Parse a RTM_NEWNDUSEROPT message. |
| 413 | */ |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 414 | bool NetlinkEvent::parseNdUserOptMessage(const struct nlmsghdr *nh) { |
| 415 | struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(nh); |
| 416 | if (!checkRtNetlinkLength(nh, sizeof(*msg))) |
| 417 | return false; |
| 418 | |
Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 419 | // Check the length is valid. |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 420 | int len = NLMSG_PAYLOAD(nh, sizeof(*msg)); |
Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 421 | if (msg->nduseropt_opts_len > len) { |
| 422 | SLOGE("RTM_NEWNDUSEROPT invalid length %d > %d\n", |
| 423 | msg->nduseropt_opts_len, len); |
| 424 | return false; |
| 425 | } |
| 426 | len = msg->nduseropt_opts_len; |
| 427 | |
| 428 | // Check address family and packet type. |
| 429 | if (msg->nduseropt_family != AF_INET6) { |
| 430 | SLOGE("RTM_NEWNDUSEROPT message for unknown family %d\n", |
| 431 | msg->nduseropt_family); |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | if (msg->nduseropt_icmp_type != ND_ROUTER_ADVERT || |
| 436 | msg->nduseropt_icmp_code != 0) { |
| 437 | SLOGE("RTM_NEWNDUSEROPT message for unknown ICMPv6 type/code %d/%d\n", |
| 438 | msg->nduseropt_icmp_type, msg->nduseropt_icmp_code); |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | // Find the interface name. |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 443 | char ifname[IFNAMSIZ]; |
Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 444 | if (!if_indextoname(msg->nduseropt_ifindex, ifname)) { |
| 445 | SLOGE("RTM_NEWNDUSEROPT on unknown ifindex %d\n", |
| 446 | msg->nduseropt_ifindex); |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | // The kernel sends a separate netlink message for each ND option in the RA. |
| 451 | // So only parse the first ND option in the message. |
| 452 | struct nd_opt_hdr *opthdr = (struct nd_opt_hdr *) (msg + 1); |
| 453 | |
| 454 | // The length is in multiples of 8 octets. |
| 455 | uint16_t optlen = opthdr->nd_opt_len; |
| 456 | if (optlen * 8 > len) { |
| 457 | SLOGE("Invalid option length %d > %d for ND option %d\n", |
| 458 | optlen * 8, len, opthdr->nd_opt_type); |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | if (opthdr->nd_opt_type == ND_OPT_RDNSS) { |
| 463 | // DNS Servers (RFC 6106). |
| 464 | // Each address takes up 2*8 octets, and the header takes up 8 octets. |
| 465 | // So for a valid option with one or more addresses, optlen must be |
| 466 | // odd and greater than 1. |
| 467 | if ((optlen < 3) || !(optlen & 0x1)) { |
| 468 | SLOGE("Invalid optlen %d for RDNSS option\n", optlen); |
| 469 | return false; |
| 470 | } |
| 471 | int numaddrs = (optlen - 1) / 2; |
| 472 | |
| 473 | // Find the lifetime. |
| 474 | struct nd_opt_rdnss *rndss_opt = (struct nd_opt_rdnss *) opthdr; |
| 475 | uint32_t lifetime = ntohl(rndss_opt->nd_opt_rdnss_lifetime); |
| 476 | |
| 477 | // Construct "SERVERS=<comma-separated string of DNS addresses>". |
| 478 | // Reserve (INET6_ADDRSTRLEN + 1) chars for each address: all but the |
| 479 | // the last address are followed by ','; the last is followed by '\0'. |
| 480 | static const char kServerTag[] = "SERVERS="; |
| 481 | static const int kTagLength = sizeof(kServerTag) - 1; |
| 482 | int bufsize = kTagLength + numaddrs * (INET6_ADDRSTRLEN + 1); |
| 483 | char *buf = (char *) malloc(bufsize); |
| 484 | if (!buf) { |
| 485 | SLOGE("RDNSS option: out of memory\n"); |
| 486 | return false; |
| 487 | } |
| 488 | strcpy(buf, kServerTag); |
| 489 | int pos = kTagLength; |
| 490 | |
| 491 | struct in6_addr *addrs = (struct in6_addr *) (rndss_opt + 1); |
| 492 | for (int i = 0; i < numaddrs; i++) { |
| 493 | if (i > 0) { |
| 494 | buf[pos++] = ','; |
| 495 | } |
| 496 | inet_ntop(AF_INET6, addrs + i, buf + pos, bufsize - pos); |
| 497 | pos += strlen(buf + pos); |
| 498 | } |
| 499 | buf[pos] = '\0'; |
| 500 | |
Jeff Sharkey | e4f3940 | 2015-03-13 13:27:33 -0700 | [diff] [blame] | 501 | mAction = Action::kRdnss; |
Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 502 | mSubsystem = strdup("net"); |
| 503 | asprintf(&mParams[0], "INTERFACE=%s", ifname); |
| 504 | asprintf(&mParams[1], "LIFETIME=%u", lifetime); |
| 505 | mParams[2] = buf; |
| 506 | } else { |
| 507 | SLOGD("Unknown ND option type %d\n", opthdr->nd_opt_type); |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | return true; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Parse a binary message from a NETLINK_ROUTE netlink socket. |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 516 | * |
| 517 | * Note that this function can only parse one message, because the message's |
| 518 | * content has to be stored in the class's member variables (mAction, |
| 519 | * mSubsystem, etc.). Invalid or unrecognized messages are skipped, but if |
| 520 | * there are multiple valid messages in the buffer, only the first one will be |
| 521 | * returned. |
| 522 | * |
| 523 | * TODO: consider only ever looking at the first message. |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 524 | */ |
| 525 | bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) { |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 526 | struct nlmsghdr *nh; |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 527 | |
Lorenzo Colitti | 9683456 | 2013-08-17 03:40:31 +0900 | [diff] [blame] | 528 | for (nh = (struct nlmsghdr *) buffer; |
Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 529 | NLMSG_OK(nh, (unsigned) size) && (nh->nlmsg_type != NLMSG_DONE); |
Lorenzo Colitti | 9683456 | 2013-08-17 03:40:31 +0900 | [diff] [blame] | 530 | nh = NLMSG_NEXT(nh, size)) { |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 531 | |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 532 | if (!rtMessageName(nh->nlmsg_type)) { |
| 533 | SLOGD("Unexpected netlink message type %d\n", nh->nlmsg_type); |
| 534 | continue; |
| 535 | } |
| 536 | |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 537 | if (nh->nlmsg_type == RTM_NEWLINK) { |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 538 | if (parseIfInfoMessage(nh)) |
| 539 | return true; |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 540 | |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 541 | } else if (nh->nlmsg_type == LOCAL_QLOG_NL_EVENT) { |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 542 | if (parseUlogPacketMessage(nh)) |
| 543 | return true; |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 544 | |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame] | 545 | } else if (nh->nlmsg_type == RTM_NEWADDR || |
| 546 | nh->nlmsg_type == RTM_DELADDR) { |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 547 | if (parseIfAddrMessage(nh)) |
| 548 | return true; |
Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 549 | |
Lorenzo Colitti | d7ff7ea | 2014-06-11 17:37:12 +0900 | [diff] [blame] | 550 | } else if (nh->nlmsg_type == RTM_NEWROUTE || |
| 551 | nh->nlmsg_type == RTM_DELROUTE) { |
| 552 | if (parseRtMessage(nh)) |
| 553 | return true; |
| 554 | |
Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 555 | } else if (nh->nlmsg_type == RTM_NEWNDUSEROPT) { |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 556 | if (parseNdUserOptMessage(nh)) |
| 557 | return true; |
Lorenzo Colitti | c7eec83 | 2013-08-12 17:03:32 +0900 | [diff] [blame] | 558 | |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 559 | } else if (nh->nlmsg_type == LOCAL_NFLOG_PACKET) { |
| 560 | if (parseNfPacketMessage(nh)) |
| 561 | return true; |
| 562 | |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 563 | } |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Lorenzo Colitti | 9b34293 | 2014-06-19 13:16:04 +0900 | [diff] [blame] | 566 | return false; |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 567 | } |
| 568 | |
David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 569 | /* If the string between 'str' and 'end' begins with 'prefixlen' characters |
| 570 | * from the 'prefix' array, then return 'str + prefixlen', otherwise return |
| 571 | * NULL. |
| 572 | */ |
| 573 | static const char* |
| 574 | has_prefix(const char* str, const char* end, const char* prefix, size_t prefixlen) |
| 575 | { |
| 576 | if ((end-str) >= (ptrdiff_t)prefixlen && !memcmp(str, prefix, prefixlen)) |
| 577 | return str + prefixlen; |
| 578 | else |
| 579 | return NULL; |
| 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 | } |