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 31 NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket, 32 int format) : 33 NetlinkListener(listenerSocket, format) { 34 mNm = nm; 35 } 36 37 NetlinkHandler::~NetlinkHandler() { 38 } 39 40 int NetlinkHandler::start() { 41 return this->startListener(); 42 } 43 44 int NetlinkHandler::stop() { 45 return this->stopListener(); 46 } 47 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 } 71 72 } else if (!strcmp(subsys, "qlog")) { 73 const char *alertName = evt->findParam("ALERT_NAME"); 74 const char *iface = evt->findParam("INTERFACE"); 75 notifyQuotaLimitReached(alertName, iface); 76 77 } else if (!strcmp(subsys, "xt_idletimer")) { 78 int action = evt->getAction(); 79 const char *iface = evt->findParam("INTERFACE"); 80 const char *state = evt->findParam("STATE"); 81 if (state) 82 notifyInterfaceActivity(iface, !strcmp("active", state)); 83 84 #if !LOG_NDEBUG 85 } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) { 86 /* It is not a VSYNC or a backlight event */ 87 ALOGV("unexpected event from subsystem %s", subsys); 88 #endif 89 } 90 } 91 92 void NetlinkHandler::notifyInterfaceAdded(const char *name) { 93 char msg[255]; 94 snprintf(msg, sizeof(msg), "Iface added %s", name); 95 96 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange, 97 msg, false); 98 } 99 100 void NetlinkHandler::notifyInterfaceRemoved(const char *name) { 101 char msg[255]; 102 snprintf(msg, sizeof(msg), "Iface removed %s", name); 103 104 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange, 105 msg, false); 106 } 107 108 void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) { 109 char msg[255]; 110 snprintf(msg, sizeof(msg), "Iface changed %s %s", name, 111 (isUp ? "up" : "down")); 112 113 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange, 114 msg, false); 115 } 116 117 void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) { 118 char msg[255]; 119 snprintf(msg, sizeof(msg), "Iface linkstate %s %s", name, 120 (isUp ? "up" : "down")); 121 122 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange, 123 msg, false); 124 } 125 126 void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) { 127 char msg[255]; 128 snprintf(msg, sizeof(msg), "limit alert %s %s", name, iface); 129 130 mNm->getBroadcaster()->sendBroadcast(ResponseCode::BandwidthControl, 131 msg, false); 132 } 133 134 void NetlinkHandler::notifyInterfaceActivity(const char *name, bool isActive) { 135 char msg[255]; 136 137 snprintf(msg, sizeof(msg), "Iface %s %s", name, isActive ? "active" : "idle"); 138 ALOGV("Broadcasting interface activity msg: %s", msg); 139 mNm->getBroadcaster()->sendBroadcast(isActive ? ResponseCode::InterfaceActive 140 : ResponseCode::InterfaceIdle, 141 msg, false); 142 } 143