blob: 534f3861e3eb77e78bc62231fcdcb884adff3665 [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
Mike J. Chen564df4e2011-06-23 15:07:35 -070032NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
33 int format) :
34 NetlinkListener(listenerSocket, format) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080035 mNm = nm;
San Mehatd1830422010-01-15 08:02:39 -080036}
37
38NetlinkHandler::~NetlinkHandler() {
39}
40
41int NetlinkHandler::start() {
42 return this->startListener();
43}
44
45int NetlinkHandler::stop() {
46 return this->stopListener();
47}
48
49void NetlinkHandler::onEvent(NetlinkEvent *evt) {
50 const char *subsys = evt->getSubsystem();
San Mehatd1830422010-01-15 08:02:39 -080051 if (!subsys) {
Steve Block0e76b762012-01-05 23:21:51 +000052 ALOGW("No subsystem found in netlink event");
San Mehatd1830422010-01-15 08:02:39 -080053 return;
54 }
Mike J. Chen564df4e2011-06-23 15:07:35 -070055
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090056 if (!strcmp(subsys, "net")) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080057 int action = evt->getAction();
Mike J. Chen564df4e2011-06-23 15:07:35 -070058 const char *iface = evt->findParam("INTERFACE");
59
Robert Greenwalt67c57532010-02-17 17:42:37 -080060 if (action == evt->NlActionAdd) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080061 notifyInterfaceAdded(iface);
62 } else if (action == evt->NlActionRemove) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080063 notifyInterfaceRemoved(iface);
64 } else if (action == evt->NlActionChange) {
65 evt->dump();
Robert Greenwalt67c57532010-02-17 17:42:37 -080066 notifyInterfaceChanged("nana", true);
Mike J. Chen564df4e2011-06-23 15:07:35 -070067 } else if (action == evt->NlActionLinkUp) {
68 notifyInterfaceLinkChanged(iface, true);
69 } else if (action == evt->NlActionLinkDown) {
70 notifyInterfaceLinkChanged(iface, false);
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090071 } 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);
78 }
Lorenzo Colitti12acae82013-10-24 14:51:57 +090079 } 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);
84 }
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +090085 }
86
JP Abgralle0ebc462011-07-21 17:21:49 -070087 } else if (!strcmp(subsys, "qlog")) {
88 const char *alertName = evt->findParam("ALERT_NAME");
89 const char *iface = evt->findParam("INTERFACE");
90 notifyQuotaLimitReached(alertName, iface);
JP Abgralle07effe2012-04-27 00:02:53 -070091
92 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharma6337b882012-04-10 19:47:09 -070093 int action = evt->getAction();
Haoyu Bai98f65d32012-06-28 16:16:51 -070094 const char *label = evt->findParam("LABEL");
JP Abgralle07effe2012-04-27 00:02:53 -070095 const char *state = evt->findParam("STATE");
Haoyu Bai98f65d32012-06-28 16:16:51 -070096 // if no LABEL, use INTERFACE instead
97 if (label == NULL) {
98 label = evt->findParam("INTERFACE");
99 }
JP Abgralle07effe2012-04-27 00:02:53 -0700100 if (state)
Haoyu Bai98f65d32012-06-28 16:16:51 -0700101 notifyInterfaceClassActivity(label, !strcmp("active", state));
JP Abgralle0ebc462011-07-21 17:21:49 -0700102
JP Abgrall40baed82012-05-08 14:48:45 -0700103#if !LOG_NDEBUG
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);
107#endif
Ashish Sharma6337b882012-04-10 19:47:09 -0700108 }
San Mehatd1830422010-01-15 08:02:39 -0800109}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800110
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900111void NetlinkHandler::notify(int code, const char *format, ...) {
112 char *msg;
113 va_list args;
114 va_start(args, format);
115 if (vasprintf(&msg, format, args) >= 0) {
116 mNm->getBroadcaster()->sendBroadcast(code, msg, false);
117 free(msg);
118 } else {
119 SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
120 }
121 va_end(args);
122}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800123
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900124void NetlinkHandler::notifyInterfaceAdded(const char *name) {
125 notify(ResponseCode::InterfaceChange, "Iface added %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800126}
127
128void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900129 notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
Robert Greenwalt67c57532010-02-17 17:42:37 -0800130}
131
132void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900133 notify(ResponseCode::InterfaceChange,
134 "Iface changed %s %s", name, (isUp ? "up" : "down"));
Mike J. Chen564df4e2011-06-23 15:07:35 -0700135}
136
137void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900138 notify(ResponseCode::InterfaceChange,
139 "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
Robert Greenwalt67c57532010-02-17 17:42:37 -0800140}
JP Abgralle0ebc462011-07-21 17:21:49 -0700141
142void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900143 notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
JP Abgralle0ebc462011-07-21 17:21:49 -0700144}
Ashish Sharma6337b882012-04-10 19:47:09 -0700145
Haoyu Bai98f65d32012-06-28 16:16:51 -0700146void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
147 bool isActive) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900148 notify(ResponseCode::InterfaceClassActivity,
149 "IfaceClass %s %s", isActive ? "active" : "idle", name);
Ashish Sharma6337b882012-04-10 19:47:09 -0700150}
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900151
152void NetlinkHandler::notifyAddressChanged(int action, const char *addr,
153 const char *iface, const char *flags,
154 const char *scope) {
Lorenzo Colitti0b454ea2013-10-25 19:53:31 +0900155 notify(ResponseCode::InterfaceAddressChange,
156 "Address %s %s %s %s %s",
157 (action == NetlinkEvent::NlActionAddressUpdated) ?
158 "updated" : "removed", addr, iface, flags, scope);
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900159}
Lorenzo Colitti12acae82013-10-24 14:51:57 +0900160
161void 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);
166}