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.
22 #define LOG_TAG "Netd"
24 #include <cutils/log.h>
26 #include <sysutils/NetlinkEvent.h>
27 #include "NetlinkHandler.h"
28 #include "NetlinkManager.h"
29 #include "ResponseCode.h"
31 NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
33 NetlinkListener(listenerSocket, format) {
37 NetlinkHandler::~NetlinkHandler() {
40 int NetlinkHandler::start() {
41 return this->startListener();
44 int NetlinkHandler::stop() {
45 return this->stopListener();
48 void NetlinkHandler::onEvent(NetlinkEvent *evt) {
49 const char *subsys = evt->getSubsystem();
51 ALOGW("No subsystem found in netlink event");
55 if (!strcmp(subsys, "net")) {
56 int action = evt->getAction();
57 const char *iface = evt->findParam("INTERFACE");
59 if (action == evt->NlActionAdd) {
60 notifyInterfaceAdded(iface);
61 } else if (action == evt->NlActionRemove) {
62 notifyInterfaceRemoved(iface);
63 } else if (action == evt->NlActionChange) {
65 notifyInterfaceChanged("nana", true);
66 } else if (action == evt->NlActionLinkUp) {
67 notifyInterfaceLinkChanged(iface, true);
68 } else if (action == evt->NlActionLinkDown) {
69 notifyInterfaceLinkChanged(iface, false);
71 } else if (!strcmp(subsys, "qlog")) {
72 const char *alertName = evt->findParam("ALERT_NAME");
73 const char *iface = evt->findParam("INTERFACE");
74 notifyQuotaLimitReached(alertName, iface);
75 } else if (!strcmp(subsys, "idletimer")) {
76 int action = evt->getAction();
77 const char *iface = evt->findParam("INTERFACE");
79 if (action == evt->NlActionIfaceActive) {
80 notifyInterfaceActivity(iface, true);
81 } else if (action == evt->NlActionIfaceIdle) {
82 notifyInterfaceActivity(iface, false);
87 void NetlinkHandler::notifyInterfaceAdded(const char *name) {
89 snprintf(msg, sizeof(msg), "Iface added %s", name);
91 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
95 void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
97 snprintf(msg, sizeof(msg), "Iface removed %s", name);
99 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
103 void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
105 snprintf(msg, sizeof(msg), "Iface changed %s %s", name,
106 (isUp ? "up" : "down"));
108 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
112 void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
114 snprintf(msg, sizeof(msg), "Iface linkstate %s %s", name,
115 (isUp ? "up" : "down"));
117 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
121 void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
123 snprintf(msg, sizeof(msg), "limit alert %s %s", name, iface);
125 mNm->getBroadcaster()->sendBroadcast(ResponseCode::BandwidthControl,
129 void NetlinkHandler::notifyInterfaceActivity(const char *name, bool isActive) {
132 snprintf(msg, sizeof(msg), "Iface %s %s", isActive ? "active" : "idle", name);
134 mNm->getBroadcaster()->sendBroadcast(isActive ? ResponseCode::InterfaceActive
135 : ResponseCode::InterfaceIdle,