blob: ac6f5a355000ff23d265655d021dd91018a37370 [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
Lorenzo Colitti7035f222017-02-13 18:29:00 +090037namespace android {
38namespace net {
39
Mike J. Chen564df4e2011-06-23 15:07:35 -070040NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
41 int format) :
42 NetlinkListener(listenerSocket, format) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080043 mNm = nm;
San Mehatd1830422010-01-15 08:02:39 -080044}
45
46NetlinkHandler::~NetlinkHandler() {
47}
48
49int NetlinkHandler::start() {
50 return this->startListener();
51}
52
53int NetlinkHandler::stop() {
54 return this->stopListener();
55}
56
57void NetlinkHandler::onEvent(NetlinkEvent *evt) {
58 const char *subsys = evt->getSubsystem();
San Mehatd1830422010-01-15 08:02:39 -080059 if (!subsys) {
Steve Block0e76b762012-01-05 23:21:51 +000060 ALOGW("No subsystem found in netlink event");
San Mehatd1830422010-01-15 08:02:39 -080061 return;
62 }
Mike J. Chen564df4e2011-06-23 15:07:35 -070063
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090064 if (!strcmp(subsys, "net")) {
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070065 NetlinkEvent::Action action = evt->getAction();
Mike J. Chen564df4e2011-06-23 15:07:35 -070066 const char *iface = evt->findParam("INTERFACE");
67
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070068 if (action == NetlinkEvent::Action::kAdd) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080069 notifyInterfaceAdded(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070070 } else if (action == NetlinkEvent::Action::kRemove) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080071 notifyInterfaceRemoved(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070072 } else if (action == NetlinkEvent::Action::kChange) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080073 evt->dump();
Robert Greenwalt67c57532010-02-17 17:42:37 -080074 notifyInterfaceChanged("nana", true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070075 } else if (action == NetlinkEvent::Action::kLinkUp) {
Mike J. Chen564df4e2011-06-23 15:07:35 -070076 notifyInterfaceLinkChanged(iface, true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070077 } else if (action == NetlinkEvent::Action::kLinkDown) {
Mike J. Chen564df4e2011-06-23 15:07:35 -070078 notifyInterfaceLinkChanged(iface, false);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070079 } else if (action == NetlinkEvent::Action::kAddressUpdated ||
80 action == NetlinkEvent::Action::kAddressRemoved) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090081 const char *address = evt->findParam("ADDRESS");
82 const char *flags = evt->findParam("FLAGS");
83 const char *scope = evt->findParam("SCOPE");
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070084 if (action == NetlinkEvent::Action::kAddressRemoved && iface && address) {
Lorenzo Colittif32fc592016-02-15 01:09:14 +090085 // Note: if this interface was deleted, iface is "" and we don't notify.
86 SockDiag sd;
87 if (sd.open()) {
88 char addrstr[INET6_ADDRSTRLEN];
89 strncpy(addrstr, address, sizeof(addrstr));
90 char *slash = strchr(addrstr, '/');
91 if (slash) {
92 *slash = '\0';
93 }
94
95 int ret = sd.destroySockets(addrstr);
96 if (ret < 0) {
97 ALOGE("Error destroying sockets: %s", strerror(ret));
98 }
99 } else {
100 ALOGE("Error opening NETLINK_SOCK_DIAG socket: %s", strerror(errno));
101 }
Sreeram Ramachandranaf37dd32014-09-08 16:03:18 -0700102 }
Lorenzo Colittif32fc592016-02-15 01:09:14 +0900103 if (iface && iface[0] && address && flags && scope) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900104 notifyAddressChanged(action, address, iface, flags, scope);
105 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700106 } else if (action == NetlinkEvent::Action::kRdnss) {
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900107 const char *lifetime = evt->findParam("LIFETIME");
108 const char *servers = evt->findParam("SERVERS");
109 if (lifetime && servers) {
110 notifyInterfaceDnsServers(iface, lifetime, servers);
111 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700112 } else if (action == NetlinkEvent::Action::kRouteUpdated ||
113 action == NetlinkEvent::Action::kRouteRemoved) {
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900114 const char *route = evt->findParam("ROUTE");
115 const char *gateway = evt->findParam("GATEWAY");
116 const char *iface = evt->findParam("INTERFACE");
117 if (route && (gateway || iface)) {
118 notifyRouteChange(action, route, gateway, iface);
119 }
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900120 }
121
Bryse Flowersbc5f0602016-06-01 13:00:12 -0700122 } else if (!strcmp(subsys, "qlog") || !strcmp(subsys, "xt_quota2")) {
JP Abgralle0ebc462011-07-21 17:21:49 -0700123 const char *alertName = evt->findParam("ALERT_NAME");
124 const char *iface = evt->findParam("INTERFACE");
125 notifyQuotaLimitReached(alertName, iface);
JP Abgralle07effe2012-04-27 00:02:53 -0700126
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700127 } else if (!strcmp(subsys, "strict")) {
128 const char *uid = evt->findParam("UID");
129 const char *hex = evt->findParam("HEX");
130 notifyStrictCleartext(uid, hex);
131
JP Abgralle07effe2012-04-27 00:02:53 -0700132 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharmac79bcc42014-02-12 11:53:00 -0800133 const char *label = evt->findParam("INTERFACE");
JP Abgralle07effe2012-04-27 00:02:53 -0700134 const char *state = evt->findParam("STATE");
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700135 const char *timestamp = evt->findParam("TIME_NS");
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700136 const char *uid = evt->findParam("UID");
JP Abgralle07effe2012-04-27 00:02:53 -0700137 if (state)
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700138 notifyInterfaceClassActivity(label, !strcmp("active", state),
139 timestamp, uid);
JP Abgralle0ebc462011-07-21 17:21:49 -0700140
JP Abgrall40baed82012-05-08 14:48:45 -0700141#if !LOG_NDEBUG
142 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
143 /* It is not a VSYNC or a backlight event */
144 ALOGV("unexpected event from subsystem %s", subsys);
145#endif
Ashish Sharma6337b882012-04-10 19:47:09 -0700146 }
San Mehatd1830422010-01-15 08:02:39 -0800147}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800148
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900149void NetlinkHandler::notify(int code, const char *format, ...) {
150 char *msg;
151 va_list args;
152 va_start(args, format);
153 if (vasprintf(&msg, format, args) >= 0) {
154 mNm->getBroadcaster()->sendBroadcast(code, msg, false);
155 free(msg);
156 } else {
157 SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
158 }
159 va_end(args);
160}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800161
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900162void NetlinkHandler::notifyInterfaceAdded(const char *name) {
163 notify(ResponseCode::InterfaceChange, "Iface added %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800164}
165
166void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900167 notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800168}
169
170void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900171 notify(ResponseCode::InterfaceChange,
172 "Iface changed %s %s", name, (isUp ? "up" : "down"));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700173}
174
175void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900176 notify(ResponseCode::InterfaceChange,
177 "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
Robert Greenwalt67c57532010-02-17 17:42:37 -0800178}
JP Abgralle0ebc462011-07-21 17:21:49 -0700179
180void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900181 notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
JP Abgralle0ebc462011-07-21 17:21:49 -0700182}
Ashish Sharma6337b882012-04-10 19:47:09 -0700183
Haoyu Bai98f65d32012-06-28 16:16:51 -0700184void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700185 bool isActive,
186 const char *timestamp,
187 const char *uid) {
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700188 if (timestamp == NULL)
189 notify(ResponseCode::InterfaceClassActivity,
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900190 "IfaceClass %s %s", isActive ? "active" : "idle", name);
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700191 else if (uid != NULL && isActive)
192 notify(ResponseCode::InterfaceClassActivity,
193 "IfaceClass active %s %s %s", name, timestamp, uid);
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700194 else
195 notify(ResponseCode::InterfaceClassActivity,
196 "IfaceClass %s %s %s", isActive ? "active" : "idle", name, timestamp);
Ashish Sharma6337b882012-04-10 19:47:09 -0700197}
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900198
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700199void NetlinkHandler::notifyAddressChanged(NetlinkEvent::Action action, const char *addr,
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900200 const char *iface, const char *flags,
201 const char *scope) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900202 notify(ResponseCode::InterfaceAddressChange,
203 "Address %s %s %s %s %s",
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700204 (action == NetlinkEvent::Action::kAddressUpdated) ? kUpdated : kRemoved,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900205 addr, iface, flags, scope);
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900206}
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900207
208void NetlinkHandler::notifyInterfaceDnsServers(const char *iface,
209 const char *lifetime,
210 const char *servers) {
211 notify(ResponseCode::InterfaceDnsInfo, "DnsInfo servers %s %s %s",
212 iface, lifetime, servers);
213}
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900214
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700215void NetlinkHandler::notifyRouteChange(NetlinkEvent::Action action, const char *route,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900216 const char *gateway, const char *iface) {
217 notify(ResponseCode::RouteChange,
218 "Route %s %s%s%s%s%s",
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700219 (action == NetlinkEvent::Action::kRouteUpdated) ? kUpdated : kRemoved,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900220 route,
Manoj Gupta75c970f2016-11-22 21:15:59 -0800221 (gateway && *gateway) ? " via " : "",
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900222 gateway,
Manoj Gupta75c970f2016-11-22 21:15:59 -0800223 (iface && *iface) ? " dev " : "",
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900224 iface);
225}
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700226
227void NetlinkHandler::notifyStrictCleartext(const char* uid, const char* hex) {
228 notify(ResponseCode::StrictCleartext, "%s %s", uid, hex);
229}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900230
231} // namespace net
232} // namespace android