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 <sysutils/NetlinkEvent.h>
28 #include "NetlinkHandler.h"
29 #include "NetlinkManager.h"
30 #include "ResponseCode.h"
32 NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
34 NetlinkListener(listenerSocket, format) {
38 NetlinkHandler::~NetlinkHandler() {
41 int NetlinkHandler::start() {
42 return this->startListener();
45 int NetlinkHandler::stop() {
46 return this->stopListener();
49 void NetlinkHandler::onEvent(NetlinkEvent *evt) {
50 const char *subsys = evt->getSubsystem();
52 ALOGW("No subsystem found in netlink event");
56 if (!strcmp(subsys, "net")) {
57 int action = evt->getAction();
58 const char *iface = evt->findParam("INTERFACE");
60 if (action == evt->NlActionAdd) {
61 notifyInterfaceAdded(iface);
62 } else if (action == evt->NlActionRemove) {
63 notifyInterfaceRemoved(iface);
64 } else if (action == evt->NlActionChange) {
66 notifyInterfaceChanged("nana", true);
67 } else if (action == evt->NlActionLinkUp) {
68 notifyInterfaceLinkChanged(iface, true);
69 } else if (action == evt->NlActionLinkDown) {
70 notifyInterfaceLinkChanged(iface, false);
71 } else if (action == evt->NlActionAddressUpdated ||
72 action == evt->NlActionAddressRemoved) {
73 const char *address = evt->findParam("ADDRESS");
74 const char *flags = evt->findParam("FLAGS");
75 const char *scope = evt->findParam("SCOPE");
76 if (iface && flags && scope) {
77 notifyAddressChanged(action, address, iface, flags, scope);
79 } else if (action == evt->NlActionRdnss) {
80 const char *lifetime = evt->findParam("LIFETIME");
81 const char *servers = evt->findParam("SERVERS");
82 if (lifetime && servers) {
83 notifyInterfaceDnsServers(iface, lifetime, servers);
87 } else if (!strcmp(subsys, "qlog")) {
88 const char *alertName = evt->findParam("ALERT_NAME");
89 const char *iface = evt->findParam("INTERFACE");
90 notifyQuotaLimitReached(alertName, iface);
92 } else if (!strcmp(subsys, "xt_idletimer")) {
93 int action = evt->getAction();
94 const char *label = evt->findParam("LABEL");
95 const char *state = evt->findParam("STATE");
96 // if no LABEL, use INTERFACE instead
98 label = evt->findParam("INTERFACE");
101 notifyInterfaceClassActivity(label, !strcmp("active", state));
104 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
105 /* It is not a VSYNC or a backlight event */
106 ALOGV("unexpected event from subsystem %s", subsys);
111 void NetlinkHandler::notify(int code, const char *format, ...) {
114 va_start(args, format);
115 if (vasprintf(&msg, format, args) >= 0) {
116 mNm->getBroadcaster()->sendBroadcast(code, msg, false);
119 SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
124 void NetlinkHandler::notifyInterfaceAdded(const char *name) {
125 notify(ResponseCode::InterfaceChange, "Iface added %s", name);
128 void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
129 notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
132 void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
133 notify(ResponseCode::InterfaceChange,
134 "Iface changed %s %s", name, (isUp ? "up" : "down"));
137 void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
138 notify(ResponseCode::InterfaceChange,
139 "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
142 void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
143 notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
146 void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
148 notify(ResponseCode::InterfaceClassActivity,
149 "IfaceClass %s %s", isActive ? "active" : "idle", name);
152 void NetlinkHandler::notifyAddressChanged(int action, const char *addr,
153 const char *iface, const char *flags,
155 notify(ResponseCode::InterfaceAddressChange,
156 "Address %s %s %s %s %s",
157 (action == NetlinkEvent::NlActionAddressUpdated) ?
158 "updated" : "removed", addr, iface, flags, scope);
161 void NetlinkHandler::notifyInterfaceDnsServers(const char *iface,
162 const char *lifetime,
163 const char *servers) {
164 notify(ResponseCode::InterfaceDnsInfo, "DnsInfo servers %s %s %s",
165 iface, lifetime, servers);