1 /*
2  * Copyright (C) 2019 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 #pragma once
18 
19 #include <linux/if_ether.h>
20 
21 #include <array>
22 #include <optional>
23 #include <set>
24 #include <string>
25 
26 namespace android::netdevice {
27 
28 typedef std::array<uint8_t, ETH_ALEN> hwaddr_t;
29 
30 /**
31  * Configures libnetdevice to use other socket domain than AF_INET,
32  * what requires less permissive SEPolicy rules for a given process.
33  *
34  * In such case, the process would only be able to control interfaces of a given kind.
35 
36  * \param domain Socket domain to use (e.g. AF_CAN), see socket(2) for details
37  */
38 void useSocketDomain(int domain);
39 
40 /**
41  * Checks, if the network interface exists.
42  *
43  * \param ifname Interface to check
44  * \return true if it exists, false otherwise
45  */
46 bool exists(std::string ifname);
47 
48 /**
49  * Checks if network interface is up.
50  *
51  * \param ifname Interface to check
52  * \return true/false if the check succeeded, nullopt otherwise
53  */
54 std::optional<bool> isUp(std::string ifname);
55 
56 /**
57  * Interface condition to wait for.
58  */
59 enum class WaitCondition {
60     /**
61      * Interface is present (but not necessarily up).
62      */
63     PRESENT,
64 
65     /**
66      * Interface is up.
67      */
68     PRESENT_AND_UP,
69 
70     /**
71      * Interface is up and with IPv4 address configured.
72      */
73     PRESENT_AND_IPV4,
74 
75     /**
76      * Interface is down or not present (disconnected) at all.
77      */
78     DOWN_OR_GONE,
79 };
80 
81 enum class Quantifier {
82     ALL_OF,
83     ANY_OF,
84 };
85 
86 /**
87  * Listens for interface changes until anticipated condition takes place.
88  *
89  * \param ifnames List of interfaces to watch for.
90  * \param cnd Awaited condition.
91  * \param quant Whether all interfaces need to satisfy the condition or just one satistying
92  *        interface should stop the wait.
93  * \return name of one interface that satisfied the condition
94  */
95 std::optional<std::string> waitFor(std::set<std::string> ifnames, WaitCondition cnd,
96                                    Quantifier quant = Quantifier::ALL_OF);
97 
98 /**
99  * Brings network interface up.
100  *
101  * \param ifname Interface to bring up
102  * \return true in case of success, false otherwise
103  */
104 bool up(std::string ifname);
105 
106 /**
107  * Brings network interface down.
108  *
109  * \param ifname Interface to bring down
110  * \return true in case of success, false otherwise
111  */
112 bool down(std::string ifname);
113 
114 /**
115  * Adds virtual link.
116  *
117  * \param dev the name of the new virtual device
118  * \param type the type of the new device
119  * \return true in case of success, false otherwise
120  */
121 bool add(std::string dev, std::string type);
122 
123 /**
124  * Deletes virtual link.
125  *
126  * \param dev the name of the device to remove
127  * \return true in case of success, false otherwise
128  */
129 bool del(std::string dev);
130 
131 /**
132  * Fetches interface's hardware address.
133  *
134  * \param ifname Interface name
135  * \return Hardware address (MAC address) or nullopt if the lookup failed
136  */
137 std::optional<hwaddr_t> getHwAddr(const std::string& ifname);
138 
139 /**
140  * Changes interface's hardware address.
141  *
142  * \param ifname Interface name
143  * \param hwaddr New hardware address to set
144  */
145 bool setHwAddr(const std::string& ifname, hwaddr_t hwaddr);
146 
147 }  // namespace android::netdevice
148 
149 bool operator==(const android::netdevice::hwaddr_t lhs, const unsigned char rhs[ETH_ALEN]);
150