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 #ifndef _INTERFACE_CONTROLLER_H
18 #define _INTERFACE_CONTROLLER_H
19 
20 #include <functional>
21 #include <map>
22 #include <string>
23 
24 #include <android/net/InterfaceConfigurationParcel.h>
25 #include <netdutils/Status.h>
26 #include <netdutils/StatusOr.h>
27 
28 namespace android {
29 namespace net {
30 
31 class StablePrivacyTest;
32 
33 class InterfaceController {
34 public:
35     static void initializeAll();
36 
37     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);
46     static int disableIcmpRedirects();
47     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);
51 
52     // Read and write values in files of the form:
53     //     /proc/sys/net/<family>/<which>/<ifName>/<parameter>
54     //
55     // NOTE: getParameter() trims whitespace so the caller does not need extra
56     // code to crop trailing newlines, for example.
57     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);
61 
62     static android::netdutils::StatusOr<std::vector<std::string>> getIfaceNames();
63     static android::netdutils::StatusOr<std::map<std::string, uint32_t>> getIfaceList();
64 
65     static std::mutex mutex;
66 
67   private:
68     friend class android::net::StablePrivacyTest;
69 
70     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)>;
74 
75     // 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);
79 
80     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;
87 };
88 
89 }  // namespace net
90 }  // namespace android
91 
92 #endif
93