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_prefix, "", "mac prefix to use for wlan0");
36 DEFINE_string(interface, "eth2", "interface to create wlan wrapper on");
37
prefix_to_mac(const std::string & mac_prefix)38 static std::array<unsigned char, 6> prefix_to_mac(
39 const std::string& mac_prefix) {
40 std::array<unsigned char, 6> mac;
41 int macPrefix = stoi(mac_prefix);
42 mac[0] = 0x02;
43 mac[1] = (macPrefix >> CHAR_BIT) & 0xFF;
44 mac[2] = macPrefix & 0xFF;
45 return mac;
46 }
47
CreateWifiWrapper(const std::string & source,const std::string & destination)48 int CreateWifiWrapper(const std::string& source,
49 const std::string& destination) {
50 auto factory = cuttlefish::NetlinkClientFactory::Default();
51 std::unique_ptr<cuttlefish::NetlinkClient> nl(factory->New(NETLINK_ROUTE));
52
53 LOG(INFO) << "Setting " << source << " mac address based on "
54 << FLAGS_mac_prefix;
55 int32_t index = if_nametoindex(source.c_str());
56 // Setting the address is available in RTM_SETLINK, but not RTM_NEWLINK.
57 // https://elixir.bootlin.com/linux/v5.4.44/source/net/core/rtnetlink.c#L2785
58 // https://elixir.bootlin.com/linux/v5.4.44/source/net/core/rtnetlink.c#L2460
59 // Setting the address seems to work better on the underlying ethernet device,
60 // and this mac address is inherited by the virt_wifi device.
61 cuttlefish::NetlinkRequest fix_mac_request(
62 RTM_SETLINK, NLM_F_REQUEST|NLM_F_ACK|0x600);
63 fix_mac_request.Append(ifinfomsg {
64 .ifi_index = index,
65 .ifi_change = 0xFFFFFFFF,
66 });
67 fix_mac_request.AddMacAddress(prefix_to_mac(FLAGS_mac_prefix));
68 bool fix_mac = nl->Send(fix_mac_request);
69 if (!fix_mac) {
70 LOG(ERROR) << "setup_network: could not fix mac address";
71 return -5;
72 }
73
74 // http://maz-programmersdiary.blogspot.com/2011/09/netlink-sockets.html
75 cuttlefish::NetlinkRequest link_add_request(RTM_NEWLINK,
76 NLM_F_REQUEST|NLM_F_ACK|0x600);
77 link_add_request.Append(ifinfomsg {
78 .ifi_change = 0xFFFFFFFF,
79 });
80 if (index == 0) {
81 LOG(ERROR) << "setup_network: invalid interface name '" << source << "'\n";
82 return -2;
83 }
84 link_add_request.AddString(IFLA_IFNAME, destination);
85 link_add_request.AddInt(IFLA_LINK, index);
86
87 link_add_request.PushList(IFLA_LINKINFO);
88 link_add_request.AddString(IFLA_INFO_KIND, "virt_wifi");
89 link_add_request.PushList(IFLA_INFO_DATA);
90 link_add_request.PopList();
91 link_add_request.PopList();
92
93 bool link_add_success = nl->Send(link_add_request);
94 if (!link_add_success) {
95 LOG(ERROR) << "setup_network: could not add link " << destination;
96 return -3;
97 }
98
99 cuttlefish::NetlinkRequest bring_up_backing_request(RTM_SETLINK,
100 NLM_F_REQUEST|NLM_F_ACK|0x600);
101 bring_up_backing_request.Append(ifinfomsg {
102 .ifi_index = index,
103 .ifi_flags = IFF_UP,
104 .ifi_change = 0xFFFFFFFF,
105 });
106
107 bool link_backing_up = nl->Send(bring_up_backing_request);
108 if (!link_backing_up) {
109 LOG(ERROR) << "setup_network: could not bring up backing " << source;
110 return -4;
111 }
112
113 return 0;
114 }
115
RenameNetwork(const std::string & name,const std::string & new_name)116 int RenameNetwork(const std::string& name, const std::string& new_name) {
117 static auto net_manager =
118 cuttlefish::NetworkInterfaceManager::New(cuttlefish::NetlinkClientFactory::Default());
119 auto connection = net_manager->Open(name, "ignore");
120 if (!connection) {
121 LOG(ERROR) << "setup_network: could not open " << name << " on device.";
122 return -1;
123 }
124 connection->SetName(new_name);
125 bool changes_applied = net_manager->ApplyChanges(*connection);
126 if (!changes_applied) {
127 LOG(ERROR) << "setup_network: can't rename " << name << " to " << new_name;
128 return -1;
129 }
130 return 0;
131 }
132
main(int argc,char ** argv)133 int main(int argc, char** argv) {
134 char wifi_mac_prefix[PROPERTY_VALUE_MAX + 1];
135 property_get("ro.boot.wifi_mac_prefix", wifi_mac_prefix, "");
136
137 SetCommandLineOptionWithMode("mac_prefix", wifi_mac_prefix,
138 google::FlagSettingMode::SET_FLAGS_DEFAULT);
139
140 gflags::ParseCommandLineFlags(&argc, &argv, true);
141
142 int renamed_if = RenameNetwork(FLAGS_interface, "buried_" + FLAGS_interface);
143 if (renamed_if != 0) {
144 return renamed_if;
145 }
146 return CreateWifiWrapper("buried_" + FLAGS_interface, "wlan0");
147 }
148