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.add(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 
103 struct WaitState {
104     bool present;
105     bool up;
106 
satisfiedandroid::netdevice::WaitState107     bool satisfied(WaitCondition cnd) const {
108         switch (cnd) {
109             case WaitCondition::PRESENT:
110                 if (present) return true;
111                 break;
112             case WaitCondition::PRESENT_AND_UP:
113                 if (present && up) return true;
114                 break;
115             case WaitCondition::DOWN_OR_GONE:
116                 if (!present || !up) return true;
117                 break;
118         }
119         return false;
120     }
121 };
122 
toString(WaitCondition cnd)123 static std::string toString(WaitCondition cnd) {
124     switch (cnd) {
125         case WaitCondition::PRESENT:
126             return "become present";
127         case WaitCondition::PRESENT_AND_UP:
128             return "come up";
129         case WaitCondition::DOWN_OR_GONE:
130             return "go down";
131     }
132 }
133 
toString(const std::set<std::string> & ifnames)134 static std::string toString(const std::set<std::string>& ifnames) {
135     std::stringstream ss;
136     std::copy(ifnames.begin(), ifnames.end(), std::ostream_iterator<std::string>(ss, ","));
137     auto str = ss.str();
138     str.pop_back();
139     return str;
140 }
141 
waitFor(std::set<std::string> ifnames,WaitCondition cnd,bool allOf)142 void waitFor(std::set<std::string> ifnames, WaitCondition cnd, bool allOf) {
143     nl::Socket sock(NETLINK_ROUTE, 0, RTMGRP_LINK);
144 
145     using StatesMap = std::map<std::string, WaitState>;
146     StatesMap states = {};
147     for (const auto ifname : ifnames) {
148         const auto present = exists(ifname);
149         const auto up = present && isUp(ifname).value_or(false);
150         states[ifname] = {present, up};
151     }
152 
153     const auto mapConditionChecker = [cnd](const StatesMap::iterator::value_type& it) {
154         return it.second.satisfied(cnd);
155     };
156     const auto isFullySatisfied = [&states, allOf, mapConditionChecker]() {
157         if (allOf) {
158             return std::all_of(states.begin(), states.end(), mapConditionChecker);
159         } else {
160             return std::any_of(states.begin(), states.end(), mapConditionChecker);
161         }
162     };
163 
164     if (isFullySatisfied()) return;
165 
166     LOG(DEBUG) << "Waiting for " << (allOf ? "" : "any of ") << toString(ifnames) << " to "
167                << toString(cnd);
168     for (const auto rawMsg : sock) {
169         const auto msg = nl::Message<ifinfomsg>::parse(rawMsg, {RTM_NEWLINK, RTM_DELLINK});
170         if (!msg.has_value()) continue;
171 
172         const auto ifname = msg->attributes.get<std::string>(IFLA_IFNAME);
173         if (ifnames.count(ifname) == 0) continue;
174 
175         const bool present = (msg->header.nlmsg_type != RTM_DELLINK);
176         const bool up = present && (msg->data.ifi_flags & IFF_UP) != 0;
177         states[ifname] = {present, up};
178 
179         if (isFullySatisfied()) {
180             LOG(DEBUG) << "Finished waiting for " << (allOf ? "" : "some of ") << toString(ifnames)
181                        << " to " << toString(cnd);
182             return;
183         }
184     }
185     LOG(FATAL) << "Can't read Netlink socket";
186 }
187 
188 }  // namespace android::netdevice
189 
operator ==(const android::netdevice::hwaddr_t lhs,const unsigned char rhs[ETH_ALEN])190 bool operator==(const android::netdevice::hwaddr_t lhs, const unsigned char rhs[ETH_ALEN]) {
191     static_assert(lhs.size() == ETH_ALEN);
192     return 0 == memcmp(lhs.data(), rhs, lhs.size());
193 }
194