blob: 35a30639b57799372abff45f7be07119d2fbf202 [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 */
San Mehat168415b2009-05-06 11:14:21 -070016
17#define LOG_TAG "NetlinkEvent"
San Mehat168415b2009-05-06 11:14:21 -070018
Lorenzo Colitti381f70f2013-08-02 05:58:37 +090019#include <arpa/inet.h>
Lorenzo Colittie439ffc2017-10-03 18:44:11 +090020#include <limits.h>
21#include <linux/genetlink.h>
Mike J. Chenec16b9d2011-06-23 14:55:28 -070022#include <linux/if.h>
Lorenzo Colitti9b342932014-06-19 13:16:04 +090023#include <linux/if_addr.h>
24#include <linux/if_link.h>
JP Abgralle6f80142011-07-14 16:46:32 -070025#include <linux/netfilter/nfnetlink.h>
Jeff Sharkey9a20e672014-10-30 14:51:59 -070026#include <linux/netfilter/nfnetlink_log.h>
JP Abgralle6f80142011-07-14 16:46:32 -070027#include <linux/netfilter_ipv4/ipt_ULOG.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070028#include <linux/netlink.h>
29#include <linux/rtnetlink.h>
30#include <net/if.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070031#include <netinet/icmp6.h>
Lorenzo Colittie439ffc2017-10-03 18:44:11 +090032#include <netinet/in.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070033#include <stdlib.h>
34#include <string.h>
35#include <sys/socket.h>
36#include <sys/types.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
Mark Salyzyncfd5b082016-10-17 14:28:00 -070042#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070043#include <sysutils/NetlinkEvent.h>
Jeff Sharkey9a20e672014-10-30 14:51:59 -070044
San Mehat168415b2009-05-06 11:14:21 -070045NetlinkEvent::NetlinkEvent() {
Jeff Sharkeye4f39402015-03-13 13:27:33 -070046 mAction = Action::kUnknown;
San Mehatebfe3db2009-10-10 17:35:13 -070047 memset(mParams, 0, sizeof(mParams));
48 mPath = NULL;
49 mSubsystem = NULL;
San Mehat168415b2009-05-06 11:14:21 -070050}
51
52NetlinkEvent::~NetlinkEvent() {
53 int i;
54 if (mPath)
55 free(mPath);
56 if (mSubsystem)
57 free(mSubsystem);
58 for (i = 0; i < NL_PARAMS_MAX; i++) {
59 if (!mParams[i])
60 break;
61 free(mParams[i]);
62 }
63}
64
San Mehatd6744132009-12-24 07:17:09 -080065void NetlinkEvent::dump() {
66 int i;
67
68 for (i = 0; i < NL_PARAMS_MAX; i++) {
69 if (!mParams[i])
70 break;
San Mehat7e8529a2010-03-25 09:31:42 -070071 SLOGD("NL param '%s'\n", mParams[i]);
San Mehatd6744132009-12-24 07:17:09 -080072 }
73}
74
Mike J. Chenec16b9d2011-06-23 14:55:28 -070075/*
Lorenzo Colitti9b342932014-06-19 13:16:04 +090076 * Returns the message name for a message in the NETLINK_ROUTE family, or NULL
77 * if parsing that message is not supported.
78 */
79static const char *rtMessageName(int type) {
80#define NL_EVENT_RTM_NAME(rtm) case rtm: return #rtm;
81 switch (type) {
82 NL_EVENT_RTM_NAME(RTM_NEWLINK);
83 NL_EVENT_RTM_NAME(RTM_DELLINK);
84 NL_EVENT_RTM_NAME(RTM_NEWADDR);
85 NL_EVENT_RTM_NAME(RTM_DELADDR);
86 NL_EVENT_RTM_NAME(RTM_NEWROUTE);
87 NL_EVENT_RTM_NAME(RTM_DELROUTE);
88 NL_EVENT_RTM_NAME(RTM_NEWNDUSEROPT);
Jeff Sharkey9a20e672014-10-30 14:51:59 -070089 NL_EVENT_RTM_NAME(LOCAL_QLOG_NL_EVENT);
90 NL_EVENT_RTM_NAME(LOCAL_NFLOG_PACKET);
Lorenzo Colitti9b342932014-06-19 13:16:04 +090091 default:
92 return NULL;
93 }
94#undef NL_EVENT_RTM_NAME
95}
96
97/*
98 * Checks that a binary NETLINK_ROUTE message is long enough for a payload of
99 * size bytes.
100 */
101static bool checkRtNetlinkLength(const struct nlmsghdr *nh, size_t size) {
102 if (nh->nlmsg_len < NLMSG_LENGTH(size)) {
103 SLOGE("Got a short %s message\n", rtMessageName(nh->nlmsg_type));
104 return false;
105 }
106 return true;
107}
108
109/*
110 * Utility function to log errors.
111 */
112static bool maybeLogDuplicateAttribute(bool isDup,
113 const char *attributeName,
114 const char *messageName) {
115 if (isDup) {
116 SLOGE("Multiple %s attributes in %s, ignoring\n", attributeName, messageName);
117 return true;
118 }
119 return false;
120}
121
122/*
123 * Parse a RTM_NEWLINK message.
124 */
125bool NetlinkEvent::parseIfInfoMessage(const struct nlmsghdr *nh) {
126 struct ifinfomsg *ifi = (struct ifinfomsg *) NLMSG_DATA(nh);
127 if (!checkRtNetlinkLength(nh, sizeof(*ifi)))
128 return false;
129
130 if ((ifi->ifi_flags & IFF_LOOPBACK) != 0) {
131 return false;
132 }
133
134 int len = IFLA_PAYLOAD(nh);
135 struct rtattr *rta;
136 for (rta = IFLA_RTA(ifi); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
137 switch(rta->rta_type) {
138 case IFLA_IFNAME:
139 asprintf(&mParams[0], "INTERFACE=%s", (char *) RTA_DATA(rta));
Chenbo Feng5e5e5e92018-03-02 01:32:53 -0800140 // We can get the interface change information from sysfs update
141 // already. But in case we missed those message when devices start.
142 // We do a update again when received a kLinkUp event. To make
143 // the message consistent, use IFINDEX here as well since sysfs
144 // uses IFINDEX.
145 asprintf(&mParams[1], "IFINDEX=%d", ifi->ifi_index);
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700146 mAction = (ifi->ifi_flags & IFF_LOWER_UP) ? Action::kLinkUp :
147 Action::kLinkDown;
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900148 mSubsystem = strdup("net");
149 return true;
150 }
151 }
152
153 return false;
154}
155
156/*
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900157 * Parse a RTM_NEWADDR or RTM_DELADDR message.
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900158 */
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900159bool NetlinkEvent::parseIfAddrMessage(const struct nlmsghdr *nh) {
160 struct ifaddrmsg *ifaddr = (struct ifaddrmsg *) NLMSG_DATA(nh);
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900161 struct ifa_cacheinfo *cacheinfo = NULL;
162 char addrstr[INET6_ADDRSTRLEN] = "";
Lorenzo Colittief6454d2016-02-16 21:42:16 +0900163 char ifname[IFNAMSIZ] = "";
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900164
165 if (!checkRtNetlinkLength(nh, sizeof(*ifaddr)))
166 return false;
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900167
168 // Sanity check.
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900169 int type = nh->nlmsg_type;
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900170 if (type != RTM_NEWADDR && type != RTM_DELADDR) {
171 SLOGE("parseIfAddrMessage on incorrect message type 0x%x\n", type);
172 return false;
173 }
174
175 // For log messages.
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900176 const char *msgtype = rtMessageName(type);
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900177
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900178 struct rtattr *rta;
179 int len = IFA_PAYLOAD(nh);
180 for (rta = IFA_RTA(ifaddr); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900181 if (rta->rta_type == IFA_ADDRESS) {
182 // Only look at the first address, because we only support notifying
183 // one change at a time.
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900184 if (maybeLogDuplicateAttribute(*addrstr != '\0', "IFA_ADDRESS", msgtype))
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900185 continue;
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900186
187 // Convert the IP address to a string.
188 if (ifaddr->ifa_family == AF_INET) {
189 struct in_addr *addr4 = (struct in_addr *) RTA_DATA(rta);
190 if (RTA_PAYLOAD(rta) < sizeof(*addr4)) {
Mark Salyzyn80f63d42014-05-01 07:47:04 -0700191 SLOGE("Short IPv4 address (%zu bytes) in %s",
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900192 RTA_PAYLOAD(rta), msgtype);
193 continue;
194 }
195 inet_ntop(AF_INET, addr4, addrstr, sizeof(addrstr));
196 } else if (ifaddr->ifa_family == AF_INET6) {
197 struct in6_addr *addr6 = (struct in6_addr *) RTA_DATA(rta);
198 if (RTA_PAYLOAD(rta) < sizeof(*addr6)) {
Mark Salyzyn80f63d42014-05-01 07:47:04 -0700199 SLOGE("Short IPv6 address (%zu bytes) in %s",
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900200 RTA_PAYLOAD(rta), msgtype);
201 continue;
202 }
203 inet_ntop(AF_INET6, addr6, addrstr, sizeof(addrstr));
204 } else {
205 SLOGE("Unknown address family %d\n", ifaddr->ifa_family);
206 continue;
207 }
208
209 // Find the interface name.
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900210 if (!if_indextoname(ifaddr->ifa_index, ifname)) {
Lorenzo Colittief6454d2016-02-16 21:42:16 +0900211 SLOGD("Unknown ifindex %d in %s", ifaddr->ifa_index, msgtype);
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900212 }
213
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900214 } else if (rta->rta_type == IFA_CACHEINFO) {
215 // Address lifetime information.
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900216 if (maybeLogDuplicateAttribute(cacheinfo, "IFA_CACHEINFO", msgtype))
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900217 continue;
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900218
219 if (RTA_PAYLOAD(rta) < sizeof(*cacheinfo)) {
Mark Salyzyn80f63d42014-05-01 07:47:04 -0700220 SLOGE("Short IFA_CACHEINFO (%zu vs. %zu bytes) in %s",
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900221 RTA_PAYLOAD(rta), sizeof(cacheinfo), msgtype);
222 continue;
223 }
224
225 cacheinfo = (struct ifa_cacheinfo *) RTA_DATA(rta);
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900226 }
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900227 }
228
229 if (addrstr[0] == '\0') {
230 SLOGE("No IFA_ADDRESS in %s\n", msgtype);
231 return false;
232 }
233
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900234 // Fill in netlink event information.
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700235 mAction = (type == RTM_NEWADDR) ? Action::kAddressUpdated :
236 Action::kAddressRemoved;
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900237 mSubsystem = strdup("net");
Lorenzo Colittief6454d2016-02-16 21:42:16 +0900238 asprintf(&mParams[0], "ADDRESS=%s/%d", addrstr, ifaddr->ifa_prefixlen);
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900239 asprintf(&mParams[1], "INTERFACE=%s", ifname);
240 asprintf(&mParams[2], "FLAGS=%u", ifaddr->ifa_flags);
241 asprintf(&mParams[3], "SCOPE=%u", ifaddr->ifa_scope);
242
243 if (cacheinfo) {
244 asprintf(&mParams[4], "PREFERRED=%u", cacheinfo->ifa_prefered);
245 asprintf(&mParams[5], "VALID=%u", cacheinfo->ifa_valid);
246 asprintf(&mParams[6], "CSTAMP=%u", cacheinfo->cstamp);
247 asprintf(&mParams[7], "TSTAMP=%u", cacheinfo->tstamp);
248 }
249
250 return true;
251}
252
253/*
254 * Parse a QLOG_NL_EVENT message.
255 */
256bool NetlinkEvent::parseUlogPacketMessage(const struct nlmsghdr *nh) {
257 const char *devname;
258 ulog_packet_msg_t *pm = (ulog_packet_msg_t *) NLMSG_DATA(nh);
259 if (!checkRtNetlinkLength(nh, sizeof(*pm)))
260 return false;
261
262 devname = pm->indev_name[0] ? pm->indev_name : pm->outdev_name;
263 asprintf(&mParams[0], "ALERT_NAME=%s", pm->prefix);
264 asprintf(&mParams[1], "INTERFACE=%s", devname);
265 mSubsystem = strdup("qlog");
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700266 mAction = Action::kChange;
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900267 return true;
268}
269
Lorenzo Colittie439ffc2017-10-03 18:44:11 +0900270static size_t nlAttrLen(const nlattr* nla) {
271 return nla->nla_len - NLA_HDRLEN;
272}
273
274static const uint8_t* nlAttrData(const nlattr* nla) {
275 return reinterpret_cast<const uint8_t*>(nla) + NLA_HDRLEN;
276}
277
278static uint32_t nlAttrU32(const nlattr* nla) {
279 return *reinterpret_cast<const uint32_t*>(nlAttrData(nla));
280}
281
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900282/*
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700283 * Parse a LOCAL_NFLOG_PACKET message.
284 */
285bool NetlinkEvent::parseNfPacketMessage(struct nlmsghdr *nh) {
286 int uid = -1;
287 int len = 0;
288 char* raw = NULL;
289
Lorenzo Colittie439ffc2017-10-03 18:44:11 +0900290 struct nlattr* uid_attr = findNlAttr(nh, sizeof(struct genlmsghdr), NFULA_UID);
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700291 if (uid_attr) {
Lorenzo Colittie439ffc2017-10-03 18:44:11 +0900292 uid = ntohl(nlAttrU32(uid_attr));
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700293 }
294
Lorenzo Colittie439ffc2017-10-03 18:44:11 +0900295 struct nlattr* payload = findNlAttr(nh, sizeof(struct genlmsghdr), NFULA_PAYLOAD);
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700296 if (payload) {
297 /* First 256 bytes is plenty */
Lorenzo Colittie439ffc2017-10-03 18:44:11 +0900298 len = nlAttrLen(payload);
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700299 if (len > 256) len = 256;
Lorenzo Colittie439ffc2017-10-03 18:44:11 +0900300 raw = (char*)nlAttrData(payload);
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700301 }
302
303 char* hex = (char*) calloc(1, 5 + (len * 2));
304 strcpy(hex, "HEX=");
305 for (int i = 0; i < len; i++) {
306 hex[4 + (i * 2)] = "0123456789abcdef"[(raw[i] >> 4) & 0xf];
307 hex[5 + (i * 2)] = "0123456789abcdef"[raw[i] & 0xf];
308 }
309
310 asprintf(&mParams[0], "UID=%d", uid);
311 mParams[1] = hex;
312 mSubsystem = strdup("strict");
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700313 mAction = Action::kChange;
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700314 return true;
315}
316
317/*
Lorenzo Colittid7ff7ea2014-06-11 17:37:12 +0900318 * Parse a RTM_NEWROUTE or RTM_DELROUTE message.
319 */
320bool NetlinkEvent::parseRtMessage(const struct nlmsghdr *nh) {
321 uint8_t type = nh->nlmsg_type;
322 const char *msgname = rtMessageName(type);
323
324 // Sanity check.
325 if (type != RTM_NEWROUTE && type != RTM_DELROUTE) {
326 SLOGE("%s: incorrect message type %d (%s)\n", __func__, type, msgname);
327 return false;
328 }
329
330 struct rtmsg *rtm = (struct rtmsg *) NLMSG_DATA(nh);
331 if (!checkRtNetlinkLength(nh, sizeof(*rtm)))
332 return false;
333
334 if (// Ignore static routes we've set up ourselves.
335 (rtm->rtm_protocol != RTPROT_KERNEL &&
336 rtm->rtm_protocol != RTPROT_RA) ||
337 // We're only interested in global unicast routes.
338 (rtm->rtm_scope != RT_SCOPE_UNIVERSE) ||
339 (rtm->rtm_type != RTN_UNICAST) ||
340 // We don't support source routing.
341 (rtm->rtm_src_len != 0) ||
342 // Cloned routes aren't real routes.
343 (rtm->rtm_flags & RTM_F_CLONED)) {
344 return false;
345 }
346
347 int family = rtm->rtm_family;
348 int prefixLength = rtm->rtm_dst_len;
349
350 // Currently we only support: destination, (one) next hop, ifindex.
351 char dst[INET6_ADDRSTRLEN] = "";
352 char gw[INET6_ADDRSTRLEN] = "";
353 char dev[IFNAMSIZ] = "";
354
355 size_t len = RTM_PAYLOAD(nh);
356 struct rtattr *rta;
357 for (rta = RTM_RTA(rtm); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
358 switch (rta->rta_type) {
359 case RTA_DST:
360 if (maybeLogDuplicateAttribute(*dst, "RTA_DST", msgname))
361 continue;
362 if (!inet_ntop(family, RTA_DATA(rta), dst, sizeof(dst)))
363 return false;
364 continue;
365 case RTA_GATEWAY:
366 if (maybeLogDuplicateAttribute(*gw, "RTA_GATEWAY", msgname))
367 continue;
368 if (!inet_ntop(family, RTA_DATA(rta), gw, sizeof(gw)))
369 return false;
370 continue;
371 case RTA_OIF:
372 if (maybeLogDuplicateAttribute(*dev, "RTA_OIF", msgname))
373 continue;
374 if (!if_indextoname(* (int *) RTA_DATA(rta), dev))
375 return false;
376 default:
377 continue;
378 }
379 }
380
381 // If there's no RTA_DST attribute, then:
382 // - If the prefix length is zero, it's the default route.
383 // - If the prefix length is nonzero, there's something we don't understand.
384 // Ignore the event.
385 if (!*dst && !prefixLength) {
386 if (family == AF_INET) {
387 strncpy(dst, "0.0.0.0", sizeof(dst));
388 } else if (family == AF_INET6) {
389 strncpy(dst, "::", sizeof(dst));
390 }
391 }
392
393 // A useful route must have a destination and at least either a gateway or
394 // an interface.
395 if (!*dst || (!*gw && !*dev))
396 return false;
397
398 // Fill in netlink event information.
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700399 mAction = (type == RTM_NEWROUTE) ? Action::kRouteUpdated :
400 Action::kRouteRemoved;
Lorenzo Colittid7ff7ea2014-06-11 17:37:12 +0900401 mSubsystem = strdup("net");
402 asprintf(&mParams[0], "ROUTE=%s/%d", dst, prefixLength);
403 asprintf(&mParams[1], "GATEWAY=%s", (*gw) ? gw : "");
404 asprintf(&mParams[2], "INTERFACE=%s", (*dev) ? dev : "");
405
406 return true;
407}
408
409/*
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900410 * Parse a RTM_NEWNDUSEROPT message.
411 */
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900412bool NetlinkEvent::parseNdUserOptMessage(const struct nlmsghdr *nh) {
413 struct nduseroptmsg *msg = (struct nduseroptmsg *) NLMSG_DATA(nh);
414 if (!checkRtNetlinkLength(nh, sizeof(*msg)))
415 return false;
416
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900417 // Check the length is valid.
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900418 int len = NLMSG_PAYLOAD(nh, sizeof(*msg));
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900419 if (msg->nduseropt_opts_len > len) {
420 SLOGE("RTM_NEWNDUSEROPT invalid length %d > %d\n",
421 msg->nduseropt_opts_len, len);
422 return false;
423 }
424 len = msg->nduseropt_opts_len;
425
426 // Check address family and packet type.
427 if (msg->nduseropt_family != AF_INET6) {
428 SLOGE("RTM_NEWNDUSEROPT message for unknown family %d\n",
429 msg->nduseropt_family);
430 return false;
431 }
432
433 if (msg->nduseropt_icmp_type != ND_ROUTER_ADVERT ||
434 msg->nduseropt_icmp_code != 0) {
435 SLOGE("RTM_NEWNDUSEROPT message for unknown ICMPv6 type/code %d/%d\n",
436 msg->nduseropt_icmp_type, msg->nduseropt_icmp_code);
437 return false;
438 }
439
440 // Find the interface name.
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900441 char ifname[IFNAMSIZ];
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900442 if (!if_indextoname(msg->nduseropt_ifindex, ifname)) {
443 SLOGE("RTM_NEWNDUSEROPT on unknown ifindex %d\n",
444 msg->nduseropt_ifindex);
445 return false;
446 }
447
448 // The kernel sends a separate netlink message for each ND option in the RA.
449 // So only parse the first ND option in the message.
450 struct nd_opt_hdr *opthdr = (struct nd_opt_hdr *) (msg + 1);
451
452 // The length is in multiples of 8 octets.
453 uint16_t optlen = opthdr->nd_opt_len;
454 if (optlen * 8 > len) {
455 SLOGE("Invalid option length %d > %d for ND option %d\n",
456 optlen * 8, len, opthdr->nd_opt_type);
457 return false;
458 }
459
460 if (opthdr->nd_opt_type == ND_OPT_RDNSS) {
461 // DNS Servers (RFC 6106).
462 // Each address takes up 2*8 octets, and the header takes up 8 octets.
463 // So for a valid option with one or more addresses, optlen must be
464 // odd and greater than 1.
465 if ((optlen < 3) || !(optlen & 0x1)) {
466 SLOGE("Invalid optlen %d for RDNSS option\n", optlen);
467 return false;
468 }
Erik Klineba48ff72015-06-17 15:53:29 +0900469 const int numaddrs = (optlen - 1) / 2;
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900470
471 // Find the lifetime.
472 struct nd_opt_rdnss *rndss_opt = (struct nd_opt_rdnss *) opthdr;
Erik Klineba48ff72015-06-17 15:53:29 +0900473 const uint32_t lifetime = ntohl(rndss_opt->nd_opt_rdnss_lifetime);
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900474
475 // Construct "SERVERS=<comma-separated string of DNS addresses>".
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900476 static const char kServerTag[] = "SERVERS=";
Erik Klinecc451782015-07-28 17:31:19 +0900477 static const size_t kTagLength = strlen(kServerTag);
Erik Klineba48ff72015-06-17 15:53:29 +0900478 // Reserve sufficient space for an IPv6 link-local address: all but the
479 // last address are followed by ','; the last is followed by '\0'.
Erik Klinecc451782015-07-28 17:31:19 +0900480 static const size_t kMaxSingleAddressLength =
Erik Klineba48ff72015-06-17 15:53:29 +0900481 INET6_ADDRSTRLEN + strlen("%") + IFNAMSIZ + strlen(",");
Erik Klinecc451782015-07-28 17:31:19 +0900482 const size_t bufsize = kTagLength + numaddrs * kMaxSingleAddressLength;
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900483 char *buf = (char *) malloc(bufsize);
484 if (!buf) {
485 SLOGE("RDNSS option: out of memory\n");
486 return false;
487 }
488 strcpy(buf, kServerTag);
Erik Klinecc451782015-07-28 17:31:19 +0900489 size_t pos = kTagLength;
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900490
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);
Erik Klineba48ff72015-06-17 15:53:29 +0900498 if (IN6_IS_ADDR_LINKLOCAL(addrs + i)) {
499 buf[pos++] = '%';
500 pos += strlcpy(buf + pos, ifname, bufsize - pos);
501 }
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900502 }
503 buf[pos] = '\0';
504
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700505 mAction = Action::kRdnss;
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900506 mSubsystem = strdup("net");
507 asprintf(&mParams[0], "INTERFACE=%s", ifname);
508 asprintf(&mParams[1], "LIFETIME=%u", lifetime);
509 mParams[2] = buf;
Lorenzo Colitticbfd65d2017-11-28 15:32:40 +0900510 } else if (opthdr->nd_opt_type == ND_OPT_DNSSL) {
511 // TODO: support DNSSL.
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900512 } else {
513 SLOGD("Unknown ND option type %d\n", opthdr->nd_opt_type);
514 return false;
515 }
516
517 return true;
518}
519
520/*
521 * Parse a binary message from a NETLINK_ROUTE netlink socket.
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900522 *
523 * Note that this function can only parse one message, because the message's
524 * content has to be stored in the class's member variables (mAction,
525 * mSubsystem, etc.). Invalid or unrecognized messages are skipped, but if
526 * there are multiple valid messages in the buffer, only the first one will be
527 * returned.
528 *
529 * TODO: consider only ever looking at the first message.
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700530 */
531bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) {
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700532 struct nlmsghdr *nh;
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700533
Lorenzo Colitti96834562013-08-17 03:40:31 +0900534 for (nh = (struct nlmsghdr *) buffer;
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900535 NLMSG_OK(nh, (unsigned) size) && (nh->nlmsg_type != NLMSG_DONE);
Lorenzo Colitti96834562013-08-17 03:40:31 +0900536 nh = NLMSG_NEXT(nh, size)) {
JP Abgralle6f80142011-07-14 16:46:32 -0700537
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900538 if (!rtMessageName(nh->nlmsg_type)) {
539 SLOGD("Unexpected netlink message type %d\n", nh->nlmsg_type);
540 continue;
541 }
542
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700543 if (nh->nlmsg_type == RTM_NEWLINK) {
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900544 if (parseIfInfoMessage(nh))
545 return true;
JP Abgralle6f80142011-07-14 16:46:32 -0700546
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700547 } else if (nh->nlmsg_type == LOCAL_QLOG_NL_EVENT) {
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900548 if (parseUlogPacketMessage(nh))
549 return true;
JP Abgralle6f80142011-07-14 16:46:32 -0700550
Lorenzo Colitti381f70f2013-08-02 05:58:37 +0900551 } else if (nh->nlmsg_type == RTM_NEWADDR ||
552 nh->nlmsg_type == RTM_DELADDR) {
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900553 if (parseIfAddrMessage(nh))
554 return true;
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900555
Lorenzo Colittid7ff7ea2014-06-11 17:37:12 +0900556 } else if (nh->nlmsg_type == RTM_NEWROUTE ||
557 nh->nlmsg_type == RTM_DELROUTE) {
558 if (parseRtMessage(nh))
559 return true;
560
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900561 } else if (nh->nlmsg_type == RTM_NEWNDUSEROPT) {
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900562 if (parseNdUserOptMessage(nh))
563 return true;
Lorenzo Colittic7eec832013-08-12 17:03:32 +0900564
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700565 } else if (nh->nlmsg_type == LOCAL_NFLOG_PACKET) {
566 if (parseNfPacketMessage(nh))
567 return true;
568
JP Abgralle6f80142011-07-14 16:46:32 -0700569 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700570 }
571
Lorenzo Colitti9b342932014-06-19 13:16:04 +0900572 return false;
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700573}
574
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100575/* If the string between 'str' and 'end' begins with 'prefixlen' characters
576 * from the 'prefix' array, then return 'str + prefixlen', otherwise return
577 * NULL.
578 */
579static const char*
580has_prefix(const char* str, const char* end, const char* prefix, size_t prefixlen)
581{
Yunlian Jiang33f67172017-02-07 15:39:25 -0800582 if ((end - str) >= (ptrdiff_t)prefixlen &&
583 (prefixlen == 0 || !memcmp(str, prefix, prefixlen))) {
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100584 return str + prefixlen;
Yunlian Jiang33f67172017-02-07 15:39:25 -0800585 } else {
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100586 return NULL;
Yunlian Jiang33f67172017-02-07 15:39:25 -0800587 }
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100588}
589
590/* Same as strlen(x) for constant string literals ONLY */
591#define CONST_STRLEN(x) (sizeof(x)-1)
592
593/* Convenience macro to call has_prefix with a constant string literal */
594#define HAS_CONST_PREFIX(str,end,prefix) has_prefix((str),(end),prefix,CONST_STRLEN(prefix))
595
596
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700597/*
598 * Parse an ASCII-formatted message from a NETLINK_KOBJECT_UEVENT
599 * netlink socket.
600 */
601bool NetlinkEvent::parseAsciiNetlinkMessage(char *buffer, int size) {
Mike J. Chen17260b12011-06-23 15:00:30 -0700602 const char *s = buffer;
603 const char *end;
San Mehat168415b2009-05-06 11:14:21 -0700604 int param_idx = 0;
San Mehat168415b2009-05-06 11:14:21 -0700605 int first = 1;
606
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100607 if (size == 0)
608 return false;
609
610 /* Ensure the buffer is zero-terminated, the code below depends on this */
611 buffer[size-1] = '\0';
612
San Mehat168415b2009-05-06 11:14:21 -0700613 end = s + size;
614 while (s < end) {
615 if (first) {
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100616 const char *p;
617 /* buffer is 0-terminated, no need to check p < end */
618 for (p = s; *p != '@'; p++) {
619 if (!*p) { /* no '@', should not happen */
620 return false;
621 }
622 }
623 mPath = strdup(p+1);
San Mehat168415b2009-05-06 11:14:21 -0700624 first = 0;
625 } else {
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100626 const char* a;
627 if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != NULL) {
San Mehat168415b2009-05-06 11:14:21 -0700628 if (!strcmp(a, "add"))
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700629 mAction = Action::kAdd;
San Mehat168415b2009-05-06 11:14:21 -0700630 else if (!strcmp(a, "remove"))
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700631 mAction = Action::kRemove;
San Mehat168415b2009-05-06 11:14:21 -0700632 else if (!strcmp(a, "change"))
Jeff Sharkeye4f39402015-03-13 13:27:33 -0700633 mAction = Action::kChange;
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100634 } else if ((a = HAS_CONST_PREFIX(s, end, "SEQNUM=")) != NULL) {
635 mSeq = atoi(a);
636 } else if ((a = HAS_CONST_PREFIX(s, end, "SUBSYSTEM=")) != NULL) {
637 mSubsystem = strdup(a);
638 } else if (param_idx < NL_PARAMS_MAX) {
San Mehat168415b2009-05-06 11:14:21 -0700639 mParams[param_idx++] = strdup(s);
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100640 }
San Mehat168415b2009-05-06 11:14:21 -0700641 }
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100642 s += strlen(s) + 1;
San Mehat168415b2009-05-06 11:14:21 -0700643 }
644 return true;
645}
646
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700647bool NetlinkEvent::decode(char *buffer, int size, int format) {
Jeff Sharkey9a20e672014-10-30 14:51:59 -0700648 if (format == NetlinkListener::NETLINK_FORMAT_BINARY
649 || format == NetlinkListener::NETLINK_FORMAT_BINARY_UNICAST) {
Mike J. Chen17260b12011-06-23 15:00:30 -0700650 return parseBinaryNetlinkMessage(buffer, size);
651 } else {
652 return parseAsciiNetlinkMessage(buffer, size);
653 }
Mike J. Chenec16b9d2011-06-23 14:55:28 -0700654}
655
San Mehat168415b2009-05-06 11:14:21 -0700656const char *NetlinkEvent::findParam(const char *paramName) {
Chih-Wei Huang80ec37a2010-07-14 14:00:41 +0800657 size_t len = strlen(paramName);
David 'Digit' Turner3311eea2011-01-17 01:59:22 +0100658 for (int i = 0; i < NL_PARAMS_MAX && mParams[i] != NULL; ++i) {
Chih-Wei Huang80ec37a2010-07-14 14:00:41 +0800659 const char *ptr = mParams[i] + len;
660 if (!strncmp(mParams[i], paramName, len) && *ptr == '=')
661 return ++ptr;
San Mehat168415b2009-05-06 11:14:21 -0700662 }
663
San Mehat7e8529a2010-03-25 09:31:42 -0700664 SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName);
San Mehat168415b2009-05-06 11:14:21 -0700665 return NULL;
666}
Lorenzo Colittie439ffc2017-10-03 18:44:11 +0900667
668nlattr* NetlinkEvent::findNlAttr(const nlmsghdr* nh, size_t hdrlen, uint16_t attr) {
669 if (nh == nullptr || NLMSG_HDRLEN + NLMSG_ALIGN(hdrlen) > SSIZE_MAX) {
670 return nullptr;
671 }
672
673 // Skip header, padding, and family header.
674 const ssize_t NLA_START = NLMSG_HDRLEN + NLMSG_ALIGN(hdrlen);
675 ssize_t left = nh->nlmsg_len - NLA_START;
676 uint8_t* hdr = ((uint8_t*)nh) + NLA_START;
677
678 while (left >= NLA_HDRLEN) {
679 nlattr* nla = (nlattr*)hdr;
680 if (nla->nla_type == attr) {
681 return nla;
682 }
683
684 hdr += NLA_ALIGN(nla->nla_len);
685 left -= NLA_ALIGN(nla->nla_len);
686 }
687
688 return nullptr;
689}