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 | |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 25 | #include <log/log.h> |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 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" |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 32 | #include "SockDiag.h" |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 33 | #include "Controllers.h" |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 34 | |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 35 | static const char *kUpdated = "updated"; |
| 36 | static const char *kRemoved = "removed"; |
| 37 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 38 | namespace android { |
| 39 | namespace net { |
| 40 | |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 41 | NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket, |
| 42 | int format) : |
| 43 | NetlinkListener(listenerSocket, format) { |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 44 | mNm = nm; |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | NetlinkHandler::~NetlinkHandler() { |
| 48 | } |
| 49 | |
| 50 | int NetlinkHandler::start() { |
| 51 | return this->startListener(); |
| 52 | } |
| 53 | |
| 54 | int NetlinkHandler::stop() { |
| 55 | return this->stopListener(); |
| 56 | } |
| 57 | |
| 58 | void NetlinkHandler::onEvent(NetlinkEvent *evt) { |
| 59 | const char *subsys = evt->getSubsystem(); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 60 | if (!subsys) { |
Steve Block | 0e76b76 | 2012-01-05 23:21:51 +0000 | [diff] [blame] | 61 | ALOGW("No subsystem found in netlink event"); |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 62 | return; |
| 63 | } |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 64 | |
Lorenzo Colitti | 4da12db | 2013-09-03 00:26:23 +0900 | [diff] [blame] | 65 | if (!strcmp(subsys, "net")) { |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 66 | NetlinkEvent::Action action = evt->getAction(); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 67 | const char *iface = evt->findParam("INTERFACE"); |
Chenbo Feng | 04f3b17 | 2018-03-20 16:39:43 -0700 | [diff] [blame] | 68 | if ((action == NetlinkEvent::Action::kAdd) || |
| 69 | (action == NetlinkEvent::Action::kLinkUp) || |
| 70 | (action == NetlinkEvent::Action::kLinkDown)) { |
| 71 | const char *ifIndex = evt->findParam("IFINDEX"); |
| 72 | if (ifIndex) { |
| 73 | // strtol returns 0 on error, which is fine because 0 is not a valid ifindex. |
| 74 | long ifaceIndex = strtol(ifIndex, NULL, 10); |
| 75 | if (ifaceIndex == 0 || |
| 76 | (errno == ERANGE && (ifaceIndex == LONG_MAX || ifaceIndex == LONG_MIN))) { |
| 77 | ALOGE("invalid interface index: %s(%s)", iface, ifIndex); |
| 78 | } else { |
| 79 | gCtls->trafficCtrl.addInterface(iface, ifaceIndex); |
| 80 | } |
| 81 | } |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 82 | } |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 83 | |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 84 | if (action == NetlinkEvent::Action::kAdd) { |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 85 | notifyInterfaceAdded(iface); |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 86 | } else if (action == NetlinkEvent::Action::kRemove) { |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 87 | notifyInterfaceRemoved(iface); |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 88 | } else if (action == NetlinkEvent::Action::kChange) { |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 89 | evt->dump(); |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 90 | notifyInterfaceChanged("nana", true); |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 91 | } else if (action == NetlinkEvent::Action::kLinkUp) { |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 92 | notifyInterfaceLinkChanged(iface, true); |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 93 | } else if (action == NetlinkEvent::Action::kLinkDown) { |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 94 | notifyInterfaceLinkChanged(iface, false); |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 95 | } else if (action == NetlinkEvent::Action::kAddressUpdated || |
| 96 | action == NetlinkEvent::Action::kAddressRemoved) { |
Lorenzo Colitti | 4da12db | 2013-09-03 00:26:23 +0900 | [diff] [blame] | 97 | const char *address = evt->findParam("ADDRESS"); |
| 98 | const char *flags = evt->findParam("FLAGS"); |
| 99 | const char *scope = evt->findParam("SCOPE"); |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 100 | if (action == NetlinkEvent::Action::kAddressRemoved && iface && address) { |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 101 | // Note: if this interface was deleted, iface is "" and we don't notify. |
| 102 | SockDiag sd; |
| 103 | if (sd.open()) { |
| 104 | char addrstr[INET6_ADDRSTRLEN]; |
| 105 | strncpy(addrstr, address, sizeof(addrstr)); |
| 106 | char *slash = strchr(addrstr, '/'); |
| 107 | if (slash) { |
| 108 | *slash = '\0'; |
| 109 | } |
| 110 | |
| 111 | int ret = sd.destroySockets(addrstr); |
| 112 | if (ret < 0) { |
| 113 | ALOGE("Error destroying sockets: %s", strerror(ret)); |
| 114 | } |
| 115 | } else { |
| 116 | ALOGE("Error opening NETLINK_SOCK_DIAG socket: %s", strerror(errno)); |
| 117 | } |
Sreeram Ramachandran | af37dd3 | 2014-09-08 16:03:18 -0700 | [diff] [blame] | 118 | } |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 119 | if (iface && iface[0] && address && flags && scope) { |
Lorenzo Colitti | 4da12db | 2013-09-03 00:26:23 +0900 | [diff] [blame] | 120 | notifyAddressChanged(action, address, iface, flags, scope); |
| 121 | } |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 122 | } else if (action == NetlinkEvent::Action::kRdnss) { |
Lorenzo Colitti | 12acae8 | 2013-10-24 14:51:57 +0900 | [diff] [blame] | 123 | const char *lifetime = evt->findParam("LIFETIME"); |
| 124 | const char *servers = evt->findParam("SERVERS"); |
| 125 | if (lifetime && servers) { |
| 126 | notifyInterfaceDnsServers(iface, lifetime, servers); |
| 127 | } |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 128 | } else if (action == NetlinkEvent::Action::kRouteUpdated || |
| 129 | action == NetlinkEvent::Action::kRouteRemoved) { |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 130 | const char *route = evt->findParam("ROUTE"); |
| 131 | const char *gateway = evt->findParam("GATEWAY"); |
| 132 | const char *iface = evt->findParam("INTERFACE"); |
| 133 | if (route && (gateway || iface)) { |
| 134 | notifyRouteChange(action, route, gateway, iface); |
| 135 | } |
Lorenzo Colitti | 9b3cd76 | 2013-08-02 05:57:47 +0900 | [diff] [blame] | 136 | } |
| 137 | |
Bryse Flowers | bc5f060 | 2016-06-01 13:00:12 -0700 | [diff] [blame] | 138 | } else if (!strcmp(subsys, "qlog") || !strcmp(subsys, "xt_quota2")) { |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 139 | const char *alertName = evt->findParam("ALERT_NAME"); |
| 140 | const char *iface = evt->findParam("INTERFACE"); |
| 141 | notifyQuotaLimitReached(alertName, iface); |
JP Abgrall | e07effe | 2012-04-27 00:02:53 -0700 | [diff] [blame] | 142 | |
Jeff Sharkey | fbe497f | 2014-10-28 16:50:07 -0700 | [diff] [blame] | 143 | } else if (!strcmp(subsys, "strict")) { |
| 144 | const char *uid = evt->findParam("UID"); |
| 145 | const char *hex = evt->findParam("HEX"); |
| 146 | notifyStrictCleartext(uid, hex); |
| 147 | |
JP Abgrall | e07effe | 2012-04-27 00:02:53 -0700 | [diff] [blame] | 148 | } else if (!strcmp(subsys, "xt_idletimer")) { |
Ashish Sharma | c79bcc4 | 2014-02-12 11:53:00 -0800 | [diff] [blame] | 149 | const char *label = evt->findParam("INTERFACE"); |
JP Abgrall | e07effe | 2012-04-27 00:02:53 -0700 | [diff] [blame] | 150 | const char *state = evt->findParam("STATE"); |
Ruchi Kandoi | 27ab7e1 | 2014-03-11 18:00:44 -0700 | [diff] [blame] | 151 | const char *timestamp = evt->findParam("TIME_NS"); |
Ruchi Kandoi | 05c39f0 | 2015-04-23 12:40:56 -0700 | [diff] [blame] | 152 | const char *uid = evt->findParam("UID"); |
JP Abgrall | e07effe | 2012-04-27 00:02:53 -0700 | [diff] [blame] | 153 | if (state) |
Ruchi Kandoi | 05c39f0 | 2015-04-23 12:40:56 -0700 | [diff] [blame] | 154 | notifyInterfaceClassActivity(label, !strcmp("active", state), |
| 155 | timestamp, uid); |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 156 | |
JP Abgrall | 40baed8 | 2012-05-08 14:48:45 -0700 | [diff] [blame] | 157 | #if !LOG_NDEBUG |
| 158 | } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) { |
| 159 | /* It is not a VSYNC or a backlight event */ |
| 160 | ALOGV("unexpected event from subsystem %s", subsys); |
| 161 | #endif |
Ashish Sharma | 6337b88 | 2012-04-10 19:47:09 -0700 | [diff] [blame] | 162 | } |
San Mehat | d183042 | 2010-01-15 08:02:39 -0800 | [diff] [blame] | 163 | } |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 164 | |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 165 | void NetlinkHandler::notify(int code, const char *format, ...) { |
| 166 | char *msg; |
| 167 | va_list args; |
| 168 | va_start(args, format); |
| 169 | if (vasprintf(&msg, format, args) >= 0) { |
| 170 | mNm->getBroadcaster()->sendBroadcast(code, msg, false); |
| 171 | free(msg); |
| 172 | } else { |
| 173 | SLOGE("Failed to send notification: vasprintf: %s", strerror(errno)); |
| 174 | } |
| 175 | va_end(args); |
| 176 | } |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 177 | |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 178 | void NetlinkHandler::notifyInterfaceAdded(const char *name) { |
| 179 | notify(ResponseCode::InterfaceChange, "Iface added %s", name); |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void NetlinkHandler::notifyInterfaceRemoved(const char *name) { |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 183 | notify(ResponseCode::InterfaceChange, "Iface removed %s", name); |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) { |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 187 | notify(ResponseCode::InterfaceChange, |
| 188 | "Iface changed %s %s", name, (isUp ? "up" : "down")); |
Mike J. Chen | 564df4e | 2011-06-23 15:07:35 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) { |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 192 | notify(ResponseCode::InterfaceChange, |
| 193 | "Iface linkstate %s %s", name, (isUp ? "up" : "down")); |
Robert Greenwalt | 67c5753 | 2010-02-17 17:42:37 -0800 | [diff] [blame] | 194 | } |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 195 | |
| 196 | void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) { |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 197 | notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface); |
JP Abgrall | e0ebc46 | 2011-07-21 17:21:49 -0700 | [diff] [blame] | 198 | } |
Ashish Sharma | 6337b88 | 2012-04-10 19:47:09 -0700 | [diff] [blame] | 199 | |
Haoyu Bai | 98f65d3 | 2012-06-28 16:16:51 -0700 | [diff] [blame] | 200 | void NetlinkHandler::notifyInterfaceClassActivity(const char *name, |
Ruchi Kandoi | 05c39f0 | 2015-04-23 12:40:56 -0700 | [diff] [blame] | 201 | bool isActive, |
| 202 | const char *timestamp, |
| 203 | const char *uid) { |
Ruchi Kandoi | 27ab7e1 | 2014-03-11 18:00:44 -0700 | [diff] [blame] | 204 | if (timestamp == NULL) |
| 205 | notify(ResponseCode::InterfaceClassActivity, |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 206 | "IfaceClass %s %s", isActive ? "active" : "idle", name); |
Ruchi Kandoi | 05c39f0 | 2015-04-23 12:40:56 -0700 | [diff] [blame] | 207 | else if (uid != NULL && isActive) |
| 208 | notify(ResponseCode::InterfaceClassActivity, |
| 209 | "IfaceClass active %s %s %s", name, timestamp, uid); |
Ruchi Kandoi | 27ab7e1 | 2014-03-11 18:00:44 -0700 | [diff] [blame] | 210 | else |
| 211 | notify(ResponseCode::InterfaceClassActivity, |
| 212 | "IfaceClass %s %s %s", isActive ? "active" : "idle", name, timestamp); |
Ashish Sharma | 6337b88 | 2012-04-10 19:47:09 -0700 | [diff] [blame] | 213 | } |
Lorenzo Colitti | 9b3cd76 | 2013-08-02 05:57:47 +0900 | [diff] [blame] | 214 | |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 215 | void NetlinkHandler::notifyAddressChanged(NetlinkEvent::Action action, const char *addr, |
Lorenzo Colitti | 9b3cd76 | 2013-08-02 05:57:47 +0900 | [diff] [blame] | 216 | const char *iface, const char *flags, |
| 217 | const char *scope) { |
Lorenzo Colitti | 0b454ea | 2013-10-25 19:53:31 +0900 | [diff] [blame] | 218 | notify(ResponseCode::InterfaceAddressChange, |
| 219 | "Address %s %s %s %s %s", |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 220 | (action == NetlinkEvent::Action::kAddressUpdated) ? kUpdated : kRemoved, |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 221 | addr, iface, flags, scope); |
Lorenzo Colitti | 9b3cd76 | 2013-08-02 05:57:47 +0900 | [diff] [blame] | 222 | } |
Lorenzo Colitti | 12acae8 | 2013-10-24 14:51:57 +0900 | [diff] [blame] | 223 | |
| 224 | void NetlinkHandler::notifyInterfaceDnsServers(const char *iface, |
| 225 | const char *lifetime, |
| 226 | const char *servers) { |
| 227 | notify(ResponseCode::InterfaceDnsInfo, "DnsInfo servers %s %s %s", |
| 228 | iface, lifetime, servers); |
| 229 | } |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 230 | |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 231 | void NetlinkHandler::notifyRouteChange(NetlinkEvent::Action action, const char *route, |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 232 | const char *gateway, const char *iface) { |
| 233 | notify(ResponseCode::RouteChange, |
| 234 | "Route %s %s%s%s%s%s", |
Jeff Sharkey | 32fa9ba | 2015-03-13 13:35:17 -0700 | [diff] [blame] | 235 | (action == NetlinkEvent::Action::kRouteUpdated) ? kUpdated : kRemoved, |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 236 | route, |
Manoj Gupta | 75c970f | 2016-11-22 21:15:59 -0800 | [diff] [blame] | 237 | (gateway && *gateway) ? " via " : "", |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 238 | gateway, |
Manoj Gupta | 75c970f | 2016-11-22 21:15:59 -0800 | [diff] [blame] | 239 | (iface && *iface) ? " dev " : "", |
Lorenzo Colitti | bd0f224 | 2014-06-12 13:51:05 +0900 | [diff] [blame] | 240 | iface); |
| 241 | } |
Jeff Sharkey | fbe497f | 2014-10-28 16:50:07 -0700 | [diff] [blame] | 242 | |
| 243 | void NetlinkHandler::notifyStrictCleartext(const char* uid, const char* hex) { |
| 244 | notify(ResponseCode::StrictCleartext, "%s %s", uid, hex); |
| 245 | } |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 246 | |
| 247 | } // namespace net |
| 248 | } // namespace android |