1 /*
2  * Copyright (C) 2017 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 #include "common/libs/net/network_interface_manager.h"
17 
18 #include <stddef.h>
19 #include <stdint.h>
20 
21 #include <arpa/inet.h>
22 #include <linux/if_addr.h>
23 #include <linux/if_link.h>
24 #include <linux/netlink.h>
25 #include <linux/rtnetlink.h>
26 #include <net/if.h>
27 #include <netinet/in.h>
28 
29 #include <memory>
30 #include <ostream>
31 #include <string>
32 #include <utility>
33 
34 #include "android-base/logging.h"
35 #include "common/libs/net/netlink_client.h"
36 #include "common/libs/net/netlink_request.h"
37 #include "common/libs/net/network_interface.h"
38 
39 namespace cuttlefish {
40 namespace {
BuildLinkRequest(const NetworkInterface & interface)41 NetlinkRequest BuildLinkRequest(
42     const NetworkInterface& interface) {
43   NetlinkRequest request(RTM_SETLINK, 0);
44   request.AddIfInfo(interface.Index(), interface.IsOperational());
45   if (!interface.Name().empty()) {
46     request.AddString(IFLA_IFNAME, interface.Name());
47   }
48 
49   return request;
50 }
51 
BuildAddrRequest(const NetworkInterface & interface)52 NetlinkRequest BuildAddrRequest(
53     const NetworkInterface& interface) {
54   NetlinkRequest request(RTM_NEWADDR, 0);
55   request.AddAddrInfo(interface.Index(), interface.PrefixLength());
56   in_addr_t address{inet_addr(interface.Address().c_str())};
57   request.AddInt(IFA_LOCAL, address);
58   request.AddInt(IFA_ADDRESS, address);
59   request.AddInt(IFA_BROADCAST,
60                  inet_addr(interface.BroadcastAddress().c_str()));
61 
62   return request;
63 }
64 }  // namespace
65 
New(NetlinkClientFactory * nl_factory)66 std::unique_ptr<NetworkInterfaceManager> NetworkInterfaceManager::New(
67     NetlinkClientFactory* nl_factory) {
68   std::unique_ptr<NetworkInterfaceManager> mgr;
69 
70   if (nl_factory == NULL) {
71     nl_factory = NetlinkClientFactory::Default();
72   }
73 
74   auto client = nl_factory->New(NETLINK_ROUTE);
75   if (client) {
76     mgr.reset(new NetworkInterfaceManager(std::move(client)));
77   }
78 
79   return mgr;
80 }
81 
NetworkInterfaceManager(std::unique_ptr<NetlinkClient> nl_client)82 NetworkInterfaceManager::NetworkInterfaceManager(
83     std::unique_ptr<NetlinkClient> nl_client)
84     : nl_client_(std::move(nl_client)) {}
85 
Open(const std::string & if_name,const std::string & if_name_alt)86 std::unique_ptr<NetworkInterface> NetworkInterfaceManager::Open(
87     const std::string& if_name, const std::string& if_name_alt) {
88   std::unique_ptr<NetworkInterface> iface;
89   // NOTE: do not replace this code with an IOCTL call.
90   // On SELinux enabled Androids, RILD is not permitted to execute an IOCTL
91   // and this call will fail.
92   int32_t index = if_nametoindex(if_name.c_str());
93   if (index == 0) {
94     // Try the alternate name. This will be renamed to our preferred name
95     // by the kernel, because we specify IFLA_IFNAME, but open by index.
96     LOG(ERROR) << "Failed to get interface (" << if_name << ") index, "
97                << "trying alternate.";
98     index = if_nametoindex(if_name_alt.c_str());
99     if (index == 0) {
100       LOG(ERROR) << "Failed to get interface (" << if_name_alt << ") index.";
101       return iface;
102     }
103   }
104 
105   iface.reset(new NetworkInterface(index));
106   return iface;
107 }
108 
ApplyChanges(const NetworkInterface & iface)109 bool NetworkInterfaceManager::ApplyChanges(const NetworkInterface& iface) {
110   if (!nl_client_->Send(BuildLinkRequest(iface))) {
111     return false;
112   }
113   // Terminate immediately if interface is down.
114   if (!iface.IsOperational()) {
115     return true;
116   }
117   return nl_client_->Send(BuildAddrRequest(iface));
118 }
119 
120 }  // namespace cuttlefish
121