blob: 928e3294892e370dfb8e295e0b3ab44481d10163 [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
58void NetlinkHandler::onEvent(NetlinkEvent *evt) {
59 const char *subsys = evt->getSubsystem();
San Mehatd1830422010-01-15 08:02:39 -080060 if (!subsys) {
Steve Block0e76b762012-01-05 23:21:51 +000061 ALOGW("No subsystem found in netlink event");
San Mehatd1830422010-01-15 08:02:39 -080062 return;
63 }
Mike J. Chen564df4e2011-06-23 15:07:35 -070064
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090065 if (!strcmp(subsys, "net")) {
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070066 NetlinkEvent::Action action = evt->getAction();
Mike J. Chen564df4e2011-06-23 15:07:35 -070067 const char *iface = evt->findParam("INTERFACE");
Chenbo Feng04f3b172018-03-20 16:39:43 -070068 if ((action == NetlinkEvent::Action::kAdd) ||
69 (action == NetlinkEvent::Action::kLinkUp) ||
70 (action == NetlinkEvent::Action::kLinkDown)) {
71 const char *ifIndex = evt->findParam("IFINDEX");
72 if (ifIndex) {
73 // strtol returns 0 on error, which is fine because 0 is not a valid ifindex.
74 long ifaceIndex = strtol(ifIndex, NULL, 10);
75 if (ifaceIndex == 0 ||
76 (errno == ERANGE && (ifaceIndex == LONG_MAX || ifaceIndex == LONG_MIN))) {
77 ALOGE("invalid interface index: %s(%s)", iface, ifIndex);
78 } else {
79 gCtls->trafficCtrl.addInterface(iface, ifaceIndex);
80 }
81 }
Chenbo Feng7e974052018-02-28 22:57:21 -080082 }
Mike J. Chen564df4e2011-06-23 15:07:35 -070083
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070084 if (action == NetlinkEvent::Action::kAdd) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080085 notifyInterfaceAdded(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070086 } else if (action == NetlinkEvent::Action::kRemove) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080087 notifyInterfaceRemoved(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070088 } else if (action == NetlinkEvent::Action::kChange) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080089 evt->dump();
Robert Greenwalt67c57532010-02-17 17:42:37 -080090 notifyInterfaceChanged("nana", true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070091 } else if (action == NetlinkEvent::Action::kLinkUp) {
Mike J. Chen564df4e2011-06-23 15:07:35 -070092 notifyInterfaceLinkChanged(iface, true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070093 } else if (action == NetlinkEvent::Action::kLinkDown) {
Mike J. Chen564df4e2011-06-23 15:07:35 -070094 notifyInterfaceLinkChanged(iface, false);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070095 } else if (action == NetlinkEvent::Action::kAddressUpdated ||
96 action == NetlinkEvent::Action::kAddressRemoved) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090097 const char *address = evt->findParam("ADDRESS");
98 const char *flags = evt->findParam("FLAGS");
99 const char *scope = evt->findParam("SCOPE");
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700100 if (action == NetlinkEvent::Action::kAddressRemoved && iface && address) {
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900101 // Note: if this interface was deleted, iface is "" and we don't notify.
102 SockDiag sd;
103 if (sd.open()) {
104 char addrstr[INET6_ADDRSTRLEN];
105 strncpy(addrstr, address, sizeof(addrstr));
106 char *slash = strchr(addrstr, '/');
107 if (slash) {
108 *slash = '\0';
109 }
110
111 int ret = sd.destroySockets(addrstr);
112 if (ret < 0) {
113 ALOGE("Error destroying sockets: %s", strerror(ret));
114 }
115 } else {
116 ALOGE("Error opening NETLINK_SOCK_DIAG socket: %s", strerror(errno));
117 }
Sreeram Ramachandranaf37dd32014-09-08 16:03:18 -0700118 }
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900119 if (iface && iface[0] && address && flags && scope) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900120 notifyAddressChanged(action, address, iface, flags, scope);
121 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700122 } else if (action == NetlinkEvent::Action::kRdnss) {
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900123 const char *lifetime = evt->findParam("LIFETIME");
124 const char *servers = evt->findParam("SERVERS");
125 if (lifetime && servers) {
126 notifyInterfaceDnsServers(iface, lifetime, servers);
127 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700128 } else if (action == NetlinkEvent::Action::kRouteUpdated ||
129 action == NetlinkEvent::Action::kRouteRemoved) {
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900130 const char *route = evt->findParam("ROUTE");
131 const char *gateway = evt->findParam("GATEWAY");
132 const char *iface = evt->findParam("INTERFACE");
133 if (route && (gateway || iface)) {
134 notifyRouteChange(action, route, gateway, iface);
135 }
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900136 }
137
Bryse Flowersbc5f0602016-06-01 13:00:12 -0700138 } else if (!strcmp(subsys, "qlog") || !strcmp(subsys, "xt_quota2")) {
JP Abgralle0ebc462011-07-21 17:21:49 -0700139 const char *alertName = evt->findParam("ALERT_NAME");
140 const char *iface = evt->findParam("INTERFACE");
141 notifyQuotaLimitReached(alertName, iface);
JP Abgralle07effe2012-04-27 00:02:53 -0700142
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700143 } else if (!strcmp(subsys, "strict")) {
144 const char *uid = evt->findParam("UID");
145 const char *hex = evt->findParam("HEX");
146 notifyStrictCleartext(uid, hex);
147
JP Abgralle07effe2012-04-27 00:02:53 -0700148 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharmac79bcc42014-02-12 11:53:00 -0800149 const char *label = evt->findParam("INTERFACE");
JP Abgralle07effe2012-04-27 00:02:53 -0700150 const char *state = evt->findParam("STATE");
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700151 const char *timestamp = evt->findParam("TIME_NS");
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700152 const char *uid = evt->findParam("UID");
JP Abgralle07effe2012-04-27 00:02:53 -0700153 if (state)
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700154 notifyInterfaceClassActivity(label, !strcmp("active", state),
155 timestamp, uid);
JP Abgralle0ebc462011-07-21 17:21:49 -0700156
JP Abgrall40baed82012-05-08 14:48:45 -0700157#if !LOG_NDEBUG
158 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
159 /* It is not a VSYNC or a backlight event */
160 ALOGV("unexpected event from subsystem %s", subsys);
161#endif
Ashish Sharma6337b882012-04-10 19:47:09 -0700162 }
San Mehatd1830422010-01-15 08:02:39 -0800163}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800164
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900165// NOLINTNEXTLINE(cert-dcl50-cpp): Grandfathered C-style variadic function.
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900166void NetlinkHandler::notify(int code, const char *format, ...) {
167 char *msg;
168 va_list args;
169 va_start(args, format);
170 if (vasprintf(&msg, format, args) >= 0) {
171 mNm->getBroadcaster()->sendBroadcast(code, msg, false);
172 free(msg);
173 } else {
174 SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
175 }
176 va_end(args);
177}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800178
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900179void NetlinkHandler::notifyInterfaceAdded(const char *name) {
180 notify(ResponseCode::InterfaceChange, "Iface added %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800181}
182
183void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900184 notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800185}
186
187void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900188 notify(ResponseCode::InterfaceChange,
189 "Iface changed %s %s", name, (isUp ? "up" : "down"));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700190}
191
192void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900193 notify(ResponseCode::InterfaceChange,
194 "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
Robert Greenwalt67c57532010-02-17 17:42:37 -0800195}
JP Abgralle0ebc462011-07-21 17:21:49 -0700196
197void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900198 notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
JP Abgralle0ebc462011-07-21 17:21:49 -0700199}
Ashish Sharma6337b882012-04-10 19:47:09 -0700200
Haoyu Bai98f65d32012-06-28 16:16:51 -0700201void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700202 bool isActive,
203 const char *timestamp,
204 const char *uid) {
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700205 if (timestamp == NULL)
206 notify(ResponseCode::InterfaceClassActivity,
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900207 "IfaceClass %s %s", isActive ? "active" : "idle", name);
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700208 else if (uid != NULL && isActive)
209 notify(ResponseCode::InterfaceClassActivity,
210 "IfaceClass active %s %s %s", name, timestamp, uid);
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700211 else
212 notify(ResponseCode::InterfaceClassActivity,
213 "IfaceClass %s %s %s", isActive ? "active" : "idle", name, timestamp);
Ashish Sharma6337b882012-04-10 19:47:09 -0700214}
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900215
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700216void NetlinkHandler::notifyAddressChanged(NetlinkEvent::Action action, const char *addr,
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900217 const char *iface, const char *flags,
218 const char *scope) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900219 notify(ResponseCode::InterfaceAddressChange,
220 "Address %s %s %s %s %s",
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700221 (action == NetlinkEvent::Action::kAddressUpdated) ? kUpdated : kRemoved,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900222 addr, iface, flags, scope);
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900223}
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900224
225void NetlinkHandler::notifyInterfaceDnsServers(const char *iface,
226 const char *lifetime,
227 const char *servers) {
228 notify(ResponseCode::InterfaceDnsInfo, "DnsInfo servers %s %s %s",
229 iface, lifetime, servers);
230}
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900231
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700232void NetlinkHandler::notifyRouteChange(NetlinkEvent::Action action, const char *route,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900233 const char *gateway, const char *iface) {
234 notify(ResponseCode::RouteChange,
235 "Route %s %s%s%s%s%s",
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700236 (action == NetlinkEvent::Action::kRouteUpdated) ? kUpdated : kRemoved,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900237 route,
Manoj Gupta75c970f2016-11-22 21:15:59 -0800238 (gateway && *gateway) ? " via " : "",
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900239 gateway,
Manoj Gupta75c970f2016-11-22 21:15:59 -0800240 (iface && *iface) ? " dev " : "",
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900241 iface);
242}
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700243
244void NetlinkHandler::notifyStrictCleartext(const char* uid, const char* hex) {
245 notify(ResponseCode::StrictCleartext, "%s %s", uid, hex);
246}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900247
248} // namespace net
249} // namespace android