blob: 753b92b3f12aee53cb078684d10f8c8453d0e1e1 [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
17#include <stdio.h>
18#include <stdlib.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080019#include <string.h>
San Mehatd1830422010-01-15 08:02:39 -080020#include <errno.h>
21
22#define LOG_TAG "Netd"
23
24#include <cutils/log.h>
25
26#include <sysutils/NetlinkEvent.h>
27#include "NetlinkHandler.h"
Robert Greenwalt67c57532010-02-17 17:42:37 -080028#include "NetlinkManager.h"
29#include "ResponseCode.h"
San Mehatd1830422010-01-15 08:02:39 -080030
Mike J. Chen564df4e2011-06-23 15:07:35 -070031NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
32 int format) :
33 NetlinkListener(listenerSocket, format) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080034 mNm = nm;
San Mehatd1830422010-01-15 08:02:39 -080035}
36
37NetlinkHandler::~NetlinkHandler() {
38}
39
40int NetlinkHandler::start() {
41 return this->startListener();
42}
43
44int NetlinkHandler::stop() {
45 return this->stopListener();
46}
47
48void NetlinkHandler::onEvent(NetlinkEvent *evt) {
49 const char *subsys = evt->getSubsystem();
San Mehatd1830422010-01-15 08:02:39 -080050 if (!subsys) {
Steve Block0e76b762012-01-05 23:21:51 +000051 ALOGW("No subsystem found in netlink event");
San Mehatd1830422010-01-15 08:02:39 -080052 return;
53 }
Mike J. Chen564df4e2011-06-23 15:07:35 -070054
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +090055 if (!strcmp(subsys, "interface")) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080056 int action = evt->getAction();
Mike J. Chen564df4e2011-06-23 15:07:35 -070057 const char *iface = evt->findParam("INTERFACE");
58
Robert Greenwalt67c57532010-02-17 17:42:37 -080059 if (action == evt->NlActionAdd) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080060 notifyInterfaceAdded(iface);
61 } else if (action == evt->NlActionRemove) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080062 notifyInterfaceRemoved(iface);
63 } else if (action == evt->NlActionChange) {
64 evt->dump();
Robert Greenwalt67c57532010-02-17 17:42:37 -080065 notifyInterfaceChanged("nana", true);
Mike J. Chen564df4e2011-06-23 15:07:35 -070066 } else if (action == evt->NlActionLinkUp) {
67 notifyInterfaceLinkChanged(iface, true);
68 } else if (action == evt->NlActionLinkDown) {
69 notifyInterfaceLinkChanged(iface, false);
Robert Greenwalt67c57532010-02-17 17:42:37 -080070 }
JP Abgralle07effe2012-04-27 00:02:53 -070071
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +090072 } else if (!strcmp(subsys, "address")) {
73 const char *address = evt->findParam("ADDRESS");
74 const char *iface = evt->findParam("IFACE");
75 const char *flags = evt->findParam("FLAGS");
76 const char *scope = evt->findParam("SCOPE");
77
78 if (iface && flags && scope) {
79 notifyAddressChanged(evt->getAction(), address, iface, flags, scope);
80 }
81
JP Abgralle0ebc462011-07-21 17:21:49 -070082 } else if (!strcmp(subsys, "qlog")) {
83 const char *alertName = evt->findParam("ALERT_NAME");
84 const char *iface = evt->findParam("INTERFACE");
85 notifyQuotaLimitReached(alertName, iface);
JP Abgralle07effe2012-04-27 00:02:53 -070086
87 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharma6337b882012-04-10 19:47:09 -070088 int action = evt->getAction();
Haoyu Bai98f65d32012-06-28 16:16:51 -070089 const char *label = evt->findParam("LABEL");
JP Abgralle07effe2012-04-27 00:02:53 -070090 const char *state = evt->findParam("STATE");
Haoyu Bai98f65d32012-06-28 16:16:51 -070091 // if no LABEL, use INTERFACE instead
92 if (label == NULL) {
93 label = evt->findParam("INTERFACE");
94 }
JP Abgralle07effe2012-04-27 00:02:53 -070095 if (state)
Haoyu Bai98f65d32012-06-28 16:16:51 -070096 notifyInterfaceClassActivity(label, !strcmp("active", state));
JP Abgralle0ebc462011-07-21 17:21:49 -070097
JP Abgrall40baed82012-05-08 14:48:45 -070098#if !LOG_NDEBUG
99 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
100 /* It is not a VSYNC or a backlight event */
101 ALOGV("unexpected event from subsystem %s", subsys);
102#endif
Ashish Sharma6337b882012-04-10 19:47:09 -0700103 }
San Mehatd1830422010-01-15 08:02:39 -0800104}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800105
106void NetlinkHandler::notifyInterfaceAdded(const char *name) {
107 char msg[255];
108 snprintf(msg, sizeof(msg), "Iface added %s", name);
109
110 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
111 msg, false);
112}
113
114void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
115 char msg[255];
116 snprintf(msg, sizeof(msg), "Iface removed %s", name);
117
118 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
119 msg, false);
120}
121
122void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
123 char msg[255];
Mike J. Chen564df4e2011-06-23 15:07:35 -0700124 snprintf(msg, sizeof(msg), "Iface changed %s %s", name,
125 (isUp ? "up" : "down"));
126
127 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
128 msg, false);
129}
130
131void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
132 char msg[255];
Mike J. Chena583f8b2011-06-23 15:11:53 -0700133 snprintf(msg, sizeof(msg), "Iface linkstate %s %s", name,
Mike J. Chen564df4e2011-06-23 15:07:35 -0700134 (isUp ? "up" : "down"));
Robert Greenwalt67c57532010-02-17 17:42:37 -0800135
136 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
137 msg, false);
138}
JP Abgralle0ebc462011-07-21 17:21:49 -0700139
140void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
141 char msg[255];
142 snprintf(msg, sizeof(msg), "limit alert %s %s", name, iface);
143
144 mNm->getBroadcaster()->sendBroadcast(ResponseCode::BandwidthControl,
145 msg, false);
146}
Ashish Sharma6337b882012-04-10 19:47:09 -0700147
Haoyu Bai98f65d32012-06-28 16:16:51 -0700148void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
149 bool isActive) {
Ashish Sharma6337b882012-04-10 19:47:09 -0700150 char msg[255];
151
Haoyu Baidb28cbe2012-07-17 10:32:58 -0700152 snprintf(msg, sizeof(msg), "IfaceClass %s %s",
153 isActive ? "active" : "idle", name);
JP Abgralle07effe2012-04-27 00:02:53 -0700154 ALOGV("Broadcasting interface activity msg: %s", msg);
Haoyu Bai98f65d32012-06-28 16:16:51 -0700155 mNm->getBroadcaster()->sendBroadcast(
Haoyu Baidb28cbe2012-07-17 10:32:58 -0700156 ResponseCode::InterfaceClassActivity, msg, false);
Ashish Sharma6337b882012-04-10 19:47:09 -0700157}
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900158
159void NetlinkHandler::notifyAddressChanged(int action, const char *addr,
160 const char *iface, const char *flags,
161 const char *scope) {
162 char msg[255];
163 snprintf(msg, sizeof(msg), "Address %s %s %s %s %s",
164 (action == NetlinkEvent::NlActionAdd) ? "updated" : "removed",
165 addr, iface, flags, scope);
166
167 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceAddressChange,
168 msg, false);
169}