Make the network prefix configurable
BUG: 70724395
Test: Local build and boot with other patches
Change-Id: I91a80063faee70ca9b7083112ee6525bd51d2036
diff --git a/common/libs/net/netlink_request.cpp b/common/libs/net/netlink_request.cpp
index ff5b9e9..4616aa2 100644
--- a/common/libs/net/netlink_request.cpp
+++ b/common/libs/net/netlink_request.cpp
@@ -98,10 +98,10 @@
if_info->ifi_change = IFF_UP;
}
-void NetlinkRequest::AddAddrInfo(int32_t if_index) {
+void NetlinkRequest::AddAddrInfo(int32_t if_index, int prefix_len) {
ifaddrmsg* ad_info = Reserve<ifaddrmsg>();
ad_info->ifa_family = AF_INET;
- ad_info->ifa_prefixlen = 24;
+ ad_info->ifa_prefixlen = prefix_len;
ad_info->ifa_flags = IFA_F_PERMANENT | IFA_F_SECONDARY;
ad_info->ifa_scope = 0;
ad_info->ifa_index = if_index;
diff --git a/common/libs/net/netlink_request.h b/common/libs/net/netlink_request.h
index 953ea00..1fb5960 100644
--- a/common/libs/net/netlink_request.h
+++ b/common/libs/net/netlink_request.h
@@ -57,8 +57,7 @@
void AddIfInfo(int32_t if_index, bool is_operational);
// Add an address info to a specific interface.
- // This method assumes the prefix length for address info is 24.
- void AddAddrInfo(int32_t if_index);
+ void AddAddrInfo(int32_t if_index, int prefix_len = 24);
// Creates new list.
// List mimmic recursive structures in a flat, contiuous representation.
diff --git a/common/libs/net/network_interface.h b/common/libs/net/network_interface.h
index 4005f00..2f4430d 100644
--- a/common/libs/net/network_interface.h
+++ b/common/libs/net/network_interface.h
@@ -47,6 +47,10 @@
return name_;
}
+ int PrefixLength() const {
+ return prefix_len_;
+ }
+
// Set operational state of the network interface (ie. whether interface is
// up).
NetworkInterface& SetOperational(bool is_operational) {
@@ -77,6 +81,12 @@
return *this;
}
+ // Set IPv4 prefix length
+ NetworkInterface& SetPrefixLength(int len) {
+ prefix_len_ = len;
+ return *this;
+ }
+
// Get IPv4 broadcast address of the network interface.
const std::string& BroadcastAddress() const {
return bc_address_;
@@ -94,6 +104,8 @@
std::string ip_address_;
// IPv4 broadcast address of this interface.
std::string bc_address_;
+ // IPv4 prefix (aka netmask. 0 means use the default)
+ int prefix_len_ = 24;
NetworkInterface(const NetworkInterface&);
NetworkInterface& operator= (const NetworkInterface&);
diff --git a/common/libs/net/network_interface_manager.cpp b/common/libs/net/network_interface_manager.cpp
index 2e4d43f..32d5583 100644
--- a/common/libs/net/network_interface_manager.cpp
+++ b/common/libs/net/network_interface_manager.cpp
@@ -43,9 +43,10 @@
NetlinkRequest BuildAddrRequest(
const NetworkInterface& interface) {
NetlinkRequest request(RTM_NEWADDR, 0);
- request.AddAddrInfo(interface.Index());
- request.AddInt32(IFA_LOCAL, inet_addr(interface.Address().c_str()));
- request.AddInt32(IFA_ADDRESS, inet_addr(interface.Address().c_str()));
+ request.AddAddrInfo(interface.Index(), interface.PrefixLength());
+ in_addr_t address{inet_addr(interface.Address().c_str())};
+ request.AddInt32(IFA_LOCAL, address);
+ request.AddInt32(IFA_ADDRESS, address);
request.AddInt32(IFA_BROADCAST,
inet_addr(interface.BroadcastAddress().c_str()));
@@ -97,4 +98,3 @@
}
} // namespace cvd
-