OSDN Git Service

Remove the ability to enable/disable the happy box.
[android-x86/system-netd.git] / server / NetlinkHandler.cpp
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
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22
23 #define LOG_TAG "Netd"
24
25 #include <cutils/log.h>
26
27 #include <netutils/ifc.h>
28 #include <sysutils/NetlinkEvent.h>
29 #include "NetlinkHandler.h"
30 #include "NetlinkManager.h"
31 #include "ResponseCode.h"
32 #include "SockDiag.h"
33
34 static const char *kUpdated = "updated";
35 static const char *kRemoved = "removed";
36
37 NetlinkHandler::NetlinkHandler(NetlinkManager *nm, int listenerSocket,
38                                int format) :
39                         NetlinkListener(listenerSocket, format) {
40     mNm = nm;
41 }
42
43 NetlinkHandler::~NetlinkHandler() {
44 }
45
46 int NetlinkHandler::start() {
47     return this->startListener();
48 }
49
50 int NetlinkHandler::stop() {
51     return this->stopListener();
52 }
53
54 void NetlinkHandler::onEvent(NetlinkEvent *evt) {
55     const char *subsys = evt->getSubsystem();
56     if (!subsys) {
57         ALOGW("No subsystem found in netlink event");
58         return;
59     }
60
61     if (!strcmp(subsys, "net")) {
62         NetlinkEvent::Action action = evt->getAction();
63         const char *iface = evt->findParam("INTERFACE");
64
65         if (action == NetlinkEvent::Action::kAdd) {
66             notifyInterfaceAdded(iface);
67         } else if (action == NetlinkEvent::Action::kRemove) {
68             notifyInterfaceRemoved(iface);
69         } else if (action == NetlinkEvent::Action::kChange) {
70             evt->dump();
71             notifyInterfaceChanged("nana", true);
72         } else if (action == NetlinkEvent::Action::kLinkUp) {
73             notifyInterfaceLinkChanged(iface, true);
74         } else if (action == NetlinkEvent::Action::kLinkDown) {
75             notifyInterfaceLinkChanged(iface, false);
76         } else if (action == NetlinkEvent::Action::kAddressUpdated ||
77                    action == NetlinkEvent::Action::kAddressRemoved) {
78             const char *address = evt->findParam("ADDRESS");
79             const char *flags = evt->findParam("FLAGS");
80             const char *scope = evt->findParam("SCOPE");
81             if (action == NetlinkEvent::Action::kAddressRemoved && iface && address) {
82                 // Note: if this interface was deleted, iface is "" and we don't notify.
83                 SockDiag sd;
84                 if (sd.open()) {
85                     char addrstr[INET6_ADDRSTRLEN];
86                     strncpy(addrstr, address, sizeof(addrstr));
87                     char *slash = strchr(addrstr, '/');
88                     if (slash) {
89                         *slash = '\0';
90                     }
91
92                     int ret = sd.destroySockets(addrstr);
93                     if (ret < 0) {
94                         ALOGE("Error destroying sockets: %s", strerror(ret));
95                     }
96                 } else {
97                     ALOGE("Error opening NETLINK_SOCK_DIAG socket: %s", strerror(errno));
98                 }
99
100                 // TODO: delete this once SOCK_DESTROY works everywhere.
101                 if (iface[0]) {
102                     int resetMask = strchr(address, ':') ?
103                             RESET_IPV6_ADDRESSES : RESET_IPV4_ADDRESSES;
104                     resetMask |= RESET_IGNORE_INTERFACE_ADDRESS;
105                     if (int ret = ifc_reset_connections(iface, resetMask)) {
106                         ALOGE("ifc_reset_connections failed on iface %s for address %s (%s)", iface,
107                               address, strerror(ret));
108                     }
109                 }
110             }
111             if (iface && iface[0] && address && flags && scope) {
112                 notifyAddressChanged(action, address, iface, flags, scope);
113             }
114         } else if (action == NetlinkEvent::Action::kRdnss) {
115             const char *lifetime = evt->findParam("LIFETIME");
116             const char *servers = evt->findParam("SERVERS");
117             if (lifetime && servers) {
118                 notifyInterfaceDnsServers(iface, lifetime, servers);
119             }
120         } else if (action == NetlinkEvent::Action::kRouteUpdated ||
121                    action == NetlinkEvent::Action::kRouteRemoved) {
122             const char *route = evt->findParam("ROUTE");
123             const char *gateway = evt->findParam("GATEWAY");
124             const char *iface = evt->findParam("INTERFACE");
125             if (route && (gateway || iface)) {
126                 notifyRouteChange(action, route, gateway, iface);
127             }
128         }
129
130     } else if (!strcmp(subsys, "qlog")) {
131         const char *alertName = evt->findParam("ALERT_NAME");
132         const char *iface = evt->findParam("INTERFACE");
133         notifyQuotaLimitReached(alertName, iface);
134
135     } else if (!strcmp(subsys, "strict")) {
136         const char *uid = evt->findParam("UID");
137         const char *hex = evt->findParam("HEX");
138         notifyStrictCleartext(uid, hex);
139
140     } else if (!strcmp(subsys, "xt_idletimer")) {
141         const char *label = evt->findParam("INTERFACE");
142         const char *state = evt->findParam("STATE");
143         const char *timestamp = evt->findParam("TIME_NS");
144         const char *uid = evt->findParam("UID");
145         if (state)
146             notifyInterfaceClassActivity(label, !strcmp("active", state),
147                                          timestamp, uid);
148
149 #if !LOG_NDEBUG
150     } else if (strcmp(subsys, "platform") && strcmp(subsys, "backlight")) {
151         /* It is not a VSYNC or a backlight event */
152         ALOGV("unexpected event from subsystem %s", subsys);
153 #endif
154     }
155 }
156
157 void NetlinkHandler::notify(int code, const char *format, ...) {
158     char *msg;
159     va_list args;
160     va_start(args, format);
161     if (vasprintf(&msg, format, args) >= 0) {
162         mNm->getBroadcaster()->sendBroadcast(code, msg, false);
163         free(msg);
164     } else {
165         SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
166     }
167     va_end(args);
168 }
169
170 void NetlinkHandler::notifyInterfaceAdded(const char *name) {
171     notify(ResponseCode::InterfaceChange, "Iface added %s", name);
172 }
173
174 void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
175     notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
176 }
177
178 void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
179     notify(ResponseCode::InterfaceChange,
180            "Iface changed %s %s", name, (isUp ? "up" : "down"));
181 }
182
183 void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
184     notify(ResponseCode::InterfaceChange,
185            "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
186 }
187
188 void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
189     notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
190 }
191
192 void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
193                                                   bool isActive,
194                                                   const char *timestamp,
195                                                   const char *uid) {
196     if (timestamp == NULL)
197         notify(ResponseCode::InterfaceClassActivity,
198            "IfaceClass %s %s", isActive ? "active" : "idle", name);
199     else if (uid != NULL && isActive)
200         notify(ResponseCode::InterfaceClassActivity,
201            "IfaceClass active %s %s %s", name, timestamp, uid);
202     else
203         notify(ResponseCode::InterfaceClassActivity,
204            "IfaceClass %s %s %s", isActive ? "active" : "idle", name, timestamp);
205 }
206
207 void NetlinkHandler::notifyAddressChanged(NetlinkEvent::Action action, const char *addr,
208                                           const char *iface, const char *flags,
209                                           const char *scope) {
210     notify(ResponseCode::InterfaceAddressChange,
211            "Address %s %s %s %s %s",
212            (action == NetlinkEvent::Action::kAddressUpdated) ? kUpdated : kRemoved,
213            addr, iface, flags, scope);
214 }
215
216 void NetlinkHandler::notifyInterfaceDnsServers(const char *iface,
217                                                const char *lifetime,
218                                                const char *servers) {
219     notify(ResponseCode::InterfaceDnsInfo, "DnsInfo servers %s %s %s",
220            iface, lifetime, servers);
221 }
222
223 void NetlinkHandler::notifyRouteChange(NetlinkEvent::Action action, const char *route,
224                                        const char *gateway, const char *iface) {
225     notify(ResponseCode::RouteChange,
226            "Route %s %s%s%s%s%s",
227            (action == NetlinkEvent::Action::kRouteUpdated) ? kUpdated : kRemoved,
228            route,
229            *gateway ? " via " : "",
230            gateway,
231            *iface ? " dev " : "",
232            iface);
233 }
234
235 void NetlinkHandler::notifyStrictCleartext(const char* uid, const char* hex) {
236     notify(ResponseCode::StrictCleartext, "%s %s", uid, hex);
237 }