• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 <dirent.h>
18 #include <errno.h>
19 #include <malloc.h>
20 #include <net/if.h>
21 #include <net/if_arp.h>
22 #include <sys/socket.h>
23 
24 #include <functional>
25 
26 #define LOG_TAG "InterfaceController"
27 #include <android-base/file.h>
28 #include <android-base/properties.h>
29 #include <android-base/stringprintf.h>
30 #include <android-base/strings.h>
31 #include <linux/if_ether.h>
32 #include <log/log.h>
33 #include <logwrap/logwrap.h>
34 #include <netutils/ifc.h>
35 
36 #include <android/net/INetd.h>
37 #include <netdutils/Misc.h>
38 #include <netdutils/Slice.h>
39 #include <netdutils/Syscalls.h>
40 
41 #include "InterfaceController.h"
42 #include "RouteController.h"
43 
44 using android::base::ReadFileToString;
45 using android::base::StringPrintf;
46 using android::base::Trim;
47 using android::base::WriteStringToFile;
48 using android::net::INetd;
49 using android::net::RouteController;
50 using android::netdutils::isOk;
51 using android::netdutils::makeSlice;
52 using android::netdutils::sSyscalls;
53 using android::netdutils::Status;
54 using android::netdutils::statusFromErrno;
55 using android::netdutils::StatusOr;
56 using android::netdutils::toString;
57 using android::netdutils::status::ok;
58 
59 #define RETURN_STATUS_IF_IFCERROR(exp)                           \
60     do {                                                         \
61         if ((exp) == -1) {                                       \
62             return statusFromErrno(errno, "Failed to add addr"); \
63         }                                                        \
64     } while (0);
65 
66 namespace {
67 
68 const char ipv4_proc_path[] = "/proc/sys/net/ipv4/conf";
69 const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
70 
71 const char ipv4_neigh_conf_dir[] = "/proc/sys/net/ipv4/neigh";
72 const char ipv6_neigh_conf_dir[] = "/proc/sys/net/ipv6/neigh";
73 
74 const char proc_net_path[] = "/proc/sys/net";
75 const char sys_net_path[] = "/sys/class/net";
76 
77 constexpr int kRouteInfoMinPrefixLen = 48;
78 
79 // RFC 7421 prefix length.
80 constexpr int kRouteInfoMaxPrefixLen = 64;
81 
82 // Property used to persist RFC 7217 stable secret. Protected by SELinux policy.
83 const char kStableSecretProperty[] = "persist.netd.stable_secret";
84 
85 // RFC 7217 stable secret on linux is formatted as an IPv6 address.
86 // This function uses 128 bits of high quality entropy to generate an
87 // address for this purpose. This function should be not be called
88 // frequently.
randomIPv6Address()89 StatusOr<std::string> randomIPv6Address() {
90     in6_addr addr = {};
91     const auto& sys = sSyscalls.get();
92     ASSIGN_OR_RETURN(auto fd, sys.open("/dev/random", O_RDONLY));
93     RETURN_IF_NOT_OK(sys.read(fd, makeSlice(addr)));
94     return toString(addr);
95 }
96 
isNormalPathComponent(const char * component)97 inline bool isNormalPathComponent(const char *component) {
98     return (strcmp(component, ".") != 0) &&
99            (strcmp(component, "..") != 0) &&
100            (strchr(component, '/') == nullptr);
101 }
102 
isAddressFamilyPathComponent(const char * component)103 inline bool isAddressFamilyPathComponent(const char *component) {
104     return strcmp(component, "ipv4") == 0 || strcmp(component, "ipv6") == 0;
105 }
106 
isInterfaceName(const char * name)107 inline bool isInterfaceName(const char *name) {
108     return isNormalPathComponent(name) &&
109            (strcmp(name, "default") != 0) &&
110            (strcmp(name, "all") != 0);
111 }
112 
writeValueToPath(const char * dirname,const char * subdirname,const char * basename,const char * value)113 int writeValueToPath(
114         const char* dirname, const char* subdirname, const char* basename,
115         const char* value) {
116     std::string path(StringPrintf("%s/%s/%s", dirname, subdirname, basename));
117     return WriteStringToFile(value, path) ? 0 : -EREMOTEIO;
118 }
119 
120 // Run @fn on each interface as well as 'default' in the path @dirname.
forEachInterface(const std::string & dirname,const std::function<void (const std::string & path,const std::string & iface)> & fn)121 void forEachInterface(
122         const std::string& dirname,
123         const std::function<void(const std::string& path, const std::string& iface)>& fn) {
124     // Run on default, which controls the behavior of any interfaces that are created in the future.
125     fn(dirname, "default");
126     DIR* dir = opendir(dirname.c_str());
127     if (!dir) {
128         ALOGE("Can't list %s: %s", dirname.c_str(), strerror(errno));
129         return;
130     }
131     while (true) {
132         const dirent *ent = readdir(dir);
133         if (!ent) {
134             break;
135         }
136         if ((ent->d_type != DT_DIR) || !isInterfaceName(ent->d_name)) {
137             continue;
138         }
139         fn(dirname, ent->d_name);
140     }
141     closedir(dir);
142 }
143 
setOnAllInterfaces(const char * dirname,const char * basename,const char * value)144 void setOnAllInterfaces(const char* dirname, const char* basename, const char* value) {
145     auto fn = [basename, value](const std::string& path, const std::string& iface) {
146         writeValueToPath(path.c_str(), iface.c_str(), basename, value);
147     };
148     forEachInterface(dirname, fn);
149 }
150 
setIPv6UseOutgoingInterfaceAddrsOnly(const char * value)151 void setIPv6UseOutgoingInterfaceAddrsOnly(const char *value) {
152     setOnAllInterfaces(ipv6_proc_path, "use_oif_addrs_only", value);
153 }
154 
getParameterPathname(const char * family,const char * which,const char * interface,const char * parameter)155 std::string getParameterPathname(
156         const char *family, const char *which, const char *interface, const char *parameter) {
157     if (!isAddressFamilyPathComponent(family)) {
158         errno = EAFNOSUPPORT;
159         return "";
160     } else if (!isNormalPathComponent(which) ||
161                !isInterfaceName(interface) ||
162                !isNormalPathComponent(parameter)) {
163         errno = EINVAL;
164         return "";
165     }
166 
167     return StringPrintf("%s/%s/%s/%s/%s", proc_net_path, family, which, interface, parameter);
168 }
169 
setAcceptIPv6RIO(int min,int max)170 void setAcceptIPv6RIO(int min, int max) {
171     auto fn = [min, max](const std::string& prefix, const std::string& iface) {
172         int rv = writeValueToPath(prefix.c_str(), iface.c_str(), "accept_ra_rt_info_min_plen",
173                                   std::to_string(min).c_str());
174         if (rv != 0) {
175             // Only update max_plen if the write to min_plen succeeded. This ordering will prevent
176             // RIOs from being accepted unless both min and max are written successfully.
177             return;
178         }
179         writeValueToPath(prefix.c_str(), iface.c_str(), "accept_ra_rt_info_max_plen",
180                          std::to_string(max).c_str());
181     };
182     forEachInterface(ipv6_proc_path, fn);
183 }
184 
185 // Ideally this function would return StatusOr<std::string>, however
186 // there is no safe value for dflt that will always differ from the
187 // stored property. Bugs code could conceivably end up persisting the
188 // reserved value resulting in surprising behavior.
getProperty(const std::string & key,const std::string & dflt)189 std::string getProperty(const std::string& key, const std::string& dflt) {
190     return android::base::GetProperty(key, dflt);
191 };
192 
setProperty(const std::string & key,const std::string & val)193 Status setProperty(const std::string& key, const std::string& val) {
194     // SetProperty does not dependably set errno to a meaningful value. Use our own error code so
195     // callers don't get confused.
196     return android::base::SetProperty(key, val)
197         ? ok
198         : statusFromErrno(EREMOTEIO, "SetProperty failed, see libc logs");
199 };
200 
201 
202 }  // namespace
203 
204 namespace android {
205 namespace net {
206 std::mutex InterfaceController::mutex;
207 
enableStablePrivacyAddresses(const std::string & iface,const GetPropertyFn & getProperty,const SetPropertyFn & setProperty)208 android::netdutils::Status InterfaceController::enableStablePrivacyAddresses(
209         const std::string& iface,
210         const GetPropertyFn& getProperty,
211         const SetPropertyFn& setProperty) {
212     const auto& sys = sSyscalls.get();
213     const std::string procTarget = std::string(ipv6_proc_path) + "/" + iface + "/stable_secret";
214     auto procFd = sys.open(procTarget, O_CLOEXEC | O_WRONLY);
215 
216     // Devices with old kernels (typically < 4.4) don't support
217     // RFC 7217 stable privacy addresses.
218     if (equalToErrno(procFd, ENOENT)) {
219         return statusFromErrno(EOPNOTSUPP,
220                                "Failed to open stable_secret. Assuming unsupported kernel version");
221     }
222 
223     // If stable_secret exists but we can't open it, something strange is going on.
224     RETURN_IF_NOT_OK(procFd);
225 
226     const char kUninitialized[] = "uninitialized";
227     const auto oldSecret = getProperty(kStableSecretProperty, kUninitialized);
228     std::string secret = oldSecret;
229 
230     // Generate a new secret if no persistent property existed.
231     if (oldSecret == kUninitialized) {
232         ASSIGN_OR_RETURN(secret, randomIPv6Address());
233     }
234 
235     // Ask the OS to generate SLAAC addresses on iface using secret.
236     RETURN_IF_NOT_OK(sys.write(procFd.value(), makeSlice(secret)));
237 
238     // Don't persist an existing secret.
239     if (oldSecret != kUninitialized) {
240         return ok;
241     }
242 
243     return setProperty(kStableSecretProperty, secret);
244 }
245 
initializeAll()246 void InterfaceController::initializeAll() {
247     // Initial IPv6 settings.
248     // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
249     // This causes RAs to work or not work based on whether forwarding is on, and causes routes
250     // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
251     // by always setting accept_ra to 2.
252     setAcceptRA("2");
253 
254     // Accept RIOs with prefix length in the closed interval [48, 64].
255     setAcceptIPv6RIO(kRouteInfoMinPrefixLen, kRouteInfoMaxPrefixLen);
256 
257     setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
258 
259     // Enable optimistic DAD for IPv6 addresses on all interfaces.
260     setIPv6OptimisticMode("1");
261 
262     // Reduce the ARP/ND base reachable time from the default (30sec) to 15sec.
263     setBaseReachableTimeMs(15 * 1000);
264 
265     // When sending traffic via a given interface use only addresses configured
266     // on that interface as possible source addresses.
267     setIPv6UseOutgoingInterfaceAddrsOnly("1");
268 
269     // Ensure that ICMP redirects are rejected globally on all interfaces.
270     disableIcmpRedirects();
271 }
272 
setEnableIPv6(const char * interface,const int on)273 int InterfaceController::setEnableIPv6(const char *interface, const int on) {
274     if (!isIfaceName(interface)) {
275         return -ENOENT;
276     }
277     // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
278     // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
279     // addresses and routes and disables IPv6 on the interface.
280     const char *disable_ipv6 = on ? "0" : "1";
281     return writeValueToPath(ipv6_proc_path, interface, "disable_ipv6", disable_ipv6);
282 }
283 
284 // Changes to addrGenMode will not fully take effect until the next
285 // time disable_ipv6 transitions from 1 to 0.
setIPv6AddrGenMode(const std::string & interface,int mode)286 Status InterfaceController::setIPv6AddrGenMode(const std::string& interface, int mode) {
287     if (!isIfaceName(interface)) {
288         return statusFromErrno(ENOENT, "invalid iface name: " + interface);
289     }
290 
291     switch (mode) {
292         case INetd::IPV6_ADDR_GEN_MODE_EUI64:
293             // Ignore return value. If /proc/.../stable_secret is
294             // missing we're probably in EUI64 mode already.
295             writeValueToPath(ipv6_proc_path, interface.c_str(), "stable_secret", "");
296             break;
297         case INetd::IPV6_ADDR_GEN_MODE_STABLE_PRIVACY: {
298             return enableStablePrivacyAddresses(interface, getProperty, setProperty);
299         }
300         case INetd::IPV6_ADDR_GEN_MODE_NONE:
301         case INetd::IPV6_ADDR_GEN_MODE_RANDOM:
302         default:
303             return statusFromErrno(EOPNOTSUPP, "unsupported addrGenMode");
304     }
305 
306     return ok;
307 }
308 
setAcceptIPv6Ra(const char * interface,const int on)309 int InterfaceController::setAcceptIPv6Ra(const char *interface, const int on) {
310     if (!isIfaceName(interface)) {
311         errno = ENOENT;
312         return -1;
313     }
314     // Because forwarding can be enabled even when tethering is off, we always
315     // use mode "2" (accept RAs, even if forwarding is enabled).
316     const char *accept_ra = on ? "2" : "0";
317     return writeValueToPath(ipv6_proc_path, interface, "accept_ra", accept_ra);
318 }
319 
setAcceptIPv6Dad(const char * interface,const int on)320 int InterfaceController::setAcceptIPv6Dad(const char *interface, const int on) {
321     if (!isIfaceName(interface)) {
322         errno = ENOENT;
323         return -1;
324     }
325     const char *accept_dad = on ? "1" : "0";
326     return writeValueToPath(ipv6_proc_path, interface, "accept_dad", accept_dad);
327 }
328 
setIPv6DadTransmits(const char * interface,const char * value)329 int InterfaceController::setIPv6DadTransmits(const char *interface, const char *value) {
330     if (!isIfaceName(interface)) {
331         errno = ENOENT;
332         return -1;
333     }
334     return writeValueToPath(ipv6_proc_path, interface, "dad_transmits", value);
335 }
336 
setIPv6PrivacyExtensions(const char * interface,const int on)337 int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
338     if (!isIfaceName(interface)) {
339         errno = ENOENT;
340         return -errno;
341     }
342     // 0: disable IPv6 privacy addresses
343     // 2: enable IPv6 privacy addresses and prefer them over non-privacy ones.
344     return writeValueToPath(ipv6_proc_path, interface, "use_tempaddr", on ? "2" : "0");
345 }
346 
setAcceptRA(const char * value)347 void InterfaceController::setAcceptRA(const char *value) {
348     setOnAllInterfaces(ipv6_proc_path, "accept_ra", value);
349 }
350 
351 // |tableOrOffset| is interpreted as:
352 //     If == 0: default. Routes go into RT6_TABLE_MAIN.
353 //     If > 0: user set. Routes go into the specified table.
354 //     If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
355 //             ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
356 //             table 1005, etc.
setAcceptRARouteTable(int tableOrOffset)357 void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
358     std::string value(StringPrintf("%d", tableOrOffset));
359     setOnAllInterfaces(ipv6_proc_path, "accept_ra_rt_table", value.c_str());
360 }
361 
setMtu(const char * interface,const char * mtu)362 int InterfaceController::setMtu(const char *interface, const char *mtu)
363 {
364     if (!isIfaceName(interface)) {
365         errno = ENOENT;
366         return -errno;
367     }
368     return writeValueToPath(sys_net_path, interface, "mtu", mtu);
369 }
370 
addAddress(const char * interface,const char * addrString,int prefixLength)371 int InterfaceController::addAddress(const char *interface,
372         const char *addrString, int prefixLength) {
373     return ifc_add_address(interface, addrString, prefixLength);
374 }
375 
delAddress(const char * interface,const char * addrString,int prefixLength)376 int InterfaceController::delAddress(const char *interface,
377         const char *addrString, int prefixLength) {
378     return ifc_del_address(interface, addrString, prefixLength);
379 }
380 
disableIcmpRedirects()381 int InterfaceController::disableIcmpRedirects() {
382     int rv = 0;
383     rv |= writeValueToPath(ipv4_proc_path, "all", "accept_redirects", "0");
384     rv |= writeValueToPath(ipv6_proc_path, "all", "accept_redirects", "0");
385     setOnAllInterfaces(ipv4_proc_path, "accept_redirects", "0");
386     setOnAllInterfaces(ipv6_proc_path, "accept_redirects", "0");
387     return rv;
388 }
389 
getParameter(const char * family,const char * which,const char * interface,const char * parameter,std::string * value)390 int InterfaceController::getParameter(
391         const char *family, const char *which, const char *interface, const char *parameter,
392         std::string *value) {
393     const std::string path(getParameterPathname(family, which, interface, parameter));
394     if (path.empty()) {
395         return -errno;
396     }
397     if (ReadFileToString(path, value)) {
398         *value = Trim(*value);
399         return 0;
400     }
401     return -errno;
402 }
403 
setParameter(const char * family,const char * which,const char * interface,const char * parameter,const char * value)404 int InterfaceController::setParameter(
405         const char *family, const char *which, const char *interface, const char *parameter,
406         const char *value) {
407     const std::string path(getParameterPathname(family, which, interface, parameter));
408     if (path.empty()) {
409         return -errno;
410     }
411     return WriteStringToFile(value, path) ? 0 : -errno;
412 }
413 
setBaseReachableTimeMs(unsigned int millis)414 void InterfaceController::setBaseReachableTimeMs(unsigned int millis) {
415     std::string value(StringPrintf("%u", millis));
416     setOnAllInterfaces(ipv4_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
417     setOnAllInterfaces(ipv6_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
418 }
419 
setIPv6OptimisticMode(const char * value)420 void InterfaceController::setIPv6OptimisticMode(const char *value) {
421     setOnAllInterfaces(ipv6_proc_path, "optimistic_dad", value);
422     setOnAllInterfaces(ipv6_proc_path, "use_optimistic", value);
423 }
424 
getIfaceNames()425 StatusOr<std::vector<std::string>> InterfaceController::getIfaceNames() {
426     std::vector<std::string> ifaceNames;
427     DIR* d;
428     struct dirent* de;
429 
430     if (!(d = opendir("/sys/class/net"))) {
431         return statusFromErrno(errno, "Cannot open iface directory");
432     }
433     while ((de = readdir(d))) {
434         if ((de->d_type != DT_DIR) && (de->d_type != DT_LNK)) continue;
435         if (de->d_name[0] == '.') continue;
436         ifaceNames.push_back(std::string(de->d_name));
437     }
438     closedir(d);
439     return ifaceNames;
440 }
441 
getIfaceList()442 StatusOr<std::map<std::string, uint32_t>> InterfaceController::getIfaceList() {
443     std::map<std::string, uint32_t> ifacePairs;
444 
445     ASSIGN_OR_RETURN(auto ifaceNames, getIfaceNames());
446 
447     for (const auto& name : ifaceNames) {
448         uint32_t ifaceIndex = if_nametoindex(name.c_str());
449         if (ifaceIndex) {
450             ifacePairs.insert(std::pair<std::string, uint32_t>(name, ifaceIndex));
451         }
452     }
453     return ifacePairs;
454 }
455 
456 namespace {
457 
hwAddrToStr(unsigned char * hwaddr)458 std::string hwAddrToStr(unsigned char* hwaddr) {
459     return StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3],
460                         hwaddr[4], hwaddr[5]);
461 }
462 
ipv4NetmaskToPrefixLength(in_addr_t mask)463 int ipv4NetmaskToPrefixLength(in_addr_t mask) {
464     int prefixLength = 0;
465     uint32_t m = ntohl(mask);
466     while (m & (1 << 31)) {
467         prefixLength++;
468         m = m << 1;
469     }
470     return prefixLength;
471 }
472 
toStdString(const String16 & s)473 std::string toStdString(const String16& s) {
474     return std::string(String8(s.string()));
475 }
476 
477 }  // namespace
478 
setCfg(const InterfaceConfigurationParcel & cfg)479 Status InterfaceController::setCfg(const InterfaceConfigurationParcel& cfg) {
480     const auto& sys = sSyscalls.get();
481     ASSIGN_OR_RETURN(auto fd, sys.socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
482     struct ifreq ifr = {
483             .ifr_addr = {.sa_family = AF_INET},  // Clear the IPv4 address.
484     };
485     strlcpy(ifr.ifr_name, cfg.ifName.c_str(), IFNAMSIZ);
486 
487     // Make sure that clear IPv4 address before set flag
488     // SIOCGIFFLAGS might override ifr and caused clear IPv4 addr ioctl error
489     RETURN_IF_NOT_OK(sys.ioctl(fd, SIOCSIFADDR, &ifr));
490 
491     if (!cfg.flags.empty()) {
492         RETURN_IF_NOT_OK(sys.ioctl(fd, SIOCGIFFLAGS, &ifr));
493         uint16_t flags = ifr.ifr_flags;
494 
495         for (const auto& flag : cfg.flags) {
496             if (flag == toStdString(INetd::IF_STATE_UP())) {
497                 ifr.ifr_flags = ifr.ifr_flags | IFF_UP;
498             } else if (flag == toStdString(INetd::IF_STATE_DOWN())) {
499                 ifr.ifr_flags = (ifr.ifr_flags & (~IFF_UP));
500             }
501         }
502 
503         if (ifr.ifr_flags != flags) {
504             RETURN_IF_NOT_OK(sys.ioctl(fd, SIOCSIFFLAGS, &ifr));
505         }
506     }
507 
508     RETURN_STATUS_IF_IFCERROR(
509             ifc_add_address(cfg.ifName.c_str(), cfg.ipv4Addr.c_str(), cfg.prefixLength));
510 
511     return ok;
512 }
513 
getCfg(const std::string & ifName)514 StatusOr<InterfaceConfigurationParcel> InterfaceController::getCfg(const std::string& ifName) {
515     struct in_addr addr = {};
516     int prefixLength = 0;
517     unsigned char hwaddr[ETH_ALEN] = {};
518     unsigned flags = 0;
519     InterfaceConfigurationParcel cfgResult;
520 
521     const auto& sys = sSyscalls.get();
522     ASSIGN_OR_RETURN(auto fd, sys.socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0));
523 
524     struct ifreq ifr = {};
525     strlcpy(ifr.ifr_name, ifName.c_str(), IFNAMSIZ);
526 
527     if (isOk(sys.ioctl(fd, SIOCGIFADDR, &ifr))) {
528         addr.s_addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
529     }
530 
531     if (isOk(sys.ioctl(fd, SIOCGIFNETMASK, &ifr))) {
532         prefixLength =
533                 ipv4NetmaskToPrefixLength(((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr);
534     }
535 
536     if (isOk(sys.ioctl(fd, SIOCGIFFLAGS, &ifr))) {
537         flags = ifr.ifr_flags;
538     }
539 
540     // ETH_ALEN is for ARPHRD_ETHER, it is better to check the sa_family.
541     // However, we keep old design for the consistency.
542     if (isOk(sys.ioctl(fd, SIOCGIFHWADDR, &ifr))) {
543         memcpy((void*) hwaddr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
544     } else {
545         ALOGW("Failed to retrieve HW addr for %s (%s)", ifName.c_str(), strerror(errno));
546     }
547 
548     cfgResult.ifName = ifName;
549     cfgResult.hwAddr = hwAddrToStr(hwaddr);
550     cfgResult.ipv4Addr = std::string(inet_ntoa(addr));
551     cfgResult.prefixLength = prefixLength;
552     cfgResult.flags.push_back(flags & IFF_UP ? toStdString(INetd::IF_STATE_UP())
553                                              : toStdString(INetd::IF_STATE_DOWN()));
554 
555     if (flags & IFF_BROADCAST) cfgResult.flags.push_back(toStdString(INetd::IF_FLAG_BROADCAST()));
556     if (flags & IFF_LOOPBACK) cfgResult.flags.push_back(toStdString(INetd::IF_FLAG_LOOPBACK()));
557     if (flags & IFF_POINTOPOINT)
558         cfgResult.flags.push_back(toStdString(INetd::IF_FLAG_POINTOPOINT()));
559     if (flags & IFF_RUNNING) cfgResult.flags.push_back(toStdString(INetd::IF_FLAG_RUNNING()));
560     if (flags & IFF_MULTICAST) cfgResult.flags.push_back(toStdString(INetd::IF_FLAG_MULTICAST()));
561 
562     return cfgResult;
563 }
564 
clearAddrs(const std::string & ifName)565 int InterfaceController::clearAddrs(const std::string& ifName) {
566     return ifc_clear_addresses(ifName.c_str());
567 }
568 
569 }  // namespace net
570 }  // namespace android
571