1 /*
2  * Copyright (C) 2023 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/parseint.h>
18 
19 #include "host/libs/config/openwrt_args.h"
20 
21 namespace cuttlefish {
22 
23 namespace {
24 
getIpAddress(int c_class,int d_class)25 std::string getIpAddress(int c_class, int d_class) {
26   return "192.168." + std::to_string(c_class) + "." + std::to_string(d_class);
27 }
28 
29 }  // namespace
30 
OpenwrtArgsFromConfig(const CuttlefishConfig::InstanceSpecific & instance)31 std::unordered_map<std::string, std::string> OpenwrtArgsFromConfig(
32     const CuttlefishConfig::InstanceSpecific& instance) {
33   std::unordered_map<std::string, std::string> openwrt_args;
34   int instance_num;
35   if (!android::base::ParseInt(instance.id(), &instance_num, 1, 128)) {
36     return openwrt_args;
37   }
38   openwrt_args["webrtc_device_id"] = instance.webrtc_device_id();
39 
40   int c_class_base = (instance_num - 1) / 64;
41   int d_class_base = (instance_num - 1) % 64 * 4;
42 
43   // IP address for OpenWRT is pre-defined in init script of android-cuttlefish
44   // github repository by using tap interfaces created with the script.
45   // (github) base/debian/cuttlefish-base.cuttlefish-host-resources.init
46   // The command 'crosvm run' uses openwrt_args for passing the arguments into
47   // /proc/cmdline of OpenWRT instance.
48   // (AOSP) device/google/cuttlefish/host/commands/run_cvd/launch/open_wrt.cpp
49   // In OpenWRT instance, the script 0_default_config reads /proc/cmdline so
50   // that it can apply arguments defined here.
51   // (AOSP) external/openwrt-prebuilts/shared/uci-defaults/0_default_config
52   if (instance.use_bridged_wifi_tap()) {
53     openwrt_args["bridged_wifi_tap"] = "true";
54     openwrt_args["wan_gateway"] = getIpAddress(96, 1);
55     // TODO(seungjaeyoo) : Remove config after using DHCP server outside OpenWRT
56     // instead.
57     openwrt_args["wan_ipaddr"] = getIpAddress(96, d_class_base + 2);
58     openwrt_args["wan_broadcast"] = getIpAddress(96, d_class_base + 3);
59 
60   } else {
61     openwrt_args["bridged_wifi_tap"] = "false";
62     openwrt_args["wan_gateway"] =
63         getIpAddress(94 + c_class_base, d_class_base + 1);
64     openwrt_args["wan_ipaddr"] =
65         getIpAddress(94 + c_class_base, d_class_base + 2);
66     openwrt_args["wan_broadcast"] =
67         getIpAddress(94 + c_class_base, d_class_base + 3);
68   }
69 
70   return openwrt_args;
71 }
72 
73 }  // namespace cuttlefish
74