blob: 9a0a844cf32049e9fd85cf9c2594c87bde611f3d [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
JP Abgralle07effe2012-04-27 00:02:53 -070055 ALOGV("subsystem %s", subsys);
56
Robert Greenwalt67c57532010-02-17 17:42:37 -080057 if (!strcmp(subsys, "net")) {
58 int action = evt->getAction();
Mike J. Chen564df4e2011-06-23 15:07:35 -070059 const char *iface = evt->findParam("INTERFACE");
60
Robert Greenwalt67c57532010-02-17 17:42:37 -080061 if (action == evt->NlActionAdd) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080062 notifyInterfaceAdded(iface);
63 } else if (action == evt->NlActionRemove) {
Robert Greenwalt67c57532010-02-17 17:42:37 -080064 notifyInterfaceRemoved(iface);
65 } else if (action == evt->NlActionChange) {
66 evt->dump();
Robert Greenwalt67c57532010-02-17 17:42:37 -080067 notifyInterfaceChanged("nana", true);
Mike J. Chen564df4e2011-06-23 15:07:35 -070068 } else if (action == evt->NlActionLinkUp) {
69 notifyInterfaceLinkChanged(iface, true);
70 } else if (action == evt->NlActionLinkDown) {
71 notifyInterfaceLinkChanged(iface, false);
Robert Greenwalt67c57532010-02-17 17:42:37 -080072 }
JP Abgralle07effe2012-04-27 00:02:53 -070073
JP Abgralle0ebc462011-07-21 17:21:49 -070074 } else if (!strcmp(subsys, "qlog")) {
75 const char *alertName = evt->findParam("ALERT_NAME");
76 const char *iface = evt->findParam("INTERFACE");
77 notifyQuotaLimitReached(alertName, iface);
JP Abgralle07effe2012-04-27 00:02:53 -070078
79 } else if (!strcmp(subsys, "xt_idletimer")) {
Ashish Sharma6337b882012-04-10 19:47:09 -070080 int action = evt->getAction();
81 const char *iface = evt->findParam("INTERFACE");
JP Abgralle07effe2012-04-27 00:02:53 -070082 const char *state = evt->findParam("STATE");
83 if (state)
84 notifyInterfaceActivity(iface, !strcmp("active", state));
JP Abgralle0ebc462011-07-21 17:21:49 -070085
Ashish Sharma6337b882012-04-10 19:47:09 -070086 }
San Mehatd1830422010-01-15 08:02:39 -080087}
Robert Greenwalt67c57532010-02-17 17:42:37 -080088
89void NetlinkHandler::notifyInterfaceAdded(const char *name) {
90 char msg[255];
91 snprintf(msg, sizeof(msg), "Iface added %s", name);
92
93 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
94 msg, false);
95}
96
97void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
98 char msg[255];
99 snprintf(msg, sizeof(msg), "Iface removed %s", name);
100
101 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
102 msg, false);
103}
104
105void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
106 char msg[255];
Mike J. Chen564df4e2011-06-23 15:07:35 -0700107 snprintf(msg, sizeof(msg), "Iface changed %s %s", name,
108 (isUp ? "up" : "down"));
109
110 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
111 msg, false);
112}
113
114void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
115 char msg[255];
Mike J. Chena583f8b2011-06-23 15:11:53 -0700116 snprintf(msg, sizeof(msg), "Iface linkstate %s %s", name,
Mike J. Chen564df4e2011-06-23 15:07:35 -0700117 (isUp ? "up" : "down"));
Robert Greenwalt67c57532010-02-17 17:42:37 -0800118
119 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
120 msg, false);
121}
JP Abgralle0ebc462011-07-21 17:21:49 -0700122
123void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
124 char msg[255];
125 snprintf(msg, sizeof(msg), "limit alert %s %s", name, iface);
126
127 mNm->getBroadcaster()->sendBroadcast(ResponseCode::BandwidthControl,
128 msg, false);
129}
Ashish Sharma6337b882012-04-10 19:47:09 -0700130
131void NetlinkHandler::notifyInterfaceActivity(const char *name, bool isActive) {
132 char msg[255];
133
JP Abgralle07effe2012-04-27 00:02:53 -0700134 snprintf(msg, sizeof(msg), "Iface %s %s", name, isActive ? "active" : "idle");
135 ALOGV("Broadcasting interface activity msg: %s", msg);
Ashish Sharma6337b882012-04-10 19:47:09 -0700136 mNm->getBroadcaster()->sendBroadcast(isActive ? ResponseCode::InterfaceActive
137 : ResponseCode::InterfaceIdle,
138 msg, false);
139}