blob: af369e2b444b7bfbe92b89b479fd797a676b933a [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"
34#include "ResponseCode.h"
Lorenzo Colittif32fc592016-02-15 01:09:14 +090035#include "SockDiag.h"
San Mehatd1830422010-01-15 08:02:39 -080036
Luke Huang528af602018-08-29 19:06:05 +080037#include <charconv>
38
39#define BINDER_RETRY(exp) \
40 ({ \
41 bool res = true; \
42 for (int attempt = 0; /*nop*/; ++attempt) { \
43 auto _rc = (exp); \
44 if (_rc.exceptionCode() == binder::Status::EX_TRANSACTION_FAILED && \
45 attempt < RETRY_ATTEMPTS) { \
46 usleep(RETRY_INTERVAL_MICRO_S); \
47 } else { \
48 res = _rc.isOk(); \
49 break; \
50 } \
51 } \
52 res; \
53 })
54
55#define LOG_EVENT_FUNC(retry, func, ...) \
56 do { \
57 const auto listenerMap = gCtls->eventReporter.getNetdUnsolicitedEventListenerVec(); \
58 for (auto& listener : listenerMap) { \
59 auto entry = \
60 gUnsolicitedLog.newEntry().function(#func).args(__VA_ARGS__, listener.first); \
61 if (retry(listener.second->func(__VA_ARGS__))) { \
62 gUnsolicitedLog.log(entry.withAutomaticDuration()); \
63 } \
64 } \
65 } while (0)
Lorenzo Colittibd0f2242014-06-12 13:51:05 +090066
Lorenzo Colitti7035f222017-02-13 18:29:00 +090067namespace android {
68namespace net {
69
Luke Huang528af602018-08-29 19:06:05 +080070constexpr int RETRY_ATTEMPTS = 2;
71constexpr int RETRY_INTERVAL_MICRO_S = 100000;
72
Mike J. Chen564df4e2011-06-23 15:07:35 -070073NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
74 int format) :
75 NetlinkListener(listenerSocket, format) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080076 mNm = nm;
San Mehatd1830422010-01-15 08:02:39 -080077}
78
79NetlinkHandler::~NetlinkHandler() {
80}
81
82int NetlinkHandler::start() {
83 return this->startListener();
84}
85
86int NetlinkHandler::stop() {
87 return this->stopListener();
88}
89
Rubin Xu6c00b612018-04-27 14:27:59 +010090static long parseIfIndex(const char* ifIndex) {
91 if (ifIndex == nullptr) {
92 return 0;
93 }
Yi Kongbdfd57e2018-07-25 13:26:10 -070094 long ifaceIndex = strtol(ifIndex, nullptr, 10);
Rubin Xu6c00b612018-04-27 14:27:59 +010095 // strtol returns 0 on error, which is fine because 0 is not a valid ifindex.
96 if (errno == ERANGE && (ifaceIndex == LONG_MAX || ifaceIndex == LONG_MIN)) {
97 return 0;
98 }
99 return ifaceIndex;
100}
101
San Mehatd1830422010-01-15 08:02:39 -0800102void NetlinkHandler::onEvent(NetlinkEvent *evt) {
103 const char *subsys = evt->getSubsystem();
San Mehatd1830422010-01-15 08:02:39 -0800104 if (!subsys) {
Steve Block0e76b762012-01-05 23:21:51 +0000105 ALOGW("No subsystem found in netlink event");
San Mehatd1830422010-01-15 08:02:39 -0800106 return;
107 }
Mike J. Chen564df4e2011-06-23 15:07:35 -0700108
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900109 if (!strcmp(subsys, "net")) {
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700110 NetlinkEvent::Action action = evt->getAction();
Mike J. Chen564df4e2011-06-23 15:07:35 -0700111 const char *iface = evt->findParam("INTERFACE");
Chenbo Feng04f3b172018-03-20 16:39:43 -0700112 if ((action == NetlinkEvent::Action::kAdd) ||
113 (action == NetlinkEvent::Action::kLinkUp) ||
114 (action == NetlinkEvent::Action::kLinkDown)) {
115 const char *ifIndex = evt->findParam("IFINDEX");
Rubin Xu6c00b612018-04-27 14:27:59 +0100116 long ifaceIndex = parseIfIndex(ifIndex);
117 if (ifaceIndex) {
118 gCtls->trafficCtrl.addInterface(iface, ifaceIndex);
119 } else {
120 ALOGE("invalid interface index: %s(%s)", iface, ifIndex);
Chenbo Feng04f3b172018-03-20 16:39:43 -0700121 }
Chenbo Feng7e974052018-02-28 22:57:21 -0800122 }
Mike J. Chen564df4e2011-06-23 15:07:35 -0700123
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700124 if (action == NetlinkEvent::Action::kAdd) {
Robert Greenwalt67c57532010-02-17 17:42:37 -0800125 notifyInterfaceAdded(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700126 } else if (action == NetlinkEvent::Action::kRemove) {
Robert Greenwalt67c57532010-02-17 17:42:37 -0800127 notifyInterfaceRemoved(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700128 } else if (action == NetlinkEvent::Action::kChange) {
Robert Greenwalt67c57532010-02-17 17:42:37 -0800129 evt->dump();
Robert Greenwalt67c57532010-02-17 17:42:37 -0800130 notifyInterfaceChanged("nana", true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700131 } else if (action == NetlinkEvent::Action::kLinkUp) {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700132 notifyInterfaceLinkChanged(iface, true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700133 } else if (action == NetlinkEvent::Action::kLinkDown) {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700134 notifyInterfaceLinkChanged(iface, false);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700135 } else if (action == NetlinkEvent::Action::kAddressUpdated ||
136 action == NetlinkEvent::Action::kAddressRemoved) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900137 const char *address = evt->findParam("ADDRESS");
138 const char *flags = evt->findParam("FLAGS");
139 const char *scope = evt->findParam("SCOPE");
Rubin Xu6c00b612018-04-27 14:27:59 +0100140 const char *ifIndex = evt->findParam("IFINDEX");
141 char addrstr[INET6_ADDRSTRLEN + strlen("/128")];
142 strlcpy(addrstr, address, sizeof(addrstr));
143 char *slash = strchr(addrstr, '/');
144 if (slash) {
145 *slash = '\0';
146 }
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900147
Rubin Xu6c00b612018-04-27 14:27:59 +0100148 long ifaceIndex = parseIfIndex(ifIndex);
149 if (!ifaceIndex) {
150 ALOGE("invalid interface index: %s(%s)", iface, ifIndex);
151 }
Luke Huang528af602018-08-29 19:06:05 +0800152 const bool addrUpdated = (action == NetlinkEvent::Action::kAddressUpdated);
153 if (addrUpdated) {
Rubin Xu6c00b612018-04-27 14:27:59 +0100154 gCtls->netCtrl.addInterfaceAddress(ifaceIndex, address);
155 } else { // action == NetlinkEvent::Action::kAddressRemoved
156 bool shouldDestroy = gCtls->netCtrl.removeInterfaceAddress(ifaceIndex, address);
157 if (shouldDestroy) {
158 SockDiag sd;
159 if (sd.open()) {
160 int ret = sd.destroySockets(addrstr);
161 if (ret < 0) {
162 ALOGE("Error destroying sockets: %s", strerror(-ret));
163 }
164 } else {
165 ALOGE("Error opening NETLINK_SOCK_DIAG socket: %s", strerror(errno));
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900166 }
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900167 }
Sreeram Ramachandranaf37dd32014-09-08 16:03:18 -0700168 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100169 // Note: if this interface was deleted, iface is "" and we don't notify.
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900170 if (iface && iface[0] && address && flags && scope) {
Luke Huang528af602018-08-29 19:06:05 +0800171 if (addrUpdated) {
172 notifyAddressUpdated(address, iface, std::stoi(flags), std::stoi(scope));
173 } else {
174 notifyAddressRemoved(address, iface, std::stoi(flags), std::stoi(scope));
175 }
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900176 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700177 } else if (action == NetlinkEvent::Action::kRdnss) {
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900178 const char *lifetime = evt->findParam("LIFETIME");
179 const char *servers = evt->findParam("SERVERS");
180 if (lifetime && servers) {
Luke Huang528af602018-08-29 19:06:05 +0800181 notifyInterfaceDnsServers(iface, strtol(lifetime, nullptr, 10),
182 android::base::Split(servers, ","));
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900183 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700184 } else if (action == NetlinkEvent::Action::kRouteUpdated ||
185 action == NetlinkEvent::Action::kRouteRemoved) {
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900186 const char *route = evt->findParam("ROUTE");
187 const char *gateway = evt->findParam("GATEWAY");
188 const char *iface = evt->findParam("INTERFACE");
189 if (route && (gateway || iface)) {
Luke Huang528af602018-08-29 19:06:05 +0800190 notifyRouteChange((action == NetlinkEvent::Action::kRouteUpdated) ? true : false,
191 route, (gateway == nullptr) ? "" : gateway,
192 (iface == nullptr) ? "" : iface);
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900193 }
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900194 }
195
Bryse Flowersbc5f0602016-06-01 13:00:12 -0700196 } else if (!strcmp(subsys, "qlog") || !strcmp(subsys, "xt_quota2")) {
JP Abgralle0ebc462011-07-21 17:21:49 -0700197 const char *alertName = evt->findParam("ALERT_NAME");
198 const char *iface = evt->findParam("INTERFACE");
Luke Huang528af602018-08-29 19:06:05 +0800199 if (alertName && iface) {
200 notifyQuotaLimitReached(alertName, iface);
201 }
JP Abgralle07effe2012-04-27 00:02:53 -0700202
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700203 } else if (!strcmp(subsys, "strict")) {
204 const char *uid = evt->findParam("UID");
205 const char *hex = evt->findParam("HEX");
Luke Huang528af602018-08-29 19:06:05 +0800206 if (uid && hex) {
207 notifyStrictCleartext(strtol(uid, nullptr, 10), hex);
208 }
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700209
JP Abgralle07effe2012-04-27 00:02:53 -0700210 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharmac79bcc42014-02-12 11:53:00 -0800211 const char *label = evt->findParam("INTERFACE");
JP Abgralle07effe2012-04-27 00:02:53 -0700212 const char *state = evt->findParam("STATE");
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700213 const char *timestamp = evt->findParam("TIME_NS");
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700214 const char *uid = evt->findParam("UID");
Luke Huang528af602018-08-29 19:06:05 +0800215 if (state) {
216 bool isActive = !strcmp("active", state);
217 int64_t processTimestamp = (timestamp == nullptr) ? 0 : strtoll(timestamp, nullptr, 10);
218 int intLabel;
219 // NMS only accepts interface class activity changes with integer labels, and only ever
220 // creates idletimers with integer labels.
221 if (android::base::ParseInt(label, &intLabel)) {
222 const long reportedUid =
223 (uid != nullptr && isActive) ? strtol(uid, nullptr, 10) : -1;
224 notifyInterfaceClassActivityChanged(intLabel, isActive, processTimestamp,
225 reportedUid);
226 }
227 }
JP Abgralle0ebc462011-07-21 17:21:49 -0700228
JP Abgrall40baed82012-05-08 14:48:45 -0700229#if !LOG_NDEBUG
230 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
231 /* It is not a VSYNC or a backlight event */
232 ALOGV("unexpected event from subsystem %s", subsys);
233#endif
Ashish Sharma6337b882012-04-10 19:47:09 -0700234 }
San Mehatd1830422010-01-15 08:02:39 -0800235}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800236
Luke Huang528af602018-08-29 19:06:05 +0800237void NetlinkHandler::notifyInterfaceAdded(const std::string& ifName) {
238 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceAdded, ifName);
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900239}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800240
Luke Huang528af602018-08-29 19:06:05 +0800241void NetlinkHandler::notifyInterfaceRemoved(const std::string& ifName) {
242 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceRemoved, ifName);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800243}
244
Luke Huang528af602018-08-29 19:06:05 +0800245void NetlinkHandler::notifyInterfaceChanged(const std::string& ifName, bool up) {
246 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceChanged, ifName, up);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800247}
248
Luke Huang528af602018-08-29 19:06:05 +0800249void NetlinkHandler::notifyInterfaceLinkChanged(const std::string& ifName, bool up) {
250 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceLinkStateChanged, ifName, up);
Mike J. Chen564df4e2011-06-23 15:07:35 -0700251}
252
Luke Huang528af602018-08-29 19:06:05 +0800253void NetlinkHandler::notifyQuotaLimitReached(const std::string& labelName,
254 const std::string& ifName) {
255 LOG_EVENT_FUNC(BINDER_RETRY, onQuotaLimitReached, labelName, ifName);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800256}
JP Abgralle0ebc462011-07-21 17:21:49 -0700257
Luke Huang528af602018-08-29 19:06:05 +0800258void NetlinkHandler::notifyInterfaceClassActivityChanged(int label, bool isActive,
259 int64_t timestamp, int uid) {
260 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceClassActivityChanged, isActive, label, timestamp, uid);
JP Abgralle0ebc462011-07-21 17:21:49 -0700261}
Ashish Sharma6337b882012-04-10 19:47:09 -0700262
Luke Huang528af602018-08-29 19:06:05 +0800263void NetlinkHandler::notifyAddressUpdated(const std::string& addr, const std::string& ifName,
264 int flags, int scope) {
265 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceAddressUpdated, addr, ifName, flags, scope);
Ashish Sharma6337b882012-04-10 19:47:09 -0700266}
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900267
Luke Huang528af602018-08-29 19:06:05 +0800268void NetlinkHandler::notifyAddressRemoved(const std::string& addr, const std::string& ifName,
269 int flags, int scope) {
270 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceAddressRemoved, addr, ifName, flags, scope);
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900271}
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900272
Luke Huang528af602018-08-29 19:06:05 +0800273void NetlinkHandler::notifyInterfaceDnsServers(const std::string& ifName, int64_t lifetime,
274 const std::vector<std::string>& servers) {
275 LOG_EVENT_FUNC(BINDER_RETRY, onInterfaceDnsServerInfo, ifName, lifetime, servers);
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900276}
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900277
Luke Huang528af602018-08-29 19:06:05 +0800278void NetlinkHandler::notifyRouteChange(bool updated, const std::string& route,
279 const std::string& gateway, const std::string& ifName) {
280 LOG_EVENT_FUNC(BINDER_RETRY, onRouteChanged, updated, route, gateway, ifName);
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900281}
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700282
Luke Huang528af602018-08-29 19:06:05 +0800283void NetlinkHandler::notifyStrictCleartext(uid_t uid, const std::string& hex) {
284 LOG_EVENT_FUNC(BINDER_RETRY, onStrictCleartextDetected, uid, hex);
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700285}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900286
287} // namespace net
288} // namespace android