1 /*
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>
19 #include <string.h>
20 #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"
28 #include "NetlinkManager.h"
29 #include "ResponseCode.h"
30
NetlinkHandler(NetlinkManager * nm,int listenerSocket,int format)31 NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
32 int format) :
33 NetlinkListener(listenerSocket, format) {
34 mNm = nm;
35 }
36
~NetlinkHandler()37 NetlinkHandler::~NetlinkHandler() {
38 }
39
start()40 int NetlinkHandler::start() {
41 return this->startListener();
42 }
43
stop()44 int NetlinkHandler::stop() {
45 return this->stopListener();
46 }
47
onEvent(NetlinkEvent * evt)48 void NetlinkHandler::onEvent(NetlinkEvent *evt) {
49 const char *subsys = evt->getSubsystem();
50 if (!subsys) {
51 ALOGW("No subsystem found in netlink event");
52 return;
53 }
54
55 if (!strcmp(subsys, "net")) {
56 int action = evt->getAction();
57 const char *iface = evt->findParam("INTERFACE");
58
59 if (action == evt->NlActionAdd) {
60 notifyInterfaceAdded(iface);
61 } else if (action == evt->NlActionRemove) {
62 notifyInterfaceRemoved(iface);
63 } else if (action == evt->NlActionChange) {
64 evt->dump();
65 notifyInterfaceChanged("nana", true);
66 } else if (action == evt->NlActionLinkUp) {
67 notifyInterfaceLinkChanged(iface, true);
68 } else if (action == evt->NlActionLinkDown) {
69 notifyInterfaceLinkChanged(iface, false);
70 } else if (action == evt->NlActionAddressUpdated ||
71 action == evt->NlActionAddressRemoved) {
72 const char *address = evt->findParam("ADDRESS");
73 const char *flags = evt->findParam("FLAGS");
74 const char *scope = evt->findParam("SCOPE");
75 if (iface && flags && scope) {
76 notifyAddressChanged(action, address, iface, flags, scope);
77 }
78 }
79
80 } else if (!strcmp(subsys, "qlog")) {
81 const char *alertName = evt->findParam("ALERT_NAME");
82 const char *iface = evt->findParam("INTERFACE");
83 notifyQuotaLimitReached(alertName, iface);
84
85 } else if (!strcmp(subsys, "xt_idletimer")) {
86 int action = evt->getAction();
87 const char *label = evt->findParam("LABEL");
88 const char *state = evt->findParam("STATE");
89 // if no LABEL, use INTERFACE instead
90 if (label == NULL) {
91 label = evt->findParam("INTERFACE");
92 }
93 if (state)
94 notifyInterfaceClassActivity(label, !strcmp("active", state));
95
96 #if !LOG_NDEBUG
97 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
98 /* It is not a VSYNC or a backlight event */
99 ALOGV("unexpected event from subsystem %s", subsys);
100 #endif
101 }
102 }
103
notifyInterfaceAdded(const char * name)104 void NetlinkHandler::notifyInterfaceAdded(const char *name) {
105 char msg[255];
106 snprintf(msg, sizeof(msg), "Iface added %s", name);
107
108 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
109 msg, false);
110 }
111
notifyInterfaceRemoved(const char * name)112 void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
113 char msg[255];
114 snprintf(msg, sizeof(msg), "Iface removed %s", name);
115
116 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
117 msg, false);
118 }
119
notifyInterfaceChanged(const char * name,bool isUp)120 void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
121 char msg[255];
122 snprintf(msg, sizeof(msg), "Iface changed %s %s", name,
123 (isUp ? "up" : "down"));
124
125 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
126 msg, false);
127 }
128
notifyInterfaceLinkChanged(const char * name,bool isUp)129 void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
130 char msg[255];
131 snprintf(msg, sizeof(msg), "Iface linkstate %s %s", name,
132 (isUp ? "up" : "down"));
133
134 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
135 msg, false);
136 }
137
notifyQuotaLimitReached(const char * name,const char * iface)138 void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
139 char msg[255];
140 snprintf(msg, sizeof(msg), "limit alert %s %s", name, iface);
141
142 mNm->getBroadcaster()->sendBroadcast(ResponseCode::BandwidthControl,
143 msg, false);
144 }
145
notifyInterfaceClassActivity(const char * name,bool isActive)146 void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
147 bool isActive) {
148 char msg[255];
149
150 snprintf(msg, sizeof(msg), "IfaceClass %s %s",
151 isActive ? "active" : "idle", name);
152 ALOGV("Broadcasting interface activity msg: %s", msg);
153 mNm->getBroadcaster()->sendBroadcast(
154 ResponseCode::InterfaceClassActivity, msg, false);
155 }
156
notifyAddressChanged(int action,const char * addr,const char * iface,const char * flags,const char * scope)157 void NetlinkHandler::notifyAddressChanged(int action, const char *addr,
158 const char *iface, const char *flags,
159 const char *scope) {
160 char msg[255];
161 snprintf(msg, sizeof(msg), "Address %s %s %s %s %s",
162 (action == NetlinkEvent::NlActionAddressUpdated) ?
163 "updated" : "removed", addr, iface, flags, scope);
164
165 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceAddressChange,
166 msg, false);
167 }
168