blob: cd5b0d569b2f6c522402164fab07e1601a5a6b33 [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
Luke Huang528af602018-08-29 19:06:05 +080027#include <android-base/parseint.h>
28#include <android-base/strings.h>
Sreeram Ramachandranaf37dd32014-09-08 16:03:18 -070029#include <netutils/ifc.h>
San Mehatd1830422010-01-15 08:02:39 -080030#include <sysutils/NetlinkEvent.h>
Luke Huang528af602018-08-29 19:06:05 +080031#include "Controllers.h"
San Mehatd1830422010-01-15 08:02:39 -080032#include "NetlinkHandler.h"
Robert Greenwalt67c57532010-02-17 17:42:37 -080033#include "NetlinkManager.h"
Lorenzo Colittif32fc592016-02-15 01:09:14 +090034#include "SockDiag.h"
San Mehatd1830422010-01-15 08:02:39 -080035
Luke Huang528af602018-08-29 19:06:05 +080036#include <charconv>
37
38#define BINDER_RETRY(exp) \
39 ({ \
40 bool res = true; \
41 for (int attempt = 0; /*nop*/; ++attempt) { \
42 auto _rc = (exp); \
43 if (_rc.exceptionCode() == binder::Status::EX_TRANSACTION_FAILED && \
44 attempt < RETRY_ATTEMPTS) { \
45 usleep(RETRY_INTERVAL_MICRO_S); \
46 } else { \
47 res = _rc.isOk(); \
48 break; \
49 } \
50 } \
51 res; \
52 })
53
Luke Huang900f18e2019-01-31 16:50:17 +080054#define LOG_EVENT_FUNC(retry, func, ...) \
55 do { \
56 const auto listenerMap = gCtls->eventReporter.getNetdUnsolicitedEventListenerMap(); \
57 for (auto& listener : listenerMap) { \
58 auto entry = gUnsolicitedLog.newEntry().function(#func).args(__VA_ARGS__); \
59 if (retry(listener.first->func(__VA_ARGS__))) { \
60 gUnsolicitedLog.log(entry.withAutomaticDuration()); \
61 } \
62 } \
Luke Huang528af602018-08-29 19:06:05 +080063 } while (0)
Lorenzo Colittibd0f2242014-06-12 13:51:05 +090064
Lorenzo Colitti7035f222017-02-13 18:29:00 +090065namespace android {
66namespace net {
67
Luke Huang528af602018-08-29 19:06:05 +080068constexpr int RETRY_ATTEMPTS = 2;
69constexpr int RETRY_INTERVAL_MICRO_S = 100000;
70
Mike J. Chen564df4e2011-06-23 15:07:35 -070071NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
72 int format) :
73 NetlinkListener(listenerSocket, format) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080074 mNm = nm;
San Mehatd1830422010-01-15 08:02:39 -080075}
76
77NetlinkHandler::~NetlinkHandler() {
78}
79
80int NetlinkHandler::start() {
81 return this->startListener();
82}
83
84int NetlinkHandler::stop() {
85 return this->stopListener();
86}
87
Rubin Xu6c00b612018-04-27 14:27:59 +010088static long parseIfIndex(const char* ifIndex) {
89 if (ifIndex == nullptr) {
90 return 0;
91 }
Yi Kongbdfd57e2018-07-25 13:26:10 -070092 long ifaceIndex = strtol(ifIndex, nullptr, 10);
Rubin Xu6c00b612018-04-27 14:27:59 +010093 // strtol returns 0 on error, which is fine because 0 is not a valid ifindex.
94 if (errno == ERANGE && (ifaceIndex == LONG_MAX || ifaceIndex == LONG_MIN)) {
95 return 0;
96 }
97 return ifaceIndex;
98}
99
San Mehatd1830422010-01-15 08:02:39 -0800100void NetlinkHandler::onEvent(NetlinkEvent *evt) {
101 const char *subsys = evt->getSubsystem();
San Mehatd1830422010-01-15 08:02:39 -0800102 if (!subsys) {
Steve Block0e76b762012-01-05 23:21:51 +0000103 ALOGW("No subsystem found in netlink event");
San Mehatd1830422010-01-15 08:02:39 -0800104 return;
105 }
Mike J. Chen564df4e2011-06-23 15:07:35 -0700106
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900107 if (!strcmp(subsys, "net")) {
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700108 NetlinkEvent::Action action = evt->getAction();
Mike J. Chen564df4e2011-06-23 15:07:35 -0700109 const char *iface = evt->findParam("INTERFACE");
Chenbo Feng04f3b172018-03-20 16:39:43 -0700110 if ((action == NetlinkEvent::Action::kAdd) ||
111 (action == NetlinkEvent::Action::kLinkUp) ||
112 (action == NetlinkEvent::Action::kLinkDown)) {
113 const char *ifIndex = evt->findParam("IFINDEX");
Rubin Xu6c00b612018-04-27 14:27:59 +0100114 long ifaceIndex = parseIfIndex(ifIndex);
115 if (ifaceIndex) {
116 gCtls->trafficCtrl.addInterface(iface, ifaceIndex);
117 } else {
118 ALOGE("invalid interface index: %s(%s)", iface, ifIndex);
Chenbo Feng04f3b172018-03-20 16:39:43 -0700119 }
Chenbo Feng7e974052018-02-28 22:57:21 -0800120 }
Mike J. Chen564df4e2011-06-23 15:07:35 -0700121
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700122 if (action == NetlinkEvent::Action::kAdd) {
Robert Greenwalt67c57532010-02-17 17:42:37 -0800123 notifyInterfaceAdded(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700124 } else if (action == NetlinkEvent::Action::kRemove) {
Robert Greenwalt67c57532010-02-17 17:42:37 -0800125 notifyInterfaceRemoved(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700126 } else if (action == NetlinkEvent::Action::kChange) {
Robert Greenwalt67c57532010-02-17 17:42:37 -0800127 evt->dump();
Pavan Kumar M08167012018-10-22 12:31:23 +0530128 const char *alertName = evt->findParam("ALERT_NAME");
129 const char *iface = evt->findParam("INTERFACE");
130 if (alertName != NULL && iface != NULL) {
131 ALOGI("Alertname : %s iface (%s)", alertName, iface);
132 if (!strcmp(alertName, "quotaReachedAlert") && iface) {
133 notifyQuotaLimitReached(alertName, iface);
134 }
135 } else {
136 notifyInterfaceChanged("nana", true);
137 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700138 } else if (action == NetlinkEvent::Action::kLinkUp) {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700139 notifyInterfaceLinkChanged(iface, true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700140 } else if (action == NetlinkEvent::Action::kLinkDown) {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700141 notifyInterfaceLinkChanged(iface, false);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700142 } else if (action == NetlinkEvent::Action::kAddressUpdated ||
143 action == NetlinkEvent::Action::kAddressRemoved) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900144 const char *address = evt->findParam("ADDRESS");
145 const char *flags = evt->findParam("FLAGS");
146 const char *scope = evt->findParam("SCOPE");
Rubin Xu6c00b612018-04-27 14:27:59 +0100147 const char *ifIndex = evt->findParam("IFINDEX");
148 char addrstr[INET6_ADDRSTRLEN + strlen("/128")];
149 strlcpy(addrstr, address, sizeof(addrstr));
150 char *slash = strchr(addrstr, '/');
151 if (slash) {
152 *slash = '\0';
153 }
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900154
Rubin Xu6c00b612018-04-27 14:27:59 +0100155 long ifaceIndex = parseIfIndex(ifIndex);
156 if (!ifaceIndex) {
157 ALOGE("invalid interface index: %s(%s)", iface, ifIndex);
158 }
Luke Huang528af602018-08-29 19:06:05 +0800159 const bool addrUpdated = (action == NetlinkEvent::Action::kAddressUpdated);
160 if (addrUpdated) {
Rubin Xu6c00b612018-04-27 14:27:59 +0100161 gCtls->netCtrl.addInterfaceAddress(ifaceIndex, address);
162 } else { // action == NetlinkEvent::Action::kAddressRemoved
163 bool shouldDestroy = gCtls->netCtrl.removeInterfaceAddress(ifaceIndex, address);
164 if (shouldDestroy) {
165 SockDiag sd;
166 if (sd.open()) {
167 int ret = sd.destroySockets(addrstr);
168 if (ret < 0) {
169 ALOGE("Error destroying sockets: %s", strerror(-ret));
170 }
171 } else {
172 ALOGE("Error opening NETLINK_SOCK_DIAG socket: %s", strerror(errno));
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900173 }
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900174 }
Sreeram Ramachandranaf37dd32014-09-08 16:03:18 -0700175 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100176 // Note: if this interface was deleted, iface is "" and we don't notify.
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900177 if (iface && iface[0] && address && flags && scope) {
Luke Huang528af602018-08-29 19:06:05 +0800178 if (addrUpdated) {
179 notifyAddressUpdated(address, iface, std::stoi(flags), std::stoi(scope));
180 } else {
181 notifyAddressRemoved(address, iface, std::stoi(flags), std::stoi(scope));
182 }
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900183 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700184 } else if (action == NetlinkEvent::Action::kRdnss) {
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900185 const char *lifetime = evt->findParam("LIFETIME");
186 const char *servers = evt->findParam("SERVERS");
187 if (lifetime && servers) {
Luke Huang528af602018-08-29 19:06:05 +0800188 notifyInterfaceDnsServers(iface, strtol(lifetime, nullptr, 10),
189 android::base::Split(servers, ","));
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900190 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700191 } else if (action == NetlinkEvent::Action::kRouteUpdated ||
192 action == NetlinkEvent::Action::kRouteRemoved) {
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900193 const char *route = evt->findParam("ROUTE");
194 const char *gateway = evt->findParam("GATEWAY");
195 const char *iface = evt->findParam("INTERFACE");
196 if (route && (gateway || iface)) {
Luke Huang528af602018-08-29 19:06:05 +0800197 notifyRouteChange((action == NetlinkEvent::Action::kRouteUpdated) ? true : false,
198 route, (gateway == nullptr) ? "" : gateway,
199 (iface == nullptr) ? "" : iface);
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900200 }
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900201 }
202
Bryse Flowersbc5f0602016-06-01 13:00:12 -0700203 } else if (!strcmp(subsys, "qlog") || !strcmp(subsys, "xt_quota2")) {
JP Abgralle0ebc462011-07-21 17:21:49 -0700204 const char *alertName = evt->findParam("ALERT_NAME");
205 const char *iface = evt->findParam("INTERFACE");
Luke Huang528af602018-08-29 19:06:05 +0800206 if (alertName && iface) {
207 notifyQuotaLimitReached(alertName, iface);
208 }
JP Abgralle07effe2012-04-27 00:02:53 -0700209
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700210 } else if (!strcmp(subsys, "strict")) {
211 const char *uid = evt->findParam("UID");
212 const char *hex = evt->findParam("HEX");
Luke Huang528af602018-08-29 19:06:05 +0800213 if (uid && hex) {
214 notifyStrictCleartext(strtol(uid, nullptr, 10), hex);
215 }
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700216
JP Abgralle07effe2012-04-27 00:02:53 -0700217 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharmac79bcc42014-02-12 11:53:00 -0800218 const char *label = evt->findParam("INTERFACE");
JP Abgralle07effe2012-04-27 00:02:53 -0700219 const char *state = evt->findParam("STATE");
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700220 const char *timestamp = evt->findParam("TIME_NS");
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700221 const char *uid = evt->findParam("UID");
Luke Huang528af602018-08-29 19:06:05 +0800222 if (state) {
223 bool isActive = !strcmp("active", state);
224 int64_t processTimestamp = (timestamp == nullptr) ? 0 : strtoll(timestamp, nullptr, 10);
225 int intLabel;
226 // NMS only accepts interface class activity changes with integer labels, and only ever
227 // creates idletimers with integer labels.
228 if (android::base::ParseInt(label, &intLabel)) {
229 const long reportedUid =
230 (uid != nullptr && isActive) ? strtol(uid, nullptr, 10) : -1;
231 notifyInterfaceClassActivityChanged(intLabel, isActive, processTimestamp,
232 reportedUid);
233 }
234 }
JP Abgralle0ebc462011-07-21 17:21:49 -0700235
JP Abgrall40baed82012-05-08 14:48:45 -0700236#if !LOG_NDEBUG
237 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
238 /* It is not a VSYNC or a backlight event */
239 ALOGV("unexpected event from subsystem %s", subsys);
240#endif
Ashish Sharma6337b882012-04-10 19:47:09 -0700241 }
San Mehatd1830422010-01-15 08:02:39 -0800242}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800243
Luke Huang528af602018-08-29 19:06:05 +0800244void NetlinkHandler::notifyInterfaceAdded(const std::string& ifName) {
245 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceAdded, ifName);
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900246}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800247
Luke Huang528af602018-08-29 19:06:05 +0800248void NetlinkHandler::notifyInterfaceRemoved(const std::string& ifName) {
249 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceRemoved, ifName);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800250}
251
Luke Huang528af602018-08-29 19:06:05 +0800252void NetlinkHandler::notifyInterfaceChanged(const std::string& ifName, bool up) {
253 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceChanged, ifName, up);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800254}
255
Luke Huang528af602018-08-29 19:06:05 +0800256void NetlinkHandler::notifyInterfaceLinkChanged(const std::string& ifName, bool up) {
257 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceLinkStateChanged, ifName, up);
Mike J. Chen564df4e2011-06-23 15:07:35 -0700258}
259
Luke Huang528af602018-08-29 19:06:05 +0800260void NetlinkHandler::notifyQuotaLimitReached(const std::string& labelName,
261 const std::string& ifName) {
262 LOG_EVENT_FUNC(BINDER_RETRY, onQuotaLimitReached, labelName, ifName);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800263}
JP Abgralle0ebc462011-07-21 17:21:49 -0700264
Luke Huang528af602018-08-29 19:06:05 +0800265void NetlinkHandler::notifyInterfaceClassActivityChanged(int label, bool isActive,
266 int64_t timestamp, int uid) {
267 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceClassActivityChanged, isActive, label, timestamp, uid);
JP Abgralle0ebc462011-07-21 17:21:49 -0700268}
Ashish Sharma6337b882012-04-10 19:47:09 -0700269
Luke Huang528af602018-08-29 19:06:05 +0800270void NetlinkHandler::notifyAddressUpdated(const std::string& addr, const std::string& ifName,
271 int flags, int scope) {
272 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceAddressUpdated, addr, ifName, flags, scope);
Ashish Sharma6337b882012-04-10 19:47:09 -0700273}
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900274
Luke Huang528af602018-08-29 19:06:05 +0800275void NetlinkHandler::notifyAddressRemoved(const std::string& addr, const std::string& ifName,
276 int flags, int scope) {
277 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceAddressRemoved, addr, ifName, flags, scope);
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900278}
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900279
Luke Huang528af602018-08-29 19:06:05 +0800280void NetlinkHandler::notifyInterfaceDnsServers(const std::string& ifName, int64_t lifetime,
281 const std::vector<std::string>& servers) {
282 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceDnsServerInfo, ifName, lifetime, servers);
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900283}
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900284
Luke Huang528af602018-08-29 19:06:05 +0800285void NetlinkHandler::notifyRouteChange(bool updated, const std::string& route,
286 const std::string& gateway, const std::string& ifName) {
287 LOG_EVENT_FUNC(BINDER_RETRY, onRouteChanged, updated, route, gateway, ifName);
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900288}
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700289
Luke Huang528af602018-08-29 19:06:05 +0800290void NetlinkHandler::notifyStrictCleartext(uid_t uid, const std::string& hex) {
291 LOG_EVENT_FUNC(BINDER_RETRY, onStrictCleartextDetected, uid, hex);
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700292}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900293
294} // namespace net
295} // namespace android