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