1 /*
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_device.h"
31 #include "recovery_ui/ethernet_ui.h"
32
33 // Android TV defaults to eth0 for it's interface
EthernetDevice(EthernetRecoveryUI * ui)34 EthernetDevice::EthernetDevice(EthernetRecoveryUI* ui) : EthernetDevice(ui, "eth0") {}
35
36 // Allow future users to define the interface as they prefer
EthernetDevice(EthernetRecoveryUI * ui,std::string interface)37 EthernetDevice::EthernetDevice(EthernetRecoveryUI* ui, std::string interface)
38 : Device(ui), ctl_sock_(socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0)), interface_(interface) {
39 if (ctl_sock_ < 0) {
40 PLOG(ERROR) << "Failed to open socket";
41 }
42 }
43
PreRecovery()44 void EthernetDevice::PreRecovery() {
45 SetInterfaceFlags(0, IFF_UP);
46 SetTitleIPv6LinkLocalAddress(false);
47 }
48
PreFastboot()49 void EthernetDevice::PreFastboot() {
50 android::base::SetProperty("fastbootd.protocol", "tcp");
51
52 if (SetInterfaceFlags(IFF_UP, 0) < 0) {
53 LOG(ERROR) << "Failed to bring up interface";
54 return;
55 }
56
57 SetTitleIPv6LinkLocalAddress(true);
58 }
59
SetInterfaceFlags(const unsigned set,const unsigned clr)60 int EthernetDevice::SetInterfaceFlags(const unsigned set, const unsigned clr) {
61 struct ifreq ifr;
62
63 if (ctl_sock_ < 0) {
64 return -1;
65 }
66
67 memset(&ifr, 0, sizeof(struct ifreq));
68 strncpy(ifr.ifr_name, interface_.c_str(), IFNAMSIZ);
69 ifr.ifr_name[IFNAMSIZ - 1] = 0;
70
71 if (ioctl(ctl_sock_, SIOCGIFFLAGS, &ifr) < 0) {
72 PLOG(ERROR) << "Failed to get interface active flags";
73 return -1;
74 }
75 ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set;
76
77 if (ioctl(ctl_sock_, SIOCSIFFLAGS, &ifr) < 0) {
78 PLOG(ERROR) << "Failed to set interface active flags";
79 return -1;
80 }
81
82 return 0;
83 }
84
SetTitleIPv6LinkLocalAddress(const bool interface_up)85 void EthernetDevice::SetTitleIPv6LinkLocalAddress(const bool interface_up) {
86 auto recovery_ui = reinterpret_cast<EthernetRecoveryUI*>(GetUI());
87 if (!interface_up) {
88 recovery_ui->SetIPv6LinkLocalAddress();
89 return;
90 }
91
92 struct ifaddrs* ifaddr;
93 if (getifaddrs(&ifaddr) == -1) {
94 PLOG(ERROR) << "Failed to get interface addresses";
95 recovery_ui->SetIPv6LinkLocalAddress();
96 return;
97 }
98
99 std::unique_ptr<struct ifaddrs, decltype(&freeifaddrs)> guard{ ifaddr, freeifaddrs };
100 for (struct ifaddrs* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
101 if (ifa->ifa_addr->sa_family != AF_INET6 || interface_ != ifa->ifa_name) {
102 continue;
103 }
104
105 auto current_addr = reinterpret_cast<struct sockaddr_in6*>(ifa->ifa_addr);
106 if (!IN6_IS_ADDR_LINKLOCAL(&(current_addr->sin6_addr))) {
107 continue;
108 }
109
110 char addrstr[INET6_ADDRSTRLEN];
111 inet_ntop(AF_INET6, reinterpret_cast<const void*>(¤t_addr->sin6_addr), addrstr,
112 INET6_ADDRSTRLEN);
113 LOG(INFO) << "Our IPv6 link-local address is " << addrstr;
114 recovery_ui->SetIPv6LinkLocalAddress(addrstr);
115 return;
116 }
117
118 recovery_ui->SetIPv6LinkLocalAddress();
119 }
120