blob: d1dbf7d62db1f49fa68682ed5908636f8a936484 [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
27#include <sysutils/NetlinkEvent.h>
28#include "NetlinkHandler.h"
Robert Greenwalt67c57532010-02-17 17:42:37 -080029#include "NetlinkManager.h"
30#include "ResponseCode.h"
San Mehatd1830422010-01-15 08:02:39 -080031
Lorenzo Colittibd0f2242014-06-12 13:51:05 +090032static const char *kUpdated = "updated";
33static const char *kRemoved = "removed";
34
Mike J. Chen564df4e2011-06-23 15:07:35 -070035NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
36 int format) :
37 NetlinkListener(listenerSocket, format) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080038 mNm = nm;
San Mehatd1830422010-01-15 08:02:39 -080039}
40
41NetlinkHandler::~NetlinkHandler() {
42}
43
44int NetlinkHandler::start() {
45 return this->startListener();
46}
47
48int NetlinkHandler::stop() {
49 return this->stopListener();
50}
51
52void NetlinkHandler::onEvent(NetlinkEvent *evt) {
53 const char *subsys = evt->getSubsystem();
San Mehatd1830422010-01-15 08:02:39 -080054 if (!subsys) {
Steve Block0e76b762012-01-05 23:21:51 +000055 ALOGW("No subsystem found in netlink event");
San Mehatd1830422010-01-15 08:02:39 -080056 return;
57 }
Mike J. Chen564df4e2011-06-23 15:07:35 -070058
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090059 if (!strcmp(subsys, "net")) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080060 int action = evt->getAction();
Mike J. Chen564df4e2011-06-23 15:07:35 -070061 const char *iface = evt->findParam("INTERFACE");
62
Robert Greenwalt67c57532010-02-17 17:42:37 -080063 if (action == evt->NlActionAdd) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080064 notifyInterfaceAdded(iface);
65 } else if (action == evt->NlActionRemove) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080066 notifyInterfaceRemoved(iface);
67 } else if (action == evt->NlActionChange) {
68 evt->dump();
Robert Greenwalt67c57532010-02-17 17:42:37 -080069 notifyInterfaceChanged("nana", true);
Mike J. Chen564df4e2011-06-23 15:07:35 -070070 } else if (action == evt->NlActionLinkUp) {
71 notifyInterfaceLinkChanged(iface, true);
72 } else if (action == evt->NlActionLinkDown) {
73 notifyInterfaceLinkChanged(iface, false);
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090074 } else if (action == evt->NlActionAddressUpdated ||
75 action == evt->NlActionAddressRemoved) {
76 const char *address = evt->findParam("ADDRESS");
77 const char *flags = evt->findParam("FLAGS");
78 const char *scope = evt->findParam("SCOPE");
79 if (iface && flags && scope) {
80 notifyAddressChanged(action, address, iface, flags, scope);
81 }
Lorenzo Colitti12acae82013-10-24 14:51:57 +090082 } else if (action == evt->NlActionRdnss) {
83 const char *lifetime = evt->findParam("LIFETIME");
84 const char *servers = evt->findParam("SERVERS");
85 if (lifetime && servers) {
86 notifyInterfaceDnsServers(iface, lifetime, servers);
87 }
Lorenzo Colittibd0f2242014-06-12 13:51:05 +090088 } else if (action == evt->NlActionRouteUpdated ||
89 action == evt->NlActionRouteRemoved) {
90 const char *route = evt->findParam("ROUTE");
91 const char *gateway = evt->findParam("GATEWAY");
92 const char *iface = evt->findParam("INTERFACE");
93 if (route && (gateway || iface)) {
94 notifyRouteChange(action, route, gateway, iface);
95 }
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +090096 }
97
JP Abgralle0ebc462011-07-21 17:21:49 -070098 } else if (!strcmp(subsys, "qlog")) {
99 const char *alertName = evt->findParam("ALERT_NAME");
100 const char *iface = evt->findParam("INTERFACE");
101 notifyQuotaLimitReached(alertName, iface);
JP Abgralle07effe2012-04-27 00:02:53 -0700102
103 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharmac79bcc42014-02-12 11:53:00 -0800104 const char *label = evt->findParam("INTERFACE");
JP Abgralle07effe2012-04-27 00:02:53 -0700105 const char *state = evt->findParam("STATE");
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700106 const char *timestamp = evt->findParam("TIME_NS");
JP Abgralle07effe2012-04-27 00:02:53 -0700107 if (state)
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700108 notifyInterfaceClassActivity(label, !strcmp("active", state), timestamp);
JP Abgralle0ebc462011-07-21 17:21:49 -0700109
JP Abgrall40baed82012-05-08 14:48:45 -0700110#if !LOG_NDEBUG
111 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
112 /* It is not a VSYNC or a backlight event */
113 ALOGV("unexpected event from subsystem %s", subsys);
114#endif
Ashish Sharma6337b882012-04-10 19:47:09 -0700115 }
San Mehatd1830422010-01-15 08:02:39 -0800116}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800117
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900118void NetlinkHandler::notify(int code, const char *format, ...) {
119 char *msg;
120 va_list args;
121 va_start(args, format);
122 if (vasprintf(&msg, format, args) >= 0) {
123 mNm->getBroadcaster()->sendBroadcast(code, msg, false);
124 free(msg);
125 } else {
126 SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
127 }
128 va_end(args);
129}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800130
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900131void NetlinkHandler::notifyInterfaceAdded(const char *name) {
132 notify(ResponseCode::InterfaceChange, "Iface added %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800133}
134
135void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900136 notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800137}
138
139void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900140 notify(ResponseCode::InterfaceChange,
141 "Iface changed %s %s", name, (isUp ? "up" : "down"));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700142}
143
144void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900145 notify(ResponseCode::InterfaceChange,
146 "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
Robert Greenwalt67c57532010-02-17 17:42:37 -0800147}
JP Abgralle0ebc462011-07-21 17:21:49 -0700148
149void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900150 notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
JP Abgralle0ebc462011-07-21 17:21:49 -0700151}
Ashish Sharma6337b882012-04-10 19:47:09 -0700152
Haoyu Bai98f65d32012-06-28 16:16:51 -0700153void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700154 bool isActive, const char *timestamp) {
155 if (timestamp == NULL)
156 notify(ResponseCode::InterfaceClassActivity,
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900157 "IfaceClass %s %s", isActive ? "active" : "idle", name);
Ruchi Kandoi27ab7e12014-03-11 18:00:44 -0700158 else
159 notify(ResponseCode::InterfaceClassActivity,
160 "IfaceClass %s %s %s", isActive ? "active" : "idle", name, timestamp);
Ashish Sharma6337b882012-04-10 19:47:09 -0700161}
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900162
163void NetlinkHandler::notifyAddressChanged(int action, const char *addr,
164 const char *iface, const char *flags,
165 const char *scope) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900166 notify(ResponseCode::InterfaceAddressChange,
167 "Address %s %s %s %s %s",
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900168 (action == NetlinkEvent::NlActionAddressUpdated) ? kUpdated : kRemoved,
169 addr, iface, flags, scope);
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900170}
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900171
172void NetlinkHandler::notifyInterfaceDnsServers(const char *iface,
173 const char *lifetime,
174 const char *servers) {
175 notify(ResponseCode::InterfaceDnsInfo, "DnsInfo servers %s %s %s",
176 iface, lifetime, servers);
177}
Lorenzo Colittibd0f2242014-06-12 13:51:05 +0900178
179void NetlinkHandler::notifyRouteChange(int action, const char *route,
180 const char *gateway, const char *iface) {
181 notify(ResponseCode::RouteChange,
182 "Route %s %s%s%s%s%s",
183 (action == NetlinkEvent::NlActionRouteUpdated) ? kUpdated : kRemoved,
184 route,
185 *gateway ? " via " : "",
186 gateway,
187 *iface ? " dev " : "",
188 iface);
189}