2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #define LOG_TAG "Netd"
25 #include <cutils/log.h>
27 #include <netutils/ifc.h>
28 #include <sysutils/NetlinkEvent.h>
29 #include "NetlinkHandler.h"
30 #include "NetlinkManager.h"
31 #include "ResponseCode.h"
34 static const char *kUpdated = "updated";
35 static const char *kRemoved = "removed";
37 NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
39 NetlinkListener(listenerSocket, format) {
43 NetlinkHandler::~NetlinkHandler() {
46 int NetlinkHandler::start() {
47 return this->startListener();
50 int NetlinkHandler::stop() {
51 return this->stopListener();
54 void NetlinkHandler::onEvent(NetlinkEvent *evt) {
55 const char *subsys = evt->getSubsystem();
57 ALOGW("No subsystem found in netlink event");
61 if (!strcmp(subsys, "net")) {
62 NetlinkEvent::Action action = evt->getAction();
63 const char *iface = evt->findParam("INTERFACE");
65 if (action == NetlinkEvent::Action::kAdd) {
66 notifyInterfaceAdded(iface);
67 } else if (action == NetlinkEvent::Action::kRemove) {
68 notifyInterfaceRemoved(iface);
69 } else if (action == NetlinkEvent::Action::kChange) {
71 notifyInterfaceChanged("nana", true);
72 } else if (action == NetlinkEvent::Action::kLinkUp) {
73 notifyInterfaceLinkChanged(iface, true);
74 } else if (action == NetlinkEvent::Action::kLinkDown) {
75 notifyInterfaceLinkChanged(iface, false);
76 } else if (action == NetlinkEvent::Action::kAddressUpdated ||
77 action == NetlinkEvent::Action::kAddressRemoved) {
78 const char *address = evt->findParam("ADDRESS");
79 const char *flags = evt->findParam("FLAGS");
80 const char *scope = evt->findParam("SCOPE");
81 if (action == NetlinkEvent::Action::kAddressRemoved && iface && address) {
82 // Note: if this interface was deleted, iface is "" and we don't notify.
85 char addrstr[INET6_ADDRSTRLEN];
86 strncpy(addrstr, address, sizeof(addrstr));
87 char *slash = strchr(addrstr, '/');
92 int ret = sd.destroySockets(addrstr);
94 ALOGE("Error destroying sockets: %s", strerror(ret));
97 ALOGE("Error opening NETLINK_SOCK_DIAG socket: %s", strerror(errno));
100 // TODO: delete this once SOCK_DESTROY works everywhere.
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));
111 if (iface && iface[0] && address && flags && scope) {
112 notifyAddressChanged(action, address, iface, flags, scope);
114 } else if (action == NetlinkEvent::Action::kRdnss) {
115 const char *lifetime = evt->findParam("LIFETIME");
116 const char *servers = evt->findParam("SERVERS");
117 if (lifetime && servers) {
118 notifyInterfaceDnsServers(iface, lifetime, servers);
120 } else if (action == NetlinkEvent::Action::kRouteUpdated ||
121 action == NetlinkEvent::Action::kRouteRemoved) {
122 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);
130 } else if (!strcmp(subsys, "qlog")) {
131 const char *alertName = evt->findParam("ALERT_NAME");
132 const char *iface = evt->findParam("INTERFACE");
133 notifyQuotaLimitReached(alertName, iface);
135 } else if (!strcmp(subsys, "strict")) {
136 const char *uid = evt->findParam("UID");
137 const char *hex = evt->findParam("HEX");
138 notifyStrictCleartext(uid, hex);
140 } else if (!strcmp(subsys, "xt_idletimer")) {
141 const char *label = evt->findParam("INTERFACE");
142 const char *state = evt->findParam("STATE");
143 const char *timestamp = evt->findParam("TIME_NS");
144 const char *uid = evt->findParam("UID");
146 notifyInterfaceClassActivity(label, !strcmp("active", state),
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);
157 void NetlinkHandler::notify(int code, const char *format, ...) {
160 va_start(args, format);
161 if (vasprintf(&msg, format, args) >= 0) {
162 mNm->getBroadcaster()->sendBroadcast(code, msg, false);
165 SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
170 void NetlinkHandler::notifyInterfaceAdded(const char *name) {
171 notify(ResponseCode::InterfaceChange, "Iface added %s", name);
174 void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
175 notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
178 void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
179 notify(ResponseCode::InterfaceChange,
180 "Iface changed %s %s", name, (isUp ? "up" : "down"));
183 void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
184 notify(ResponseCode::InterfaceChange,
185 "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
188 void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
189 notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
192 void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
194 const char *timestamp,
196 if (timestamp == NULL)
197 notify(ResponseCode::InterfaceClassActivity,
198 "IfaceClass %s %s", isActive ? "active" : "idle", name);
199 else if (uid != NULL && isActive)
200 notify(ResponseCode::InterfaceClassActivity,
201 "IfaceClass active %s %s %s", name, timestamp, uid);
203 notify(ResponseCode::InterfaceClassActivity,
204 "IfaceClass %s %s %s", isActive ? "active" : "idle", name, timestamp);
207 void NetlinkHandler::notifyAddressChanged(NetlinkEvent::Action action, const char *addr,
208 const char *iface, const char *flags,
210 notify(ResponseCode::InterfaceAddressChange,
211 "Address %s %s %s %s %s",
212 (action == NetlinkEvent::Action::kAddressUpdated) ? kUpdated : kRemoved,
213 addr, iface, flags, scope);
216 void 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);
223 void NetlinkHandler::notifyRouteChange(NetlinkEvent::Action action, const char *route,
224 const char *gateway, const char *iface) {
225 notify(ResponseCode::RouteChange,
226 "Route %s %s%s%s%s%s",
227 (action == NetlinkEvent::Action::kRouteUpdated) ? kUpdated : kRemoved,
229 *gateway ? " via " : "",
231 *iface ? " dev " : "",
235 void NetlinkHandler::notifyStrictCleartext(const char* uid, const char* hex) {
236 notify(ResponseCode::StrictCleartext, "%s %s", uid, hex);