blob: e21c75d654b23aeed912ddb622acaec8614e4992 [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
17#ifndef _INTERFACE_CONTROLLER_H
18#define _INTERFACE_CONTROLLER_H
19
Joel Scherpelzde937962017-06-01 13:20:21 +090020#include <functional>
Bernie Innocenti97f388f2018-10-16 19:17:08 +090021#include <map>
Erik Klineb218a872016-07-04 09:57:18 +090022#include <string>
23
Luke Huangf7782042018-08-08 13:13:04 +080024#include <android/net/InterfaceConfigurationParcel.h>
Joel Scherpelzde937962017-06-01 13:20:21 +090025#include <netdutils/Status.h>
Chenbo Feng7e974052018-02-28 22:57:21 -080026#include <netdutils/StatusOr.h>
Joel Scherpelzde937962017-06-01 13:20:21 +090027
Joel Scherpelzde937962017-06-01 13:20:21 +090028namespace android {
29namespace net {
Bernie Innocenti4debf3b2018-09-12 13:54:50 +090030
Joel Scherpelzde937962017-06-01 13:20:21 +090031class StablePrivacyTest;
Joel Scherpelzde937962017-06-01 13:20:21 +090032
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070033class InterfaceController {
Erik Kline2c5aaa12016-06-08 13:24:45 +090034public:
35 static void initializeAll();
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070036
Luke Huangf7782042018-08-08 13:13:04 +080037 static int setEnableIPv6(const char* ifName, const int on);
38 static android::netdutils::Status setIPv6AddrGenMode(const std::string& ifName, int mode);
39 static int setAcceptIPv6Ra(const char* ifName, const int on);
40 static int setAcceptIPv6Dad(const char* ifName, const int on);
41 static int setIPv6DadTransmits(const char* ifName, const char* value);
42 static int setIPv6PrivacyExtensions(const char* ifName, const int on);
43 static int setMtu(const char* ifName, const char* mtu);
44 static int addAddress(const char* ifName, const char* addrString, int prefixLength);
45 static int delAddress(const char* ifName, const char* addrString, int prefixLength);
Hugo Benichic4b3a0c2018-05-22 08:48:40 +090046 static int disableIcmpRedirects();
Luke Huangf7782042018-08-08 13:13:04 +080047 static android::netdutils::Status setCfg(const InterfaceConfigurationParcel& cfg);
48 static android::netdutils::StatusOr<InterfaceConfigurationParcel> getCfg(
49 const std::string& ifName);
50 static int clearAddrs(const std::string& ifName);
Erik Kline2c5aaa12016-06-08 13:24:45 +090051
Erik Klineb218a872016-07-04 09:57:18 +090052 // Read and write values in files of the form:
Luke Huangf7782042018-08-08 13:13:04 +080053 // /proc/sys/net/<family>/<which>/<ifName>/<parameter>
Erik Kline38e51f12018-09-06 20:14:44 +090054 //
55 // NOTE: getParameter() trims whitespace so the caller does not need extra
56 // code to crop trailing newlines, for example.
Luke Huangf7782042018-08-08 13:13:04 +080057 static int getParameter(const char* family, const char* which, const char* ifName,
58 const char* parameter, std::string* value);
59 static int setParameter(const char* family, const char* which, const char* ifName,
60 const char* parameter, const char* value);
Erik Klineb218a872016-07-04 09:57:18 +090061
Nathan Harold172f8e42018-04-19 13:10:19 -070062 static android::netdutils::StatusOr<std::vector<std::string>> getIfaceNames();
Chenbo Feng7e974052018-02-28 22:57:21 -080063 static android::netdutils::StatusOr<std::map<std::string, uint32_t>> getIfaceList();
64
Luke Huangf7782042018-08-08 13:13:04 +080065 static std::mutex mutex;
Erik Kline2c5aaa12016-06-08 13:24:45 +090066
Luke Huangf7782042018-08-08 13:13:04 +080067 private:
68 friend class android::net::StablePrivacyTest;
Joel Scherpelzde937962017-06-01 13:20:21 +090069
Luke Huangf7782042018-08-08 13:13:04 +080070 using GetPropertyFn =
71 std::function<std::string(const std::string& key, const std::string& dflt)>;
72 using SetPropertyFn = std::function<android::netdutils::Status(const std::string& key,
73 const std::string& val)>;
Joel Scherpelzde937962017-06-01 13:20:21 +090074
Luke Huangf7782042018-08-08 13:13:04 +080075 // Helper function exported from this compilation unit for testing.
76 static android::netdutils::Status enableStablePrivacyAddresses(
77 const std::string& ifName, const GetPropertyFn& getProperty,
78 const SetPropertyFn& setProperty);
Joel Scherpelzde937962017-06-01 13:20:21 +090079
Luke Huangf7782042018-08-08 13:13:04 +080080 static void setAcceptRA(const char* value);
81 static void setAcceptRARouteTable(int tableOrOffset);
82 static void setBaseReachableTimeMs(unsigned int millis);
83 static void setIPv6OptimisticMode(const char* value);
84
85 InterfaceController() = delete;
86 ~InterfaceController() = delete;
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070087};
88
Bernie Innocenti4debf3b2018-09-12 13:54:50 +090089} // namespace net
90} // namespace android
91
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070092#endif