San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 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 | |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 17 | #include <stdarg.h> |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Olivier Bailly | ff2c0d8 | 2010-11-17 11:45:07 -0800 | [diff] [blame] | 20 | #include <string.h> |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | |
| 23 | #define LOG_TAG "Netd" |
| 24 | |
| 25 | #include <cutils/log.h> |
| 26 | |
Sreeram Ramachandran | af37dd3 | 2014-09-08 16:03:18 -0700 | [diff] [blame^] | 27 | #include <netutils/ifc.h> |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 28 | #include <sysutils/NetlinkEvent.h> |
| 29 | #include "NetlinkHandler.h" |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 30 | #include "NetlinkManager.h" |
| 31 | #include "ResponseCode.h" |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 32 | |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 33 | static const char *kUpdated = "updated"; |
| 34 | static const char *kRemoved = "removed"; |
| 35 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 36 | NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket, |
| 37 | int format) : |
| 38 | NetlinkListener(listenerSocket, format) { |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 39 | mNm = nm; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | NetlinkHandler::~NetlinkHandler() { |
| 43 | } |
| 44 | |
| 45 | int NetlinkHandler::start() { |
| 46 | return this->startListener(); |
| 47 | } |
| 48 | |
| 49 | int NetlinkHandler::stop() { |
| 50 | return this->stopListener(); |
| 51 | } |
| 52 | |
| 53 | void NetlinkHandler::onEvent(NetlinkEvent *evt) { |
| 54 | const char *subsys = evt->getSubsystem(); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 55 | if (!subsys) { |
Steve Block | 0e76b76 | 2012-01-05 23:21:51 +0000 | [diff] [blame] | 56 | ALOGW("No subsystem found in netlink event"); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 57 | return; |
| 58 | } |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 59 | |
Lorenzo Colitti | 4da12db | 2013-09-03 00:26:23 +0900 | [diff] [blame] | 60 | if (!strcmp(subsys, "net")) { |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 61 | int action = evt->getAction(); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 62 | const char *iface = evt->findParam("INTERFACE"); |
| 63 | |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 64 | if (action == evt->NlActionAdd) { |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 65 | notifyInterfaceAdded(iface); |
| 66 | } else if (action == evt->NlActionRemove) { |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 67 | notifyInterfaceRemoved(iface); |
| 68 | } else if (action == evt->NlActionChange) { |
| 69 | evt->dump(); |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 70 | notifyInterfaceChanged("nana", true); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 71 | } else if (action == evt->NlActionLinkUp) { |
| 72 | notifyInterfaceLinkChanged(iface, true); |
| 73 | } else if (action == evt->NlActionLinkDown) { |
| 74 | notifyInterfaceLinkChanged(iface, false); |
Lorenzo Colitti | 4da12db | 2013-09-03 00:26:23 +0900 | [diff] [blame] | 75 | } else if (action == evt->NlActionAddressUpdated || |
| 76 | action == evt->NlActionAddressRemoved) { |
| 77 | const char *address = evt->findParam("ADDRESS"); |
| 78 | const char *flags = evt->findParam("FLAGS"); |
| 79 | const char *scope = evt->findParam("SCOPE"); |
Sreeram Ramachandran | af37dd3 | 2014-09-08 16:03:18 -0700 | [diff] [blame^] | 80 | if (action == evt->NlActionAddressRemoved && iface && address) { |
| 81 | int resetMask = strchr(address, ':') ? RESET_IPV6_ADDRESSES : RESET_IPV4_ADDRESSES; |
| 82 | resetMask |= RESET_IGNORE_INTERFACE_ADDRESS; |
| 83 | if (int ret = ifc_reset_connections(iface, resetMask)) { |
| 84 | ALOGE("ifc_reset_connections failed on iface %s for address %s (%s)", iface, |
| 85 | address, strerror(ret)); |
| 86 | } |
| 87 | } |
Lorenzo Colitti | 4da12db | 2013-09-03 00:26:23 +0900 | [diff] [blame] | 88 | if (iface && flags && scope) { |
| 89 | notifyAddressChanged(action, address, iface, flags, scope); |
| 90 | } |
Lorenzo Colitti | 12acae8 | 2013-10-24 14:51:57 +0900 | [diff] [blame] | 91 | } else if (action == evt->NlActionRdnss) { |
| 92 | const char *lifetime = evt->findParam("LIFETIME"); |
| 93 | const char *servers = evt->findParam("SERVERS"); |
| 94 | if (lifetime && servers) { |
| 95 | notifyInterfaceDnsServers(iface, lifetime, servers); |
| 96 | } |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 97 | } else if (action == evt->NlActionRouteUpdated || |
| 98 | action == evt->NlActionRouteRemoved) { |
| 99 | const char *route = evt->findParam("ROUTE"); |
| 100 | const char *gateway = evt->findParam("GATEWAY"); |
| 101 | const char *iface = evt->findParam("INTERFACE"); |
| 102 | if (route && (gateway || iface)) { |
| 103 | notifyRouteChange(action, route, gateway, iface); |
| 104 | } |
Lorenzo Colitti | 9b3cd76 | 2013-08-02 05:57:47 +0900 | [diff] [blame] | 105 | } |
| 106 | |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 107 | } else if (!strcmp(subsys, "qlog")) { |
| 108 | const char *alertName = evt->findParam("ALERT_NAME"); |
| 109 | const char *iface = evt->findParam("INTERFACE"); |
| 110 | notifyQuotaLimitReached(alertName, iface); |
JP Abgrall | e07effe | 2012-04-27 00:02:53 -0700 | [diff] [blame] | 111 | |
| 112 | } else if (!strcmp(subsys, "xt_idletimer")) { |
Ashish Sharma | c79bcc4 | 2014-02-12 11:53:00 -0800 | [diff] [blame] | 113 | const char *label = evt->findParam("INTERFACE"); |
JP Abgrall | e07effe | 2012-04-27 00:02:53 -0700 | [diff] [blame] | 114 | const char *state = evt->findParam("STATE"); |
Ruchi Kandoi | 27ab7e1 | 2014-03-11 18:00:44 -0700 | [diff] [blame] | 115 | const char *timestamp = evt->findParam("TIME_NS"); |
JP Abgrall | e07effe | 2012-04-27 00:02:53 -0700 | [diff] [blame] | 116 | if (state) |
Ruchi Kandoi | 27ab7e1 | 2014-03-11 18:00:44 -0700 | [diff] [blame] | 117 | notifyInterfaceClassActivity(label, !strcmp("active", state), timestamp); |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 118 | |
JP Abgrall | 40baed8 | 2012-05-08 14:48:45 -0700 | [diff] [blame] | 119 | #if !LOG_NDEBUG |
| 120 | } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) { |
| 121 | /* It is not a VSYNC or a backlight event */ |
| 122 | ALOGV("unexpected event from subsystem %s", subsys); |
| 123 | #endif |
Ashish Sharma | 6337b88 | 2012-04-10 19:47:09 -0700 | [diff] [blame] | 124 | } |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 125 | } |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 126 | |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 127 | void NetlinkHandler::notify(int code, const char *format, ...) { |
| 128 | char *msg; |
| 129 | va_list args; |
| 130 | va_start(args, format); |
| 131 | if (vasprintf(&msg, format, args) >= 0) { |
| 132 | mNm->getBroadcaster()->sendBroadcast(code, msg, false); |
| 133 | free(msg); |
| 134 | } else { |
| 135 | SLOGE("Failed to send notification: vasprintf: %s", strerror(errno)); |
| 136 | } |
| 137 | va_end(args); |
| 138 | } |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 139 | |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 140 | void NetlinkHandler::notifyInterfaceAdded(const char *name) { |
| 141 | notify(ResponseCode::InterfaceChange, "Iface added %s", name); |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void NetlinkHandler::notifyInterfaceRemoved(const char *name) { |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 145 | notify(ResponseCode::InterfaceChange, "Iface removed %s", name); |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) { |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 149 | notify(ResponseCode::InterfaceChange, |
| 150 | "Iface changed %s %s", name, (isUp ? "up" : "down")); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) { |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 154 | notify(ResponseCode::InterfaceChange, |
| 155 | "Iface linkstate %s %s", name, (isUp ? "up" : "down")); |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 156 | } |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 157 | |
| 158 | void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) { |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 159 | notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface); |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 160 | } |
Ashish Sharma | 6337b88 | 2012-04-10 19:47:09 -0700 | [diff] [blame] | 161 | |
Haoyu Bai | 98f65d3 | 2012-06-28 16:16:51 -0700 | [diff] [blame] | 162 | void NetlinkHandler::notifyInterfaceClassActivity(const char *name, |
Ruchi Kandoi | 27ab7e1 | 2014-03-11 18:00:44 -0700 | [diff] [blame] | 163 | bool isActive, const char *timestamp) { |
| 164 | if (timestamp == NULL) |
| 165 | notify(ResponseCode::InterfaceClassActivity, |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 166 | "IfaceClass %s %s", isActive ? "active" : "idle", name); |
Ruchi Kandoi | 27ab7e1 | 2014-03-11 18:00:44 -0700 | [diff] [blame] | 167 | else |
| 168 | notify(ResponseCode::InterfaceClassActivity, |
| 169 | "IfaceClass %s %s %s", isActive ? "active" : "idle", name, timestamp); |
Ashish Sharma | 6337b88 | 2012-04-10 19:47:09 -0700 | [diff] [blame] | 170 | } |
Lorenzo Colitti | 9b3cd76 | 2013-08-02 05:57:47 +0900 | [diff] [blame] | 171 | |
| 172 | void NetlinkHandler::notifyAddressChanged(int action, const char *addr, |
| 173 | const char *iface, const char *flags, |
| 174 | const char *scope) { |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 175 | notify(ResponseCode::InterfaceAddressChange, |
| 176 | "Address %s %s %s %s %s", |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 177 | (action == NetlinkEvent::NlActionAddressUpdated) ? kUpdated : kRemoved, |
| 178 | addr, iface, flags, scope); |
Lorenzo Colitti | 9b3cd76 | 2013-08-02 05:57:47 +0900 | [diff] [blame] | 179 | } |
Lorenzo Colitti | 12acae8 | 2013-10-24 14:51:57 +0900 | [diff] [blame] | 180 | |
| 181 | void NetlinkHandler::notifyInterfaceDnsServers(const char *iface, |
| 182 | const char *lifetime, |
| 183 | const char *servers) { |
| 184 | notify(ResponseCode::InterfaceDnsInfo, "DnsInfo servers %s %s %s", |
| 185 | iface, lifetime, servers); |
| 186 | } |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 187 | |
| 188 | void NetlinkHandler::notifyRouteChange(int action, const char *route, |
| 189 | const char *gateway, const char *iface) { |
| 190 | notify(ResponseCode::RouteChange, |
| 191 | "Route %s %s%s%s%s%s", |
| 192 | (action == NetlinkEvent::NlActionRouteUpdated) ? kUpdated : kRemoved, |
| 193 | route, |
| 194 | *gateway ? " via " : "", |
| 195 | gateway, |
| 196 | *iface ? " dev " : "", |
| 197 | iface); |
| 198 | } |