Userland: Set the mask of a network adapter with ifconfig (#1388)

A new IP address or a new network mask can be specified in the command
line arguments of ifconfig to replace the old values of a given network
adapter. Additionally, more information is being printed for each adapter.
This commit is contained in:
Marios Prokopakis 2020-03-11 22:30:41 +02:00 committed by GitHub
parent 4c9bb266df
commit 8cf962a102
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: sideshowbarker 2024-07-19 08:46:06 +09:00
6 changed files with 155 additions and 49 deletions

View File

@ -485,6 +485,14 @@ int IPv4Socket::ioctl(FileDescription&, unsigned request, unsigned arg)
adapter->set_ipv4_address(IPv4Address(((sockaddr_in&)ifr->ifr_addr).sin_addr.s_addr));
return 0;
case SIOCSIFNETMASK:
if (!Process::current->is_superuser())
return -EPERM;
if (ifr->ifr_addr.sa_family != AF_INET)
return -EAFNOSUPPORT;
adapter->set_ipv4_netmask(IPv4Address(((sockaddr_in&)ifr->ifr_netmask).sin_addr.s_addr));
return 0;
case SIOCGIFADDR:
if (!Process::current->validate_write_typed(ifr))
return -EFAULT;

View File

@ -501,6 +501,7 @@ struct ifreq {
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
struct sockaddr ifru_netmask;
struct sockaddr ifru_hwaddr;
short ifru_flags;
int ifru_metric;
@ -512,6 +513,7 @@ struct ifreq {
#define ifr_addr ifr_ifru.ifru_addr // address
#define ifr_dstaddr ifr_ifru.ifru_dstaddr // other end of p-to-p link
#define ifr_broadaddr ifr_ifru.ifru_broadaddr // broadcast address
#define ifr_netmask ifr_ifru.ifru_netmask // network mask
#define ifr_flags ifr_ifru.ifru_flags // flags
#define ifr_metric ifr_ifru.ifru_metric // metric
#define ifr_mtu ifr_ifru.ifru_metric // mtu (overload)

View File

@ -38,6 +38,7 @@ struct ifreq {
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
struct sockaddr ifru_netmask;
struct sockaddr ifru_hwaddr;
short ifru_flags;
int ifru_metric;
@ -49,6 +50,7 @@ struct ifreq {
#define ifr_addr ifr_ifru.ifru_addr // address
#define ifr_dstaddr ifr_ifru.ifru_dstaddr // other end of p-to-p link
#define ifr_broadaddr ifr_ifru.ifru_broadaddr // broadcast address
#define ifr_netmask ifr_ifru.ifru_netmask // network mask
#define ifr_flags ifr_ifru.ifru_flags // flags
#define ifr_metric ifr_ifru.ifru_metric // metric
#define ifr_mtu ifr_ifru.ifru_metric // mtu (overload)

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2020, Marios Prokopakis <mariosprokopakis@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <sys/socket.h>
struct rtentry {
struct sockaddr* rt_gateway;
/* FIXME: complete the struct */
};

View File

@ -62,4 +62,6 @@ enum IOCtlNumber {
SIOCSIFADDR,
SIOCGIFADDR,
SIOCGIFHWADDR,
SIOCADDRT,
SIOCSIFNETMASK
};

View File

@ -28,6 +28,7 @@
#include <AK/JsonObject.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <net/if.h>
#include <netinet/in.h>
@ -49,12 +50,68 @@ String si_bytes(unsigned bytes)
int main(int argc, char** argv)
{
if (argc == 3) {
String ifname = argv[1];
auto address = IPv4Address::from_string(argv[2]);
const char* value_ipv4 = nullptr;
const char* value_adapter = nullptr;
const char* value_gateway = nullptr;
const char* value_mask = nullptr;
Core::ArgsParser args_parser;
args_parser.add_option(value_ipv4, "Set the IP address of the selected network", "ipv4", 'i', "The new IP of the network");
args_parser.add_option(value_adapter, "Select a specific network adapter to configure", "adapter", 'a', "The name of a network adapter");
args_parser.add_option(value_gateway, "Set the default gateway of the selected network", "gateway", 'g', "The new IP of the gateway");
args_parser.add_option(value_mask, "Set the network mask of the selected network", "mask", 'm', "The new network mask");
args_parser.parse(argc, argv);
if (!value_ipv4 && !value_adapter && !value_gateway && !value_mask) {
auto file = Core::File::construct("/proc/net/adapters");
if (!file->open(Core::IODevice::ReadOnly)) {
fprintf(stderr, "Error: %s\n", file->error_string());
return 1;
}
auto file_contents = file->read_all();
auto json = JsonValue::from_string(file_contents).as_array();
json.for_each([](auto& value) {
auto if_object = value.as_object();
auto name = if_object.get("name").to_string();
auto class_name = if_object.get("class_name").to_string();
auto mac_address = if_object.get("mac_address").to_string();
auto ipv4_address = if_object.get("ipv4_address").to_string();
auto gateway = if_object.get("ipv4_gateway").to_string();
auto netmask = if_object.get("ipv4_netmask").to_string();
auto packets_in = if_object.get("packets_in").to_u32();
auto bytes_in = if_object.get("bytes_in").to_u32();
auto packets_out = if_object.get("packets_out").to_u32();
auto bytes_out = if_object.get("bytes_out").to_u32();
auto mtu = if_object.get("mtu").to_u32();
printf("%s:\n", name.characters());
printf("\tmac: %s\n", mac_address.characters());
printf("\tipv4: %s\n", ipv4_address.characters());
printf("\tnetmask: %s\n", netmask.characters());
printf("\tgateway: %s\n", gateway.characters());
printf("\tclass: %s\n", class_name.characters());
printf("\tRX: %u packets %u bytes (%s)\n", packets_in, bytes_in, si_bytes(bytes_in).characters());
printf("\tTX: %u packets %u bytes (%s)\n", packets_out, bytes_out, si_bytes(bytes_out).characters());
printf("\tMTU: %u\n", mtu);
printf("\n");
});
} else {
if (!value_adapter) {
fprintf(stderr, "No network adapter was specified.\n");
return 1;
}
String ifname = value_adapter;
if (value_ipv4) {
auto address = IPv4Address::from_string(value_ipv4);
if (!address.has_value()) {
fprintf(stderr, "Invalid IPv4 address: '%s'\n", argv[2]);
fprintf(stderr, "Invalid IPv4 address: '%s'\n", value_ipv4);
return 1;
}
@ -76,39 +133,41 @@ int main(int argc, char** argv)
perror("ioctl(SIOCSIFADDR)");
return 1;
}
return 0;
}
auto file = Core::File::construct("/proc/net/adapters");
if (!file->open(Core::IODevice::ReadOnly)) {
fprintf(stderr, "Error: %s\n", file->error_string());
if (value_mask) {
auto address = IPv4Address::from_string(value_mask);
if (!address.has_value()) {
fprintf(stderr, "Invalid IPv4 mask: '%s'\n", value_mask);
return 1;
}
auto file_contents = file->read_all();
auto json = JsonValue::from_string(file_contents).as_array();
json.for_each([](auto& value) {
auto if_object = value.as_object();
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd < 0) {
perror("socket");
return 1;
}
auto name = if_object.get("name").to_string();
auto class_name = if_object.get("class_name").to_string();
auto mac_address = if_object.get("mac_address").to_string();
auto ipv4_address = if_object.get("ipv4_address").to_string();
auto packets_in = if_object.get("packets_in").to_u32();
auto bytes_in = if_object.get("bytes_in").to_u32();
auto packets_out = if_object.get("packets_out").to_u32();
auto bytes_out = if_object.get("bytes_out").to_u32();
auto mtu = if_object.get("mtu").to_u32();
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
printf("%s:\n", name.characters());
printf(" mac: %s\n", mac_address.characters());
printf(" ipv4: %s\n", ipv4_address.characters());
printf(" class: %s\n", class_name.characters());
printf(" RX: %u packets %u bytes (%s)\n", packets_in, bytes_in, si_bytes(bytes_in).characters());
printf(" TX: %u packets %u bytes (%s)\n", packets_out, bytes_out, si_bytes(bytes_out).characters());
printf(" MTU: %u\n", mtu);
printf("\n");
});
strncpy(ifr.ifr_name, ifname.characters(), IFNAMSIZ);
ifr.ifr_netmask.sa_family = AF_INET;
((sockaddr_in&)ifr.ifr_netmask).sin_addr.s_addr = address.value().to_in_addr_t();
int rc = ioctl(fd, SIOCSIFNETMASK, &ifr);
if (rc < 0) {
perror("ioctl(SIOCSIFNETMASK)");
return 1;
}
}
if (value_gateway) {
// ioctl does not support rtentry yet
fprintf(stderr, "Changing the gateway is not supported yet\n");
return 1;
}
}
return 0;
}