NetlinkEvent.cpp revision a4b4e9ad8e35ab424e61d76ebe6654445fc61e63
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> 17#include <string.h> 18 19#define LOG_TAG "NetlinkEvent" 20#include <cutils/log.h> 21 22#include <sysutils/NetlinkEvent.h> 23 24#include <sys/types.h> 25#include <sys/socket.h> 26#include <netinet/in.h> 27#include <arpa/inet.h> 28#include <net/if.h> 29 30#include <linux/if.h> 31#include <linux/netfilter/nfnetlink.h> 32#include <linux/netfilter_ipv4/ipt_ULOG.h> 33/* From kernel's net/netfilter/xt_quota2.c */ 34const int QLOG_NL_EVENT = 112; 35 36#include <linux/netlink.h> 37#include <linux/rtnetlink.h> 38 39const int NetlinkEvent::NlActionUnknown = 0; 40const int NetlinkEvent::NlActionAdd = 1; 41const int NetlinkEvent::NlActionRemove = 2; 42const int NetlinkEvent::NlActionChange = 3; 43const int NetlinkEvent::NlActionLinkUp = 4; 44const int NetlinkEvent::NlActionLinkDown = 5; 45 46NetlinkEvent::NetlinkEvent() { 47 mAction = NlActionUnknown; 48 memset(mParams, 0, sizeof(mParams)); 49 mPath = NULL; 50 mSubsystem = NULL; 51} 52 53NetlinkEvent::~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 66void NetlinkEvent::dump() { 67 int i; 68 69 for (i = 0; i < NL_PARAMS_MAX; i++) { 70 if (!mParams[i]) 71 break; 72 SLOGD("NL param '%s'\n", mParams[i]); 73 } 74} 75 76/* 77 * Decode a RTM_NEWADDR or RTM_DELADDR message. 78 */ 79bool 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/* 173 * Parse an binary message from a NETLINK_ROUTE netlink socket. 174 */ 175bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) { 176 size_t sz = size; 177 const struct nlmsghdr *nh = (struct nlmsghdr *) buffer; 178 179 while (NLMSG_OK(nh, sz) && (nh->nlmsg_type != NLMSG_DONE)) { 180 181 if (nh->nlmsg_type == RTM_NEWLINK) { 182 int len = nh->nlmsg_len - sizeof(*nh); 183 struct ifinfomsg *ifi; 184 185 if (sizeof(*ifi) > (size_t) len) { 186 SLOGE("Got a short RTM_NEWLINK message\n"); 187 continue; 188 } 189 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; 208 mSubsystem = strdup("interface"); 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; 225 asprintf(&mParams[0], "ALERT_NAME=%s", pm->prefix); 226 asprintf(&mParams[1], "INTERFACE=%s", devname); 227 mSubsystem = strdup("qlog"); 228 mAction = NlActionChange; 229 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 } 245 } else { 246 SLOGD("Unexpected netlink message. type=0x%x\n", nh->nlmsg_type); 247 } 248 nh = NLMSG_NEXT(nh, size); 249 } 250 251 return true; 252} 253 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 */ 258static const char* 259has_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 274/* 275 * Parse an ASCII-formatted message from a NETLINK_KOBJECT_UEVENT 276 * netlink socket. 277 */ 278bool NetlinkEvent::parseAsciiNetlinkMessage(char *buffer, int size) { 279 const char *s = buffer; 280 const char *end; 281 int param_idx = 0; 282 int i; 283 int first = 1; 284 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 291 end = s + size; 292 while (s < end) { 293 if (first) { 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); 302 first = 0; 303 } else { 304 const char* a; 305 if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != NULL) { 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; 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) { 317 mParams[param_idx++] = strdup(s); 318 } 319 } 320 s += strlen(s) + 1; 321 } 322 return true; 323} 324 325bool NetlinkEvent::decode(char *buffer, int size, int format) { 326 if (format == NetlinkListener::NETLINK_FORMAT_BINARY) { 327 return parseBinaryNetlinkMessage(buffer, size); 328 } else { 329 return parseAsciiNetlinkMessage(buffer, size); 330 } 331} 332 333const char *NetlinkEvent::findParam(const char *paramName) { 334 size_t len = strlen(paramName); 335 for (int i = 0; i < NL_PARAMS_MAX && mParams[i] != NULL; ++i) { 336 const char *ptr = mParams[i] + len; 337 if (!strncmp(mParams[i], paramName, len) && *ptr == '=') 338 return ++ptr; 339 } 340 341 SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName); 342 return NULL; 343} 344