blob: df30045f7243d722bbd766270c272a51e6366580 [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
25#include <cutils/log.h>
26
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 Feng8cc480c2018-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 Xuacbb6b72018-04-27 14:27:59 +010058static long parseIfIndex(const char* ifIndex) {
59 if (ifIndex == nullptr) {
60 return 0;
61 }
62 long ifaceIndex = strtol(ifIndex, NULL, 10);
63 // 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 Fenga0890c52018-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 Xuacbb6b72018-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 Fenga0890c52018-03-20 16:39:43 -070089 }
Chenbo Feng8cc480c2018-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();
Pavan Kumar M08167012018-10-22 12:31:23 +053098 const char *alertName = evt->findParam("ALERT_NAME");
99 const char *iface = evt->findParam("INTERFACE");
100 if (alertName != NULL && iface != NULL) {
101 ALOGI("Alertname : %s iface (%s)", alertName, iface);
102 if (!strcmp(alertName, "quotaReachedAlert") && iface) {
103 notifyQuotaLimitReached(alertName, iface);
104 }
105 } else {
106 notifyInterfaceChanged("nana", true);
107 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700108 } else if (action == NetlinkEvent::Action::kLinkUp) {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700109 notifyInterfaceLinkChanged(iface, true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700110 } else if (action == NetlinkEvent::Action::kLinkDown) {
Mike J. Chen564df4e2011-06-23 15:07:35 -0700111 notifyInterfaceLinkChanged(iface, false);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700112 } else if (action == NetlinkEvent::Action::kAddressUpdated ||
113 action == NetlinkEvent::Action::kAddressRemoved) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900114 const char *address = evt->findParam("ADDRESS");
115 const char *flags = evt->findParam("FLAGS");
116 const char *scope = evt->findParam("SCOPE");
Rubin Xuacbb6b72018-04-27 14:27:59 +0100117 const char *ifIndex = evt->findParam("IFINDEX");
118 char addrstr[INET6_ADDRSTRLEN + strlen("/128")];
119 strlcpy(addrstr, address, sizeof(addrstr));
120 char *slash = strchr(addrstr, '/');
121 if (slash) {
122 *slash = '\0';
123 }
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900124
Rubin Xuacbb6b72018-04-27 14:27:59 +0100125 long ifaceIndex = parseIfIndex(ifIndex);
126 if (!ifaceIndex) {
127 ALOGE("invalid interface index: %s(%s)", iface, ifIndex);
128 }
129 if (action == NetlinkEvent::Action::kAddressUpdated) {
130 gCtls->netCtrl.addInterfaceAddress(ifaceIndex, address);
131 } else { // action == NetlinkEvent::Action::kAddressRemoved
132 bool shouldDestroy = gCtls->netCtrl.removeInterfaceAddress(ifaceIndex, address);
133 if (shouldDestroy) {
134 SockDiag sd;
135 if (sd.open()) {
136 int ret = sd.destroySockets(addrstr);
137 if (ret < 0) {
138 ALOGE("Error destroying sockets: %s", strerror(-ret));
139 }
140 } else {
141 ALOGE("Error opening NETLINK_SOCK_DIAG socket: %s", strerror(errno));
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900142 }
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900143 }
Sreeram Ramachandranaf37dd32014-09-08 16:03:18 -0700144 }
Rubin Xuacbb6b72018-04-27 14:27:59 +0100145 // Note: if this interface was deleted, iface is "" and we don't notify.
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900146 if (iface && iface[0] && address && flags && scope) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900147 notifyAddressChanged(action, address, iface, flags, scope);
148 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700149 } else if (action == NetlinkEvent::Action::kRdnss) {
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900150 const char *lifetime = evt->findParam("LIFETIME");
151 const char *servers = evt->findParam("SERVERS");
152 if (lifetime && servers) {
153 notifyInterfaceDnsServers(iface, lifetime, servers);
154 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700155 } else if (action == NetlinkEvent::Action::kRouteUpdated ||
156 action == NetlinkEvent::Action::kRouteRemoved) {
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900157 const char *route = evt->findParam("ROUTE");
158 const char *gateway = evt->findParam("GATEWAY");
159 const char *iface = evt->findParam("INTERFACE");
160 if (route && (gateway || iface)) {
161 notifyRouteChange(action, route, gateway, iface);
162 }
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900163 }
164
Bryse Flowersbc5f0602016-06-01 13:00:12 -0700165 } else if (!strcmp(subsys, "qlog") || !strcmp(subsys, "xt_quota2")) {
JP Abgralle0ebc462011-07-21 17:21:49 -0700166 const char *alertName = evt->findParam("ALERT_NAME");
167 const char *iface = evt->findParam("INTERFACE");
168 notifyQuotaLimitReached(alertName, iface);
JP Abgralle07effe2012-04-27 00:02:53 -0700169
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700170 } else if (!strcmp(subsys, "strict")) {
171 const char *uid = evt->findParam("UID");
172 const char *hex = evt->findParam("HEX");
173 notifyStrictCleartext(uid, hex);
174
JP Abgralle07effe2012-04-27 00:02:53 -0700175 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharmac79bcc42014-02-12 11:53:00 -0800176 const char *label = evt->findParam("INTERFACE");
JP Abgralle07effe2012-04-27 00:02:53 -0700177 const char *state = evt->findParam("STATE");
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700178 const char *timestamp = evt->findParam("TIME_NS");
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700179 const char *uid = evt->findParam("UID");
JP Abgralle07effe2012-04-27 00:02:53 -0700180 if (state)
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700181 notifyInterfaceClassActivity(label, !strcmp("active", state),
182 timestamp, uid);
JP Abgralle0ebc462011-07-21 17:21:49 -0700183
JP Abgrall40baed82012-05-08 14:48:45 -0700184#if !LOG_NDEBUG
185 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
186 /* It is not a VSYNC or a backlight event */
187 ALOGV("unexpected event from subsystem %s", subsys);
188#endif
Ashish Sharma6337b882012-04-10 19:47:09 -0700189 }
San Mehatd1830422010-01-15 08:02:39 -0800190}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800191
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900192void NetlinkHandler::notify(int code, const char *format, ...) {
193 char *msg;
194 va_list args;
195 va_start(args, format);
196 if (vasprintf(&msg, format, args) >= 0) {
197 mNm->getBroadcaster()->sendBroadcast(code, msg, false);
198 free(msg);
199 } else {
200 SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
201 }
202 va_end(args);
203}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800204
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900205void NetlinkHandler::notifyInterfaceAdded(const char *name) {
206 notify(ResponseCode::InterfaceChange, "Iface added %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800207}
208
209void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900210 notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800211}
212
213void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900214 notify(ResponseCode::InterfaceChange,
215 "Iface changed %s %s", name, (isUp ? "up" : "down"));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700216}
217
218void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900219 notify(ResponseCode::InterfaceChange,
220 "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
Robert Greenwalt67c57532010-02-17 17:42:37 -0800221}
JP Abgralle0ebc462011-07-21 17:21:49 -0700222
223void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900224 notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
JP Abgralle0ebc462011-07-21 17:21:49 -0700225}
Ashish Sharma6337b882012-04-10 19:47:09 -0700226
Haoyu Bai98f65d32012-06-28 16:16:51 -0700227void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700228 bool isActive,
229 const char *timestamp,
230 const char *uid) {
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700231 if (timestamp == NULL)
232 notify(ResponseCode::InterfaceClassActivity,
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900233 "IfaceClass %s %s", isActive ? "active" : "idle", name);
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700234 else if (uid != NULL && isActive)
235 notify(ResponseCode::InterfaceClassActivity,
236 "IfaceClass active %s %s %s", name, timestamp, uid);
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700237 else
238 notify(ResponseCode::InterfaceClassActivity,
239 "IfaceClass %s %s %s", isActive ? "active" : "idle", name, timestamp);
Ashish Sharma6337b882012-04-10 19:47:09 -0700240}
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900241
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700242void NetlinkHandler::notifyAddressChanged(NetlinkEvent::Action action, const char *addr,
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900243 const char *iface, const char *flags,
244 const char *scope) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900245 notify(ResponseCode::InterfaceAddressChange,
246 "Address %s %s %s %s %s",
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700247 (action == NetlinkEvent::Action::kAddressUpdated) ? kUpdated : kRemoved,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900248 addr, iface, flags, scope);
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900249}
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900250
251void NetlinkHandler::notifyInterfaceDnsServers(const char *iface,
252 const char *lifetime,
253 const char *servers) {
254 notify(ResponseCode::InterfaceDnsInfo, "DnsInfo servers %s %s %s",
255 iface, lifetime, servers);
256}
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900257
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700258void NetlinkHandler::notifyRouteChange(NetlinkEvent::Action action, const char *route,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900259 const char *gateway, const char *iface) {
260 notify(ResponseCode::RouteChange,
261 "Route %s %s%s%s%s%s",
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700262 (action == NetlinkEvent::Action::kRouteUpdated) ? kUpdated : kRemoved,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900263 route,
Manoj Gupta75c970f2016-11-22 21:15:59 -0800264 (gateway && *gateway) ? " via " : "",
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900265 gateway,
Manoj Gupta75c970f2016-11-22 21:15:59 -0800266 (iface && *iface) ? " dev " : "",
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900267 iface);
268}
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700269
270void NetlinkHandler::notifyStrictCleartext(const char* uid, const char* hex) {
271 notify(ResponseCode::StrictCleartext, "%s %s", uid, hex);
272}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900273
274} // namespace net
275} // namespace android