summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenzo Colitti2014-06-11 03:37:12 -0500
committerLorenzo Colitti2014-06-20 20:55:07 -0500
commitd7ff7ea65220718398cae1e1aa0005072229b4e7 (patch)
treeac90523bc7e81c6580b869f74de7e8b8590e4dcc /libsysutils
parent9b34293566833ead1d7bac7518e5ccad0d92d61c (diff)
downloadplatform-system-core-d7ff7ea65220718398cae1e1aa0005072229b4e7.tar.gz
platform-system-core-d7ff7ea65220718398cae1e1aa0005072229b4e7.tar.xz
platform-system-core-d7ff7ea65220718398cae1e1aa0005072229b4e7.zip
Support parsing netlink route messages.
Bug: 9180552 Change-Id: I4a36d869ac692647fb226d0912285bdf2ffcb1fa
Diffstat (limited to 'libsysutils')
-rw-r--r--libsysutils/src/NetlinkEvent.cpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/libsysutils/src/NetlinkEvent.cpp b/libsysutils/src/NetlinkEvent.cpp
index 41bd2151a..9d596ef55 100644
--- a/libsysutils/src/NetlinkEvent.cpp
+++ b/libsysutils/src/NetlinkEvent.cpp
@@ -48,6 +48,8 @@ const int NetlinkEvent::NlActionLinkDown = 5;
48const int NetlinkEvent::NlActionAddressUpdated = 6; 48const int NetlinkEvent::NlActionAddressUpdated = 6;
49const int NetlinkEvent::NlActionAddressRemoved = 7; 49const int NetlinkEvent::NlActionAddressRemoved = 7;
50const int NetlinkEvent::NlActionRdnss = 8; 50const int NetlinkEvent::NlActionRdnss = 8;
51const int NetlinkEvent::NlActionRouteUpdated = 9;
52const int NetlinkEvent::NlActionRouteRemoved = 10;
51 53
52NetlinkEvent::NetlinkEvent() { 54NetlinkEvent::NetlinkEvent() {
53 mAction = NlActionUnknown; 55 mAction = NlActionUnknown;
@@ -270,6 +272,98 @@ bool NetlinkEvent::parseUlogPacketMessage(const struct nlmsghdr *nh) {
270} 272}
271 273
272/* 274/*
275 * Parse a RTM_NEWROUTE or RTM_DELROUTE message.
276 */
277bool NetlinkEvent::parseRtMessage(const struct nlmsghdr *nh) {
278 uint8_t type = nh->nlmsg_type;
279 const char *msgname = rtMessageName(type);
280
281 // Sanity check.
282 if (type != RTM_NEWROUTE && type != RTM_DELROUTE) {
283 SLOGE("%s: incorrect message type %d (%s)\n", __func__, type, msgname);
284 return false;
285 }
286
287 struct rtmsg *rtm = (struct rtmsg *) NLMSG_DATA(nh);
288 if (!checkRtNetlinkLength(nh, sizeof(*rtm)))
289 return false;
290
291 if (// Ignore static routes we've set up ourselves.
292 (rtm->rtm_protocol != RTPROT_KERNEL &&
293 rtm->rtm_protocol != RTPROT_RA) ||
294 // We're only interested in global unicast routes.
295 (rtm->rtm_scope != RT_SCOPE_UNIVERSE) ||
296 (rtm->rtm_type != RTN_UNICAST) ||
297 // We don't support source routing.
298 (rtm->rtm_src_len != 0) ||
299 // Cloned routes aren't real routes.
300 (rtm->rtm_flags & RTM_F_CLONED)) {
301 return false;
302 }
303
304 int family = rtm->rtm_family;
305 int prefixLength = rtm->rtm_dst_len;
306
307 // Currently we only support: destination, (one) next hop, ifindex.
308 char dst[INET6_ADDRSTRLEN] = "";
309 char gw[INET6_ADDRSTRLEN] = "";
310 char dev[IFNAMSIZ] = "";
311
312 size_t len = RTM_PAYLOAD(nh);
313 struct rtattr *rta;
314 for (rta = RTM_RTA(rtm); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
315 switch (rta->rta_type) {
316 case RTA_DST:
317 if (maybeLogDuplicateAttribute(*dst, "RTA_DST", msgname))
318 continue;
319 if (!inet_ntop(family, RTA_DATA(rta), dst, sizeof(dst)))
320 return false;
321 continue;
322 case RTA_GATEWAY:
323 if (maybeLogDuplicateAttribute(*gw, "RTA_GATEWAY", msgname))
324 continue;
325 if (!inet_ntop(family, RTA_DATA(rta), gw, sizeof(gw)))
326 return false;
327 continue;
328 case RTA_OIF:
329 if (maybeLogDuplicateAttribute(*dev, "RTA_OIF", msgname))
330 continue;
331 if (!if_indextoname(* (int *) RTA_DATA(rta), dev))
332 return false;
333 default:
334 continue;
335 }
336 }
337
338 // If there's no RTA_DST attribute, then:
339 // - If the prefix length is zero, it's the default route.
340 // - If the prefix length is nonzero, there's something we don't understand.
341 // Ignore the event.
342 if (!*dst && !prefixLength) {
343 if (family == AF_INET) {
344 strncpy(dst, "0.0.0.0", sizeof(dst));
345 } else if (family == AF_INET6) {
346 strncpy(dst, "::", sizeof(dst));
347 }
348 }
349
350 // A useful route must have a destination and at least either a gateway or
351 // an interface.
352 if (!*dst || (!*gw && !*dev))
353 return false;
354
355 // Fill in netlink event information.
356 mAction = (type == RTM_NEWROUTE) ? NlActionRouteUpdated :
357 NlActionRouteRemoved;
358 mSubsystem = strdup("net");
359 asprintf(&mParams[0], "ROUTE=%s/%d", dst, prefixLength);
360 asprintf(&mParams[1], "GATEWAY=%s", (*gw) ? gw : "");
361 asprintf(&mParams[2], "INTERFACE=%s", (*dev) ? dev : "");
362
363 return true;
364}
365
366/*
273 * Parse a RTM_NEWNDUSEROPT message. 367 * Parse a RTM_NEWNDUSEROPT message.
274 */ 368 */
275bool NetlinkEvent::parseNdUserOptMessage(const struct nlmsghdr *nh) { 369bool NetlinkEvent::parseNdUserOptMessage(const struct nlmsghdr *nh) {
@@ -408,6 +502,11 @@ bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) {
408 if (parseIfAddrMessage(nh)) 502 if (parseIfAddrMessage(nh))
409 return true; 503 return true;
410 504
505 } else if (nh->nlmsg_type == RTM_NEWROUTE ||
506 nh->nlmsg_type == RTM_DELROUTE) {
507 if (parseRtMessage(nh))
508 return true;
509
411 } else if (nh->nlmsg_type == RTM_NEWNDUSEROPT) { 510 } else if (nh->nlmsg_type == RTM_NEWNDUSEROPT) {
412 if (parseNdUserOptMessage(nh)) 511 if (parseNdUserOptMessage(nh))
413 return true; 512 return true;