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