blob: 34b8004a1448e257d50477176a79e6199914d37f [file] [log] [blame]
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -07001/*
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 *
Sasha Levitskiy329c3b42012-07-30 16:11:23 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -07009 *
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 Colitti37f2e372013-04-12 00:44:06 +090017#include <dirent.h>
Dan Albertaa1be2b2015-01-06 09:36:17 -080018#include <errno.h>
Elliott Hughes5f4938f2015-01-28 11:22:38 -080019#include <malloc.h>
Chenbo Feng7e974052018-02-28 22:57:21 -080020#include <net/if.h>
Erik Klineb218a872016-07-04 09:57:18 +090021#include <sys/socket.h>
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070022
Joel Scherpelz31e25992017-03-24 12:40:00 +090023#include <functional>
24
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070025#define LOG_TAG "InterfaceController"
Elliott Hughesbbd56262015-12-04 15:45:10 -080026#include <android-base/file.h>
Joel Scherpelzde937962017-06-01 13:20:21 +090027#include <android-base/properties.h>
Elliott Hughesbbd56262015-12-04 15:45:10 -080028#include <android-base/stringprintf.h>
Logan Chien3f461482018-04-23 14:31:32 +080029#include <log/log.h>
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +090030#include <logwrap/logwrap.h>
Erik Klinec296f092016-08-02 15:22:53 +090031#include <netutils/ifc.h>
Lorenzo Colitti70afde62013-03-04 17:58:40 +090032
Joel Scherpelzde937962017-06-01 13:20:21 +090033#include <android/net/INetd.h>
34#include <netdutils/Misc.h>
35#include <netdutils/Slice.h>
36#include <netdutils/Syscalls.h>
37
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070038#include "InterfaceController.h"
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070039#include "RouteController.h"
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070040
Erik Klineb218a872016-07-04 09:57:18 +090041using android::base::ReadFileToString;
Joel Scherpelzde937962017-06-01 13:20:21 +090042using android::base::StringPrintf;
Dan Albert5407e142015-03-16 10:05:59 -070043using android::base::WriteStringToFile;
Joel Scherpelzde937962017-06-01 13:20:21 +090044using android::net::INetd;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090045using android::net::RouteController;
Nathan Harold172f8e42018-04-19 13:10:19 -070046using android::netdutils::isOk;
Joel Scherpelzde937962017-06-01 13:20:21 +090047using android::netdutils::Status;
48using android::netdutils::StatusOr;
49using android::netdutils::makeSlice;
50using android::netdutils::sSyscalls;
51using android::netdutils::status::ok;
52using android::netdutils::statusFromErrno;
53using android::netdutils::toString;
Dan Albert5407e142015-03-16 10:05:59 -070054
Erik Klinee1da4842015-05-12 15:56:06 +090055namespace {
56
Hugo Benichic4b3a0c2018-05-22 08:48:40 +090057const char ipv4_proc_path[] = "/proc/sys/net/ipv4/conf";
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090058const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
59
Erik Kline145fd252015-05-12 15:58:49 +090060const char ipv4_neigh_conf_dir[] = "/proc/sys/net/ipv4/neigh";
Erik Kline145fd252015-05-12 15:58:49 +090061const char ipv6_neigh_conf_dir[] = "/proc/sys/net/ipv6/neigh";
62
Erik Klineb218a872016-07-04 09:57:18 +090063const char proc_net_path[] = "/proc/sys/net";
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -070064const char sys_net_path[] = "/sys/class/net";
65
Joel Scherpelz31e25992017-03-24 12:40:00 +090066constexpr int kRouteInfoMinPrefixLen = 48;
67
68// RFC 7421 prefix length.
69constexpr int kRouteInfoMaxPrefixLen = 64;
70
Joel Scherpelzde937962017-06-01 13:20:21 +090071// Property used to persist RFC 7217 stable secret. Protected by SELinux policy.
72const char kStableSecretProperty[] = "persist.netd.stable_secret";
73
74// RFC 7217 stable secret on linux is formatted as an IPv6 address.
75// This function uses 128 bits of high quality entropy to generate an
76// address for this purpose. This function should be not be called
77// frequently.
78StatusOr<std::string> randomIPv6Address() {
79 in6_addr addr = {};
80 const auto& sys = sSyscalls.get();
81 ASSIGN_OR_RETURN(auto fd, sys.open("/dev/random", O_RDONLY));
82 RETURN_IF_NOT_OK(sys.read(fd, makeSlice(addr)));
83 return toString(addr);
84}
85
Erik Klineb218a872016-07-04 09:57:18 +090086inline bool isNormalPathComponent(const char *component) {
87 return (strcmp(component, ".") != 0) &&
88 (strcmp(component, "..") != 0) &&
89 (strchr(component, '/') == nullptr);
90}
91
92inline bool isAddressFamilyPathComponent(const char *component) {
93 return strcmp(component, "ipv4") == 0 || strcmp(component, "ipv6") == 0;
94}
95
96inline bool isInterfaceName(const char *name) {
97 return isNormalPathComponent(name) &&
98 (strcmp(name, "default") != 0) &&
99 (strcmp(name, "all") != 0);
Erik Klinee1da4842015-05-12 15:56:06 +0900100}
101
102int writeValueToPath(
103 const char* dirname, const char* subdirname, const char* basename,
104 const char* value) {
105 std::string path(StringPrintf("%s/%s/%s", dirname, subdirname, basename));
Erik Kline3f957772015-06-03 17:44:24 +0900106 return WriteStringToFile(value, path) ? 0 : -1;
Erik Klinee1da4842015-05-12 15:56:06 +0900107}
108
Joel Scherpelz31e25992017-03-24 12:40:00 +0900109// Run @fn on each interface as well as 'default' in the path @dirname.
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900110void forEachInterface(
111 const std::string& dirname,
112 const std::function<void(const std::string& path, const std::string& iface)>& fn) {
Joel Scherpelz31e25992017-03-24 12:40:00 +0900113 // Run on default, which controls the behavior of any interfaces that are created in the future.
114 fn(dirname, "default");
115 DIR* dir = opendir(dirname.c_str());
Erik Klinee1da4842015-05-12 15:56:06 +0900116 if (!dir) {
Joel Scherpelz31e25992017-03-24 12:40:00 +0900117 ALOGE("Can't list %s: %s", dirname.c_str(), strerror(errno));
Erik Klinee1da4842015-05-12 15:56:06 +0900118 return;
119 }
Joel Scherpelz31e25992017-03-24 12:40:00 +0900120 while (true) {
121 const dirent *ent = readdir(dir);
122 if (!ent) {
123 break;
124 }
125 if ((ent->d_type != DT_DIR) || !isInterfaceName(ent->d_name)) {
Erik Klinee1da4842015-05-12 15:56:06 +0900126 continue;
127 }
Joel Scherpelz31e25992017-03-24 12:40:00 +0900128 fn(dirname, ent->d_name);
Erik Klinee1da4842015-05-12 15:56:06 +0900129 }
130 closedir(dir);
131}
132
Joel Scherpelz31e25992017-03-24 12:40:00 +0900133void setOnAllInterfaces(const char* dirname, const char* basename, const char* value) {
134 auto fn = [basename, value](const std::string& path, const std::string& iface) {
135 writeValueToPath(path.c_str(), iface.c_str(), basename, value);
136 };
137 forEachInterface(dirname, fn);
138}
139
Erik Kline7adf8d72015-07-28 18:51:01 +0900140void setIPv6UseOutgoingInterfaceAddrsOnly(const char *value) {
141 setOnAllInterfaces(ipv6_proc_path, "use_oif_addrs_only", value);
142}
143
Erik Klineb218a872016-07-04 09:57:18 +0900144std::string getParameterPathname(
145 const char *family, const char *which, const char *interface, const char *parameter) {
146 if (!isAddressFamilyPathComponent(family)) {
147 errno = EAFNOSUPPORT;
148 return "";
149 } else if (!isNormalPathComponent(which) ||
150 !isInterfaceName(interface) ||
151 !isNormalPathComponent(parameter)) {
152 errno = EINVAL;
153 return "";
154 }
155
156 return StringPrintf("%s/%s/%s/%s/%s", proc_net_path, family, which, interface, parameter);
157}
158
Joel Scherpelz31e25992017-03-24 12:40:00 +0900159void setAcceptIPv6RIO(int min, int max) {
160 auto fn = [min, max](const std::string& prefix, const std::string& iface) {
161 int rv = writeValueToPath(prefix.c_str(), iface.c_str(), "accept_ra_rt_info_min_plen",
162 std::to_string(min).c_str());
163 if (rv != 0) {
164 // Only update max_plen if the write to min_plen succeeded. This ordering will prevent
165 // RIOs from being accepted unless both min and max are written successfully.
166 return;
167 }
168 writeValueToPath(prefix.c_str(), iface.c_str(), "accept_ra_rt_info_max_plen",
169 std::to_string(max).c_str());
170 };
171 forEachInterface(ipv6_proc_path, fn);
172}
173
Joel Scherpelzde937962017-06-01 13:20:21 +0900174// Ideally this function would return StatusOr<std::string>, however
175// there is no safe value for dflt that will always differ from the
176// stored property. Bugs code could conceivably end up persisting the
177// reserved value resulting in surprising behavior.
178std::string getProperty(const std::string& key, const std::string& dflt) {
179 return android::base::GetProperty(key, dflt);
180};
181
182Status setProperty(const std::string& key, const std::string& val) {
Lorenzo Colitti516764f2017-07-10 19:13:23 +0900183 // SetProperty does not dependably set errno to a meaningful value. Use our own error code so
184 // callers don't get confused.
Joel Scherpelzde937962017-06-01 13:20:21 +0900185 return android::base::SetProperty(key, val)
186 ? ok
Lorenzo Colitti516764f2017-07-10 19:13:23 +0900187 : statusFromErrno(EREMOTEIO, "SetProperty failed, see libc logs");
Joel Scherpelzde937962017-06-01 13:20:21 +0900188};
189
190
Erik Klinee1da4842015-05-12 15:56:06 +0900191} // namespace
192
Joel Scherpelzde937962017-06-01 13:20:21 +0900193android::netdutils::Status InterfaceController::enableStablePrivacyAddresses(
Bernie Innocenti15bb55c2018-06-03 16:19:51 +0900194 const std::string& iface,
195 const GetPropertyFn& getProperty,
196 const SetPropertyFn& setProperty) {
Joel Scherpelzde937962017-06-01 13:20:21 +0900197 const auto& sys = sSyscalls.get();
198 const std::string procTarget = std::string(ipv6_proc_path) + "/" + iface + "/stable_secret";
199 auto procFd = sys.open(procTarget, O_CLOEXEC | O_WRONLY);
200
201 // Devices with old kernels (typically < 4.4) don't support
202 // RFC 7217 stable privacy addresses.
203 if (equalToErrno(procFd, ENOENT)) {
204 return statusFromErrno(EOPNOTSUPP,
205 "Failed to open stable_secret. Assuming unsupported kernel version");
206 }
207
208 // If stable_secret exists but we can't open it, something strange is going on.
209 RETURN_IF_NOT_OK(procFd);
210
211 const char kUninitialized[] = "uninitialized";
212 const auto oldSecret = getProperty(kStableSecretProperty, kUninitialized);
213 std::string secret = oldSecret;
214
215 // Generate a new secret if no persistent property existed.
216 if (oldSecret == kUninitialized) {
217 ASSIGN_OR_RETURN(secret, randomIPv6Address());
218 }
219
220 // Ask the OS to generate SLAAC addresses on iface using secret.
221 RETURN_IF_NOT_OK(sys.write(procFd.value(), makeSlice(secret)));
222
223 // Don't persist an existing secret.
224 if (oldSecret != kUninitialized) {
225 return ok;
226 }
227
228 return setProperty(kStableSecretProperty, secret);
229}
230
Erik Kline2c5aaa12016-06-08 13:24:45 +0900231void InterfaceController::initializeAll() {
232 // Initial IPv6 settings.
233 // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
234 // This causes RAs to work or not work based on whether forwarding is on, and causes routes
235 // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
236 // by always setting accept_ra to 2.
237 setAcceptRA("2");
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900238
Joel Scherpelz31e25992017-03-24 12:40:00 +0900239 // Accept RIOs with prefix length in the closed interval [48, 64].
240 setAcceptIPv6RIO(kRouteInfoMinPrefixLen, kRouteInfoMaxPrefixLen);
241
Erik Kline2c5aaa12016-06-08 13:24:45 +0900242 setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
Erik Kline59273ed2014-12-08 16:05:28 +0900243
Erik Kline2c5aaa12016-06-08 13:24:45 +0900244 // Enable optimistic DAD for IPv6 addresses on all interfaces.
245 setIPv6OptimisticMode("1");
Erik Kline145fd252015-05-12 15:58:49 +0900246
Erik Kline2c5aaa12016-06-08 13:24:45 +0900247 // Reduce the ARP/ND base reachable time from the default (30sec) to 15sec.
248 setBaseReachableTimeMs(15 * 1000);
Erik Kline7adf8d72015-07-28 18:51:01 +0900249
Erik Kline2c5aaa12016-06-08 13:24:45 +0900250 // When sending traffic via a given interface use only addresses configured
Hugo Benichic4b3a0c2018-05-22 08:48:40 +0900251 // on that interface as possible source addresses.
Erik Kline2c5aaa12016-06-08 13:24:45 +0900252 setIPv6UseOutgoingInterfaceAddrsOnly("1");
Hugo Benichic4b3a0c2018-05-22 08:48:40 +0900253
254 // Ensure that ICMP redirects are rejected globally on all interfaces.
255 disableIcmpRedirects();
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -0700256}
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900257
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900258int InterfaceController::setEnableIPv6(const char *interface, const int on) {
Erik Klinee1da4842015-05-12 15:56:06 +0900259 if (!isIfaceName(interface)) {
260 errno = ENOENT;
261 return -1;
262 }
263 // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
264 // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
265 // addresses and routes and disables IPv6 on the interface.
266 const char *disable_ipv6 = on ? "0" : "1";
267 return writeValueToPath(ipv6_proc_path, interface, "disable_ipv6", disable_ipv6);
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900268}
269
Joel Scherpelzde937962017-06-01 13:20:21 +0900270// Changes to addrGenMode will not fully take effect until the next
271// time disable_ipv6 transitions from 1 to 0.
272Status InterfaceController::setIPv6AddrGenMode(const std::string& interface, int mode) {
273 if (!isIfaceName(interface)) {
274 return statusFromErrno(ENOENT, "invalid iface name: " + interface);
275 }
276
277 switch (mode) {
278 case INetd::IPV6_ADDR_GEN_MODE_EUI64:
279 // Ignore return value. If /proc/.../stable_secret is
280 // missing we're probably in EUI64 mode already.
281 writeValueToPath(ipv6_proc_path, interface.c_str(), "stable_secret", "");
282 break;
283 case INetd::IPV6_ADDR_GEN_MODE_STABLE_PRIVACY: {
284 return enableStablePrivacyAddresses(interface, getProperty, setProperty);
285 }
286 case INetd::IPV6_ADDR_GEN_MODE_NONE:
287 case INetd::IPV6_ADDR_GEN_MODE_RANDOM:
288 default:
289 return statusFromErrno(EOPNOTSUPP, "unsupported addrGenMode");
290 }
291
292 return ok;
293}
294
Erik Kline2c5aaa12016-06-08 13:24:45 +0900295int InterfaceController::setAcceptIPv6Ra(const char *interface, const int on) {
296 if (!isIfaceName(interface)) {
297 errno = ENOENT;
298 return -1;
299 }
300 // Because forwarding can be enabled even when tethering is off, we always
301 // use mode "2" (accept RAs, even if forwarding is enabled).
302 const char *accept_ra = on ? "2" : "0";
303 return writeValueToPath(ipv6_proc_path, interface, "accept_ra", accept_ra);
304}
305
306int InterfaceController::setAcceptIPv6Dad(const char *interface, const int on) {
307 if (!isIfaceName(interface)) {
308 errno = ENOENT;
309 return -1;
310 }
311 const char *accept_dad = on ? "1" : "0";
312 return writeValueToPath(ipv6_proc_path, interface, "accept_dad", accept_dad);
313}
314
Erik Kline59d8c482016-08-09 15:28:42 +0900315int InterfaceController::setIPv6DadTransmits(const char *interface, const char *value) {
316 if (!isIfaceName(interface)) {
317 errno = ENOENT;
318 return -1;
319 }
320 return writeValueToPath(ipv6_proc_path, interface, "dad_transmits", value);
321}
322
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900323int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
Erik Klinee1da4842015-05-12 15:56:06 +0900324 if (!isIfaceName(interface)) {
325 errno = ENOENT;
326 return -1;
327 }
328 // 0: disable IPv6 privacy addresses
Joel Scherpelzde937962017-06-01 13:20:21 +0900329 // 2: enable IPv6 privacy addresses and prefer them over non-privacy ones.
Erik Klinee1da4842015-05-12 15:56:06 +0900330 return writeValueToPath(ipv6_proc_path, interface, "use_tempaddr", on ? "2" : "0");
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900331}
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900332
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700333void InterfaceController::setAcceptRA(const char *value) {
Erik Klinee1da4842015-05-12 15:56:06 +0900334 setOnAllInterfaces(ipv6_proc_path, "accept_ra", value);
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700335}
336
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700337// |tableOrOffset| is interpreted as:
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700338// If == 0: default. Routes go into RT6_TABLE_MAIN.
339// If > 0: user set. Routes go into the specified table.
340// If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
341// ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
342// table 1005, etc.
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700343void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
Erik Klinee1da4842015-05-12 15:56:06 +0900344 std::string value(StringPrintf("%d", tableOrOffset));
345 setOnAllInterfaces(ipv6_proc_path, "accept_ra_rt_table", value.c_str());
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900346}
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700347
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700348int InterfaceController::setMtu(const char *interface, const char *mtu)
349{
Erik Klinee1da4842015-05-12 15:56:06 +0900350 if (!isIfaceName(interface)) {
351 errno = ENOENT;
352 return -1;
353 }
354 return writeValueToPath(sys_net_path, interface, "mtu", mtu);
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700355}
Erik Kline59273ed2014-12-08 16:05:28 +0900356
Erik Klinec296f092016-08-02 15:22:53 +0900357int InterfaceController::addAddress(const char *interface,
358 const char *addrString, int prefixLength) {
359 return ifc_add_address(interface, addrString, prefixLength);
360}
361
362int InterfaceController::delAddress(const char *interface,
363 const char *addrString, int prefixLength) {
364 return ifc_del_address(interface, addrString, prefixLength);
365}
366
Hugo Benichic4b3a0c2018-05-22 08:48:40 +0900367int InterfaceController::disableIcmpRedirects() {
368 int rv = 0;
369 rv |= writeValueToPath(ipv4_proc_path, "all", "accept_redirects", "0");
370 rv |= writeValueToPath(ipv6_proc_path, "all", "accept_redirects", "0");
371 setOnAllInterfaces(ipv4_proc_path, "accept_redirects", "0");
372 setOnAllInterfaces(ipv6_proc_path, "accept_redirects", "0");
373 return rv;
374}
375
Erik Klineb218a872016-07-04 09:57:18 +0900376int InterfaceController::getParameter(
377 const char *family, const char *which, const char *interface, const char *parameter,
378 std::string *value) {
379 const std::string path(getParameterPathname(family, which, interface, parameter));
380 if (path.empty()) {
381 return -errno;
382 }
383 return ReadFileToString(path, value) ? 0 : -errno;
384}
385
386int InterfaceController::setParameter(
387 const char *family, const char *which, const char *interface, const char *parameter,
388 const char *value) {
389 const std::string path(getParameterPathname(family, which, interface, parameter));
390 if (path.empty()) {
391 return -errno;
392 }
393 return WriteStringToFile(value, path) ? 0 : -errno;
394}
395
Erik Kline145fd252015-05-12 15:58:49 +0900396void InterfaceController::setBaseReachableTimeMs(unsigned int millis) {
397 std::string value(StringPrintf("%u", millis));
398 setOnAllInterfaces(ipv4_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
399 setOnAllInterfaces(ipv6_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
400}
401
Erik Kline59273ed2014-12-08 16:05:28 +0900402void InterfaceController::setIPv6OptimisticMode(const char *value) {
Erik Klinee1da4842015-05-12 15:56:06 +0900403 setOnAllInterfaces(ipv6_proc_path, "optimistic_dad", value);
404 setOnAllInterfaces(ipv6_proc_path, "use_optimistic", value);
Erik Kline59273ed2014-12-08 16:05:28 +0900405}
Chenbo Feng7e974052018-02-28 22:57:21 -0800406
Nathan Harold172f8e42018-04-19 13:10:19 -0700407StatusOr<std::vector<std::string>> InterfaceController::getIfaceNames() {
408 std::vector<std::string> ifaceNames;
Chenbo Feng7e974052018-02-28 22:57:21 -0800409 DIR* d;
410 struct dirent* de;
411
412 if (!(d = opendir("/sys/class/net"))) {
413 return statusFromErrno(errno, "Cannot open iface directory");
414 }
415 while ((de = readdir(d))) {
416 if (de->d_name[0] == '.') continue;
Nathan Harold172f8e42018-04-19 13:10:19 -0700417 ifaceNames.push_back(std::string(de->d_name));
Chenbo Feng7e974052018-02-28 22:57:21 -0800418 }
419 closedir(d);
Nathan Harold172f8e42018-04-19 13:10:19 -0700420 return ifaceNames;
421}
422
423StatusOr<std::map<std::string, uint32_t>> InterfaceController::getIfaceList() {
424 std::map<std::string, uint32_t> ifacePairs;
425
426 ASSIGN_OR_RETURN(auto ifaceNames, getIfaceNames());
427
428 for (const auto& name : ifaceNames) {
429 uint32_t ifaceIndex = if_nametoindex(name.c_str());
430 if (ifaceIndex) {
431 ifacePairs.insert(std::pair<std::string, uint32_t>(name, ifaceIndex));
432 }
433 }
Chenbo Feng7e974052018-02-28 22:57:21 -0800434 return ifacePairs;
435}