blob: a4f039cee74a2cf3b0d7815fa510cbeefc4f4f4a [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
Robert Greenwalt67c57532010-02-17 17:42:37 -080055 if (!strcmp(subsys, "net")) {
56 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);
Robert Greenwalt67c57532010-02-17 17:42:37 -080070 }
JP Abgralle0ebc462011-07-21 17:21:49 -070071 } else if (!strcmp(subsys, "qlog")) {
72 const char *alertName = evt->findParam("ALERT_NAME");
73 const char *iface = evt->findParam("INTERFACE");
74 notifyQuotaLimitReached(alertName, iface);
Ashish Sharma6337b882012-04-10 19:47:09 -070075 } else if (!strcmp(subsys, "idletimer")) {
76 int action = evt->getAction();
77 const char *iface = evt->findParam("INTERFACE");
JP Abgralle0ebc462011-07-21 17:21:49 -070078
Ashish Sharma6337b882012-04-10 19:47:09 -070079 if (action == evt->NlActionIfaceActive) {
80 notifyInterfaceActivity(iface, true);
81 } else if (action == evt->NlActionIfaceIdle) {
82 notifyInterfaceActivity(iface, false);
83 }
84 }
San Mehatd1830422010-01-15 08:02:39 -080085}
Robert Greenwalt67c57532010-02-17 17:42:37 -080086
87void NetlinkHandler::notifyInterfaceAdded(const char *name) {
88 char msg[255];
89 snprintf(msg, sizeof(msg), "Iface added %s", name);
90
91 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
92 msg, false);
93}
94
95void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
96 char msg[255];
97 snprintf(msg, sizeof(msg), "Iface removed %s", name);
98
99 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
100 msg, false);
101}
102
103void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
104 char msg[255];
Mike J. Chen564df4e2011-06-23 15:07:35 -0700105 snprintf(msg, sizeof(msg), "Iface changed %s %s", name,
106 (isUp ? "up" : "down"));
107
108 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
109 msg, false);
110}
111
112void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
113 char msg[255];
Mike J. Chena583f8b2011-06-23 15:11:53 -0700114 snprintf(msg, sizeof(msg), "Iface linkstate %s %s", name,
Mike J. Chen564df4e2011-06-23 15:07:35 -0700115 (isUp ? "up" : "down"));
Robert Greenwalt67c57532010-02-17 17:42:37 -0800116
117 mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
118 msg, false);
119}
JP Abgralle0ebc462011-07-21 17:21:49 -0700120
121void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
122 char msg[255];
123 snprintf(msg, sizeof(msg), "limit alert %s %s", name, iface);
124
125 mNm->getBroadcaster()->sendBroadcast(ResponseCode::BandwidthControl,
126 msg, false);
127}
Ashish Sharma6337b882012-04-10 19:47:09 -0700128
129void NetlinkHandler::notifyInterfaceActivity(const char *name, bool isActive) {
130 char msg[255];
131
132 snprintf(msg, sizeof(msg), "Iface %s %s", isActive ? "active" : "idle", name);
133
134 mNm->getBroadcaster()->sendBroadcast(isActive ? ResponseCode::InterfaceActive
135 : ResponseCode::InterfaceIdle,
136 msg, false);
137}