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> |
| 27 | #include <arpa/inet.h> |
| 28 | #include <net/if.h> |
| 29 | |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 30 | #include <linux/if.h> |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 31 | #include <linux/netfilter/nfnetlink.h> |
| 32 | #include <linux/netfilter_ipv4/ipt_ULOG.h> |
| 33 | /* From kernel's net/netfilter/xt_quota2.c */ |
| 34 | const int QLOG_NL_EVENT = 112; |
| 35 | |
| 36 | #include <linux/netlink.h> |
| 37 | #include <linux/rtnetlink.h> |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 38 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 39 | const int NetlinkEvent::NlActionUnknown = 0; |
| 40 | const int NetlinkEvent::NlActionAdd = 1; |
| 41 | const int NetlinkEvent::NlActionRemove = 2; |
| 42 | const int NetlinkEvent::NlActionChange = 3; |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 43 | const int NetlinkEvent::NlActionLinkUp = 4; |
| 44 | const int NetlinkEvent::NlActionLinkDown = 5; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 45 | |
| 46 | NetlinkEvent::NetlinkEvent() { |
| 47 | mAction = NlActionUnknown; |
San Mehat | ebfe3db | 2009-10-10 17:35:13 -0700 | [diff] [blame] | 48 | memset(mParams, 0, sizeof(mParams)); |
| 49 | mPath = NULL; |
| 50 | mSubsystem = NULL; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | NetlinkEvent::~NetlinkEvent() { |
| 54 | int i; |
| 55 | if (mPath) |
| 56 | free(mPath); |
| 57 | if (mSubsystem) |
| 58 | free(mSubsystem); |
| 59 | for (i = 0; i < NL_PARAMS_MAX; i++) { |
| 60 | if (!mParams[i]) |
| 61 | break; |
| 62 | free(mParams[i]); |
| 63 | } |
| 64 | } |
| 65 | |
San Mehat | d674413 | 2009-12-24 07:17:09 -0800 | [diff] [blame] | 66 | void NetlinkEvent::dump() { |
| 67 | int i; |
| 68 | |
| 69 | for (i = 0; i < NL_PARAMS_MAX; i++) { |
| 70 | if (!mParams[i]) |
| 71 | break; |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 72 | SLOGD("NL param '%s'\n", mParams[i]); |
San Mehat | d674413 | 2009-12-24 07:17:09 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 76 | /* |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame^] | 77 | * Decode a RTM_NEWADDR or RTM_DELADDR message. |
| 78 | */ |
| 79 | bool NetlinkEvent::parseIfAddrMessage(int type, struct ifaddrmsg *ifaddr, |
| 80 | int rtasize) { |
| 81 | struct rtattr *rta = IFA_RTA(ifaddr); |
| 82 | struct ifa_cacheinfo *cacheinfo = NULL; |
| 83 | char addrstr[INET6_ADDRSTRLEN] = ""; |
| 84 | |
| 85 | // Sanity check. |
| 86 | if (type != RTM_NEWADDR && type != RTM_DELADDR) { |
| 87 | SLOGE("parseIfAddrMessage on incorrect message type 0x%x\n", type); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | // For log messages. |
| 92 | const char *msgtype = (type == RTM_NEWADDR) ? "RTM_NEWADDR" : "RTM_DELADDR"; |
| 93 | |
| 94 | while(RTA_OK(rta, rtasize)) { |
| 95 | if (rta->rta_type == IFA_ADDRESS) { |
| 96 | // Only look at the first address, because we only support notifying |
| 97 | // one change at a time. |
| 98 | if (*addrstr != '\0') { |
| 99 | SLOGE("Multiple IFA_ADDRESSes in %s, ignoring\n", msgtype); |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | // Convert the IP address to a string. |
| 104 | if (ifaddr->ifa_family == AF_INET) { |
| 105 | struct in_addr *addr4 = (struct in_addr *) RTA_DATA(rta); |
| 106 | if (RTA_PAYLOAD(rta) < sizeof(*addr4)) { |
| 107 | SLOGE("Short IPv4 address (%d bytes) in %s", |
| 108 | RTA_PAYLOAD(rta), msgtype); |
| 109 | continue; |
| 110 | } |
| 111 | inet_ntop(AF_INET, addr4, addrstr, sizeof(addrstr)); |
| 112 | } else if (ifaddr->ifa_family == AF_INET6) { |
| 113 | struct in6_addr *addr6 = (struct in6_addr *) RTA_DATA(rta); |
| 114 | if (RTA_PAYLOAD(rta) < sizeof(*addr6)) { |
| 115 | SLOGE("Short IPv6 address (%d bytes) in %s", |
| 116 | RTA_PAYLOAD(rta), msgtype); |
| 117 | continue; |
| 118 | } |
| 119 | inet_ntop(AF_INET6, addr6, addrstr, sizeof(addrstr)); |
| 120 | } else { |
| 121 | SLOGE("Unknown address family %d\n", ifaddr->ifa_family); |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | // Find the interface name. |
| 126 | char ifname[IFNAMSIZ + 1]; |
| 127 | if (!if_indextoname(ifaddr->ifa_index, ifname)) { |
| 128 | SLOGE("Unknown ifindex %d in %s", ifaddr->ifa_index, msgtype); |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | // Fill in interface information. |
| 133 | mAction = (type == RTM_NEWADDR) ? NlActionAdd : NlActionRemove; |
| 134 | mSubsystem = strdup("address"); |
| 135 | asprintf(&mParams[0], "ADDRESS=%s/%d", addrstr, |
| 136 | ifaddr->ifa_prefixlen); |
| 137 | asprintf(&mParams[1], "IFACE=%s", ifname); |
| 138 | asprintf(&mParams[2], "FLAGS=%u", ifaddr->ifa_flags); |
| 139 | asprintf(&mParams[3], "SCOPE=%u", ifaddr->ifa_scope); |
| 140 | } else if (rta->rta_type == IFA_CACHEINFO) { |
| 141 | // Address lifetime information. |
| 142 | if (cacheinfo) { |
| 143 | // We only support one address. |
| 144 | SLOGE("Multiple IFA_CACHEINFOs in %s, ignoring\n", msgtype); |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | if (RTA_PAYLOAD(rta) < sizeof(*cacheinfo)) { |
| 149 | SLOGE("Short IFA_CACHEINFO (%d vs. %d bytes) in %s", |
| 150 | RTA_PAYLOAD(rta), sizeof(cacheinfo), msgtype); |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | cacheinfo = (struct ifa_cacheinfo *) RTA_DATA(rta); |
| 155 | asprintf(&mParams[4], "PREFERRED=%u", cacheinfo->ifa_prefered); |
| 156 | asprintf(&mParams[5], "VALID=%u", cacheinfo->ifa_valid); |
| 157 | asprintf(&mParams[6], "CSTAMP=%u", cacheinfo->cstamp); |
| 158 | asprintf(&mParams[7], "TSTAMP=%u", cacheinfo->tstamp); |
| 159 | } |
| 160 | |
| 161 | rta = RTA_NEXT(rta, rtasize); |
| 162 | } |
| 163 | |
| 164 | if (addrstr[0] == '\0') { |
| 165 | SLOGE("No IFA_ADDRESS in %s\n", msgtype); |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | /* |
JP Abgrall | b982bce | 2012-04-26 23:52:58 -0700 | [diff] [blame] | 173 | * Parse an binary message from a NETLINK_ROUTE netlink socket. |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 174 | */ |
| 175 | bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) { |
| 176 | size_t sz = size; |
Mike J. Chen | 17260b1 | 2011-06-23 15:00:30 -0700 | [diff] [blame] | 177 | const struct nlmsghdr *nh = (struct nlmsghdr *) buffer; |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 178 | |
| 179 | while (NLMSG_OK(nh, sz) && (nh->nlmsg_type != NLMSG_DONE)) { |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 180 | |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 181 | if (nh->nlmsg_type == RTM_NEWLINK) { |
| 182 | int len = nh->nlmsg_len - sizeof(*nh); |
| 183 | struct ifinfomsg *ifi; |
| 184 | |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 185 | if (sizeof(*ifi) > (size_t) len) { |
| 186 | SLOGE("Got a short RTM_NEWLINK message\n"); |
| 187 | continue; |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 188 | } |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 189 | |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 190 | ifi = (ifinfomsg *)NLMSG_DATA(nh); |
| 191 | if ((ifi->ifi_flags & IFF_LOOPBACK) != 0) { |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | struct rtattr *rta = (struct rtattr *) |
| 196 | ((char *) ifi + NLMSG_ALIGN(sizeof(*ifi))); |
| 197 | len = NLMSG_PAYLOAD(nh, sizeof(*ifi)); |
| 198 | |
| 199 | while(RTA_OK(rta, len)) { |
| 200 | switch(rta->rta_type) { |
| 201 | case IFLA_IFNAME: |
| 202 | char buffer[16 + IFNAMSIZ]; |
| 203 | snprintf(buffer, sizeof(buffer), "INTERFACE=%s", |
| 204 | (char *) RTA_DATA(rta)); |
| 205 | mParams[0] = strdup(buffer); |
| 206 | mAction = (ifi->ifi_flags & IFF_LOWER_UP) ? |
| 207 | NlActionLinkUp : NlActionLinkDown; |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame^] | 208 | mSubsystem = strdup("interface"); |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 209 | break; |
| 210 | } |
| 211 | |
| 212 | rta = RTA_NEXT(rta, len); |
| 213 | } |
| 214 | |
| 215 | } else if (nh->nlmsg_type == QLOG_NL_EVENT) { |
| 216 | char *devname; |
| 217 | ulog_packet_msg_t *pm; |
| 218 | size_t len = nh->nlmsg_len - sizeof(*nh); |
| 219 | if (sizeof(*pm) > len) { |
| 220 | SLOGE("Got a short QLOG message\n"); |
| 221 | continue; |
| 222 | } |
| 223 | pm = (ulog_packet_msg_t *)NLMSG_DATA(nh); |
| 224 | devname = pm->indev_name[0] ? pm->indev_name : pm->outdev_name; |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 225 | asprintf(&mParams[0], "ALERT_NAME=%s", pm->prefix); |
| 226 | asprintf(&mParams[1], "INTERFACE=%s", devname); |
| 227 | mSubsystem = strdup("qlog"); |
| 228 | mAction = NlActionChange; |
| 229 | |
Lorenzo Colitti | 381f70f | 2013-08-02 05:58:37 +0900 | [diff] [blame^] | 230 | } else if (nh->nlmsg_type == RTM_NEWADDR || |
| 231 | nh->nlmsg_type == RTM_DELADDR) { |
| 232 | int len = nh->nlmsg_len - sizeof(*nh); |
| 233 | struct ifaddrmsg *ifa; |
| 234 | |
| 235 | if (sizeof(*ifa) > (size_t) len) { |
| 236 | SLOGE("Got a short RTM_xxxADDR message\n"); |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | ifa = (ifaddrmsg *)NLMSG_DATA(nh); |
| 241 | size_t rtasize = IFA_PAYLOAD(nh); |
| 242 | if (!parseIfAddrMessage(nh->nlmsg_type, ifa, rtasize)) { |
| 243 | continue; |
| 244 | } |
JP Abgrall | e6f8014 | 2011-07-14 16:46:32 -0700 | [diff] [blame] | 245 | } else { |
| 246 | SLOGD("Unexpected netlink message. type=0x%x\n", nh->nlmsg_type); |
| 247 | } |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 248 | nh = NLMSG_NEXT(nh, size); |
| 249 | } |
| 250 | |
| 251 | return true; |
| 252 | } |
| 253 | |
David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 254 | /* If the string between 'str' and 'end' begins with 'prefixlen' characters |
| 255 | * from the 'prefix' array, then return 'str + prefixlen', otherwise return |
| 256 | * NULL. |
| 257 | */ |
| 258 | static const char* |
| 259 | has_prefix(const char* str, const char* end, const char* prefix, size_t prefixlen) |
| 260 | { |
| 261 | if ((end-str) >= (ptrdiff_t)prefixlen && !memcmp(str, prefix, prefixlen)) |
| 262 | return str + prefixlen; |
| 263 | else |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | /* Same as strlen(x) for constant string literals ONLY */ |
| 268 | #define CONST_STRLEN(x) (sizeof(x)-1) |
| 269 | |
| 270 | /* Convenience macro to call has_prefix with a constant string literal */ |
| 271 | #define HAS_CONST_PREFIX(str,end,prefix) has_prefix((str),(end),prefix,CONST_STRLEN(prefix)) |
| 272 | |
| 273 | |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 274 | /* |
| 275 | * Parse an ASCII-formatted message from a NETLINK_KOBJECT_UEVENT |
| 276 | * netlink socket. |
| 277 | */ |
| 278 | bool NetlinkEvent::parseAsciiNetlinkMessage(char *buffer, int size) { |
Mike J. Chen | 17260b1 | 2011-06-23 15:00:30 -0700 | [diff] [blame] | 279 | const char *s = buffer; |
| 280 | const char *end; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 281 | int param_idx = 0; |
| 282 | int i; |
| 283 | int first = 1; |
| 284 | |
David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 285 | if (size == 0) |
| 286 | return false; |
| 287 | |
| 288 | /* Ensure the buffer is zero-terminated, the code below depends on this */ |
| 289 | buffer[size-1] = '\0'; |
| 290 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 291 | end = s + size; |
| 292 | while (s < end) { |
| 293 | if (first) { |
David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 294 | const char *p; |
| 295 | /* buffer is 0-terminated, no need to check p < end */ |
| 296 | for (p = s; *p != '@'; p++) { |
| 297 | if (!*p) { /* no '@', should not happen */ |
| 298 | return false; |
| 299 | } |
| 300 | } |
| 301 | mPath = strdup(p+1); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 302 | first = 0; |
| 303 | } else { |
David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 304 | const char* a; |
| 305 | if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != NULL) { |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 306 | if (!strcmp(a, "add")) |
| 307 | mAction = NlActionAdd; |
| 308 | else if (!strcmp(a, "remove")) |
| 309 | mAction = NlActionRemove; |
| 310 | else if (!strcmp(a, "change")) |
| 311 | mAction = NlActionChange; |
David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 312 | } else if ((a = HAS_CONST_PREFIX(s, end, "SEQNUM=")) != NULL) { |
| 313 | mSeq = atoi(a); |
| 314 | } else if ((a = HAS_CONST_PREFIX(s, end, "SUBSYSTEM=")) != NULL) { |
| 315 | mSubsystem = strdup(a); |
| 316 | } else if (param_idx < NL_PARAMS_MAX) { |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 317 | mParams[param_idx++] = strdup(s); |
David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 318 | } |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 319 | } |
David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 320 | s += strlen(s) + 1; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 321 | } |
| 322 | return true; |
| 323 | } |
| 324 | |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 325 | bool NetlinkEvent::decode(char *buffer, int size, int format) { |
Mike J. Chen | 17260b1 | 2011-06-23 15:00:30 -0700 | [diff] [blame] | 326 | if (format == NetlinkListener::NETLINK_FORMAT_BINARY) { |
| 327 | return parseBinaryNetlinkMessage(buffer, size); |
| 328 | } else { |
| 329 | return parseAsciiNetlinkMessage(buffer, size); |
| 330 | } |
Mike J. Chen | ec16b9d | 2011-06-23 14:55:28 -0700 | [diff] [blame] | 331 | } |
| 332 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 333 | const char *NetlinkEvent::findParam(const char *paramName) { |
Chih-Wei Huang | 80ec37a | 2010-07-14 14:00:41 +0800 | [diff] [blame] | 334 | size_t len = strlen(paramName); |
David 'Digit' Turner | 3311eea | 2011-01-17 01:59:22 +0100 | [diff] [blame] | 335 | 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] | 336 | const char *ptr = mParams[i] + len; |
| 337 | if (!strncmp(mParams[i], paramName, len) && *ptr == '=') |
| 338 | return ++ptr; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 339 | } |
| 340 | |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 341 | SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 342 | return NULL; |
| 343 | } |