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>
139 lines
4.2 KiB
C++
139 lines
4.2 KiB
C++
/*
|
|
* Copyright (c) 2016, 2017 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/ether.cpp
|
|
* Ethernet.
|
|
*/
|
|
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <endian.h>
|
|
#include <netinet/if_ether.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include <sortix/kernel/crc32.h>
|
|
#include <sortix/kernel/kernel.h>
|
|
#include <sortix/kernel/if.h>
|
|
#include <sortix/kernel/packet.h>
|
|
|
|
#include "arp.h"
|
|
#include "ether.h"
|
|
#include "ip.h"
|
|
|
|
namespace Sortix {
|
|
namespace Ether {
|
|
|
|
size_t GetMTU(NetworkInterface* netif)
|
|
{
|
|
(void) netif;
|
|
return ETHERMTU;
|
|
}
|
|
|
|
void Handle(Ref<Packet> pkt, bool checksum_offloaded)
|
|
{
|
|
assert(pkt->netif);
|
|
assert(pkt->offset <= pkt->length);
|
|
const unsigned char* in = pkt->from + pkt->offset;
|
|
size_t inlen = pkt->length - pkt->offset;
|
|
struct ether_header hdr;
|
|
if ( !checksum_offloaded )
|
|
{
|
|
struct ether_footer ftr;
|
|
if ( inlen < sizeof(hdr) + sizeof(ftr) /* ETHER_LEN */ )
|
|
return;
|
|
size_t ftr_offset = inlen - sizeof(ftr);
|
|
memcpy(&ftr, in + ftr_offset, sizeof(ftr));
|
|
pkt->length -= sizeof(ftr);
|
|
inlen -= sizeof(ftr);
|
|
ftr.ether_crc = le32toh(ftr.ether_crc);
|
|
if ( ftr.ether_crc != crc32(0, in, inlen) )
|
|
return;
|
|
}
|
|
else if ( inlen < sizeof(hdr) /* ETHER_HDR_LEN */ )
|
|
return;
|
|
memcpy(&hdr, in, sizeof(hdr));
|
|
hdr.ether_type = be16toh(hdr.ether_type);
|
|
pkt->offset += sizeof(hdr);
|
|
const struct ether_addr* src = (const struct ether_addr*) &hdr.ether_shost;
|
|
const struct ether_addr* dst = (const struct ether_addr*) &hdr.ether_dhost;
|
|
// Drop invalid frames with broadcast source.
|
|
if ( !memcmp(src, ðeraddr_broadcast, sizeof(struct ether_addr)) )
|
|
return;
|
|
// Accept only frames with destination being broadcast or our address.
|
|
bool dst_broadcast =
|
|
!memcmp(dst, ðeraddr_broadcast, sizeof(struct ether_addr));
|
|
if ( !dst_broadcast )
|
|
{
|
|
ScopedLock(&pkt->netif->cfg_lock);
|
|
const struct ether_addr* local = &pkt->netif->cfg.ether.address;
|
|
if ( pkt->netif->ifinfo.type != IF_TYPE_LOOPBACK &&
|
|
memcmp(dst, local, sizeof(struct ether_addr)) != 0 )
|
|
return;
|
|
}
|
|
switch ( hdr.ether_type )
|
|
{
|
|
case ETHERTYPE_IP: // IPv4
|
|
IP::Handle(pkt, src, dst, dst_broadcast);
|
|
break;
|
|
case ETHERTYPE_ARP: // Address Resolution Protocol
|
|
ARP::Handle(pkt, src, dst, dst_broadcast);
|
|
break;
|
|
case ETHERTYPE_IPV6: // IPv6
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
bool Send(Ref<Packet> pktin,
|
|
const struct ether_addr* src,
|
|
const struct ether_addr* dst,
|
|
uint16_t ether_type,
|
|
NetworkInterface* netif)
|
|
{
|
|
if ( ETHERMTU < pktin->length )
|
|
return errno = EMSGSIZE, false;
|
|
Ref<Packet> pkt = GetPacket();
|
|
if ( !pkt )
|
|
return false;
|
|
const unsigned char* in = pktin->from;
|
|
size_t inlen = pktin->length;
|
|
size_t padding = inlen < ETHERMIN ? ETHERMIN - inlen : 0;
|
|
unsigned char* out = pkt->from;
|
|
struct ether_header hdr;
|
|
struct ether_footer ftr;
|
|
size_t outlen = sizeof(hdr) /* ETHER_HDR_LEN */ + inlen + padding;
|
|
if ( !(netif->ifinfo.features & IF_FEATURE_ETHERNET_CRC_OFFLOAD) )
|
|
outlen += sizeof(ftr) /* ETHER_CRC_LEN */;
|
|
if ( pkt->pmap.size < outlen )
|
|
return errno = EMSGSIZE, false;
|
|
pkt->length = outlen;
|
|
memcpy(&hdr.ether_dhost, dst, sizeof(struct ether_addr));
|
|
memcpy(&hdr.ether_shost, src, sizeof(struct ether_addr));
|
|
hdr.ether_type = htobe16(ether_type);
|
|
memcpy(out, &hdr, sizeof(hdr));
|
|
memcpy(out + sizeof(hdr), in, inlen);
|
|
memset(out + sizeof(hdr) + inlen, 0, padding);
|
|
if ( !(netif->ifinfo.features & IF_FEATURE_ETHERNET_CRC_OFFLOAD) )
|
|
{
|
|
ftr.ether_crc = htole32(crc32(0, out, pkt->length));
|
|
memcpy(out + sizeof(hdr) + inlen + padding, &ftr, sizeof(ftr));
|
|
}
|
|
return netif->Send(pkt);
|
|
}
|
|
|
|
} // namespace Ether
|
|
} // namespace Sortix
|