1 /*
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
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22
23 #define LOG_TAG "Netd"
24
25 #include <log/log.h>
26
27 #include <android-base/parseint.h>
28 #include <android-base/strings.h>
29 #include <netutils/ifc.h>
30 #include <sysutils/NetlinkEvent.h>
31 #include "Controllers.h"
32 #include "NetlinkHandler.h"
33 #include "NetlinkManager.h"
34 #include "SockDiag.h"
35
36 #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
54 #define NOTIFY_AND_LOG(retry, func, ...) \
55 do { \
56 const auto listenerMap = gCtls->eventReporter.getNetdUnsolicitedEventListenerMap(); \
57 for (bool needLog = true; auto& listener : listenerMap) { \
58 auto entry = gUnsolicitedLog.newEntry().function(#func).args(__VA_ARGS__); \
59 if (retry(listener.first->func(__VA_ARGS__))) { \
60 if (needLog) { \
61 gUnsolicitedLog.log(entry.withAutomaticDuration()); \
62 needLog = false; \
63 } \
64 } \
65 } \
66 } while (0)
67
68 namespace android {
69 namespace net {
70
71 constexpr int RETRY_ATTEMPTS = 2;
72 constexpr int RETRY_INTERVAL_MICRO_S = 100000;
73
NetlinkHandler(NetlinkManager * nm,int listenerSocket,int format)74 NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
75 int format) :
76 NetlinkListener(listenerSocket, format) {
77 mNm = nm;
78 }
79
~NetlinkHandler()80 NetlinkHandler::~NetlinkHandler() {
81 }
82
start()83 int NetlinkHandler::start() {
84 return this->startListener();
85 }
86
stop()87 int NetlinkHandler::stop() {
88 return this->stopListener();
89 }
90
parseIfIndex(const char * ifIndex)91 static long parseIfIndex(const char* ifIndex) {
92 if (ifIndex == nullptr) {
93 return 0;
94 }
95 long ifaceIndex = strtol(ifIndex, nullptr, 10);
96 // strtol returns 0 on error, which is fine because 0 is not a valid ifindex.
97 if (errno == ERANGE && (ifaceIndex == LONG_MAX || ifaceIndex == LONG_MIN)) {
98 return 0;
99 }
100 return ifaceIndex;
101 }
102
onEvent(NetlinkEvent * evt)103 void NetlinkHandler::onEvent(NetlinkEvent *evt) {
104 const char *subsys = evt->getSubsystem();
105 if (!subsys) {
106 ALOGW("No subsystem found in netlink event");
107 return;
108 }
109
110 if (!strcmp(subsys, "net")) {
111 NetlinkEvent::Action action = evt->getAction();
112 const char *iface = evt->findParam("INTERFACE") ?: "";
113 if (action == NetlinkEvent::Action::kAdd) {
114 notifyInterfaceAdded(iface);
115 } else if (action == NetlinkEvent::Action::kRemove) {
116 notifyInterfaceRemoved(iface);
117 } else if (action == NetlinkEvent::Action::kChange) {
118 evt->dump();
119 notifyInterfaceChanged("nana", true);
120 } else if (action == NetlinkEvent::Action::kLinkUp) {
121 notifyInterfaceLinkChanged(iface, true);
122 } else if (action == NetlinkEvent::Action::kLinkDown) {
123 notifyInterfaceLinkChanged(iface, false);
124 } else if (action == NetlinkEvent::Action::kAddressUpdated ||
125 action == NetlinkEvent::Action::kAddressRemoved) {
126 const char *address = evt->findParam("ADDRESS");
127 const char *flags = evt->findParam("FLAGS");
128 const char *scope = evt->findParam("SCOPE");
129 const char *ifIndex = evt->findParam("IFINDEX");
130 char addrstr[INET6_ADDRSTRLEN + strlen("/128")];
131 strlcpy(addrstr, address, sizeof(addrstr));
132 char *slash = strchr(addrstr, '/');
133 if (slash) {
134 *slash = '\0';
135 }
136
137 long ifaceIndex = parseIfIndex(ifIndex);
138 if (!ifaceIndex) {
139 ALOGE("invalid interface index: %s(%s)", iface, ifIndex);
140 }
141 const bool addrUpdated = (action == NetlinkEvent::Action::kAddressUpdated);
142 if (addrUpdated) {
143 gCtls->netCtrl.addInterfaceAddress(ifaceIndex, address);
144 } else { // action == NetlinkEvent::Action::kAddressRemoved
145 bool shouldDestroy = gCtls->netCtrl.removeInterfaceAddress(ifaceIndex, address);
146 if (shouldDestroy) {
147 SockDiag sd;
148 if (sd.open()) {
149 // Pass the interface index iff. destroying sockets on a link-local address.
150 // This cannot use an interface name as the interface might no longer exist.
151 int destroyIfaceIndex =
152 std::string_view(addrstr).starts_with("fe80:") ? ifaceIndex : 0;
153 int ret = sd.destroySockets(addrstr, destroyIfaceIndex);
154 if (ret < 0) {
155 ALOGE("Error destroying sockets: %s", strerror(-ret));
156 }
157 } else {
158 ALOGE("Error opening NETLINK_SOCK_DIAG socket: %s", strerror(errno));
159 }
160 }
161 }
162 // Note: if this interface was deleted, iface is "" and we don't notify.
163 if (iface && iface[0] && address && flags && scope) {
164 if (addrUpdated) {
165 notifyAddressUpdated(address, iface, std::stoi(flags), std::stoi(scope));
166 } else {
167 notifyAddressRemoved(address, iface, std::stoi(flags), std::stoi(scope));
168 }
169 }
170 } else if (action == NetlinkEvent::Action::kRdnss) {
171 const char *lifetime = evt->findParam("LIFETIME");
172 const char *servers = evt->findParam("SERVERS");
173 if (lifetime && servers) {
174 notifyInterfaceDnsServers(iface, strtol(lifetime, nullptr, 10),
175 android::base::Split(servers, ","));
176 }
177 } else if (action == NetlinkEvent::Action::kRouteUpdated ||
178 action == NetlinkEvent::Action::kRouteRemoved) {
179 const char *route = evt->findParam("ROUTE");
180 const char *gateway = evt->findParam("GATEWAY");
181 const char *iface = evt->findParam("INTERFACE");
182 if (route && (gateway || iface)) {
183 notifyRouteChange((action == NetlinkEvent::Action::kRouteUpdated) ? true : false,
184 route, (gateway == nullptr) ? "" : gateway,
185 (iface == nullptr) ? "" : iface);
186 }
187 }
188
189 } else if (!strcmp(subsys, "qlog") || !strcmp(subsys, "xt_quota2")) {
190 const char *alertName = evt->findParam("ALERT_NAME");
191 const char *iface = evt->findParam("INTERFACE");
192 if (alertName && iface) {
193 notifyQuotaLimitReached(alertName, iface);
194 }
195
196 } else if (!strcmp(subsys, "strict")) {
197 const char *uid = evt->findParam("UID");
198 const char *hex = evt->findParam("HEX");
199 if (uid && hex) {
200 notifyStrictCleartext(strtol(uid, nullptr, 10), hex);
201 }
202
203 } else if (!strcmp(subsys, "xt_idletimer")) {
204 const char *label = evt->findParam("INTERFACE");
205 const char *state = evt->findParam("STATE");
206 const char *timestamp = evt->findParam("TIME_NS");
207 const char *uid = evt->findParam("UID");
208 if (state) {
209 bool isActive = !strcmp("active", state);
210 int64_t processTimestamp = (timestamp == nullptr) ? 0 : strtoll(timestamp, nullptr, 10);
211 int intLabel;
212 // NMS only accepts interface class activity changes with integer labels, and only ever
213 // creates idletimers with integer labels.
214 if (android::base::ParseInt(label, &intLabel)) {
215 const long reportedUid =
216 (uid != nullptr && isActive) ? strtol(uid, nullptr, 10) : -1;
217 notifyInterfaceClassActivityChanged(intLabel, isActive, processTimestamp,
218 reportedUid);
219 }
220 }
221
222 #if !LOG_NDEBUG
223 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
224 /* It is not a VSYNC or a backlight event */
225 ALOGV("unexpected event from subsystem %s", subsys);
226 #endif
227 }
228 }
229
notifyInterfaceAdded(const std::string & ifName)230 void NetlinkHandler::notifyInterfaceAdded(const std::string& ifName) {
231 NOTIFY_AND_LOG(BINDER_RETRY, onInterfaceAdded, ifName);
232 }
233
notifyInterfaceRemoved(const std::string & ifName)234 void NetlinkHandler::notifyInterfaceRemoved(const std::string& ifName) {
235 NOTIFY_AND_LOG(BINDER_RETRY, onInterfaceRemoved, ifName);
236 }
237
notifyInterfaceChanged(const std::string & ifName,bool up)238 void NetlinkHandler::notifyInterfaceChanged(const std::string& ifName, bool up) {
239 NOTIFY_AND_LOG(BINDER_RETRY, onInterfaceChanged, ifName, up);
240 }
241
notifyInterfaceLinkChanged(const std::string & ifName,bool up)242 void NetlinkHandler::notifyInterfaceLinkChanged(const std::string& ifName, bool up) {
243 NOTIFY_AND_LOG(BINDER_RETRY, onInterfaceLinkStateChanged, ifName, up);
244 }
245
notifyQuotaLimitReached(const std::string & labelName,const std::string & ifName)246 void NetlinkHandler::notifyQuotaLimitReached(const std::string& labelName,
247 const std::string& ifName) {
248 NOTIFY_AND_LOG(BINDER_RETRY, onQuotaLimitReached, labelName, ifName);
249 }
250
notifyInterfaceClassActivityChanged(int label,bool isActive,int64_t timestamp,int uid)251 void NetlinkHandler::notifyInterfaceClassActivityChanged(int label, bool isActive,
252 int64_t timestamp, int uid) {
253 NOTIFY_AND_LOG(BINDER_RETRY, onInterfaceClassActivityChanged, isActive, label, timestamp, uid);
254 }
255
notifyAddressUpdated(const std::string & addr,const std::string & ifName,int flags,int scope)256 void NetlinkHandler::notifyAddressUpdated(const std::string& addr, const std::string& ifName,
257 int flags, int scope) {
258 NOTIFY_AND_LOG(BINDER_RETRY, onInterfaceAddressUpdated, addr, ifName, flags, scope);
259 }
260
notifyAddressRemoved(const std::string & addr,const std::string & ifName,int flags,int scope)261 void NetlinkHandler::notifyAddressRemoved(const std::string& addr, const std::string& ifName,
262 int flags, int scope) {
263 NOTIFY_AND_LOG(BINDER_RETRY, onInterfaceAddressRemoved, addr, ifName, flags, scope);
264 }
265
notifyInterfaceDnsServers(const std::string & ifName,int64_t lifetime,const std::vector<std::string> & servers)266 void NetlinkHandler::notifyInterfaceDnsServers(const std::string& ifName, int64_t lifetime,
267 const std::vector<std::string>& servers) {
268 NOTIFY_AND_LOG(BINDER_RETRY, onInterfaceDnsServerInfo, ifName, lifetime, servers);
269 }
270
notifyRouteChange(bool updated,const std::string & route,const std::string & gateway,const std::string & ifName)271 void NetlinkHandler::notifyRouteChange(bool updated, const std::string& route,
272 const std::string& gateway, const std::string& ifName) {
273 NOTIFY_AND_LOG(BINDER_RETRY, onRouteChanged, updated, route, gateway, ifName);
274 }
275
notifyStrictCleartext(uid_t uid,const std::string & hex)276 void NetlinkHandler::notifyStrictCleartext(uid_t uid, const std::string& hex) {
277 NOTIFY_AND_LOG(BINDER_RETRY, onStrictCleartextDetected, uid, hex);
278 }
279
280 } // namespace net
281 } // namespace android
282