blob: 97dc3e03690f3fce2edecc20263f6b3bf200f6b4 [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"
San Mehatd1830422010-01-15 08:02:39 -080032
Lorenzo Colittibd0f2242014-06-12 13:51:05 +090033static const char *kUpdated = "updated";
34static const char *kRemoved = "removed";
35
Mike J. Chen564df4e2011-06-23 15:07:35 -070036NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
37 int format) :
38 NetlinkListener(listenerSocket, format) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080039 mNm = nm;
San Mehatd1830422010-01-15 08:02:39 -080040}
41
42NetlinkHandler::~NetlinkHandler() {
43}
44
45int NetlinkHandler::start() {
46 return this->startListener();
47}
48
49int NetlinkHandler::stop() {
50 return this->stopListener();
51}
52
53void NetlinkHandler::onEvent(NetlinkEvent *evt) {
54 const char *subsys = evt->getSubsystem();
San Mehatd1830422010-01-15 08:02:39 -080055 if (!subsys) {
Steve Block0e76b762012-01-05 23:21:51 +000056 ALOGW("No subsystem found in netlink event");
San Mehatd1830422010-01-15 08:02:39 -080057 return;
58 }
Mike J. Chen564df4e2011-06-23 15:07:35 -070059
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090060 if (!strcmp(subsys, "net")) {
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070061 NetlinkEvent::Action action = evt->getAction();
Mike J. Chen564df4e2011-06-23 15:07:35 -070062 const char *iface = evt->findParam("INTERFACE");
63
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070064 if (action == NetlinkEvent::Action::kAdd) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080065 notifyInterfaceAdded(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070066 } else if (action == NetlinkEvent::Action::kRemove) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080067 notifyInterfaceRemoved(iface);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070068 } else if (action == NetlinkEvent::Action::kChange) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080069 evt->dump();
Robert Greenwalt67c57532010-02-17 17:42:37 -080070 notifyInterfaceChanged("nana", true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070071 } else if (action == NetlinkEvent::Action::kLinkUp) {
Mike J. Chen564df4e2011-06-23 15:07:35 -070072 notifyInterfaceLinkChanged(iface, true);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070073 } else if (action == NetlinkEvent::Action::kLinkDown) {
Mike J. Chen564df4e2011-06-23 15:07:35 -070074 notifyInterfaceLinkChanged(iface, false);
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070075 } else if (action == NetlinkEvent::Action::kAddressUpdated ||
76 action == NetlinkEvent::Action::kAddressRemoved) {
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090077 const char *address = evt->findParam("ADDRESS");
78 const char *flags = evt->findParam("FLAGS");
79 const char *scope = evt->findParam("SCOPE");
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070080 if (action == NetlinkEvent::Action::kAddressRemoved && iface && address) {
Sreeram Ramachandranaf37dd32014-09-08 16:03:18 -070081 int resetMask = strchr(address, ':') ? RESET_IPV6_ADDRESSES : RESET_IPV4_ADDRESSES;
82 resetMask |= RESET_IGNORE_INTERFACE_ADDRESS;
83 if (int ret = ifc_reset_connections(iface, resetMask)) {
84 ALOGE("ifc_reset_connections failed on iface %s for address %s (%s)", iface,
85 address, strerror(ret));
86 }
87 }
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090088 if (iface && flags && scope) {
89 notifyAddressChanged(action, address, iface, flags, scope);
90 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070091 } else if (action == NetlinkEvent::Action::kRdnss) {
Lorenzo Colitti12acae82013-10-24 14:51:57 +090092 const char *lifetime = evt->findParam("LIFETIME");
93 const char *servers = evt->findParam("SERVERS");
94 if (lifetime && servers) {
95 notifyInterfaceDnsServers(iface, lifetime, servers);
96 }
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -070097 } else if (action == NetlinkEvent::Action::kRouteUpdated ||
98 action == NetlinkEvent::Action::kRouteRemoved) {
Lorenzo Colittibd0f2242014-06-12 13:51:05 +090099 const char *route = evt->findParam("ROUTE");
100 const char *gateway = evt->findParam("GATEWAY");
101 const char *iface = evt->findParam("INTERFACE");
102 if (route && (gateway || iface)) {
103 notifyRouteChange(action, route, gateway, iface);
104 }
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900105 }
106
JP Abgralle0ebc462011-07-21 17:21:49 -0700107 } else if (!strcmp(subsys, "qlog")) {
108 const char *alertName = evt->findParam("ALERT_NAME");
109 const char *iface = evt->findParam("INTERFACE");
110 notifyQuotaLimitReached(alertName, iface);
JP Abgralle07effe2012-04-27 00:02:53 -0700111
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700112 } else if (!strcmp(subsys, "strict")) {
113 const char *uid = evt->findParam("UID");
114 const char *hex = evt->findParam("HEX");
115 notifyStrictCleartext(uid, hex);
116
JP Abgralle07effe2012-04-27 00:02:53 -0700117 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharmac79bcc42014-02-12 11:53:00 -0800118 const char *label = evt->findParam("INTERFACE");
JP Abgralle07effe2012-04-27 00:02:53 -0700119 const char *state = evt->findParam("STATE");
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700120 const char *timestamp = evt->findParam("TIME_NS");
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700121 const char *uid = evt->findParam("UID");
JP Abgralle07effe2012-04-27 00:02:53 -0700122 if (state)
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700123 notifyInterfaceClassActivity(label, !strcmp("active", state),
124 timestamp, uid);
JP Abgralle0ebc462011-07-21 17:21:49 -0700125
JP Abgrall40baed82012-05-08 14:48:45 -0700126#if !LOG_NDEBUG
127 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
128 /* It is not a VSYNC or a backlight event */
129 ALOGV("unexpected event from subsystem %s", subsys);
130#endif
Ashish Sharma6337b882012-04-10 19:47:09 -0700131 }
San Mehatd1830422010-01-15 08:02:39 -0800132}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800133
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900134void NetlinkHandler::notify(int code, const char *format, ...) {
135 char *msg;
136 va_list args;
137 va_start(args, format);
138 if (vasprintf(&msg, format, args) >= 0) {
139 mNm->getBroadcaster()->sendBroadcast(code, msg, false);
140 free(msg);
141 } else {
142 SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
143 }
144 va_end(args);
145}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800146
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900147void NetlinkHandler::notifyInterfaceAdded(const char *name) {
148 notify(ResponseCode::InterfaceChange, "Iface added %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800149}
150
151void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900152 notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800153}
154
155void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900156 notify(ResponseCode::InterfaceChange,
157 "Iface changed %s %s", name, (isUp ? "up" : "down"));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700158}
159
160void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900161 notify(ResponseCode::InterfaceChange,
162 "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
Robert Greenwalt67c57532010-02-17 17:42:37 -0800163}
JP Abgralle0ebc462011-07-21 17:21:49 -0700164
165void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900166 notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
JP Abgralle0ebc462011-07-21 17:21:49 -0700167}
Ashish Sharma6337b882012-04-10 19:47:09 -0700168
Haoyu Bai98f65d32012-06-28 16:16:51 -0700169void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700170 bool isActive,
171 const char *timestamp,
172 const char *uid) {
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700173 if (timestamp == NULL)
174 notify(ResponseCode::InterfaceClassActivity,
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900175 "IfaceClass %s %s", isActive ? "active" : "idle", name);
Ruchi Kandoi05c39f02015-04-23 12:40:56 -0700176 else if (uid != NULL && isActive)
177 notify(ResponseCode::InterfaceClassActivity,
178 "IfaceClass active %s %s %s", name, timestamp, uid);
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700179 else
180 notify(ResponseCode::InterfaceClassActivity,
181 "IfaceClass %s %s %s", isActive ? "active" : "idle", name, timestamp);
Ashish Sharma6337b882012-04-10 19:47:09 -0700182}
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900183
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700184void NetlinkHandler::notifyAddressChanged(NetlinkEvent::Action action, const char *addr,
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900185 const char *iface, const char *flags,
186 const char *scope) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900187 notify(ResponseCode::InterfaceAddressChange,
188 "Address %s %s %s %s %s",
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700189 (action == NetlinkEvent::Action::kAddressUpdated) ? kUpdated : kRemoved,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900190 addr, iface, flags, scope);
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900191}
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900192
193void NetlinkHandler::notifyInterfaceDnsServers(const char *iface,
194 const char *lifetime,
195 const char *servers) {
196 notify(ResponseCode::InterfaceDnsInfo, "DnsInfo servers %s %s %s",
197 iface, lifetime, servers);
198}
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900199
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700200void NetlinkHandler::notifyRouteChange(NetlinkEvent::Action action, const char *route,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900201 const char *gateway, const char *iface) {
202 notify(ResponseCode::RouteChange,
203 "Route %s %s%s%s%s%s",
Jeff Sharkey32fa9ba2015-03-13 13:35:17 -0700204 (action == NetlinkEvent::Action::kRouteUpdated) ? kUpdated : kRemoved,
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900205 route,
206 *gateway ? " via " : "",
207 gateway,
208 *iface ? " dev " : "",
209 iface);
210}
Jeff Sharkeyfbe497f2014-10-28 16:50:07 -0700211
212void NetlinkHandler::notifyStrictCleartext(const char* uid, const char* hex) {
213 notify(ResponseCode::StrictCleartext, "%s %s", uid, hex);
214}