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 #include <libnetdevice/libnetdevice.h>
18
19 #include "common.h"
20 #include "ifreqs.h"
21
22 #include <android-base/logging.h>
23 #include <libnl++/MessageFactory.h>
24 #include <libnl++/Socket.h>
25
26 #include <linux/can.h>
27 #include <linux/rtnetlink.h>
28 #include <net/if.h>
29
30 #include <algorithm>
31 #include <iterator>
32 #include <sstream>
33
34 namespace android::netdevice {
35
useSocketDomain(int domain)36 void useSocketDomain(int domain) {
37 ifreqs::socketDomain = domain;
38 }
39
exists(std::string ifname)40 bool exists(std::string ifname) {
41 return nametoindex(ifname) != 0;
42 }
43
up(std::string ifname)44 bool up(std::string ifname) {
45 auto ifr = ifreqs::fromName(ifname);
46 if (!ifreqs::send(SIOCGIFFLAGS, ifr)) return false;
47 ifr.ifr_flags |= IFF_UP;
48 return ifreqs::send(SIOCSIFFLAGS, ifr);
49 }
50
down(std::string ifname)51 bool down(std::string ifname) {
52 auto ifr = ifreqs::fromName(ifname);
53 if (!ifreqs::send(SIOCGIFFLAGS, ifr)) return false;
54 ifr.ifr_flags &= ~IFF_UP;
55 return ifreqs::send(SIOCSIFFLAGS, ifr);
56 }
57
add(std::string dev,std::string type)58 bool add(std::string dev, std::string type) {
59 nl::MessageFactory<ifinfomsg> req(RTM_NEWLINK,
60 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK);
61 req.add(IFLA_IFNAME, dev);
62
63 {
64 auto linkinfo = req.addNested(IFLA_LINKINFO);
65 req.addBuffer(IFLA_INFO_KIND, type);
66 }
67
68 nl::Socket sock(NETLINK_ROUTE);
69 return sock.send(req) && sock.receiveAck(req);
70 }
71
del(std::string dev)72 bool del(std::string dev) {
73 nl::MessageFactory<ifinfomsg> req(RTM_DELLINK, NLM_F_REQUEST | NLM_F_ACK);
74 req.add(IFLA_IFNAME, dev);
75
76 nl::Socket sock(NETLINK_ROUTE);
77 return sock.send(req) && sock.receiveAck(req);
78 }
79
getHwAddr(const std::string & ifname)80 std::optional<hwaddr_t> getHwAddr(const std::string& ifname) {
81 auto ifr = ifreqs::fromName(ifname);
82 if (!ifreqs::send(SIOCGIFHWADDR, ifr)) return std::nullopt;
83
84 hwaddr_t hwaddr;
85 memcpy(hwaddr.data(), ifr.ifr_hwaddr.sa_data, hwaddr.size());
86 return hwaddr;
87 }
88
setHwAddr(const std::string & ifname,hwaddr_t hwaddr)89 bool setHwAddr(const std::string& ifname, hwaddr_t hwaddr) {
90 auto ifr = ifreqs::fromName(ifname);
91
92 // fetch sa_family
93 if (!ifreqs::send(SIOCGIFHWADDR, ifr)) return false;
94
95 memcpy(ifr.ifr_hwaddr.sa_data, hwaddr.data(), hwaddr.size());
96 return ifreqs::send(SIOCSIFHWADDR, ifr);
97 }
98
isUp(std::string ifname)99 std::optional<bool> isUp(std::string ifname) {
100 auto ifr = ifreqs::fromName(ifname);
101 if (!ifreqs::send(SIOCGIFFLAGS, ifr)) return std::nullopt;
102 return ifr.ifr_flags & IFF_UP;
103 }
104
hasIpv4(std::string ifname)105 static bool hasIpv4(std::string ifname) {
106 auto ifr = ifreqs::fromName(ifname);
107 switch (ifreqs::trySend(SIOCGIFADDR, ifr)) {
108 case 0:
109 return true;
110 case EADDRNOTAVAIL:
111 case ENODEV:
112 return false;
113 default:
114 PLOG(WARNING) << "Failed checking IPv4 address";
115 return false;
116 }
117 }
118
119 struct WaitState {
120 bool present;
121 bool up;
122 bool hasIpv4Addr;
123
satisfiedandroid::netdevice::WaitState124 bool satisfied(WaitCondition cnd) const {
125 switch (cnd) {
126 case WaitCondition::PRESENT:
127 return present;
128 case WaitCondition::PRESENT_AND_UP:
129 return present && up;
130 case WaitCondition::PRESENT_AND_IPV4:
131 return present && up && hasIpv4Addr;
132 case WaitCondition::DOWN_OR_GONE:
133 return !present || !up;
134 }
135 }
136 };
137
toString(WaitCondition cnd)138 static std::string toString(WaitCondition cnd) {
139 switch (cnd) {
140 case WaitCondition::PRESENT:
141 return "become present";
142 case WaitCondition::PRESENT_AND_UP:
143 return "come up";
144 case WaitCondition::PRESENT_AND_IPV4:
145 return "get IPv4 address";
146 case WaitCondition::DOWN_OR_GONE:
147 return "go down";
148 }
149 }
150
toString(Quantifier quant)151 static std::string toString(Quantifier quant) {
152 switch (quant) {
153 case Quantifier::ALL_OF:
154 return "all of";
155 case Quantifier::ANY_OF:
156 return "any of";
157 }
158 }
159
toString(const std::set<std::string> & ifnames)160 static std::string toString(const std::set<std::string>& ifnames) {
161 std::stringstream ss;
162 std::copy(ifnames.begin(), ifnames.end(), std::ostream_iterator<std::string>(ss, ","));
163 auto str = ss.str();
164 str.pop_back();
165 return str;
166 }
167
waitFor(std::set<std::string> ifnames,WaitCondition cnd,Quantifier quant)168 std::optional<std::string> waitFor(std::set<std::string> ifnames, WaitCondition cnd,
169 Quantifier quant) {
170 nl::Socket sock(NETLINK_ROUTE, 0, RTMGRP_LINK | RTMGRP_IPV4_IFADDR);
171
172 using StatesMap = std::map<std::string, WaitState>;
173 StatesMap states = {};
174 for (const auto ifname : ifnames) {
175 const auto present = exists(ifname);
176 const auto up = present && isUp(ifname).value_or(false);
177 const auto hasIpv4Addr = present && hasIpv4(ifname);
178 states[ifname] = {present, up, hasIpv4Addr};
179 }
180
181 const auto mapConditionChecker = [cnd](const StatesMap::iterator::value_type& it) {
182 return it.second.satisfied(cnd);
183 };
184 const auto isFullySatisfied = [&states, quant,
185 mapConditionChecker]() -> std::optional<std::string> {
186 if (quant == Quantifier::ALL_OF) {
187 if (!std::all_of(states.begin(), states.end(), mapConditionChecker)) return {};
188 return states.begin()->first;
189 } else { // Quantifier::ANY_OF
190 const auto it = std::find_if(states.begin(), states.end(), mapConditionChecker);
191 if (it == states.end()) return {};
192 return it->first;
193 }
194 };
195
196 if (const auto iface = isFullySatisfied()) return iface;
197
198 LOG(DEBUG) << "Waiting for " << toString(quant) << " " << toString(ifnames) << " to "
199 << toString(cnd);
200 for (const auto rawMsg : sock) {
201 if (const auto msg = nl::Message<ifinfomsg>::parse(rawMsg, {RTM_NEWLINK, RTM_DELLINK});
202 msg.has_value()) {
203 // Interface added / removed
204 const auto ifname = msg->attributes.get<std::string>(IFLA_IFNAME);
205 if (ifnames.count(ifname) == 0) continue;
206
207 auto& state = states[ifname];
208 state.present = (msg->header.nlmsg_type != RTM_DELLINK);
209 state.up = state.present && (msg->data.ifi_flags & IFF_UP) != 0;
210 if (!state.present) state.hasIpv4Addr = false;
211
212 } else if (const auto msg =
213 nl::Message<ifaddrmsg>::parse(rawMsg, {RTM_NEWADDR, RTM_DELADDR});
214 msg.has_value()) {
215 // Address added / removed
216 const auto ifname = msg->attributes.get<std::string>(IFLA_IFNAME);
217 if (ifnames.count(ifname) == 0) continue;
218
219 if (msg->header.nlmsg_type == RTM_NEWADDR) {
220 states[ifname].hasIpv4Addr = true;
221 } else {
222 // instead of tracking which one got deleted, let's just ask
223 states[ifname].hasIpv4Addr = hasIpv4(ifname);
224 }
225 }
226
227 if (const auto iface = isFullySatisfied()) {
228 LOG(DEBUG) << "Finished waiting for " << toString(quant) << " " << toString(ifnames)
229 << " to " << toString(cnd);
230 return iface;
231 }
232 }
233 LOG(FATAL) << "Can't read Netlink socket";
234 return {};
235 }
236
237 } // namespace android::netdevice
238
operator ==(const android::netdevice::hwaddr_t lhs,const unsigned char rhs[ETH_ALEN])239 bool operator==(const android::netdevice::hwaddr_t lhs, const unsigned char rhs[ETH_ALEN]) {
240 static_assert(lhs.size() == ETH_ALEN);
241 return 0 == memcmp(lhs.data(), rhs, lhs.size());
242 }
243