blob: 718fbdb987fe5d9f2da19f8b85becc91c37b9fd4 [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"
San Mehatd1830422010-01-15 08:02:39 -080033
Lorenzo Colittibd0f2242014-06-12 13:51:05 +090034static const char *kUpdated = "updated";
35static const char *kRemoved = "removed";
36
Mike J. Chen564df4e2011-06-23 15:07:35 -070037NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
38 int format) :
39 NetlinkListener(listenerSocket, format) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080040 mNm = nm;
San Mehatd1830422010-01-15 08:02:39 -080041}
42
43NetlinkHandler::~NetlinkHandler() {
44}
45
46int NetlinkHandler::start() {
47 return this->startListener();
48}
49
50int NetlinkHandler::stop() {
51 return this->stopListener();
52}
53
54void NetlinkHandler::onEvent(NetlinkEvent *evt) {
55 const char *subsys = evt->getSubsystem();
San Mehatd1830422010-01-15 08:02:39 -080056 if (!subsys) {
Steve Block0e76b762012-01-05 23:21:51 +000057 ALOGW("No subsystem found in netlink event");
San Mehatd1830422010-01-15 08:02:39 -080058 return;
59 }
Mike J. Chen564df4e2011-06-23 15:07:35 -070060
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090061 if (!strcmp(subsys, "net")) {
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070062 NetlinkEvent::Action action = evt->getAction();
Mike J. Chen564df4e2011-06-23 15:07:35 -070063 const char *iface = evt->findParam("INTERFACE");
64
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070065 if (action == NetlinkEvent::Action::kAdd) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080066 notifyInterfaceAdded(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070067 } else if (action == NetlinkEvent::Action::kRemove) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080068 notifyInterfaceRemoved(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070069 } else if (action == NetlinkEvent::Action::kChange) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080070 evt->dump();
Robert Greenwalt67c57532010-02-17 17:42:37 -080071 notifyInterfaceChanged("nana", true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070072 } else if (action == NetlinkEvent::Action::kLinkUp) {
Mike J. Chen564df4e2011-06-23 15:07:35 -070073 notifyInterfaceLinkChanged(iface, true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070074 } else if (action == NetlinkEvent::Action::kLinkDown) {
Mike J. Chen564df4e2011-06-23 15:07:35 -070075 notifyInterfaceLinkChanged(iface, false);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070076 } else if (action == NetlinkEvent::Action::kAddressUpdated ||
77 action == NetlinkEvent::Action::kAddressRemoved) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090078 const char *address = evt->findParam("ADDRESS");
79 const char *flags = evt->findParam("FLAGS");
80 const char *scope = evt->findParam("SCOPE");
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070081 if (action == NetlinkEvent::Action::kAddressRemoved && iface && address) {
Lorenzo Colittif32fc592016-02-15 01:09:14 +090082 // Note: if this interface was deleted, iface is "" and we don't notify.
83 SockDiag sd;
84 if (sd.open()) {
85 char addrstr[INET6_ADDRSTRLEN];
86 strncpy(addrstr, address, sizeof(addrstr));
87 char *slash = strchr(addrstr, '/');
88 if (slash) {
89 *slash = '\0';
90 }
91
92 int ret = sd.destroySockets(addrstr);
93 if (ret < 0) {
94 ALOGE("Error destroying sockets: %s", strerror(ret));
95 }
96 } else {
97 ALOGE("Error opening NETLINK_SOCK_DIAG socket: %s", strerror(errno));
98 }
99
100 // TODO: delete this once SOCK_DESTROY works everywhere.
101 if (iface[0]) {
102 int resetMask = strchr(address, ':') ?
103 RESET_IPV6_ADDRESSES : RESET_IPV4_ADDRESSES;
104 resetMask |= RESET_IGNORE_INTERFACE_ADDRESS;
105 if (int ret = ifc_reset_connections(iface, resetMask)) {
106 ALOGE("ifc_reset_connections failed on iface %s for address %s (%s)", iface,
107 address, strerror(ret));
108 }
Sreeram Ramachandranaf37dd32014-09-08 16:03:18 -0700109 }
110 }
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900111 if (iface && iface[0] && address && flags && scope) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900112 notifyAddressChanged(action, address, iface, flags, scope);
113 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700114 } else if (action == NetlinkEvent::Action::kRdnss) {
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900115 const char *lifetime = evt->findParam("LIFETIME");
116 const char *servers = evt->findParam("SERVERS");
117 if (lifetime && servers) {
118 notifyInterfaceDnsServers(iface, lifetime, servers);
119 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700120 } else if (action == NetlinkEvent::Action::kRouteUpdated ||
121 action == NetlinkEvent::Action::kRouteRemoved) {
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900122 const char *route = evt->findParam("ROUTE");
123 const char *gateway = evt->findParam("GATEWAY");
124 const char *iface = evt->findParam("INTERFACE");
125 if (route && (gateway || iface)) {
126 notifyRouteChange(action, route, gateway, iface);
127 }
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900128 }
129
JP Abgralle0ebc462011-07-21 17:21:49 -0700130 } else if (!strcmp(subsys, "qlog")) {
131 const char *alertName = evt->findParam("ALERT_NAME");
132 const char *iface = evt->findParam("INTERFACE");
133 notifyQuotaLimitReached(alertName, iface);
JP Abgralle07effe2012-04-27 00:02:53 -0700134
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700135 } else if (!strcmp(subsys, "strict")) {
136 const char *uid = evt->findParam("UID");
137 const char *hex = evt->findParam("HEX");
138 notifyStrictCleartext(uid, hex);
139
JP Abgralle07effe2012-04-27 00:02:53 -0700140 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharmac79bcc42014-02-12 11:53:00 -0800141 const char *label = evt->findParam("INTERFACE");
JP Abgralle07effe2012-04-27 00:02:53 -0700142 const char *state = evt->findParam("STATE");
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700143 const char *timestamp = evt->findParam("TIME_NS");
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700144 const char *uid = evt->findParam("UID");
JP Abgralle07effe2012-04-27 00:02:53 -0700145 if (state)
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700146 notifyInterfaceClassActivity(label, !strcmp("active", state),
147 timestamp, uid);
JP Abgralle0ebc462011-07-21 17:21:49 -0700148
JP Abgrall40baed82012-05-08 14:48:45 -0700149#if !LOG_NDEBUG
150 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
151 /* It is not a VSYNC or a backlight event */
152 ALOGV("unexpected event from subsystem %s", subsys);
153#endif
Ashish Sharma6337b882012-04-10 19:47:09 -0700154 }
San Mehatd1830422010-01-15 08:02:39 -0800155}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800156
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900157void NetlinkHandler::notify(int code, const char *format, ...) {
158 char *msg;
159 va_list args;
160 va_start(args, format);
161 if (vasprintf(&msg, format, args) >= 0) {
162 mNm->getBroadcaster()->sendBroadcast(code, msg, false);
163 free(msg);
164 } else {
165 SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
166 }
167 va_end(args);
168}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800169
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900170void NetlinkHandler::notifyInterfaceAdded(const char *name) {
171 notify(ResponseCode::InterfaceChange, "Iface added %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800172}
173
174void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900175 notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800176}
177
178void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900179 notify(ResponseCode::InterfaceChange,
180 "Iface changed %s %s", name, (isUp ? "up" : "down"));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700181}
182
183void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900184 notify(ResponseCode::InterfaceChange,
185 "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
Robert Greenwalt67c57532010-02-17 17:42:37 -0800186}
JP Abgralle0ebc462011-07-21 17:21:49 -0700187
188void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900189 notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
JP Abgralle0ebc462011-07-21 17:21:49 -0700190}
Ashish Sharma6337b882012-04-10 19:47:09 -0700191
Haoyu Bai98f65d32012-06-28 16:16:51 -0700192void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700193 bool isActive,
194 const char *timestamp,
195 const char *uid) {
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700196 if (timestamp == NULL)
197 notify(ResponseCode::InterfaceClassActivity,
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900198 "IfaceClass %s %s", isActive ? "active" : "idle", name);
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700199 else if (uid != NULL && isActive)
200 notify(ResponseCode::InterfaceClassActivity,
201 "IfaceClass active %s %s %s", name, timestamp, uid);
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700202 else
203 notify(ResponseCode::InterfaceClassActivity,
204 "IfaceClass %s %s %s", isActive ? "active" : "idle", name, timestamp);
Ashish Sharma6337b882012-04-10 19:47:09 -0700205}
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900206
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700207void NetlinkHandler::notifyAddressChanged(NetlinkEvent::Action action, const char *addr,
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900208 const char *iface, const char *flags,
209 const char *scope) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900210 notify(ResponseCode::InterfaceAddressChange,
211 "Address %s %s %s %s %s",
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700212 (action == NetlinkEvent::Action::kAddressUpdated) ? kUpdated : kRemoved,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900213 addr, iface, flags, scope);
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900214}
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900215
216void NetlinkHandler::notifyInterfaceDnsServers(const char *iface,
217 const char *lifetime,
218 const char *servers) {
219 notify(ResponseCode::InterfaceDnsInfo, "DnsInfo servers %s %s %s",
220 iface, lifetime, servers);
221}
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900222
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700223void NetlinkHandler::notifyRouteChange(NetlinkEvent::Action action, const char *route,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900224 const char *gateway, const char *iface) {
225 notify(ResponseCode::RouteChange,
226 "Route %s %s%s%s%s%s",
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700227 (action == NetlinkEvent::Action::kRouteUpdated) ? kUpdated : kRemoved,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900228 route,
229 *gateway ? " via " : "",
230 gateway,
231 *iface ? " dev " : "",
232 iface);
233}
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700234
235void NetlinkHandler::notifyStrictCleartext(const char* uid, const char* hex) {
236 notify(ResponseCode::StrictCleartext, "%s %s", uid, hex);
237}