mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
2ef6804ead
This change adds all the kernel parts of a network stack. The network stack is partial but implements many of the important parts. Add if(4) network interface abstraction. Network interfaces are registered in a global list that can be iterated and each assigned an unique integer identifier. Add reference counted packets with a cache that recycles recent packets. Add support for lo(4) loopback and ether(4) ethernet network interfaces. The /dev/lo0 loopback device is created automatically on boot. Add arp(4) address resolution protocol driver for translation of inet(4) network layer addresses into ether(4) link layer addresses. arp(4) entries are cached and evicted from the cache when needed or when the entry has not been used for a while. The cache is limited to 256 entries for now. Add ip(4) internet protocol version 4 support. IP fragmentation and options are not implemented yet. Add tcp(4) transmission control protocol sockets for a reliable transport layer protocol that provides a reliable byte stream connection between two hosts. The implementation is incomplete and does not yet implement out of band data, options, and high performance extensions. Add udp(4) user datagram protocol sockets for a connectionless transport layer that provides best-effort delivery of datagrams. Add ping(4) sockets for a best-effort delivery of echo datagrams. Change type of sa_family_t from unsigned short to uint16_t. Add --disable-network-drivers to the kernel(7) options and expose it with a bootloader menu. tix-iso-bootconfig can set this option by default. Import CRC32 code from libz for the Ethernet checksum. This is a compatible ABI change that adds features to socket(2) (AF_INET, IPPROTO_TCP, IPPROTO_UDP, IPPROTO_PING), the ioctls for if(4), socket options, and the lo0 loopback interface. This commit is based on work by Meisaka Yukara contributed as the commit bbf7f1e8a5238a2bd1fe8eb1d2cc5c9c2421e2c4. Almost no lines of this work remains in this final commit as it has been rewritten or refactored away over the years, see the individual file headers for which files contain remnants of this work. Co-authored-by: Meisaka Yukara <Meisaka.Yukara@gmail.com>
237 lines
6.4 KiB
C++
237 lines
6.4 KiB
C++
/*
|
|
* Copyright (c) 2015 Meisaka Yukara.
|
|
* Copyright (c) 2016, 2017, 2022 Jonas 'Sortie' Termansen.
|
|
*
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
* copyright notice and this permission notice appear in all copies.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*
|
|
* net/if.cpp
|
|
* Network Interface.
|
|
*/
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <limits.h>
|
|
#include <poll.h>
|
|
#include <stdarg.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <sortix/stat.h>
|
|
|
|
#include <sortix/kernel/addralloc.h>
|
|
#include <sortix/kernel/copy.h>
|
|
#include <sortix/kernel/descriptor.h>
|
|
#include <sortix/kernel/if.h>
|
|
#include <sortix/kernel/inode.h>
|
|
#include <sortix/kernel/ioctx.h>
|
|
#include <sortix/kernel/kthread.h>
|
|
#include <sortix/kernel/log.h>
|
|
#include <sortix/kernel/memorymanagement.h>
|
|
#include <sortix/kernel/packet.h>
|
|
#include <sortix/kernel/panic.h>
|
|
#include <sortix/kernel/pci.h>
|
|
#include <sortix/kernel/pci-mmio.h>
|
|
#include <sortix/kernel/refcount.h>
|
|
|
|
#include "arp.h"
|
|
|
|
namespace Sortix {
|
|
|
|
kthread_mutex_t netifs_lock = KTHREAD_MUTEX_INITIALIZER;
|
|
NetworkInterface** netifs = NULL;
|
|
size_t netifs_count = 0;
|
|
static size_t netifs_allocated = 0;
|
|
|
|
class NetworkInterfaceNode : public AbstractInode
|
|
{
|
|
public:
|
|
NetworkInterfaceNode(dev_t dev, uid_t owner, gid_t group, mode_t mode,
|
|
NetworkInterface* netif);
|
|
virtual ~NetworkInterfaceNode();
|
|
|
|
public:
|
|
virtual int ioctl(ioctx_t* ctx, int cmd, uintptr_t ptr);
|
|
virtual int poll(ioctx_t* ctx, PollNode* node);
|
|
|
|
private:
|
|
NetworkInterface* netif;
|
|
|
|
};
|
|
|
|
bool RegisterNetworkInterface(NetworkInterface* netif,
|
|
Ref<Descriptor> dev)
|
|
{
|
|
ScopedLock lock(&netifs_lock);
|
|
if ( netifs_count == netifs_allocated )
|
|
{
|
|
size_t new_length_half = netifs_allocated;
|
|
if ( new_length_half == 0 )
|
|
new_length_half = 8;
|
|
NetworkInterface** new_netifs =
|
|
(NetworkInterface**) reallocarray(netifs, new_length_half,
|
|
2 * sizeof(NetworkInterface*));
|
|
if ( !new_netifs )
|
|
return false;
|
|
netifs = new_netifs;
|
|
netifs_allocated = new_length_half * 2;
|
|
}
|
|
Ref<Inode> node(new NetworkInterfaceNode(dev->dev, 0, 0, 0666, netif));
|
|
if ( !node )
|
|
return false;
|
|
ioctx_t ctx; SetupKernelIOCtx(&ctx);
|
|
if ( LinkInodeInDir(&ctx, dev, netif->ifinfo.name, node) != 0 )
|
|
return false;
|
|
// Interfaces are counted from 1 inclusive up to UINT_MAX exclusive.
|
|
if ( netifs_count == 0 )
|
|
netifs[netifs_count++] = NULL;
|
|
if ( UINT_MAX <= netifs_count + 1 )
|
|
return errno = EOVERFLOW, false;
|
|
unsigned int linkid = netifs_count++;
|
|
netifs[linkid] = netif;
|
|
netif->ifinfo.linkid = linkid;
|
|
return true;
|
|
}
|
|
|
|
NetworkInterface::NetworkInterface()
|
|
{
|
|
cfg_lock = KTHREAD_MUTEX_INITIALIZER;
|
|
cfg_cond = KTHREAD_COND_INITIALIZER;
|
|
memset(&ifinfo, 0, sizeof(ifinfo));
|
|
memset(&ifstatus, 0, sizeof(ifstatus));
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
arp_table = NULL;
|
|
// poll_channel is initialized by its constructor.
|
|
}
|
|
|
|
NetworkInterface::~NetworkInterface()
|
|
{
|
|
}
|
|
|
|
short NetworkInterface::PollEventStatus()
|
|
{
|
|
short status = 0;
|
|
if ( ifstatus.flags & IF_STATUS_FLAGS_UP )
|
|
status |= POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM;
|
|
return status;
|
|
}
|
|
|
|
int NetworkInterface::poll(ioctx_t* /*ctx*/, PollNode* node)
|
|
{
|
|
ScopedLock lock(&cfg_lock);
|
|
short ret_status = PollEventStatus() & node->events;
|
|
if ( ret_status )
|
|
{
|
|
node->master->revents |= ret_status;
|
|
return 0;
|
|
}
|
|
poll_channel.Register(node);
|
|
return errno = EAGAIN, -1;
|
|
}
|
|
|
|
NetworkInterfaceNode::NetworkInterfaceNode(dev_t dev, uid_t owner, gid_t group,
|
|
mode_t mode, NetworkInterface* netif)
|
|
{
|
|
inode_type = INODE_TYPE_UNKNOWN;
|
|
if ( !dev )
|
|
dev = (dev_t) this;
|
|
ino = (ino_t) this;
|
|
this->type = S_IFCHR;
|
|
this->dev = dev;
|
|
this->stat_uid = owner;
|
|
this->stat_gid = group;
|
|
this->stat_mode = (mode & S_SETABLE) | this->type;
|
|
this->netif = netif;
|
|
}
|
|
|
|
NetworkInterfaceNode::~NetworkInterfaceNode()
|
|
{
|
|
}
|
|
|
|
int NetworkInterfaceNode::ioctl(ioctx_t* ctx, int cmd, uintptr_t arg)
|
|
{
|
|
void* ptr = (void*) arg;
|
|
|
|
if ( cmd == NIOC_SETCONFIG ||
|
|
cmd == NIOC_SETCONFIG_ETHER ||
|
|
cmd == NIOC_SETCONFIG_INET )
|
|
{
|
|
ScopedLock outer(&ARP::arp_lock);
|
|
ScopedLock inner(&netif->cfg_lock);
|
|
struct if_config new_cfg;
|
|
memcpy(&new_cfg, &netif->cfg, sizeof(new_cfg));
|
|
switch ( cmd )
|
|
{
|
|
case NIOC_SETCONFIG:
|
|
if ( !ctx->copy_from_src(&new_cfg, ptr, sizeof(new_cfg)) )
|
|
return -1;
|
|
break;
|
|
case NIOC_SETCONFIG_ETHER:
|
|
if ( !ctx->copy_from_src(&new_cfg.ether, ptr,
|
|
sizeof(new_cfg.ether)) )
|
|
return -1;
|
|
break;
|
|
case NIOC_SETCONFIG_INET:
|
|
if ( !ctx->copy_from_src(&new_cfg.inet, ptr, sizeof(new_cfg.inet)) )
|
|
return -1;
|
|
break;
|
|
}
|
|
// Let the ARP cache know the configuration changed, so it can purge any
|
|
// entries that are no longer valid.
|
|
ARP::OnConfiguration(netif, &netif->cfg, &new_cfg);
|
|
memcpy(&netif->cfg, &new_cfg, sizeof(new_cfg));
|
|
kthread_cond_broadcast(&netif->cfg_cond);
|
|
return 0;
|
|
}
|
|
|
|
ScopedLock lock(&netif->cfg_lock);
|
|
switch ( cmd )
|
|
{
|
|
case IOCGETTYPE:
|
|
return IOC_MAKE_TYPE(IOC_TYPE_NETWORK_INTERFACE, 0);
|
|
case NIOC_GETINFO:
|
|
if ( !ctx->copy_to_dest(ptr, &netif->ifinfo, sizeof(netif->ifinfo)) )
|
|
return -1;
|
|
return 0;
|
|
case NIOC_GETSTATUS:
|
|
if ( !ctx->copy_to_dest(ptr, &netif->ifstatus,
|
|
sizeof(netif->ifstatus)) )
|
|
return -1;
|
|
return 0;
|
|
case NIOC_GETCONFIG:
|
|
if ( !ctx->copy_to_dest(ptr, &netif->cfg, sizeof(netif->cfg)) )
|
|
return -1;
|
|
return 0;
|
|
case NIOC_GETCONFIG_ETHER:
|
|
if ( !ctx->copy_to_dest(ptr, &netif->cfg.ether,
|
|
sizeof(netif->cfg.ether)) )
|
|
return -1;
|
|
return 0;
|
|
case NIOC_GETCONFIG_INET:
|
|
if ( !ctx->copy_to_dest(ptr, &netif->cfg.inet,
|
|
sizeof(netif->cfg.inet)) )
|
|
return -1;
|
|
return 0;
|
|
default:
|
|
return errno = ENOTTY, -1;
|
|
}
|
|
}
|
|
|
|
int NetworkInterfaceNode::poll(ioctx_t* ctx, PollNode* node)
|
|
{
|
|
return netif->poll(ctx, node);
|
|
}
|
|
|
|
} // namespace Sortix
|