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