1 /*
2  * Copyright (C) 2018 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 <linux/rtnetlink.h>
18 #include <net/if.h>
19 
20 #include <array>
21 #include <cstdlib>
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 
26 #include "android-base/logging.h"
27 #include <cutils/properties.h>
28 #include <gflags/gflags.h>
29 
30 #include "common/libs/net/netlink_client.h"
31 #include "common/libs/net/netlink_request.h"
32 #include "common/libs/net/network_interface.h"
33 #include "common/libs/net/network_interface_manager.h"
34 
35 DEFINE_string(mac_address, "", "mac address to use for wlan0");
36 
str_to_mac(const std::string & mac_str)37 static std::array<unsigned char, 6> str_to_mac(const std::string& mac_str) {
38   std::array<unsigned char, 6> mac;
39   std::istringstream stream(mac_str);
40   for (int i = 0; i < 6; i++) {
41     int num;
42     stream >> std::hex >> num;
43     mac[i] = num;
44     stream.get();
45   }
46   return mac;
47 }
48 
49 // TODO(schuffelen): Merge this with the ip_link_add binary.
CreateWifiWrapper(const std::string & source,const std::string & destination)50 int CreateWifiWrapper(const std::string& source,
51                       const std::string& destination) {
52   auto factory = cuttlefish::NetlinkClientFactory::Default();
53   std::unique_ptr<cuttlefish::NetlinkClient> nl(factory->New(NETLINK_ROUTE));
54 
55   LOG(INFO) << "Setting " << source << " mac address to " << FLAGS_mac_address;
56   int32_t index = if_nametoindex(source.c_str());
57   // Setting the address is available in RTM_SETLINK, but not RTM_NEWLINK.
58   // https://elixir.bootlin.com/linux/v5.4.44/source/net/core/rtnetlink.c#L2785
59   // https://elixir.bootlin.com/linux/v5.4.44/source/net/core/rtnetlink.c#L2460
60   // Setting the address seems to work better on the underlying ethernet device,
61   // and this mac address is inherited by the virt_wifi device.
62   cuttlefish::NetlinkRequest fix_mac_request(
63       RTM_SETLINK, NLM_F_REQUEST|NLM_F_ACK|0x600);
64   fix_mac_request.Append(ifinfomsg {
65     .ifi_index = index,
66     .ifi_change = 0xFFFFFFFF,
67   });
68   fix_mac_request.AddMacAddress(str_to_mac(FLAGS_mac_address));
69   bool fix_mac = nl->Send(fix_mac_request);
70   if (!fix_mac) {
71     LOG(ERROR) << "setup_network: could not fix mac address";
72     return -5;
73   }
74 
75   // http://maz-programmersdiary.blogspot.com/2011/09/netlink-sockets.html
76   cuttlefish::NetlinkRequest link_add_request(RTM_NEWLINK,
77                                        NLM_F_REQUEST|NLM_F_ACK|0x600);
78   link_add_request.Append(ifinfomsg {
79     .ifi_change = 0xFFFFFFFF,
80   });
81   if (index == 0) {
82     LOG(ERROR) << "setup_network: invalid interface name '" << source << "'\n";
83     return -2;
84   }
85   link_add_request.AddString(IFLA_IFNAME, destination);
86   link_add_request.AddInt(IFLA_LINK, index);
87 
88   link_add_request.PushList(IFLA_LINKINFO);
89   link_add_request.AddString(IFLA_INFO_KIND, "virt_wifi");
90   link_add_request.PushList(IFLA_INFO_DATA);
91   link_add_request.PopList();
92   link_add_request.PopList();
93 
94   bool link_add_success = nl->Send(link_add_request);
95   if (!link_add_success) {
96     LOG(ERROR) << "setup_network: could not add link " << destination;
97     return -3;
98   }
99 
100   cuttlefish::NetlinkRequest bring_up_backing_request(RTM_SETLINK,
101                                                NLM_F_REQUEST|NLM_F_ACK|0x600);
102   bring_up_backing_request.Append(ifinfomsg {
103     .ifi_index = index,
104     .ifi_flags = IFF_UP,
105     .ifi_change = 0xFFFFFFFF,
106   });
107 
108   bool link_backing_up = nl->Send(bring_up_backing_request);
109   if (!link_backing_up) {
110     LOG(ERROR) << "setup_network: could not bring up backing " << source;
111     return -4;
112   }
113 
114   return 0;
115 }
116 
RenameNetwork(const std::string & name,const std::string & new_name)117 int RenameNetwork(const std::string& name, const std::string& new_name) {
118   static auto net_manager =
119       cuttlefish::NetworkInterfaceManager::New(cuttlefish::NetlinkClientFactory::Default());
120   auto connection = net_manager->Open(name, "ignore");
121   if (!connection) {
122     LOG(ERROR) << "setup_network: could not open " << name << " on device.";
123     return -1;
124   }
125   connection->SetName(new_name);
126   bool changes_applied = net_manager->ApplyChanges(*connection);
127   if (!changes_applied) {
128     LOG(ERROR) << "setup_network: can't rename " << name << " to " << new_name;
129     return -1;
130   }
131   return 0;
132 }
133 
main(int argc,char ** argv)134 int main(int argc, char** argv) {
135   char wifi_address[PROPERTY_VALUE_MAX + 1];
136   property_get("ro.boot.wifi_mac_address", wifi_address, "");
137 
138   SetCommandLineOptionWithMode("mac_address", wifi_address,
139                                google::FlagSettingMode::SET_FLAGS_DEFAULT);
140 
141   gflags::ParseCommandLineFlags(&argc, &argv, true);
142 
143   int renamed_eth0 = RenameNetwork("eth0", "buried_eth0");
144   if (renamed_eth0 != 0) {
145     return renamed_eth0;
146   }
147   return CreateWifiWrapper("buried_eth0", "wlan0");
148 }
149