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 down or not present (disconnected) at all. 72 */ 73 DOWN_OR_GONE, 74 }; 75 76 /** 77 * Listens for interface changes until anticipated condition takes place. 78 * 79 * \param ifnames List of interfaces to watch for. 80 * \param cnd Awaited condition. 81 * \param allOf true if all interfaces need to satisfy the condition, false if only one satistying 82 * interface should stop the wait. 83 */ 84 void waitFor(std::set<std::string> ifnames, WaitCondition cnd, bool allOf = true); 85 86 /** 87 * Brings network interface up. 88 * 89 * \param ifname Interface to bring up 90 * \return true in case of success, false otherwise 91 */ 92 bool up(std::string ifname); 93 94 /** 95 * Brings network interface down. 96 * 97 * \param ifname Interface to bring down 98 * \return true in case of success, false otherwise 99 */ 100 bool down(std::string ifname); 101 102 /** 103 * Adds virtual link. 104 * 105 * \param dev the name of the new virtual device 106 * \param type the type of the new device 107 * \return true in case of success, false otherwise 108 */ 109 bool add(std::string dev, std::string type); 110 111 /** 112 * Deletes virtual link. 113 * 114 * \param dev the name of the device to remove 115 * \return true in case of success, false otherwise 116 */ 117 bool del(std::string dev); 118 119 /** 120 * Fetches interface's hardware address. 121 * 122 * \param ifname Interface name 123 * \return Hardware address (MAC address) or nullopt if the lookup failed 124 */ 125 std::optional<hwaddr_t> getHwAddr(const std::string& ifname); 126 127 /** 128 * Changes interface's hardware address. 129 * 130 * \param ifname Interface name 131 * \param hwaddr New hardware address to set 132 */ 133 bool setHwAddr(const std::string& ifname, hwaddr_t hwaddr); 134 135 } // namespace android::netdevice 136 137 bool operator==(const android::netdevice::hwaddr_t lhs, const unsigned char rhs[ETH_ALEN]); 138