blob: 85fbe175724cd039cb9c9718b9e208ab69667a6c [file] [log] [blame]
San Mehatd1830422010-01-15 08:02:39 -08001/*
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
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +090017#include <stdarg.h>
San Mehatd1830422010-01-15 08:02:39 -080018#include <stdio.h>
19#include <stdlib.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080020#include <string.h>
San Mehatd1830422010-01-15 08:02:39 -080021#include <errno.h>
22
23#define LOG_TAG "Netd"
24
Logan Chien3f461482018-04-23 14:31:32 +080025#include <log/log.h>
San Mehatd1830422010-01-15 08:02:39 -080026
Sreeram Ramachandranaf37dd32014-09-08 16:03:18 -070027#include <netutils/ifc.h>
San Mehatd1830422010-01-15 08:02:39 -080028#include <sysutils/NetlinkEvent.h>
29#include "NetlinkHandler.h"
Robert Greenwalt67c57532010-02-17 17:42:37 -080030#include "NetlinkManager.h"
31#include "ResponseCode.h"
Lorenzo Colittif32fc592016-02-15 01:09:14 +090032#include "SockDiag.h"
Chenbo Feng7e974052018-02-28 22:57:21 -080033#include "Controllers.h"
San Mehatd1830422010-01-15 08:02:39 -080034
Lorenzo Colittibd0f2242014-06-12 13:51:05 +090035static const char *kUpdated = "updated";
36static const char *kRemoved = "removed";
37
Lorenzo Colitti7035f222017-02-13 18:29:00 +090038namespace android {
39namespace net {
40
Mike J. Chen564df4e2011-06-23 15:07:35 -070041NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
42 int format) :
43 NetlinkListener(listenerSocket, format) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080044 mNm = nm;
San Mehatd1830422010-01-15 08:02:39 -080045}
46
47NetlinkHandler::~NetlinkHandler() {
48}
49
50int NetlinkHandler::start() {
51 return this->startListener();
52}
53
54int NetlinkHandler::stop() {
55 return this->stopListener();
56}
57
Rubin Xu6c00b612018-04-27 14:27:59 +010058static long parseIfIndex(const char* ifIndex) {
59 if (ifIndex == nullptr) {
60 return 0;
61 }
Yi Kongbdfd57e2018-07-25 13:26:10 -070062 long ifaceIndex = strtol(ifIndex, nullptr, 10);
Rubin Xu6c00b612018-04-27 14:27:59 +010063 // strtol returns 0 on error, which is fine because 0 is not a valid ifindex.
64 if (errno == ERANGE && (ifaceIndex == LONG_MAX || ifaceIndex == LONG_MIN)) {
65 return 0;
66 }
67 return ifaceIndex;
68}
69
San Mehatd1830422010-01-15 08:02:39 -080070void NetlinkHandler::onEvent(NetlinkEvent *evt) {
71 const char *subsys = evt->getSubsystem();
San Mehatd1830422010-01-15 08:02:39 -080072 if (!subsys) {
Steve Block0e76b762012-01-05 23:21:51 +000073 ALOGW("No subsystem found in netlink event");
San Mehatd1830422010-01-15 08:02:39 -080074 return;
75 }
Mike J. Chen564df4e2011-06-23 15:07:35 -070076
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090077 if (!strcmp(subsys, "net")) {
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070078 NetlinkEvent::Action action = evt->getAction();
Mike J. Chen564df4e2011-06-23 15:07:35 -070079 const char *iface = evt->findParam("INTERFACE");
Chenbo Feng04f3b172018-03-20 16:39:43 -070080 if ((action == NetlinkEvent::Action::kAdd) ||
81 (action == NetlinkEvent::Action::kLinkUp) ||
82 (action == NetlinkEvent::Action::kLinkDown)) {
83 const char *ifIndex = evt->findParam("IFINDEX");
Rubin Xu6c00b612018-04-27 14:27:59 +010084 long ifaceIndex = parseIfIndex(ifIndex);
85 if (ifaceIndex) {
86 gCtls->trafficCtrl.addInterface(iface, ifaceIndex);
87 } else {
88 ALOGE("invalid interface index: %s(%s)", iface, ifIndex);
Chenbo Feng04f3b172018-03-20 16:39:43 -070089 }
Chenbo Feng7e974052018-02-28 22:57:21 -080090 }
Mike J. Chen564df4e2011-06-23 15:07:35 -070091
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070092 if (action == NetlinkEvent::Action::kAdd) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080093 notifyInterfaceAdded(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070094 } else if (action == NetlinkEvent::Action::kRemove) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080095 notifyInterfaceRemoved(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070096 } else if (action == NetlinkEvent::Action::kChange) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080097 evt->dump();
Robert Greenwalt67c57532010-02-17 17:42:37 -080098 notifyInterfaceChanged("nana", true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070099 } else if (action == NetlinkEvent::Action::kLinkUp) {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700100 notifyInterfaceLinkChanged(iface, true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700101 } else if (action == NetlinkEvent::Action::kLinkDown) {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700102 notifyInterfaceLinkChanged(iface, false);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700103 } else if (action == NetlinkEvent::Action::kAddressUpdated ||
104 action == NetlinkEvent::Action::kAddressRemoved) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900105 const char *address = evt->findParam("ADDRESS");
106 const char *flags = evt->findParam("FLAGS");
107 const char *scope = evt->findParam("SCOPE");
Rubin Xu6c00b612018-04-27 14:27:59 +0100108 const char *ifIndex = evt->findParam("IFINDEX");
109 char addrstr[INET6_ADDRSTRLEN + strlen("/128")];
110 strlcpy(addrstr, address, sizeof(addrstr));
111 char *slash = strchr(addrstr, '/');
112 if (slash) {
113 *slash = '\0';
114 }
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900115
Rubin Xu6c00b612018-04-27 14:27:59 +0100116 long ifaceIndex = parseIfIndex(ifIndex);
117 if (!ifaceIndex) {
118 ALOGE("invalid interface index: %s(%s)", iface, ifIndex);
119 }
120 if (action == NetlinkEvent::Action::kAddressUpdated) {
121 gCtls->netCtrl.addInterfaceAddress(ifaceIndex, address);
122 } else { // action == NetlinkEvent::Action::kAddressRemoved
123 bool shouldDestroy = gCtls->netCtrl.removeInterfaceAddress(ifaceIndex, address);
124 if (shouldDestroy) {
125 SockDiag sd;
126 if (sd.open()) {
127 int ret = sd.destroySockets(addrstr);
128 if (ret < 0) {
129 ALOGE("Error destroying sockets: %s", strerror(-ret));
130 }
131 } else {
132 ALOGE("Error opening NETLINK_SOCK_DIAG socket: %s", strerror(errno));
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900133 }
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900134 }
Sreeram Ramachandranaf37dd32014-09-08 16:03:18 -0700135 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100136 // Note: if this interface was deleted, iface is "" and we don't notify.
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900137 if (iface && iface[0] && address && flags && scope) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900138 notifyAddressChanged(action, address, iface, flags, scope);
139 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700140 } else if (action == NetlinkEvent::Action::kRdnss) {
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900141 const char *lifetime = evt->findParam("LIFETIME");
142 const char *servers = evt->findParam("SERVERS");
143 if (lifetime && servers) {
144 notifyInterfaceDnsServers(iface, lifetime, servers);
145 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700146 } else if (action == NetlinkEvent::Action::kRouteUpdated ||
147 action == NetlinkEvent::Action::kRouteRemoved) {
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900148 const char *route = evt->findParam("ROUTE");
149 const char *gateway = evt->findParam("GATEWAY");
150 const char *iface = evt->findParam("INTERFACE");
151 if (route && (gateway || iface)) {
152 notifyRouteChange(action, route, gateway, iface);
153 }
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900154 }
155
Bryse Flowersbc5f0602016-06-01 13:00:12 -0700156 } else if (!strcmp(subsys, "qlog") || !strcmp(subsys, "xt_quota2")) {
JP Abgralle0ebc462011-07-21 17:21:49 -0700157 const char *alertName = evt->findParam("ALERT_NAME");
158 const char *iface = evt->findParam("INTERFACE");
159 notifyQuotaLimitReached(alertName, iface);
JP Abgralle07effe2012-04-27 00:02:53 -0700160
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700161 } else if (!strcmp(subsys, "strict")) {
162 const char *uid = evt->findParam("UID");
163 const char *hex = evt->findParam("HEX");
164 notifyStrictCleartext(uid, hex);
165
JP Abgralle07effe2012-04-27 00:02:53 -0700166 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharmac79bcc42014-02-12 11:53:00 -0800167 const char *label = evt->findParam("INTERFACE");
JP Abgralle07effe2012-04-27 00:02:53 -0700168 const char *state = evt->findParam("STATE");
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700169 const char *timestamp = evt->findParam("TIME_NS");
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700170 const char *uid = evt->findParam("UID");
JP Abgralle07effe2012-04-27 00:02:53 -0700171 if (state)
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700172 notifyInterfaceClassActivity(label, !strcmp("active", state),
173 timestamp, uid);
JP Abgralle0ebc462011-07-21 17:21:49 -0700174
JP Abgrall40baed82012-05-08 14:48:45 -0700175#if !LOG_NDEBUG
176 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
177 /* It is not a VSYNC or a backlight event */
178 ALOGV("unexpected event from subsystem %s", subsys);
179#endif
Ashish Sharma6337b882012-04-10 19:47:09 -0700180 }
San Mehatd1830422010-01-15 08:02:39 -0800181}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800182
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900183// NOLINTNEXTLINE(cert-dcl50-cpp): Grandfathered C-style variadic function.
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900184void NetlinkHandler::notify(int code, const char *format, ...) {
185 char *msg;
186 va_list args;
187 va_start(args, format);
188 if (vasprintf(&msg, format, args) >= 0) {
189 mNm->getBroadcaster()->sendBroadcast(code, msg, false);
190 free(msg);
191 } else {
192 SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
193 }
194 va_end(args);
195}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800196
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900197void NetlinkHandler::notifyInterfaceAdded(const char *name) {
198 notify(ResponseCode::InterfaceChange, "Iface added %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800199}
200
201void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900202 notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800203}
204
205void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900206 notify(ResponseCode::InterfaceChange,
207 "Iface changed %s %s", name, (isUp ? "up" : "down"));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700208}
209
210void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900211 notify(ResponseCode::InterfaceChange,
212 "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
Robert Greenwalt67c57532010-02-17 17:42:37 -0800213}
JP Abgralle0ebc462011-07-21 17:21:49 -0700214
215void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900216 notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
JP Abgralle0ebc462011-07-21 17:21:49 -0700217}
Ashish Sharma6337b882012-04-10 19:47:09 -0700218
Haoyu Bai98f65d32012-06-28 16:16:51 -0700219void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700220 bool isActive,
221 const char *timestamp,
222 const char *uid) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700223 if (timestamp == nullptr)
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700224 notify(ResponseCode::InterfaceClassActivity,
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900225 "IfaceClass %s %s", isActive ? "active" : "idle", name);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700226 else if (uid != nullptr && isActive)
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700227 notify(ResponseCode::InterfaceClassActivity,
228 "IfaceClass active %s %s %s", name, timestamp, uid);
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700229 else
230 notify(ResponseCode::InterfaceClassActivity,
231 "IfaceClass %s %s %s", isActive ? "active" : "idle", name, timestamp);
Ashish Sharma6337b882012-04-10 19:47:09 -0700232}
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900233
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700234void NetlinkHandler::notifyAddressChanged(NetlinkEvent::Action action, const char *addr,
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900235 const char *iface, const char *flags,
236 const char *scope) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900237 notify(ResponseCode::InterfaceAddressChange,
238 "Address %s %s %s %s %s",
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700239 (action == NetlinkEvent::Action::kAddressUpdated) ? kUpdated : kRemoved,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900240 addr, iface, flags, scope);
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900241}
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900242
243void NetlinkHandler::notifyInterfaceDnsServers(const char *iface,
244 const char *lifetime,
245 const char *servers) {
246 notify(ResponseCode::InterfaceDnsInfo, "DnsInfo servers %s %s %s",
247 iface, lifetime, servers);
248}
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900249
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700250void NetlinkHandler::notifyRouteChange(NetlinkEvent::Action action, const char *route,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900251 const char *gateway, const char *iface) {
252 notify(ResponseCode::RouteChange,
253 "Route %s %s%s%s%s%s",
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700254 (action == NetlinkEvent::Action::kRouteUpdated) ? kUpdated : kRemoved,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900255 route,
Manoj Gupta75c970f2016-11-22 21:15:59 -0800256 (gateway && *gateway) ? " via " : "",
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900257 gateway,
Manoj Gupta75c970f2016-11-22 21:15:59 -0800258 (iface && *iface) ? " dev " : "",
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900259 iface);
260}
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700261
262void NetlinkHandler::notifyStrictCleartext(const char* uid, const char* hex) {
263 notify(ResponseCode::StrictCleartext, "%s %s", uid, hex);
264}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900265
266} // namespace net
267} // namespace android