blob: 5d8a8b57bfa6ffb33b732c7fb9e11b8a3ae7cd22 [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 Colitti4da12db2013-09-03 00:26:23 +090055 if (!strcmp(subsys, "net")) {
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);
Lorenzo Colitti4da12db2013-09-03 00:26:23 +090070 } 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 }
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +090078 }
79
JP Abgralle0ebc462011-07-21 17:21:49 -070080 } else if (!strcmp(subsys, "qlog")) {
81 const char *alertName = evt->findParam("ALERT_NAME");
82 const char *iface = evt->findParam("INTERFACE");
83 notifyQuotaLimitReached(alertName, iface);
JP Abgralle07effe2012-04-27 00:02:53 -070084
85 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharma6337b882012-04-10 19:47:09 -070086 int action = evt->getAction();
Haoyu Bai98f65d32012-06-28 16:16:51 -070087 const char *label = evt->findParam("LABEL");
JP Abgralle07effe2012-04-27 00:02:53 -070088 const char *state = evt->findParam("STATE");
Haoyu Bai98f65d32012-06-28 16:16:51 -070089 // if no LABEL, use INTERFACE instead
90 if (label == NULL) {
91 label = evt->findParam("INTERFACE");
92 }
JP Abgralle07effe2012-04-27 00:02:53 -070093 if (state)
Haoyu Bai98f65d32012-06-28 16:16:51 -070094 notifyInterfaceClassActivity(label, !strcmp("active", state));
JP Abgralle0ebc462011-07-21 17:21:49 -070095
JP Abgrall40baed82012-05-08 14:48:45 -070096#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
Ashish Sharma6337b882012-04-10 19:47:09 -0700101 }
San Mehatd1830422010-01-15 08:02:39 -0800102}
Robert Greenwalt67c57532010-02-17 17:42:37 -0800103
104void 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
112void 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
120void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
121 char msg[255];
Mike J. Chen564df4e2011-06-23 15:07:35 -0700122 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
129void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
130 char msg[255];
Mike J. Chena583f8b2011-06-23 15:11:53 -0700131 snprintf(msg, sizeof(msg), "Iface linkstate %s %s", name,
Mike J. Chen564df4e2011-06-23 15:07:35 -0700132 (isUp ? "up" : "down"));
Robert Greenwalt67c57532010-02-17 17:42:37 -0800133
134 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
135 msg, false);
136}
JP Abgralle0ebc462011-07-21 17:21:49 -0700137
138void 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}
Ashish Sharma6337b882012-04-10 19:47:09 -0700145
Haoyu Bai98f65d32012-06-28 16:16:51 -0700146void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
147 bool isActive) {
Ashish Sharma6337b882012-04-10 19:47:09 -0700148 char msg[255];
149
Haoyu Baidb28cbe2012-07-17 10:32:58 -0700150 snprintf(msg, sizeof(msg), "IfaceClass %s %s",
151 isActive ? "active" : "idle", name);
JP Abgralle07effe2012-04-27 00:02:53 -0700152 ALOGV("Broadcasting interface activity msg: %s", msg);
Haoyu Bai98f65d32012-06-28 16:16:51 -0700153 mNm->getBroadcaster()->sendBroadcast(
Haoyu Baidb28cbe2012-07-17 10:32:58 -0700154 ResponseCode::InterfaceClassActivity, msg, false);
Ashish Sharma6337b882012-04-10 19:47:09 -0700155}
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900156
157void 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",
Lorenzo Colitti4da12db2013-09-03 00:26:23 +0900162 (action == NetlinkEvent::NlActionAddressUpdated) ?
163 "updated" : "removed", addr, iface, flags, scope);
Lorenzo Colitti9b3cd762013-08-02 05:57:47 +0900164
165 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceAddressChange,
166 msg, false);
167}