blob: 39ec65dc45680814e46c86266a6b0167332b4f06 [file] [log] [blame]
Hongguang Chen4d0df882020-04-21 20:58:04 -07001/*
2 * Copyright (C) 2020 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 <android-base/logging.h>
18#include <android-base/properties.h>
19#include <android-base/strings.h>
20#include <android-base/unique_fd.h>
21#include <arpa/inet.h>
22#include <ifaddrs.h>
23#include <linux/if.h>
24#include <string.h>
25#include <sys/ioctl.h>
26#include <sys/socket.h>
27#include <sys/types.h>
28
29#include "recovery_ui/device.h"
30#include "recovery_ui/ethernet_ui.h"
31
32class EthernetDevice : public Device {
33 public:
34 explicit EthernetDevice(EthernetRecoveryUI* ui);
35
36 void PreRecovery() override;
37 void PreFastboot() override;
38
39 private:
40 int SetInterfaceFlags(const unsigned set, const unsigned clr);
41 void SetTitleIPv6LinkLocalAddress(const bool interface_up);
42
43 android::base::unique_fd ctl_sock_;
44 static const std::string interface;
45};
46
47const std::string EthernetDevice::interface = "eth0";
48
49EthernetDevice::EthernetDevice(EthernetRecoveryUI* ui)
50 : Device(ui), ctl_sock_(socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0)) {
51 if (ctl_sock_ < 0) {
52 PLOG(ERROR) << "Failed to open socket";
53 }
54}
55
56void EthernetDevice::PreRecovery() {
57 SetInterfaceFlags(0, IFF_UP);
58 SetTitleIPv6LinkLocalAddress(false);
59}
60
61void EthernetDevice::PreFastboot() {
62 android::base::SetProperty("fastbootd.protocol", "tcp");
63
64 if (SetInterfaceFlags(IFF_UP, 0) < 0) {
65 LOG(ERROR) << "Failed to bring up interface";
66 return;
67 }
68
69 SetTitleIPv6LinkLocalAddress(true);
70}
71
72int EthernetDevice::SetInterfaceFlags(const unsigned set, const unsigned clr) {
73 struct ifreq ifr;
74
75 if (ctl_sock_ < 0) {
76 return -1;
77 }
78
79 memset(&ifr, 0, sizeof(struct ifreq));
80 strncpy(ifr.ifr_name, interface.c_str(), IFNAMSIZ);
81 ifr.ifr_name[IFNAMSIZ - 1] = 0;
82
83 if (ioctl(ctl_sock_, SIOCGIFFLAGS, &ifr) < 0) {
84 PLOG(ERROR) << "Failed to get interface active flags";
85 return -1;
86 }
87 ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set;
88
89 if (ioctl(ctl_sock_, SIOCSIFFLAGS, &ifr) < 0) {
90 PLOG(ERROR) << "Failed to set interface active flags";
91 return -1;
92 }
93
94 return 0;
95}
96
97void EthernetDevice::SetTitleIPv6LinkLocalAddress(const bool interface_up) {
98 auto recovery_ui = reinterpret_cast<EthernetRecoveryUI*>(GetUI());
99 if (!interface_up) {
100 recovery_ui->SetIPv6LinkLocalAddress();
101 return;
102 }
103
104 struct ifaddrs* ifaddr;
105 if (getifaddrs(&ifaddr) == -1) {
106 PLOG(ERROR) << "Failed to get interface addresses";
107 recovery_ui->SetIPv6LinkLocalAddress();
108 return;
109 }
110
111 std::unique_ptr<struct ifaddrs, decltype(&freeifaddrs)> guard{ ifaddr, freeifaddrs };
112 for (struct ifaddrs* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
113 if (ifa->ifa_addr->sa_family != AF_INET6 || interface != ifa->ifa_name) {
114 continue;
115 }
116
117 auto current_addr = reinterpret_cast<struct sockaddr_in6*>(ifa->ifa_addr);
118 if (!IN6_IS_ADDR_LINKLOCAL(&(current_addr->sin6_addr))) {
119 continue;
120 }
121
122 char addrstr[INET6_ADDRSTRLEN];
123 inet_ntop(AF_INET6, reinterpret_cast<const void*>(&current_addr->sin6_addr), addrstr,
124 INET6_ADDRSTRLEN);
125 LOG(INFO) << "Our IPv6 link-local address is " << addrstr;
126 recovery_ui->SetIPv6LinkLocalAddress(addrstr);
127 return;
128 }
129
130 recovery_ui->SetIPv6LinkLocalAddress();
131}
132
133// -----------------------------------------------------------------------------------------
134Device* make_device() {
135 return new EthernetDevice(new EthernetRecoveryUI);
136}