blob: 29a86d82ab7b820662c41d9b8d45e2988bf5b1d9 [file] [log] [blame]
San Mehat168415b2009-05-06 11:14:21 -07001/*
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 Mehat3d407292009-05-07 08:49:30 -070017#include <string.h>
San Mehat168415b2009-05-06 11:14:21 -070018
19#define LOG_TAG "NetlinkEvent"
20#include <cutils/log.h>
21
22#include <sysutils/NetlinkEvent.h>
23
Mike J. Chenec16b9d2011-06-23 14:55:28 -070024#include <sys/types.h>
25#include <sys/socket.h>
Lorenzo Colitti381f70f2013-08-02 05:58:37 +090026#include <netinet/in.h>
Lorenzo Colittic7eec832013-08-12 17:03:32 +090027#include <netinet/icmp6.h>
Lorenzo Colitti381f70f2013-08-02 05:58:37 +090028#include <arpa/inet.h>
29#include <net/if.h>
30
Mike J. Chenec16b9d2011-06-23 14:55:28 -070031#include <linux/if.h>
Lorenzo Colitti9b342932014-06-19 13:16:04 +090032#include <linux/if_addr.h>
33#include <linux/if_link.h>
JP Abgralle6f80142011-07-14 16:46:32 -070034#include <linux/netfilter/nfnetlink.h>
Jeff Sharkey9a20e672014-10-30 14:51:59 -070035#include <linux/netfilter/nfnetlink_log.h>
JP Abgralle6f80142011-07-14 16:46:32 -070036#include <linux/netfilter_ipv4/ipt_ULOG.h>
Jeff Sharkey9a20e672014-10-30 14:51:59 -070037
JP Abgralle6f80142011-07-14 16:46:32 -070038/* From kernel's net/netfilter/xt_quota2.c */
Jeff Sharkey9a20e672014-10-30 14:51:59 -070039const int LOCAL_QLOG_NL_EVENT = 112;
40const int LOCAL_NFLOG_PACKET = NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET;
JP Abgralle6f80142011-07-14 16:46:32 -070041
42#include <linux/netlink.h>
43#include <linux/rtnetlink.h>
Mike J. Chenec16b9d2011-06-23 14:55:28 -070044
Jeff Sharkey9a20e672014-10-30 14:51:59 -070045#include <netlink/attr.h>
46#include <netlink/genl/genl.h>
47#include <netlink/handlers.h>
48#include <netlink/msg.h>
49
Jeff Sharkey31837292015-03-16 10:52:03 -070050// STOPSHIP: remove these deprecated constants once we have updated prebuilts
51const int NetlinkEvent::NlActionUnknown = static_cast<int>(Action::kUnknown);
52const int NetlinkEvent::NlActionAdd = static_cast<int>(Action::kAdd);
53const int NetlinkEvent::NlActionRemove = static_cast<int>(Action::kRemove);
54const int NetlinkEvent::NlActionChange = static_cast<int>(Action::kChange);
55const int NetlinkEvent::NlActionLinkDown = static_cast<int>(Action::kLinkDown);
56const int NetlinkEvent::NlActionLinkUp = static_cast<int>(Action::kLinkUp);
57const int NetlinkEvent::NlActionAddressUpdated = static_cast<int>(Action::kAddressUpdated);
58const int NetlinkEvent::NlActionAddressRemoved = static_cast<int>(Action::kAddressRemoved);
59const int NetlinkEvent::NlActionRdnss = static_cast<int>(Action::kRdnss);
60const int NetlinkEvent::NlActionRouteUpdated = static_cast<int>(Action::kRouteUpdated);
61const int NetlinkEvent::NlActionRouteRemoved = static_cast<int>(Action::kRouteRemoved);
62
San Mehat168415b2009-05-06 11:14:21 -070063NetlinkEvent::NetlinkEvent() {
Jeff Sharkeye4f39402015-03-13 13:27:33 -070064 mAction = Action::kUnknown;
San Mehatebfe3db2009-10-10 17:35:13 -070065 memset(mParams, 0, sizeof(mParams));
66 mPath = NULL;
67 mSubsystem = NULL;
San Mehat168415b2009-05-06 11:14:21 -070068}
69
70NetlinkEvent::~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 Mehatd6744132009-12-24 07:17:09 -080083void NetlinkEvent::dump() {
84 int i;
85
86 for (i = 0; i < NL_PARAMS_MAX; i++) {
87 if (!mParams[i])
88 break;
San Mehat7e8529a2010-03-25 09:31:42 -070089 SLOGD("NL param '%s'\n", mParams[i]);
San Mehatd6744132009-12-24 07:17:09 -080090 }
91}
92
Mike J. Chenec16b9d2011-06-23 14:55:28 -070093/*
Lorenzo Colitti9b342932014-06-19 13:16:04 +090094 * Returns the message name for a message in the NETLINK_ROUTE family, or NULL
95 * if parsing that message is not supported.
96 */
97static 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 Sharkey9a20e672014-10-30 14:51:59 -0700107 NL_EVENT_RTM_NAME(LOCAL_QLOG_NL_EVENT);
108 NL_EVENT_RTM_NAME(LOCAL_NFLOG_PACKET);
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900109 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 */
119static 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 */
130static 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 */
143bool 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 Sharkeye4f39402015-03-13 13:27:33 -0700158 mAction = (ifi->ifi_flags & IFF_LOWER_UP) ? Action::kLinkUp :
159 Action::kLinkDown;
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900160 mSubsystem = strdup("net");
161 return true;
162 }
163 }
164
165 return false;
166}
167
168/*
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900169 * Parse a RTM_NEWADDR or RTM_DELADDR message.
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900170 */
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900171bool NetlinkEvent::parseIfAddrMessage(const struct nlmsghdr *nh) {
172 struct ifaddrmsg *ifaddr = (struct ifaddrmsg *) NLMSG_DATA(nh);
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900173 struct ifa_cacheinfo *cacheinfo = NULL;
174 char addrstr[INET6_ADDRSTRLEN] = "";
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900175 char ifname[IFNAMSIZ];
176
177 if (!checkRtNetlinkLength(nh, sizeof(*ifaddr)))
178 return false;
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900179
180 // Sanity check.
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900181 int type = nh->nlmsg_type;
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900182 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 Colitti9b342932014-06-19 13:16:04 +0900188 const char *msgtype = rtMessageName(type);
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900189
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900190 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 Colitti381f70f2013-08-02 05:58:37 +0900193 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 Colitti9b342932014-06-19 13:16:04 +0900196 if (maybeLogDuplicateAttribute(*addrstr != '\0', "IFA_ADDRESS", msgtype))
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900197 continue;
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900198
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 Salyzyn80f63d42014-05-01 07:47:04 -0700203 SLOGE("Short IPv4 address (%zu bytes) in %s",
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900204 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 Salyzyn80f63d42014-05-01 07:47:04 -0700211 SLOGE("Short IPv6 address (%zu bytes) in %s",
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900212 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 Colitti381f70f2013-08-02 05:58:37 +0900222 if (!if_indextoname(ifaddr->ifa_index, ifname)) {
223 SLOGE("Unknown ifindex %d in %s", ifaddr->ifa_index, msgtype);
224 return false;
225 }
226
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900227 } else if (rta->rta_type == IFA_CACHEINFO) {
228 // Address lifetime information.
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900229 if (maybeLogDuplicateAttribute(cacheinfo, "IFA_CACHEINFO", msgtype))
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900230 continue;
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900231
232 if (RTA_PAYLOAD(rta) < sizeof(*cacheinfo)) {
Mark Salyzyn80f63d42014-05-01 07:47:04 -0700233 SLOGE("Short IFA_CACHEINFO (%zu vs. %zu bytes) in %s",
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900234 RTA_PAYLOAD(rta), sizeof(cacheinfo), msgtype);
235 continue;
236 }
237
238 cacheinfo = (struct ifa_cacheinfo *) RTA_DATA(rta);
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900239 }
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900240 }
241
242 if (addrstr[0] == '\0') {
243 SLOGE("No IFA_ADDRESS in %s\n", msgtype);
244 return false;
245 }
246
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900247 // Fill in netlink event information.
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700248 mAction = (type == RTM_NEWADDR) ? Action::kAddressUpdated :
249 Action::kAddressRemoved;
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900250 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 */
270bool 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 Sharkeye4f39402015-03-13 13:27:33 -0700280 mAction = Action::kChange;
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900281 return true;
282}
283
284/*
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700285 * Parse a LOCAL_NFLOG_PACKET message.
286 */
287bool 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 Sharkeye4f39402015-03-13 13:27:33 -0700315 mAction = Action::kChange;
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700316 return true;
317}
318
319/*
Lorenzo Colittid7ff7ea2014-06-11 17:37:12 +0900320 * Parse a RTM_NEWROUTE or RTM_DELROUTE message.
321 */
322bool 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 Sharkeye4f39402015-03-13 13:27:33 -0700401 mAction = (type == RTM_NEWROUTE) ? Action::kRouteUpdated :
402 Action::kRouteRemoved;
Lorenzo Colittid7ff7ea2014-06-11 17:37:12 +0900403 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 Colittic7eec832013-08-12 17:03:32 +0900412 * Parse a RTM_NEWNDUSEROPT message.
413 */
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900414bool 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 Colittic7eec832013-08-12 17:03:32 +0900419 // Check the length is valid.
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900420 int len = NLMSG_PAYLOAD(nh, sizeof(*msg));
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900421 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 Colitti9b342932014-06-19 13:16:04 +0900443 char ifname[IFNAMSIZ];
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900444 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 Sharkeye4f39402015-03-13 13:27:33 -0700501 mAction = Action::kRdnss;
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900502 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 Colitti9b342932014-06-19 13:16:04 +0900516 *
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. Chenec16b9d2011-06-23 14:55:28 -0700524 */
525bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) {
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700526 struct nlmsghdr *nh;
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700527
Lorenzo Colitti96834562013-08-17 03:40:31 +0900528 for (nh = (struct nlmsghdr *) buffer;
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900529 NLMSG_OK(nh, (unsigned) size) && (nh->nlmsg_type != NLMSG_DONE);
Lorenzo Colitti96834562013-08-17 03:40:31 +0900530 nh = NLMSG_NEXT(nh, size)) {
JP Abgralle6f80142011-07-14 16:46:32 -0700531
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900532 if (!rtMessageName(nh->nlmsg_type)) {
533 SLOGD("Unexpected netlink message type %d\n", nh->nlmsg_type);
534 continue;
535 }
536
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700537 if (nh->nlmsg_type == RTM_NEWLINK) {
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900538 if (parseIfInfoMessage(nh))
539 return true;
JP Abgralle6f80142011-07-14 16:46:32 -0700540
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700541 } else if (nh->nlmsg_type == LOCAL_QLOG_NL_EVENT) {
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900542 if (parseUlogPacketMessage(nh))
543 return true;
JP Abgralle6f80142011-07-14 16:46:32 -0700544
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900545 } else if (nh->nlmsg_type == RTM_NEWADDR ||
546 nh->nlmsg_type == RTM_DELADDR) {
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900547 if (parseIfAddrMessage(nh))
548 return true;
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900549
Lorenzo Colittid7ff7ea2014-06-11 17:37:12 +0900550 } else if (nh->nlmsg_type == RTM_NEWROUTE ||
551 nh->nlmsg_type == RTM_DELROUTE) {
552 if (parseRtMessage(nh))
553 return true;
554
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900555 } else if (nh->nlmsg_type == RTM_NEWNDUSEROPT) {
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900556 if (parseNdUserOptMessage(nh))
557 return true;
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900558
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700559 } else if (nh->nlmsg_type == LOCAL_NFLOG_PACKET) {
560 if (parseNfPacketMessage(nh))
561 return true;
562
JP Abgralle6f80142011-07-14 16:46:32 -0700563 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700564 }
565
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900566 return false;
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700567}
568
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100569/* 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 */
573static const char*
574has_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. Chenec16b9d2011-06-23 14:55:28 -0700589/*
590 * Parse an ASCII-formatted message from a NETLINK_KOBJECT_UEVENT
591 * netlink socket.
592 */
593bool NetlinkEvent::parseAsciiNetlinkMessage(char *buffer, int size) {
Mike J. Chen17260b12011-06-23 15:00:30 -0700594 const char *s = buffer;
595 const char *end;
San Mehat168415b2009-05-06 11:14:21 -0700596 int param_idx = 0;
San Mehat168415b2009-05-06 11:14:21 -0700597 int first = 1;
598
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100599 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 Mehat168415b2009-05-06 11:14:21 -0700605 end = s + size;
606 while (s < end) {
607 if (first) {
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100608 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 Mehat168415b2009-05-06 11:14:21 -0700616 first = 0;
617 } else {
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100618 const char* a;
619 if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != NULL) {
San Mehat168415b2009-05-06 11:14:21 -0700620 if (!strcmp(a, "add"))
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700621 mAction = Action::kAdd;
San Mehat168415b2009-05-06 11:14:21 -0700622 else if (!strcmp(a, "remove"))
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700623 mAction = Action::kRemove;
San Mehat168415b2009-05-06 11:14:21 -0700624 else if (!strcmp(a, "change"))
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700625 mAction = Action::kChange;
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100626 } 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 Mehat168415b2009-05-06 11:14:21 -0700631 mParams[param_idx++] = strdup(s);
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100632 }
San Mehat168415b2009-05-06 11:14:21 -0700633 }
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100634 s += strlen(s) + 1;
San Mehat168415b2009-05-06 11:14:21 -0700635 }
636 return true;
637}
638
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700639bool NetlinkEvent::decode(char *buffer, int size, int format) {
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700640 if (format == NetlinkListener::NETLINK_FORMAT_BINARY
641 || format == NetlinkListener::NETLINK_FORMAT_BINARY_UNICAST) {
Mike J. Chen17260b12011-06-23 15:00:30 -0700642 return parseBinaryNetlinkMessage(buffer, size);
643 } else {
644 return parseAsciiNetlinkMessage(buffer, size);
645 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700646}
647
San Mehat168415b2009-05-06 11:14:21 -0700648const char *NetlinkEvent::findParam(const char *paramName) {
Chih-Wei Huang80ec37a2010-07-14 14:00:41 +0800649 size_t len = strlen(paramName);
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100650 for (int i = 0; i < NL_PARAMS_MAX && mParams[i] != NULL; ++i) {
Chih-Wei Huang80ec37a2010-07-14 14:00:41 +0800651 const char *ptr = mParams[i] + len;
652 if (!strncmp(mParams[i], paramName, len) && *ptr == '=')
653 return ++ptr;
San Mehat168415b2009-05-06 11:14:21 -0700654 }
655
San Mehat7e8529a2010-03-25 09:31:42 -0700656 SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName);
San Mehat168415b2009-05-06 11:14:21 -0700657 return NULL;
658}